13111: Merge branch 'master' into 13111-webdav-projects
[arvados.git] / services / nodemanager / tests / test_computenode_driver_azure.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.azure as azure
16 from . import testutil
17
18 class AzureComputeNodeDriverTestCase(testutil.DriverTestMixin, unittest.TestCase):
19     TEST_CLASS = azure.ComputeNodeDriver
20
21     def new_driver(self, auth_kwargs={}, list_kwargs={}, create_kwargs={}):
22         list_kwargs.setdefault("ex_resource_group", "TestResourceGroup")
23         return super(AzureComputeNodeDriverTestCase, self).new_driver(auth_kwargs, list_kwargs, create_kwargs)
24
25     def test_driver_instantiation(self):
26         kwargs = {'key': 'testkey'}
27         driver = self.new_driver(auth_kwargs=kwargs)
28         self.assertTrue(self.driver_mock.called)
29         self.assertEqual(kwargs, self.driver_mock.call_args[1])
30
31     def test_create_image_loaded_at_initialization(self):
32         get_method = self.driver_mock().get_image
33         get_method.return_value = testutil.cloud_object_mock('id_b')
34         driver = self.new_driver(create_kwargs={'image': 'id_b'})
35         self.assertEqual(1, get_method.call_count)
36
37     def test_create_includes_ping(self):
38         arv_node = testutil.arvados_node_mock(info={'ping_secret': 'ssshh'})
39         arv_node["hostname"] = None
40         driver = self.new_driver()
41         driver.create_node(testutil.MockSize(1), arv_node)
42         create_method = self.driver_mock().create_node
43         self.assertTrue(create_method.called)
44         self.assertIn('ping_secret=ssshh',
45                       create_method.call_args[1].get('ex_tags', {}).get('arv-ping-url', ""))
46
47     def test_name_from_new_arvados_node(self):
48         arv_node = testutil.arvados_node_mock(hostname=None)
49         driver = self.new_driver()
50         self.assertEqual('compute-000000000000063-zzzzz',
51                          driver.arvados_create_kwargs(testutil.MockSize(1), arv_node)['name'])
52
53
54
55     def check_node_tagged(self, cloud_node, expected_tags):
56         tag_mock = self.driver_mock().ex_create_tags
57         self.assertTrue(tag_mock.called)
58         self.assertIs(cloud_node, tag_mock.call_args[0][0])
59         self.assertEqual(expected_tags, tag_mock.call_args[0][1])
60
61     def test_node_create_time(self):
62         refsecs = int(time.time())
63         reftuple = time.gmtime(refsecs)
64         node = testutil.cloud_node_mock()
65         node.extra = {'tags': {'booted_at': time.strftime('%Y-%m-%dT%H:%M:%S.000Z',
66                                                    reftuple)}}
67         self.assertEqual(refsecs, azure.ComputeNodeDriver.node_start_time(node))
68
69     def test_node_fqdn(self):
70         name = 'fqdntest.zzzzz.arvadosapi.com'
71         node = testutil.cloud_node_mock()
72         node.extra = {'tags': {"hostname": name}}
73         self.assertEqual(name, azure.ComputeNodeDriver.node_fqdn(node))
74
75     def test_sync_node(self):
76         arv_node = testutil.arvados_node_mock(1)
77         cloud_node = testutil.cloud_node_mock(2)
78         driver = self.new_driver()
79         driver.sync_node(cloud_node, arv_node)
80         self.check_node_tagged(cloud_node,
81                                {'hostname': 'compute1.zzzzz.arvadosapi.com'})
82
83     def test_custom_data(self):
84         arv_node = testutil.arvados_node_mock(hostname=None)
85         driver = self.new_driver()
86         self.assertEqual("""#!/bin/sh
87 mkdir -p    /var/tmp/arv-node-data/meta-data
88 echo 'https://100::/arvados/v1/nodes/zzzzz-yyyyy-000000000000063/ping?ping_secret=defaulttestsecret' > /var/tmp/arv-node-data/arv-ping-url
89 echo compute-000000000000063-zzzzz > /var/tmp/arv-node-data/meta-data/instance-id
90 echo z1.test > /var/tmp/arv-node-data/meta-data/instance-type
91 """,
92                          driver.arvados_create_kwargs(testutil.MockSize(1), arv_node)['ex_customdata'])
93
94     def test_create_raises_but_actually_succeeded(self):
95         arv_node = testutil.arvados_node_mock(1, hostname=None)
96         driver = self.new_driver(create_kwargs={"tag_arvados-class": "dynamic-compute"})
97         nodelist = [testutil.cloud_node_mock(1, tags={"arvados-class": "dynamic-compute"})]
98         nodelist[0].name = 'compute-000000000000001-zzzzz'
99         self.driver_mock().list_nodes.return_value = nodelist
100         self.driver_mock().create_node.side_effect = IOError
101         n = driver.create_node(testutil.MockSize(1), arv_node)
102         self.assertEqual('compute-000000000000001-zzzzz', n.name)
103
104     def test_ex_fetch_nic_false(self):
105         arv_node = testutil.arvados_node_mock(1, hostname=None)
106         driver = self.new_driver(create_kwargs={"tag_arvados-class": "dynamic-compute"})
107         nodelist = [testutil.cloud_node_mock(1, tags={"arvados-class": "dynamic-compute"})]
108         nodelist[0].name = 'compute-000000000000001-zzzzz'
109         self.driver_mock().list_nodes.return_value = nodelist
110         n = driver.list_nodes()
111         self.assertEqual(nodelist, n)
112         self.driver_mock().list_nodes.assert_called_with(ex_fetch_nic=False, ex_fetch_power_state=False, ex_resource_group='TestResourceGroup')
113
114     def test_create_can_find_node_after_timeout(self):
115         super(AzureComputeNodeDriverTestCase,
116               self).test_create_can_find_node_after_timeout(
117                   create_kwargs={'tag_arvados-class': 'test'},
118                   node_extra={'tags': {'arvados-class': 'test'}})
119
120     def test_node_found_after_timeout_has_fixed_size(self):
121         size = testutil.MockSize(4)
122         node_props = {'hardwareProfile': {'vmSize': size.id}}
123         cloud_node = testutil.cloud_node_mock(tags={'arvados-class': 'test'}, properties=node_props)
124         cloud_node.size = None
125         self.check_node_found_after_timeout_has_fixed_size(
126             size, cloud_node, {'tag_arvados-class': 'test'})