15422: Provenance for collections handles containers
[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_create_includes_arvados_node_size(self):
48         arv_node = testutil.arvados_node_mock()
49         arv_node["hostname"] = None
50         size = testutil.MockSize(1)
51         driver = self.new_driver()
52         driver.create_node(size, arv_node)
53         create_method = self.driver_mock().create_node
54         self.assertTrue(create_method.called)
55         self.assertIn(
56             ('arvados_node_size', size.id),
57             create_method.call_args[1].get('ex_tags', {'tags': 'missing'}).items()
58         )
59
60     def test_name_from_new_arvados_node(self):
61         arv_node = testutil.arvados_node_mock(hostname=None)
62         driver = self.new_driver()
63         self.assertEqual('compute-000000000000063-zzzzz',
64                          driver.arvados_create_kwargs(testutil.MockSize(1), arv_node)['name'])
65
66     def check_node_tagged(self, cloud_node, expected_tags):
67         tag_mock = self.driver_mock().ex_create_tags
68         self.assertTrue(tag_mock.called)
69         self.assertIs(cloud_node, tag_mock.call_args[0][0])
70         self.assertEqual(expected_tags, tag_mock.call_args[0][1])
71
72     def test_node_create_time(self):
73         refsecs = int(time.time())
74         reftuple = time.gmtime(refsecs)
75         node = testutil.cloud_node_mock()
76         node.extra = {'tags': {'booted_at': time.strftime('%Y-%m-%dT%H:%M:%S.000Z',
77                                                    reftuple)}}
78         self.assertEqual(refsecs, azure.ComputeNodeDriver.node_start_time(node))
79
80     def test_node_fqdn(self):
81         name = 'fqdntest.zzzzz.arvadosapi.com'
82         node = testutil.cloud_node_mock()
83         node.extra = {'tags': {"hostname": name}}
84         self.assertEqual(name, azure.ComputeNodeDriver.node_fqdn(node))
85
86     def test_sync_node(self):
87         arv_node = testutil.arvados_node_mock(1)
88         cloud_node = testutil.cloud_node_mock(2)
89         driver = self.new_driver()
90         driver.sync_node(cloud_node, arv_node)
91         self.check_node_tagged(cloud_node,
92                                {'hostname': 'compute1.zzzzz.arvadosapi.com'})
93
94     def test_custom_data(self):
95         arv_node = testutil.arvados_node_mock(hostname=None)
96         driver = self.new_driver()
97         self.assertEqual("""#!/bin/sh
98 mkdir -p    /var/tmp/arv-node-data/meta-data
99 echo 'https://100::/arvados/v1/nodes/zzzzz-yyyyy-000000000000063/ping?ping_secret=defaulttestsecret' > /var/tmp/arv-node-data/arv-ping-url
100 echo compute-000000000000063-zzzzz > /var/tmp/arv-node-data/meta-data/instance-id
101 echo z1.test > /var/tmp/arv-node-data/meta-data/instance-type
102 """,
103                          driver.arvados_create_kwargs(testutil.MockSize(1), arv_node)['ex_customdata'])
104
105     def test_list_nodes_ignores_nodes_without_tags(self):
106         driver = self.new_driver(create_kwargs={"tag_arvados-class": "dynamic-compute"})
107         # Mock cloud node without tags
108         nodelist = [testutil.cloud_node_mock(1)]
109         self.driver_mock().list_nodes.return_value = nodelist
110         n = driver.list_nodes()
111         self.assertEqual([], n)
112
113     def test_create_raises_but_actually_succeeded(self):
114         arv_node = testutil.arvados_node_mock(1, hostname=None)
115         driver = self.new_driver(create_kwargs={"tag_arvados-class": "dynamic-compute"})
116         nodelist = [testutil.cloud_node_mock(1, tags={"arvados-class": "dynamic-compute"})]
117         nodelist[0].name = 'compute-000000000000001-zzzzz'
118         self.driver_mock().list_nodes.return_value = nodelist
119         self.driver_mock().create_node.side_effect = IOError
120         n = driver.create_node(testutil.MockSize(1), arv_node)
121         self.assertEqual('compute-000000000000001-zzzzz', n.name)
122
123     def test_ex_fetch_nic_false(self):
124         arv_node = testutil.arvados_node_mock(1, hostname=None)
125         driver = self.new_driver(create_kwargs={"tag_arvados-class": "dynamic-compute"})
126         nodelist = [testutil.cloud_node_mock(1, tags={"arvados-class": "dynamic-compute"})]
127         nodelist[0].name = 'compute-000000000000001-zzzzz'
128         self.driver_mock().list_nodes.return_value = nodelist
129         n = driver.list_nodes()
130         self.assertEqual(nodelist, n)
131         self.driver_mock().list_nodes.assert_called_with(ex_fetch_nic=False, ex_fetch_power_state=False, ex_resource_group='TestResourceGroup')
132
133     def test_create_can_find_node_after_timeout(self):
134         super(AzureComputeNodeDriverTestCase,
135               self).test_create_can_find_node_after_timeout(
136                   create_kwargs={'tag_arvados-class': 'test'},
137                   node_extra={'tags': {'arvados-class': 'test'}})
138
139     def test_node_found_after_timeout_has_fixed_size(self):
140         size = testutil.MockSize(4)
141         node_props = {'hardwareProfile': {'vmSize': size.id}}
142         cloud_node = testutil.cloud_node_mock(tags={'arvados-class': 'test'}, properties=node_props)
143         cloud_node.size = None
144         self.check_node_found_after_timeout_has_fixed_size(
145             size, cloud_node, {'tag_arvados-class': 'test'})