4024: Merge branch 'master' into 4024-pipeline-instances-scroll
[arvados.git] / services / nodemanager / tests / test_computenode_driver_ec2.py
1 #!/usr/bin/env python
2
3 from __future__ import absolute_import, print_function
4
5 import time
6 import unittest
7
8 import mock
9
10 import arvnodeman.computenode.driver.ec2 as ec2
11 from . import testutil
12
13 class EC2ComputeNodeDriverTestCase(unittest.TestCase):
14     def setUp(self):
15         self.driver_mock = mock.MagicMock(name='driver_mock')
16
17     def new_driver(self, auth_kwargs={}, list_kwargs={}, create_kwargs={}):
18         create_kwargs.setdefault('ping_host', '100::')
19         return ec2.ComputeNodeDriver(
20             auth_kwargs, list_kwargs, create_kwargs,
21             driver_class=self.driver_mock)
22
23     def test_driver_instantiation(self):
24         kwargs = {'key': 'testkey'}
25         driver = self.new_driver(auth_kwargs=kwargs)
26         self.assertTrue(self.driver_mock.called)
27         self.assertEqual(kwargs, self.driver_mock.call_args[1])
28
29     def test_list_kwargs_become_filters(self):
30         # We're also testing tag name translation.
31         driver = self.new_driver(list_kwargs={'tag_test': 'true'})
32         driver.list_nodes()
33         list_method = self.driver_mock().list_nodes
34         self.assertTrue(list_method.called)
35         self.assertEqual({'tag:test': 'true'},
36                           list_method.call_args[1].get('ex_filters'))
37
38     def test_create_location_loaded_at_initialization(self):
39         kwargs = {'location': 'testregion'}
40         driver = self.new_driver(create_kwargs=kwargs)
41         self.assertTrue(self.driver_mock().list_locations)
42
43     def test_create_image_loaded_at_initialization(self):
44         kwargs = {'image': 'testimage'}
45         driver = self.new_driver(create_kwargs=kwargs)
46         self.assertTrue(self.driver_mock().list_images)
47
48     def test_create_includes_ping_secret(self):
49         arv_node = testutil.arvados_node_mock(info={'ping_secret': 'ssshh'})
50         driver = self.new_driver()
51         driver.create_node(testutil.MockSize(1), arv_node)
52         create_method = self.driver_mock().create_node
53         self.assertTrue(create_method.called)
54         self.assertIn('ping_secret=ssshh',
55                       create_method.call_args[1].get('ex_userdata',
56                                                      'arg missing'))
57
58     def test_tags_created_from_arvados_node(self):
59         arv_node = testutil.arvados_node_mock(8)
60         cloud_node = testutil.cloud_node_mock(8)
61         driver = self.new_driver(list_kwargs={'tag:list': 'test'})
62         self.assertEqual({'ex_metadata': {'list': 'test'},
63                           'name': 'compute8.zzzzz.arvadosapi.com'},
64                          driver.arvados_create_kwargs(arv_node))
65
66     def test_tags_set_default_hostname_from_new_arvados_node(self):
67         arv_node = testutil.arvados_node_mock(hostname=None)
68         driver = self.new_driver()
69         actual = driver.arvados_create_kwargs(arv_node)
70         self.assertEqual('dynamic.compute.zzzzz.arvadosapi.com',
71                          actual['name'])
72
73     def test_sync_node(self):
74         arv_node = testutil.arvados_node_mock(1)
75         cloud_node = testutil.cloud_node_mock(2)
76         driver = self.new_driver()
77         driver.sync_node(cloud_node, arv_node)
78         tag_mock = self.driver_mock().ex_create_tags
79         self.assertTrue(tag_mock.called)
80         self.assertEqual('compute1.zzzzz.arvadosapi.com',
81                          tag_mock.call_args[0][1].get('Name', 'no name'))
82
83     def test_node_create_time(self):
84         refsecs = int(time.time())
85         reftuple = time.gmtime(refsecs)
86         node = testutil.cloud_node_mock()
87         node.extra = {'launch_time': time.strftime('%Y-%m-%dT%H:%M:%S.000Z',
88                                                    reftuple)}
89         self.assertEqual(refsecs, ec2.ComputeNodeDriver.node_start_time(node))