3 from __future__ import absolute_import, print_function
9 import libcloud.common.types as cloud_types
12 import arvnodeman.computenode.driver.ec2 as ec2
13 from . import testutil
15 class EC2ComputeNodeDriverTestCase(unittest.TestCase):
17 self.driver_mock = mock.MagicMock(name='driver_mock')
19 def new_driver(self, auth_kwargs={}, list_kwargs={}, create_kwargs={}):
20 create_kwargs.setdefault('ping_host', '100::')
21 return ec2.ComputeNodeDriver(
22 auth_kwargs, list_kwargs, create_kwargs,
23 driver_class=self.driver_mock)
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])
31 def test_list_kwargs_become_filters(self):
32 # We're also testing tag name translation.
33 driver = self.new_driver(list_kwargs={'tag_test': 'true'})
35 list_method = self.driver_mock().list_nodes
36 self.assertTrue(list_method.called)
37 self.assertEqual({'tag:test': 'true'},
38 list_method.call_args[1].get('ex_filters'))
40 def test_create_location_loaded_at_initialization(self):
41 kwargs = {'location': 'testregion'}
42 driver = self.new_driver(create_kwargs=kwargs)
43 self.assertTrue(self.driver_mock().list_locations)
45 def test_create_image_loaded_at_initialization(self):
46 kwargs = {'image': 'testimage'}
47 driver = self.new_driver(create_kwargs=kwargs)
48 self.assertTrue(self.driver_mock().list_images)
50 def test_create_includes_ping_secret(self):
51 arv_node = testutil.arvados_node_mock(info={'ping_secret': 'ssshh'})
52 driver = self.new_driver()
53 driver.create_node(testutil.MockSize(1), arv_node)
54 create_method = self.driver_mock().create_node
55 self.assertTrue(create_method.called)
56 self.assertIn('ping_secret=ssshh',
57 create_method.call_args[1].get('ex_userdata',
60 def test_hostname_from_arvados_node(self):
61 arv_node = testutil.arvados_node_mock(8)
62 driver = self.new_driver()
63 self.assertEqual('compute8.zzzzz.arvadosapi.com',
64 driver.arvados_create_kwargs(arv_node)['name'])
66 def test_default_hostname_from_new_arvados_node(self):
67 arv_node = testutil.arvados_node_mock(hostname=None)
68 driver = self.new_driver()
69 self.assertEqual('dynamic.compute.zzzzz.arvadosapi.com',
70 driver.arvados_create_kwargs(arv_node)['name'])
72 def check_node_tagged(self, cloud_node, expected_tags):
73 tag_mock = self.driver_mock().ex_create_tags
74 self.assertTrue(tag_mock.called)
75 self.assertIs(cloud_node, tag_mock.call_args[0][0])
76 self.assertEqual(expected_tags, tag_mock.call_args[0][1])
78 def test_post_create_node_tags_from_list_kwargs(self):
79 expect_tags = {'key1': 'test value 1', 'key2': 'test value 2'}
80 list_kwargs = {('tag_' + key): value
81 for key, value in expect_tags.iteritems()}
82 list_kwargs['instance-state-name'] = 'running'
83 cloud_node = testutil.cloud_node_mock()
84 driver = self.new_driver(list_kwargs=list_kwargs)
85 driver.post_create_node(cloud_node)
86 self.check_node_tagged(cloud_node, expect_tags)
88 def test_sync_node(self):
89 arv_node = testutil.arvados_node_mock(1)
90 cloud_node = testutil.cloud_node_mock(2)
91 driver = self.new_driver()
92 driver.sync_node(cloud_node, arv_node)
93 self.check_node_tagged(cloud_node,
94 {'Name': 'compute1.zzzzz.arvadosapi.com'})
96 def test_node_create_time(self):
97 refsecs = int(time.time())
98 reftuple = time.gmtime(refsecs)
99 node = testutil.cloud_node_mock()
100 node.extra = {'launch_time': time.strftime('%Y-%m-%dT%H:%M:%S.000Z',
102 self.assertEqual(refsecs, ec2.ComputeNodeDriver.node_start_time(node))
104 def test_cloud_exceptions(self):
105 for error in [Exception("test exception"),
106 IOError("test exception"),
107 ssl.SSLError("test exception"),
108 cloud_types.LibcloudError("test exception")]:
109 self.assertTrue(ec2.ComputeNodeDriver.is_cloud_exception(error),
110 "{} not flagged as cloud exception".format(error))
112 def test_noncloud_exceptions(self):
114 ec2.ComputeNodeDriver.is_cloud_exception(ValueError("test error")),
115 "ValueError flagged as cloud exception")