Update to Alpine 3.14
[functest.git] / functest / tests / unit / openstack / vmtp / test_vmtp.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2018 Orange and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 # pylint: disable=missing-docstring
11
12 import logging
13 import unittest
14
15 import mock
16 import munch
17 import shade
18
19 from functest.opnfv_tests.openstack.vmtp import vmtp
20
21
22 class VmtpInitTesting(unittest.TestCase):
23
24     def _test_exc_init(self):
25         testcase = vmtp.Vmtp()
26         self.assertEqual(testcase.case_name, "vmtp")
27         self.assertEqual(testcase.result, 0)
28         for func in ['generate_keys', 'write_config', 'run_vmtp']:
29             with self.assertRaises(AssertionError):
30                 getattr(testcase, func)()
31         self.assertEqual(testcase.run(), testcase.EX_RUN_ERROR)
32         self.assertEqual(testcase.clean(), None)
33
34     @mock.patch('os_client_config.get_config', side_effect=Exception)
35     def test_init1(self, *args):
36         self._test_exc_init()
37         args[0].assert_called_once_with()
38
39     @mock.patch('os_client_config.get_config')
40     @mock.patch('shade.OpenStackCloud', side_effect=Exception)
41     def test_init2(self, *args):
42         self._test_exc_init()
43         args[0].assert_called_once_with(cloud_config=mock.ANY)
44         args[1].assert_called_once_with()
45
46     @mock.patch('os_client_config.get_config')
47     @mock.patch('shade.OpenStackCloud')
48     def test_case_name(self, *args):
49         testcase = vmtp.Vmtp(case_name="foo")
50         self.assertEqual(testcase.case_name, "foo")
51         args[0].assert_called_once_with(cloud_config=mock.ANY)
52         args[1].assert_called_once_with()
53
54
55 class VmtpTesting(unittest.TestCase):
56
57     def setUp(self):
58         with mock.patch('os_client_config.get_config'), \
59                 mock.patch('shade.OpenStackCloud'):
60             self.testcase = vmtp.Vmtp()
61             self.testcase.cloud = mock.Mock()
62         self.testcase.cloud.create_keypair.return_value = munch.Munch(
63             private_key="priv", public_key="pub", id="id")
64
65     @mock.patch('six.moves.builtins.open')
66     def test_generate_keys1(self, *args):
67         self.testcase.generate_keys()
68         self.testcase.cloud.create_keypair.assert_called_once_with(
69             'vmtp_{}'.format(self.testcase.guid))
70         self.testcase.cloud.delete_keypair.assert_called_once_with('id')
71         calls = [mock.call(self.testcase.privkey_filename, 'w'),
72                  mock.call(self.testcase.pubkey_filename, 'w')]
73         args[0].assert_has_calls(calls, any_order=True)
74
75     @mock.patch('six.moves.builtins.open')
76     def test_generate_keys2(self, *args):
77         with mock.patch.object(
78                 self.testcase.cloud, "create_keypair",
79                 side_effect=shade.OpenStackCloudException(None)) as mock_obj, \
80                 self.assertRaises(shade.OpenStackCloudException):
81             self.testcase.generate_keys()
82         mock_obj.assert_called_once_with('vmtp_{}'.format(self.testcase.guid))
83         args[0].assert_not_called()
84
85
86 if __name__ == "__main__":
87     logging.disable(logging.CRITICAL)
88     unittest.main(verbosity=2)