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
17 from functest.core import vnf
18 from functest.core import testcase
19 from functest.utils import constants
22 class VnfBaseTesting(unittest.TestCase):
23 """The class testing VNF."""
24 # pylint: disable=missing-docstring,too-many-public-methods
26 tenant_name = 'test_tenant_name'
27 tenant_description = 'description'
30 constants.CONST.__setattr__("vnf_foo_tenant_name", self.tenant_name)
31 constants.CONST.__setattr__(
32 "vnf_foo_tenant_description", self.tenant_description)
33 self.test = vnf.VnfOnBoarding(project='functest', case_name='foo')
35 def test_run_deploy_vnf_exc(self):
36 with mock.patch.object(self.test, 'prepare'),\
37 mock.patch.object(self.test, 'deploy_orchestrator',
39 mock.patch.object(self.test, 'deploy_vnf',
40 side_effect=Exception):
41 self.assertEqual(self.test.run(),
42 testcase.TestCase.EX_TESTCASE_FAILED)
44 def test_run_test_vnf_exc(self):
45 with mock.patch.object(self.test, 'prepare'),\
46 mock.patch.object(self.test, 'deploy_orchestrator',
48 mock.patch.object(self.test, 'deploy_vnf'), \
49 mock.patch.object(self.test, 'test_vnf',
50 side_effect=Exception):
51 self.assertEqual(self.test.run(),
52 testcase.TestCase.EX_TESTCASE_FAILED)
54 def test_run_deploy_orch_ko(self):
55 with mock.patch.object(self.test, 'prepare'),\
56 mock.patch.object(self.test, 'deploy_orchestrator',
57 return_value=False), \
58 mock.patch.object(self.test, 'deploy_vnf',
60 mock.patch.object(self.test, 'test_vnf',
62 self.assertEqual(self.test.run(),
63 testcase.TestCase.EX_TESTCASE_FAILED)
65 def test_run_vnf_deploy_ko(self):
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 return_value=False), \
71 mock.patch.object(self.test, 'test_vnf',
73 self.assertEqual(self.test.run(),
74 testcase.TestCase.EX_TESTCASE_FAILED)
76 def test_run_vnf_test_ko(self):
77 with mock.patch.object(self.test, 'prepare'),\
78 mock.patch.object(self.test, 'deploy_orchestrator',
80 mock.patch.object(self.test, 'deploy_vnf',
82 mock.patch.object(self.test, 'test_vnf',
84 self.assertEqual(self.test.run(),
85 testcase.TestCase.EX_TESTCASE_FAILED)
87 def test_run_default(self):
88 with mock.patch.object(self.test, 'prepare'),\
89 mock.patch.object(self.test, 'deploy_orchestrator',
91 mock.patch.object(self.test, 'deploy_vnf',
93 mock.patch.object(self.test, 'test_vnf',
95 self.assertEqual(self.test.run(), testcase.TestCase.EX_OK)
97 def test_deploy_vnf_unimplemented(self):
98 with self.assertRaises(vnf.VnfDeploymentException):
99 self.test.deploy_vnf()
101 def test_test_vnf_unimplemented(self):
102 with self.assertRaises(vnf.VnfTestException):
105 @mock.patch('functest.core.vnf.os_utils.get_keystone_client')
106 @mock.patch('functest.core.vnf.os_utils.delete_user',
108 def test_clean_user_already_exist(self, *args):
109 self.test.exist_obj['user'] = True
111 args[0].assert_not_called()
112 args[1].assert_called_once_with()
114 @mock.patch('functest.core.vnf.os_utils.get_keystone_client')
115 @mock.patch('functest.core.vnf.os_utils.delete_user',
117 def test_clean_user_created(self, *args):
118 self.test.exist_obj['user'] = False
120 args[0].assert_called_once_with(mock.ANY, self.tenant_name)
121 args[1].assert_called_once_with()
123 @mock.patch('functest.core.vnf.os_utils.get_keystone_client')
124 @mock.patch('functest.core.vnf.os_utils.delete_tenant',
126 def test_clean_tenant_already_exist(self, *args):
127 self.test.exist_obj['tenant'] = True
129 args[0].assert_not_called()
130 args[1].assert_called_once_with()
132 @mock.patch('functest.core.vnf.os_utils.get_keystone_client')
133 @mock.patch('functest.core.vnf.os_utils.delete_tenant',
135 def test_clean_tenant_created(self, *args):
136 self.test.exist_obj['tenant'] = False
138 args[0].assert_called_once_with(mock.ANY, self.tenant_name)
139 args[1].assert_called_once_with()
141 def test_deploy_orch_unimplemented(self):
142 self.assertTrue(self.test.deploy_orchestrator())
144 @mock.patch('functest.core.vnf.os_utils.get_keystone_client',
146 @mock.patch('functest.core.vnf.os_utils.get_or_create_tenant_for_vnf',
148 @mock.patch('functest.core.vnf.os_utils.get_or_create_user_for_vnf',
150 @mock.patch('functest.core.vnf.os_utils.get_credentials',
151 return_value={'auth_url': 'test'})
152 def test_prepare(self, *args):
153 self.assertEqual(self.test.prepare(),
154 testcase.TestCase.EX_OK)
155 args[0].assert_called_once_with()
156 args[1].assert_called_once_with('test', self.tenant_name)
157 args[2].assert_called_once_with(
158 'test', self.tenant_name, self.tenant_description)
159 args[3].assert_called_once_with()
161 @mock.patch('functest.core.vnf.os_utils.get_keystone_client',
162 side_effect=Exception)
163 def test_prepare_keystone_client_ko(self, *args):
164 with self.assertRaises(vnf.VnfPreparationException):
166 args[0].assert_called_once_with()
168 @mock.patch('functest.core.vnf.os_utils.get_keystone_client')
169 @mock.patch('functest.core.vnf.os_utils.get_or_create_tenant_for_vnf',
170 side_effect=Exception)
171 def test_prepare_tenant_creation_ko(self, *args):
172 with self.assertRaises(vnf.VnfPreparationException):
174 args[0].assert_called_once_with(
175 mock.ANY, self.tenant_name, self.tenant_description)
176 args[1].assert_called_once_with()
178 @mock.patch('functest.core.vnf.os_utils.get_keystone_client')
179 @mock.patch('functest.core.vnf.os_utils.get_or_create_tenant_for_vnf',
181 @mock.patch('functest.core.vnf.os_utils.get_or_create_user_for_vnf',
182 side_effect=Exception)
183 def test_prepare_user_creation_ko(self, *args):
184 with self.assertRaises(vnf.VnfPreparationException):
186 args[0].assert_called_once_with(mock.ANY, self.tenant_name)
187 args[1].assert_called_once_with(
188 mock.ANY, self.tenant_name, self.tenant_description)
189 args[2].assert_called_once_with()
192 if __name__ == "__main__":
193 logging.disable(logging.CRITICAL)
194 unittest.main(verbosity=2)