Merge branch '13892-deprecate-jobs-api' closes #13892
[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.assertIn(
60             ('test', 'testvalue'),
61             create_method.call_args[1].get('ex_metadata', {'arg': 'missing'}).items()
62         )
63
64     def test_create_includes_arvados_node_size(self):
65         arv_node = testutil.arvados_node_mock()
66         size = testutil.MockSize(1)
67         driver = self.new_driver()
68         driver.create_node(size, arv_node)
69         create_method = self.driver_mock().create_node
70         self.assertTrue(create_method.called)
71         self.assertIn(
72             ('arvados_node_size', size.id),
73             create_method.call_args[1].get('ex_metadata', {'arg': 'missing'}).items()
74         )
75
76     def test_create_preemptible_instance(self):
77         arv_node = testutil.arvados_node_mock()
78         driver = self.new_driver()
79         driver.create_node(testutil.MockSize(1, preemptible=True), arv_node)
80         create_method = self.driver_mock().create_node
81         self.assertTrue(create_method.called)
82         self.assertEqual(
83             True,
84             create_method.call_args[1].get('ex_spot_market', 'arg missing')
85         )
86
87     def test_hostname_from_arvados_node(self):
88         arv_node = testutil.arvados_node_mock(8)
89         driver = self.new_driver()
90         self.assertEqual('compute8.zzzzz.arvadosapi.com',
91                          driver.arvados_create_kwargs(testutil.MockSize(1), arv_node)['name'])
92
93     def test_default_hostname_from_new_arvados_node(self):
94         arv_node = testutil.arvados_node_mock(hostname=None)
95         driver = self.new_driver()
96         self.assertEqual('dynamic.compute.zzzzz.arvadosapi.com',
97                          driver.arvados_create_kwargs(testutil.MockSize(1), arv_node)['name'])
98
99     def check_node_tagged(self, cloud_node, expected_tags):
100         tag_mock = self.driver_mock().ex_create_tags
101         self.assertTrue(tag_mock.called)
102         self.assertIs(cloud_node, tag_mock.call_args[0][0])
103         self.assertEqual(expected_tags, tag_mock.call_args[0][1])
104
105     def test_sync_node(self):
106         arv_node = testutil.arvados_node_mock(1)
107         cloud_node = testutil.cloud_node_mock(2)
108         driver = self.new_driver()
109         driver.sync_node(cloud_node, arv_node)
110         self.check_node_tagged(cloud_node,
111                                {'Name': 'compute1.zzzzz.arvadosapi.com'})
112
113     def test_node_create_time(self):
114         refsecs = int(time.time())
115         reftuple = time.gmtime(refsecs)
116         node = testutil.cloud_node_mock()
117         node.extra = {'launch_time': time.strftime('%Y-%m-%dT%H:%M:%S.000Z',
118                                                    reftuple)}
119         self.assertEqual(refsecs, ec2.ComputeNodeDriver.node_start_time(node))
120
121     def test_node_fqdn(self):
122         name = 'fqdntest.zzzzz.arvadosapi.com'
123         node = testutil.cloud_node_mock()
124         node.name = name
125         self.assertEqual(name, ec2.ComputeNodeDriver.node_fqdn(node))
126
127     def test_create_ebs_volume(self):
128         arv_node = testutil.arvados_node_mock()
129         driver = self.new_driver()
130         # libcloud/ec2 "disk" sizes are in GB, Arvados/SLURM "scratch" value is in MB
131         size = testutil.MockSize(1)
132         size.disk=5
133         size.scratch=20000
134         driver.create_node(size, arv_node)
135         create_method = self.driver_mock().create_node
136         self.assertTrue(create_method.called)
137         self.assertEqual([{
138             "DeviceName": "/dev/xvdt",
139             "Ebs": {
140                 "DeleteOnTermination": True,
141                 "VolumeSize": 16,
142                 "VolumeType": "gp2"
143             }}],
144                          create_method.call_args[1].get('ex_blockdevicemappings'))
145
146     def test_ebs_volume_not_needed(self):
147         arv_node = testutil.arvados_node_mock()
148         driver = self.new_driver()
149         # libcloud/ec2 "disk" sizes are in GB, Arvados/SLURM "scratch" value is in MB
150         size = testutil.MockSize(1)
151         size.disk=80
152         size.scratch=20000
153         driver.create_node(size, arv_node)
154         create_method = self.driver_mock().create_node
155         self.assertTrue(create_method.called)
156         self.assertIsNone(create_method.call_args[1].get('ex_blockdevicemappings'))
157
158     def test_ebs_volume_too_big(self):
159         arv_node = testutil.arvados_node_mock()
160         driver = self.new_driver()
161         # libcloud/ec2 "disk" sizes are in GB, Arvados/SLURM "scratch" value is in MB
162         size = testutil.MockSize(1)
163         size.disk=80
164         size.scratch=20000000
165         driver.create_node(size, arv_node)
166         create_method = self.driver_mock().create_node
167         self.assertTrue(create_method.called)
168         self.assertEqual([{
169             "DeviceName": "/dev/xvdt",
170             "Ebs": {
171                 "DeleteOnTermination": True,
172                 "VolumeSize": 16384,
173                 "VolumeType": "gp2"
174             }}],
175                          create_method.call_args[1].get('ex_blockdevicemappings'))