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):
24 logging.disable(logging.CRITICAL)
27 self.test = vnf.VnfOnBoarding(
28 project='functest', case_name='aaa')
29 self.test.project = "functest"
30 self.test.start_time = "1"
31 self.test.stop_time = "5"
34 "orchestrator": {"status": "PASS", "result": "", "duration": 20},
35 "vnf": {"status": "PASS", "result": "", "duration": 15},
36 "test_vnf": {"status": "FAIL", "result": "", "duration": 5}}
37 self.test.keystone_client = 'test_client'
38 self.test.tenant_name = 'test_tenant_name'
40 def test_execute_deploy_vnf_fail(self):
41 with mock.patch.object(self.test, 'prepare'),\
42 mock.patch.object(self.test, 'deploy_orchestrator',
44 mock.patch.object(self.test, 'deploy_vnf',
45 side_effect=Exception):
46 self.assertEqual(self.test.execute(),
47 testcase.TestCase.EX_TESTCASE_FAILED)
49 def test_execute_test_vnf_fail(self):
50 with mock.patch.object(self.test, 'prepare'),\
51 mock.patch.object(self.test, 'deploy_orchestrator',
53 mock.patch.object(self.test, 'deploy_vnf'), \
54 mock.patch.object(self.test, 'test_vnf',
55 side_effect=Exception):
56 self.assertEqual(self.test.execute(),
57 testcase.TestCase.EX_TESTCASE_FAILED)
59 @mock.patch('functest.core.vnf.os_utils.get_tenant_id',
60 return_value='test_tenant_id')
61 @mock.patch('functest.core.vnf.os_utils.delete_tenant',
63 @mock.patch('functest.core.vnf.os_utils.get_user_id',
64 return_value='test_user_id')
65 @mock.patch('functest.core.vnf.os_utils.delete_user',
67 def test_execute_default(self, *args):
68 with mock.patch.object(self.test, 'prepare'),\
69 mock.patch.object(self.test, 'deploy_orchestrator',
71 mock.patch.object(self.test, 'deploy_vnf'), \
72 mock.patch.object(self.test, 'test_vnf'), \
73 mock.patch.object(self.test, 'parse_results',
74 return_value='ret_exit_code'), \
75 mock.patch.object(self.test, 'log_results'):
76 self.assertEqual(self.test.execute(),
79 @mock.patch('functest.core.vnf.os_utils.get_credentials')
80 @mock.patch('functest.core.vnf.os_utils.get_keystone_client')
81 @mock.patch('functest.core.vnf.os_utils.get_user_id', return_value='')
82 def test_prepare_missing_userid(self, *args):
83 with self.assertRaises(Exception):
86 @mock.patch('functest.core.vnf.os_utils.get_credentials')
87 @mock.patch('functest.core.vnf.os_utils.get_keystone_client')
88 @mock.patch('functest.core.vnf.os_utils.get_user_id',
89 return_value='test_roleid')
90 @mock.patch('functest.core.vnf.os_utils.create_tenant',
92 def test_prepare_missing_tenantid(self, *args):
93 with self.assertRaises(Exception):
96 @mock.patch('functest.core.vnf.os_utils.get_credentials')
97 @mock.patch('functest.core.vnf.os_utils.get_keystone_client')
98 @mock.patch('functest.core.vnf.os_utils.get_user_id',
99 return_value='test_roleid')
100 @mock.patch('functest.core.vnf.os_utils.create_tenant',
101 return_value='test_tenantid')
102 @mock.patch('functest.core.vnf.os_utils.get_role_id',
104 def test_prepare_missing_roleid(self, *args):
105 with self.assertRaises(Exception):
108 @mock.patch('functest.core.vnf.os_utils.get_credentials')
109 @mock.patch('functest.core.vnf.os_utils.get_keystone_client')
110 @mock.patch('functest.core.vnf.os_utils.get_user_id',
111 return_value='test_roleid')
112 @mock.patch('functest.core.vnf.os_utils.create_tenant',
113 return_value='test_tenantid')
114 @mock.patch('functest.core.vnf.os_utils.get_role_id',
115 return_value='test_roleid')
116 @mock.patch('functest.core.vnf.os_utils.add_role_user',
118 def test_prepare_role_add_failure(self, *args):
119 with self.assertRaises(Exception):
122 @mock.patch('functest.core.vnf.os_utils.get_credentials')
123 @mock.patch('functest.core.vnf.os_utils.get_keystone_client')
124 @mock.patch('functest.core.vnf.os_utils.get_user_id',
125 return_value='test_roleid')
126 @mock.patch('functest.core.vnf.os_utils.create_tenant',
127 return_value='test_tenantid')
128 @mock.patch('functest.core.vnf.os_utils.get_role_id',
129 return_value='test_roleid')
130 @mock.patch('functest.core.vnf.os_utils.add_role_user')
131 @mock.patch('functest.core.vnf.os_utils.create_user',
133 def test_create_user_failure(self, *args):
134 with self.assertRaises(Exception):
137 def test_log_results_default(self):
138 with mock.patch('functest.core.vnf.'
139 'ft_utils.logger_test_results') \
141 self.test.log_results()
142 self.assertTrue(mock_method.called)
144 def test_step_failures_default(self):
145 with self.assertRaises(Exception):
146 self.test.step_failure("error_msg")
148 def test_deploy_vnf_unimplemented(self):
149 with self.assertRaises(Exception) as context:
150 self.test.deploy_vnf()
151 self.assertTrue('VNF not deployed' in context.exception)
153 def test_test_vnf_unimplemented(self):
154 with self.assertRaises(Exception) as context:
155 self.test.test_vnf()()
156 self.assertTrue('VNF not tested' in context.exception)
158 def test_parse_results_ex_ok(self):
159 self.test.details['test_vnf']['status'] = 'PASS'
160 self.assertEqual(self.test.parse_results(), os.EX_OK)
162 def test_parse_results_ex_run_error(self):
163 self.test.details['vnf']['status'] = 'FAIL'
164 self.assertEqual(self.test.parse_results(), os.EX_SOFTWARE)
167 if __name__ == "__main__":
168 unittest.main(verbosity=2)