4670: Node Manager handles more libcloud exceptions.
[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(unittest.TestCase):
16     def setUp(self):
17         self.driver_mock = mock.MagicMock(name='driver_mock')
18
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)
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_list_kwargs_become_filters(self):
32         # We're also testing tag name translation.
33         driver = self.new_driver(list_kwargs={'tag_test': 'true'})
34         driver.list_nodes()
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'))
39
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)
44
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)
49
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',
58                                                      'arg missing'))
59
60     def test_tags_created_from_arvados_node(self):
61         arv_node = testutil.arvados_node_mock(8)
62         cloud_node = testutil.cloud_node_mock(8)
63         driver = self.new_driver(list_kwargs={'tag:list': 'test'})
64         self.assertEqual({'ex_metadata': {'list': 'test'},
65                           'name': 'compute8.zzzzz.arvadosapi.com'},
66                          driver.arvados_create_kwargs(arv_node))
67
68     def test_tags_set_default_hostname_from_new_arvados_node(self):
69         arv_node = testutil.arvados_node_mock(hostname=None)
70         driver = self.new_driver()
71         actual = driver.arvados_create_kwargs(arv_node)
72         self.assertEqual('dynamic.compute.zzzzz.arvadosapi.com',
73                          actual['name'])
74
75     def test_sync_node(self):
76         arv_node = testutil.arvados_node_mock(1)
77         cloud_node = testutil.cloud_node_mock(2)
78         driver = self.new_driver()
79         driver.sync_node(cloud_node, arv_node)
80         tag_mock = self.driver_mock().ex_create_tags
81         self.assertTrue(tag_mock.called)
82         self.assertEqual('compute1.zzzzz.arvadosapi.com',
83                          tag_mock.call_args[0][1].get('Name', 'no name'))
84
85     def test_node_create_time(self):
86         refsecs = int(time.time())
87         reftuple = time.gmtime(refsecs)
88         node = testutil.cloud_node_mock()
89         node.extra = {'launch_time': time.strftime('%Y-%m-%dT%H:%M:%S.000Z',
90                                                    reftuple)}
91         self.assertEqual(refsecs, ec2.ComputeNodeDriver.node_start_time(node))
92
93     def test_cloud_exceptions(self):
94         for error in [Exception("test exception"),
95                       IOError("test exception"),
96                       ssl.SSLError("test exception"),
97                       cloud_types.LibcloudError("test exception")]:
98             self.assertTrue(ec2.ComputeNodeDriver.is_cloud_exception(error),
99                             "{} not flagged as cloud exception".format(error))
100
101     def test_noncloud_exceptions(self):
102         self.assertFalse(
103             ec2.ComputeNodeDriver.is_cloud_exception(ValueError("test error")),
104             "ValueError flagged as cloud exception")