3 # Copyright (c) 2018 Orange and others.
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
10 # pylint: disable=missing-docstring
19 from functest.opnfv_tests.openstack.vmtp import vmtp
22 class VmtpInitTesting(unittest.TestCase):
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)
34 @mock.patch('os_client_config.get_config', side_effect=Exception)
35 def test_init1(self, *args):
37 args[0].assert_called_once_with()
39 @mock.patch('os_client_config.get_config')
40 @mock.patch('shade.OpenStackCloud', side_effect=Exception)
41 def test_init2(self, *args):
43 args[0].assert_called_once_with(cloud_config=mock.ANY)
44 args[1].assert_called_once_with()
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()
55 class VmtpTesting(unittest.TestCase):
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")
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 f'vmtp_{self.testcase.guid}')
70 self.testcase.cloud.delete_keypair.assert_called_once_with('id')
72 self.testcase.privkey_filename, 'w', encoding='utf-8'),
74 self.testcase.pubkey_filename, 'w', encoding='utf-8')]
75 args[0].assert_has_calls(calls, any_order=True)
77 @mock.patch('six.moves.builtins.open')
78 def test_generate_keys2(self, *args):
79 with mock.patch.object(
80 self.testcase.cloud, "create_keypair",
81 side_effect=shade.OpenStackCloudException(None)) as mock_obj, \
82 self.assertRaises(shade.OpenStackCloudException):
83 self.testcase.generate_keys()
84 mock_obj.assert_called_once_with(f'vmtp_{self.testcase.guid}')
85 args[0].assert_not_called()
88 if __name__ == "__main__":
89 logging.disable(logging.CRITICAL)
90 unittest.main(verbosity=2)