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 '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)
75 @mock.patch('six.moves.builtins.open')
76 def test_generate_keys2(self, *args):
77 # pylint: disable=bad-continuation
78 with mock.patch.object(
79 self.testcase.cloud, "create_keypair",
80 side_effect=shade.OpenStackCloudException(None)) as mock_obj, \
81 self.assertRaises(shade.OpenStackCloudException):
82 self.testcase.generate_keys()
83 mock_obj.assert_called_once_with('vmtp_{}'.format(self.testcase.guid))
84 args[0].assert_not_called()
87 if __name__ == "__main__":
88 logging.disable(logging.CRITICAL)
89 unittest.main(verbosity=2)