12199: Fix errant uses of size.name that should be size.id.
[arvados.git] / services / nodemanager / tests / test_computenode_driver_ec2.py
1 #!/usr/bin/env python
2 # Copyright (C) The Arvados Authors. All rights reserved.
3 #
4 # SPDX-License-Identifier: AGPL-3.0
5
6 from __future__ import absolute_import, print_function
7
8 import ssl
9 import time
10 import unittest
11
12 import libcloud.common.types as cloud_types
13 import mock
14
15 import arvnodeman.computenode.driver.ec2 as ec2
16 from . import testutil
17
18 class EC2ComputeNodeDriverTestCase(testutil.DriverTestMixin, unittest.TestCase):
19     TEST_CLASS = ec2.ComputeNodeDriver
20
21     def test_driver_instantiation(self):
22         kwargs = {'key': 'testkey'}
23         driver = self.new_driver(auth_kwargs=kwargs)
24         self.assertTrue(self.driver_mock.called)
25         self.assertEqual(kwargs, self.driver_mock.call_args[1])
26
27     def test_list_kwargs_become_filters(self):
28         # We're also testing tag name translation.
29         driver = self.new_driver(list_kwargs={'tag_test': 'true'})
30         driver.list_nodes()
31         list_method = self.driver_mock().list_nodes
32         self.assertTrue(list_method.called)
33         self.assertEqual({'tag:test': 'true'},
34                           list_method.call_args[1].get('ex_filters'))
35
36     def test_create_image_loaded_at_initialization(self):
37         list_method = self.driver_mock().list_images
38         list_method.return_value = [testutil.cloud_object_mock(c)
39                                     for c in 'abc']
40         driver = self.new_driver(create_kwargs={'image_id': 'id_b'})
41         self.assertEqual(1, list_method.call_count)
42
43     def test_create_includes_ping_secret(self):
44         arv_node = testutil.arvados_node_mock(info={'ping_secret': 'ssshh'})
45         driver = self.new_driver()
46         driver.create_node(testutil.MockSize(1), arv_node)
47         create_method = self.driver_mock().create_node
48         self.assertTrue(create_method.called)
49         self.assertIn('ping_secret=ssshh',
50                       create_method.call_args[1].get('ex_userdata',
51                                                      'arg missing'))
52
53     def test_create_includes_metadata(self):
54         arv_node = testutil.arvados_node_mock()
55         driver = self.new_driver(list_kwargs={'tag_test': 'testvalue'})
56         driver.create_node(testutil.MockSize(1), arv_node)
57         create_method = self.driver_mock().create_node
58         self.assertTrue(create_method.called)
59         self.assertEqual(
60             {'test':'testvalue'},
61             create_method.call_args[1].get('ex_metadata', {'arg': 'missing'})
62         )
63
64     def test_hostname_from_arvados_node(self):
65         arv_node = testutil.arvados_node_mock(8)
66         driver = self.new_driver()
67         self.assertEqual('compute8.zzzzz.arvadosapi.com',
68                          driver.arvados_create_kwargs(testutil.MockSize(1), arv_node)['name'])
69
70     def test_default_hostname_from_new_arvados_node(self):
71         arv_node = testutil.arvados_node_mock(hostname=None)
72         driver = self.new_driver()
73         self.assertEqual('dynamic.compute.zzzzz.arvadosapi.com',
74                          driver.arvados_create_kwargs(testutil.MockSize(1), arv_node)['name'])
75
76     def check_node_tagged(self, cloud_node, expected_tags):
77         tag_mock = self.driver_mock().ex_create_tags
78         self.assertTrue(tag_mock.called)
79         self.assertIs(cloud_node, tag_mock.call_args[0][0])
80         self.assertEqual(expected_tags, tag_mock.call_args[0][1])
81
82     def test_sync_node(self):
83         arv_node = testutil.arvados_node_mock(1)
84         cloud_node = testutil.cloud_node_mock(2)
85         driver = self.new_driver()
86         driver.sync_node(cloud_node, arv_node)
87         self.check_node_tagged(cloud_node,
88                                {'Name': 'compute1.zzzzz.arvadosapi.com'})
89
90     def test_node_create_time(self):
91         refsecs = int(time.time())
92         reftuple = time.gmtime(refsecs)
93         node = testutil.cloud_node_mock()
94         node.extra = {'launch_time': time.strftime('%Y-%m-%dT%H:%M:%S.000Z',
95                                                    reftuple)}
96         self.assertEqual(refsecs, ec2.ComputeNodeDriver.node_start_time(node))
97
98     def test_node_fqdn(self):
99         name = 'fqdntest.zzzzz.arvadosapi.com'
100         node = testutil.cloud_node_mock()
101         node.name = name
102         self.assertEqual(name, ec2.ComputeNodeDriver.node_fqdn(node))
103
104     def test_create_ebs_volume(self):
105         arv_node = testutil.arvados_node_mock()
106         driver = self.new_driver()
107         # libcloud/ec2 "disk" sizes are in GB, Arvados/SLURM "scratch" value is in MB
108         size = testutil.MockSize(1)
109         size.disk=5
110         size.scratch=20000
111         driver.create_node(size, arv_node)
112         create_method = self.driver_mock().create_node
113         self.assertTrue(create_method.called)
114         self.assertEqual([{
115             "DeviceName": "/dev/xvdt",
116             "Ebs": {
117                 "DeleteOnTermination": True,
118                 "VolumeSize": 16,
119                 "VolumeType": "gp2"
120             }}],
121                          create_method.call_args[1].get('ex_blockdevicemappings'))
122
123     def test_ebs_volume_not_needed(self):
124         arv_node = testutil.arvados_node_mock()
125         driver = self.new_driver()
126         # libcloud/ec2 "disk" sizes are in GB, Arvados/SLURM "scratch" value is in MB
127         size = testutil.MockSize(1)
128         size.disk=80
129         size.scratch=20000
130         driver.create_node(size, arv_node)
131         create_method = self.driver_mock().create_node
132         self.assertTrue(create_method.called)
133         self.assertIsNone(create_method.call_args[1].get('ex_blockdevicemappings'))
134
135     def test_ebs_volume_too_big(self):
136         arv_node = testutil.arvados_node_mock()
137         driver = self.new_driver()
138         # libcloud/ec2 "disk" sizes are in GB, Arvados/SLURM "scratch" value is in MB
139         size = testutil.MockSize(1)
140         size.disk=80
141         size.scratch=20000000
142         driver.create_node(size, arv_node)
143         create_method = self.driver_mock().create_node
144         self.assertTrue(create_method.called)
145         self.assertEqual([{
146             "DeviceName": "/dev/xvdt",
147             "Ebs": {
148                 "DeleteOnTermination": True,
149                 "VolumeSize": 16384,
150                 "VolumeType": "gp2"
151             }}],
152                          create_method.call_args[1].get('ex_blockdevicemappings'))