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