3 # Copyright (c) 2016 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
18 from functest.core import vnf
19 from functest.core import testcase
22 class VnfBaseTesting(unittest.TestCase):
25 self.test = vnf.VnfOnBoarding(
26 project='functest', case_name='aaa')
27 self.test.project = "functest"
28 self.test.start_time = "1"
29 self.test.stop_time = "5"
32 "orchestrator": {"status": "PASS", "result": "", "duration": 20},
33 "vnf": {"status": "PASS", "result": "", "duration": 15},
34 "test_vnf": {"status": "FAIL", "result": "", "duration": 5}}
35 self.test.keystone_client = 'test_client'
36 self.test.tenant_name = 'test_tenant_name'
38 def test_execute_deploy_vnf_fail(self):
39 with mock.patch.object(self.test, 'prepare'),\
40 mock.patch.object(self.test, 'deploy_orchestrator',
42 mock.patch.object(self.test, 'deploy_vnf',
43 side_effect=Exception):
44 self.assertEqual(self.test.execute(),
45 testcase.TestCase.EX_TESTCASE_FAILED)
47 def test_execute_test_vnf_fail(self):
48 with mock.patch.object(self.test, 'prepare'),\
49 mock.patch.object(self.test, 'deploy_orchestrator',
51 mock.patch.object(self.test, 'deploy_vnf'), \
52 mock.patch.object(self.test, 'test_vnf',
53 side_effect=Exception):
54 self.assertEqual(self.test.execute(),
55 testcase.TestCase.EX_TESTCASE_FAILED)
57 @mock.patch('functest.core.vnf.os_utils.get_tenant_id',
58 return_value='test_tenant_id')
59 @mock.patch('functest.core.vnf.os_utils.delete_tenant',
61 @mock.patch('functest.core.vnf.os_utils.get_user_id',
62 return_value='test_user_id')
63 @mock.patch('functest.core.vnf.os_utils.delete_user',
65 def test_execute_default(self, *args):
66 with mock.patch.object(self.test, 'prepare'),\
67 mock.patch.object(self.test, 'deploy_orchestrator',
69 mock.patch.object(self.test, 'deploy_vnf'), \
70 mock.patch.object(self.test, 'test_vnf'), \
71 mock.patch.object(self.test, 'parse_results',
72 return_value='ret_exit_code'), \
73 mock.patch.object(self.test, 'log_results'):
74 self.assertEqual(self.test.execute(),
77 @mock.patch('functest.core.vnf.os_utils.get_credentials')
78 @mock.patch('functest.core.vnf.os_utils.get_keystone_client')
79 @mock.patch('functest.core.vnf.os_utils.get_user_id', return_value='')
80 def test_prepare_missing_userid(self, *args):
81 with self.assertRaises(Exception):
84 @mock.patch('functest.core.vnf.os_utils.get_credentials')
85 @mock.patch('functest.core.vnf.os_utils.get_keystone_client')
86 @mock.patch('functest.core.vnf.os_utils.get_user_id',
87 return_value='test_roleid')
88 @mock.patch('functest.core.vnf.os_utils.create_tenant',
90 def test_prepare_missing_tenantid(self, *args):
91 with self.assertRaises(Exception):
94 @mock.patch('functest.core.vnf.os_utils.get_credentials')
95 @mock.patch('functest.core.vnf.os_utils.get_keystone_client')
96 @mock.patch('functest.core.vnf.os_utils.get_user_id',
97 return_value='test_roleid')
98 @mock.patch('functest.core.vnf.os_utils.create_tenant',
99 return_value='test_tenantid')
100 @mock.patch('functest.core.vnf.os_utils.get_role_id',
102 def test_prepare_missing_roleid(self, *args):
103 with self.assertRaises(Exception):
106 @mock.patch('functest.core.vnf.os_utils.get_credentials')
107 @mock.patch('functest.core.vnf.os_utils.get_keystone_client')
108 @mock.patch('functest.core.vnf.os_utils.get_user_id',
109 return_value='test_roleid')
110 @mock.patch('functest.core.vnf.os_utils.create_tenant',
111 return_value='test_tenantid')
112 @mock.patch('functest.core.vnf.os_utils.get_role_id',
113 return_value='test_roleid')
114 @mock.patch('functest.core.vnf.os_utils.add_role_user',
116 def test_prepare_role_add_failure(self, *args):
117 with self.assertRaises(Exception):
120 @mock.patch('functest.core.vnf.os_utils.get_credentials')
121 @mock.patch('functest.core.vnf.os_utils.get_keystone_client')
122 @mock.patch('functest.core.vnf.os_utils.get_user_id',
123 return_value='test_roleid')
124 @mock.patch('functest.core.vnf.os_utils.create_tenant',
125 return_value='test_tenantid')
126 @mock.patch('functest.core.vnf.os_utils.get_role_id',
127 return_value='test_roleid')
128 @mock.patch('functest.core.vnf.os_utils.add_role_user')
129 @mock.patch('functest.core.vnf.os_utils.create_user',
131 def test_create_user_failure(self, *args):
132 with self.assertRaises(Exception):
135 def test_log_results_default(self):
136 with mock.patch('functest.core.vnf.'
137 'ft_utils.logger_test_results') \
139 self.test.log_results()
140 self.assertTrue(mock_method.called)
142 def test_step_failures_default(self):
143 with self.assertRaises(Exception):
144 self.test.step_failure("error_msg")
146 def test_deploy_vnf_unimplemented(self):
147 with self.assertRaises(Exception) as context:
148 self.test.deploy_vnf()
149 self.assertIn('VNF not deployed', str(context.exception))
151 def test_test_vnf_unimplemented(self):
152 with self.assertRaises(Exception) as context:
153 self.test.test_vnf()()
154 self.assertIn('VNF not tested', str(context.exception))
156 def test_parse_results_ex_ok(self):
157 self.test.details['test_vnf']['status'] = 'PASS'
158 self.assertEqual(self.test.parse_results(), os.EX_OK)
160 def test_parse_results_ex_run_error(self):
161 self.test.details['vnf']['status'] = 'FAIL'
162 self.assertEqual(self.test.parse_results(), os.EX_SOFTWARE)
165 if __name__ == "__main__":
166 logging.disable(logging.CRITICAL)
167 unittest.main(verbosity=2)