Merge branch 'master' into 9998-unsigned_manifest
[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 ssl
6 import time
7 import unittest
8
9 import libcloud.common.types as cloud_types
10 import mock
11
12 import arvnodeman.computenode.driver.ec2 as ec2
13 from . import testutil
14
15 class EC2ComputeNodeDriverTestCase(testutil.DriverTestMixin, unittest.TestCase):
16     TEST_CLASS = ec2.ComputeNodeDriver
17
18     def test_driver_instantiation(self):
19         kwargs = {'key': 'testkey'}
20         driver = self.new_driver(auth_kwargs=kwargs)
21         self.assertTrue(self.driver_mock.called)
22         self.assertEqual(kwargs, self.driver_mock.call_args[1])
23
24     def test_list_kwargs_become_filters(self):
25         # We're also testing tag name translation.
26         driver = self.new_driver(list_kwargs={'tag_test': 'true'})
27         driver.list_nodes()
28         list_method = self.driver_mock().list_nodes
29         self.assertTrue(list_method.called)
30         self.assertEqual({'tag:test': 'true'},
31                           list_method.call_args[1].get('ex_filters'))
32
33     def test_create_image_loaded_at_initialization(self):
34         list_method = self.driver_mock().list_images
35         list_method.return_value = [testutil.cloud_object_mock(c)
36                                     for c in 'abc']
37         driver = self.new_driver(create_kwargs={'image_id': 'id_b'})
38         self.assertEqual(1, list_method.call_count)
39
40     def test_create_includes_ping_secret(self):
41         arv_node = testutil.arvados_node_mock(info={'ping_secret': 'ssshh'})
42         driver = self.new_driver()
43         driver.create_node(testutil.MockSize(1), arv_node)
44         create_method = self.driver_mock().create_node
45         self.assertTrue(create_method.called)
46         self.assertIn('ping_secret=ssshh',
47                       create_method.call_args[1].get('ex_userdata',
48                                                      'arg missing'))
49
50     def test_hostname_from_arvados_node(self):
51         arv_node = testutil.arvados_node_mock(8)
52         driver = self.new_driver()
53         self.assertEqual('compute8.zzzzz.arvadosapi.com',
54                          driver.arvados_create_kwargs(testutil.MockSize(1), arv_node)['name'])
55
56     def test_default_hostname_from_new_arvados_node(self):
57         arv_node = testutil.arvados_node_mock(hostname=None)
58         driver = self.new_driver()
59         self.assertEqual('dynamic.compute.zzzzz.arvadosapi.com',
60                          driver.arvados_create_kwargs(testutil.MockSize(1), arv_node)['name'])
61
62     def check_node_tagged(self, cloud_node, expected_tags):
63         tag_mock = self.driver_mock().ex_create_tags
64         self.assertTrue(tag_mock.called)
65         self.assertIs(cloud_node, tag_mock.call_args[0][0])
66         self.assertEqual(expected_tags, tag_mock.call_args[0][1])
67
68     def test_post_create_node_tags_from_list_kwargs(self):
69         expect_tags = {'key1': 'test value 1', 'key2': 'test value 2'}
70         list_kwargs = {('tag_' + key): value
71                        for key, value in expect_tags.iteritems()}
72         list_kwargs['instance-state-name'] = 'running'
73         cloud_node = testutil.cloud_node_mock()
74         driver = self.new_driver(list_kwargs=list_kwargs)
75         driver.post_create_node(cloud_node)
76         self.check_node_tagged(cloud_node, expect_tags)
77
78     def test_sync_node(self):
79         arv_node = testutil.arvados_node_mock(1)
80         cloud_node = testutil.cloud_node_mock(2)
81         driver = self.new_driver()
82         driver.sync_node(cloud_node, arv_node)
83         self.check_node_tagged(cloud_node,
84                                {'Name': 'compute1.zzzzz.arvadosapi.com'})
85
86     def test_node_create_time(self):
87         refsecs = int(time.time())
88         reftuple = time.gmtime(refsecs)
89         node = testutil.cloud_node_mock()
90         node.extra = {'launch_time': time.strftime('%Y-%m-%dT%H:%M:%S.000Z',
91                                                    reftuple)}
92         self.assertEqual(refsecs, ec2.ComputeNodeDriver.node_start_time(node))
93
94     def test_node_fqdn(self):
95         name = 'fqdntest.zzzzz.arvadosapi.com'
96         node = testutil.cloud_node_mock()
97         node.name = name
98         self.assertEqual(name, ec2.ComputeNodeDriver.node_fqdn(node))
99
100     def test_cloud_exceptions(self):
101         for error in [Exception("test exception"),
102                       IOError("test exception"),
103                       ssl.SSLError("test exception"),
104                       cloud_types.LibcloudError("test exception")]:
105             self.assertTrue(ec2.ComputeNodeDriver.is_cloud_exception(error),
106                             "{} not flagged as cloud exception".format(error))
107
108     def test_noncloud_exceptions(self):
109         self.assertFalse(
110             ec2.ComputeNodeDriver.is_cloud_exception(ValueError("test error")),
111             "ValueError flagged as cloud exception")