3 from __future__ import absolute_import, print_function
9 import libcloud.common.types as cloud_types
12 import arvnodeman.computenode.driver.azure as azure
13 from . import testutil
15 class AzureComputeNodeDriverTestCase(testutil.DriverTestMixin, unittest.TestCase):
16 TEST_CLASS = azure.ComputeNodeDriver
18 def new_driver(self, auth_kwargs={}, list_kwargs={}, create_kwargs={}):
19 list_kwargs.setdefault("ex_resource_group", "TestResourceGroup")
20 return super(AzureComputeNodeDriverTestCase, self).new_driver(auth_kwargs, list_kwargs, create_kwargs)
22 def test_driver_instantiation(self):
23 kwargs = {'key': 'testkey'}
24 driver = self.new_driver(auth_kwargs=kwargs)
25 self.assertTrue(self.driver_mock.called)
26 self.assertEqual(kwargs, self.driver_mock.call_args[1])
28 def test_create_image_loaded_at_initialization(self):
29 get_method = self.driver_mock().get_image
30 get_method.return_value = testutil.cloud_object_mock('id_b')
31 driver = self.new_driver(create_kwargs={'image': 'id_b'})
32 self.assertEqual(1, get_method.call_count)
34 def test_create_includes_ping(self):
35 arv_node = testutil.arvados_node_mock(info={'ping_secret': 'ssshh'})
36 arv_node["hostname"] = None
37 driver = self.new_driver()
38 driver.create_node(testutil.MockSize(1), arv_node)
39 create_method = self.driver_mock().create_node
40 self.assertTrue(create_method.called)
41 self.assertIn('ping_secret=ssshh',
42 create_method.call_args[1].get('ex_tags', {}).get('arv-ping-url', ""))
44 def test_name_from_new_arvados_node(self):
45 arv_node = testutil.arvados_node_mock(hostname=None)
46 driver = self.new_driver()
47 self.assertEqual('compute-000000000000063-zzzzz',
48 driver.arvados_create_kwargs(testutil.MockSize(1), arv_node)['name'])
52 def check_node_tagged(self, cloud_node, expected_tags):
53 tag_mock = self.driver_mock().ex_create_tags
54 self.assertTrue(tag_mock.called)
55 self.assertIs(cloud_node, tag_mock.call_args[0][0])
56 self.assertEqual(expected_tags, tag_mock.call_args[0][1])
58 def test_node_create_time(self):
59 refsecs = int(time.time())
60 reftuple = time.gmtime(refsecs)
61 node = testutil.cloud_node_mock()
62 node.extra = {'tags': {'booted_at': time.strftime('%Y-%m-%dT%H:%M:%S.000Z',
64 self.assertEqual(refsecs, azure.ComputeNodeDriver.node_start_time(node))
66 def test_node_fqdn(self):
67 name = 'fqdntest.zzzzz.arvadosapi.com'
68 node = testutil.cloud_node_mock()
69 node.extra = {'tags': {"hostname": name}}
70 self.assertEqual(name, azure.ComputeNodeDriver.node_fqdn(node))
72 def test_cloud_exceptions(self):
73 for error in [Exception("test exception"),
74 IOError("test exception"),
75 ssl.SSLError("test exception"),
76 cloud_types.LibcloudError("test exception")]:
77 self.assertTrue(azure.ComputeNodeDriver.is_cloud_exception(error),
78 "{} not flagged as cloud exception".format(error))
80 def test_noncloud_exceptions(self):
82 azure.ComputeNodeDriver.is_cloud_exception(ValueError("test error")),
83 "ValueError flagged as cloud exception")
85 def test_sync_node(self):
86 arv_node = testutil.arvados_node_mock(1)
87 cloud_node = testutil.cloud_node_mock(2)
88 driver = self.new_driver()
89 driver.sync_node(cloud_node, arv_node)
90 self.check_node_tagged(cloud_node,
91 {'hostname': 'compute1.zzzzz.arvadosapi.com'})
93 def test_custom_data(self):
94 arv_node = testutil.arvados_node_mock(hostname=None)
95 driver = self.new_driver()
96 self.assertEqual("""#!/bin/sh
97 mkdir -p /var/tmp/arv-node-data/meta-data
98 echo 'https://100::/arvados/v1/nodes/zzzzz-yyyyy-000000000000063/ping?ping_secret=defaulttestsecret' > /var/tmp/arv-node-data/arv-ping-url
99 echo compute-000000000000063-zzzzz > /var/tmp/arv-node-data/meta-data/instance-id
100 echo z1.test > /var/tmp/arv-node-data/meta-data/instance-type
102 driver.arvados_create_kwargs(testutil.MockSize(1), arv_node)['ex_customdata'])
104 def test_create_raises_but_actually_succeeded(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 self.driver_mock().create_node.side_effect = IOError
111 n = driver.create_node(testutil.MockSize(1), arv_node)
112 self.assertEqual('compute-000000000000001-zzzzz', n.name)
114 def test_ex_fetch_nic_false(self):
115 arv_node = testutil.arvados_node_mock(1, hostname=None)
116 driver = self.new_driver(create_kwargs={"tag_arvados-class": "dynamic-compute"})
117 nodelist = [testutil.cloud_node_mock(1, tags={"arvados-class": "dynamic-compute"})]
118 nodelist[0].name = 'compute-000000000000001-zzzzz'
119 self.driver_mock().list_nodes.return_value = nodelist
120 n = driver.list_nodes()
121 self.assertEqual(nodelist, n)
122 self.driver_mock().list_nodes.assert_called_with(ex_fetch_nic=False, ex_resource_group='TestResourceGroup')
124 def test_create_can_find_node_after_timeout(self):
125 super(AzureComputeNodeDriverTestCase,
126 self).test_create_can_find_node_after_timeout(
127 create_kwargs={'tag_arvados-class': 'test'},
128 node_extra={'tags': {'arvados-class': 'test'}})
130 def test_node_found_after_timeout_has_fixed_size(self):
131 size = testutil.MockSize(4)
132 node_props = {'hardwareProfile': {'vmSize': size.id}}
133 cloud_node = testutil.cloud_node_mock(tags={'arvados-class': 'test'}, properties=node_props)
134 cloud_node.size = None
135 self.check_node_found_after_timeout_has_fixed_size(
136 size, cloud_node, {'tag_arvados-class': 'test'})