1 #!/usr/bin/env python
\r
3 # Copyright (c) 2016 Orange and others.
\r
5 # All rights reserved. This program and the accompanying materials
\r
6 # are made available under the terms of the Apache License, Version 2.0
\r
7 # which accompanies this distribution, and is available at
\r
8 # http://www.apache.org/licenses/LICENSE-2.0
\r
10 # pylint: disable=missing-docstring
\r
17 from functest.core import vnf
\r
18 from functest.core import testcase
\r
19 from functest.utils import constants
\r
22 class VnfBaseTesting(unittest.TestCase):
\r
23 """The class testing VNF."""
\r
24 # pylint: disable=missing-docstring,too-many-public-methods
\r
26 tenant_name = 'test_tenant_name'
\r
27 tenant_description = 'description'
\r
30 constants.CONST.__setattr__("vnf_foo_tenant_name", self.tenant_name)
\r
31 constants.CONST.__setattr__(
\r
32 "vnf_foo_tenant_description", self.tenant_description)
\r
33 self.test = vnf.VnfOnBoarding(project='functest', case_name='foo')
\r
35 def test_run_deploy_vnf_exc(self):
\r
36 with mock.patch.object(self.test, 'prepare'),\
\r
37 mock.patch.object(self.test, 'deploy_orchestrator',
\r
38 return_value=None), \
\r
39 mock.patch.object(self.test, 'deploy_vnf',
\r
40 side_effect=Exception):
\r
41 self.assertEqual(self.test.run(),
\r
42 testcase.TestCase.EX_TESTCASE_FAILED)
\r
44 def test_run_test_vnf_exc(self):
\r
45 with mock.patch.object(self.test, 'prepare'),\
\r
46 mock.patch.object(self.test, 'deploy_orchestrator',
\r
47 return_value=None), \
\r
48 mock.patch.object(self.test, 'deploy_vnf'), \
\r
49 mock.patch.object(self.test, 'test_vnf',
\r
50 side_effect=Exception):
\r
51 self.assertEqual(self.test.run(),
\r
52 testcase.TestCase.EX_TESTCASE_FAILED)
\r
54 def test_run_deploy_orch_ko(self):
\r
55 with mock.patch.object(self.test, 'prepare'),\
\r
56 mock.patch.object(self.test, 'deploy_orchestrator',
\r
57 return_value=False), \
\r
58 mock.patch.object(self.test, 'deploy_vnf',
\r
59 return_value=True), \
\r
60 mock.patch.object(self.test, 'test_vnf',
\r
62 self.assertEqual(self.test.run(),
\r
63 testcase.TestCase.EX_TESTCASE_FAILED)
\r
65 def test_run_vnf_deploy_ko(self):
\r
66 with mock.patch.object(self.test, 'prepare'),\
\r
67 mock.patch.object(self.test, 'deploy_orchestrator',
\r
68 return_value=True), \
\r
69 mock.patch.object(self.test, 'deploy_vnf',
\r
70 return_value=False), \
\r
71 mock.patch.object(self.test, 'test_vnf',
\r
73 self.assertEqual(self.test.run(),
\r
74 testcase.TestCase.EX_TESTCASE_FAILED)
\r
76 def test_run_vnf_test_ko(self):
\r
77 with mock.patch.object(self.test, 'prepare'),\
\r
78 mock.patch.object(self.test, 'deploy_orchestrator',
\r
79 return_value=True), \
\r
80 mock.patch.object(self.test, 'deploy_vnf',
\r
81 return_value=True), \
\r
82 mock.patch.object(self.test, 'test_vnf',
\r
83 return_value=False):
\r
84 self.assertEqual(self.test.run(),
\r
85 testcase.TestCase.EX_TESTCASE_FAILED)
\r
87 def test_run_default(self):
\r
88 with mock.patch.object(self.test, 'prepare'),\
\r
89 mock.patch.object(self.test, 'deploy_orchestrator',
\r
90 return_value=True), \
\r
91 mock.patch.object(self.test, 'deploy_vnf',
\r
92 return_value=True), \
\r
93 mock.patch.object(self.test, 'test_vnf',
\r
95 self.assertEqual(self.test.run(), testcase.TestCase.EX_OK)
\r
97 def test_deploy_vnf_unimplemented(self):
\r
98 with self.assertRaises(vnf.VnfDeploymentException):
\r
99 self.test.deploy_vnf()
\r
101 def test_test_vnf_unimplemented(self):
\r
102 with self.assertRaises(vnf.VnfTestException):
\r
103 self.test.test_vnf()
\r
105 @mock.patch('functest.core.vnf.os_utils.get_keystone_client')
\r
106 @mock.patch('functest.core.vnf.os_utils.delete_user',
\r
108 def test_clean_user_already_exist(self, *args):
\r
109 self.test.exist_obj['user'] = True
\r
111 args[0].assert_not_called()
\r
112 args[1].assert_called_once_with()
\r
114 @mock.patch('functest.core.vnf.os_utils.get_keystone_client')
\r
115 @mock.patch('functest.core.vnf.os_utils.delete_user',
\r
117 def test_clean_user_created(self, *args):
\r
118 self.test.exist_obj['user'] = False
\r
120 args[0].assert_called_once_with(mock.ANY, self.tenant_name)
\r
121 args[1].assert_called_once_with()
\r
123 @mock.patch('functest.core.vnf.os_utils.get_keystone_client')
\r
124 @mock.patch('functest.core.vnf.os_utils.delete_tenant',
\r
126 def test_clean_tenant_already_exist(self, *args):
\r
127 self.test.exist_obj['tenant'] = True
\r
129 args[0].assert_not_called()
\r
130 args[1].assert_called_once_with()
\r
132 @mock.patch('functest.core.vnf.os_utils.get_keystone_client')
\r
133 @mock.patch('functest.core.vnf.os_utils.delete_tenant',
\r
135 def test_clean_tenant_created(self, *args):
\r
136 self.test.exist_obj['tenant'] = False
\r
138 args[0].assert_called_once_with(mock.ANY, self.tenant_name)
\r
139 args[1].assert_called_once_with()
\r
141 def test_deploy_orch_unimplemented(self):
\r
142 self.assertTrue(self.test.deploy_orchestrator())
\r
144 @mock.patch('functest.core.vnf.os_utils.get_keystone_client',
\r
145 return_value='test')
\r
146 @mock.patch('functest.core.vnf.os_utils.get_or_create_tenant_for_vnf',
\r
148 @mock.patch('functest.core.vnf.os_utils.get_or_create_user_for_vnf',
\r
150 @mock.patch('functest.core.vnf.os_utils.get_credentials',
\r
151 return_value={'auth_url': 'test'})
\r
152 def test_prepare(self, *args):
\r
153 self.assertEqual(self.test.prepare(),
\r
154 testcase.TestCase.EX_OK)
\r
155 args[0].assert_called_once_with()
\r
156 args[1].assert_called_once_with('test', self.tenant_name)
\r
157 args[2].assert_called_once_with(
\r
158 'test', self.tenant_name, self.tenant_description)
\r
159 args[3].assert_called_once_with()
\r
161 @mock.patch('functest.core.vnf.os_utils.get_keystone_client',
\r
162 side_effect=Exception)
\r
163 def test_prepare_keystone_client_ko(self, *args):
\r
164 with self.assertRaises(vnf.VnfPreparationException):
\r
165 self.test.prepare()
\r
166 args[0].assert_called_once_with()
\r
168 @mock.patch('functest.core.vnf.os_utils.get_keystone_client')
\r
169 @mock.patch('functest.core.vnf.os_utils.get_or_create_tenant_for_vnf',
\r
170 side_effect=Exception)
\r
171 def test_prepare_tenant_creation_ko(self, *args):
\r
172 with self.assertRaises(vnf.VnfPreparationException):
\r
173 self.test.prepare()
\r
174 args[0].assert_called_once_with(
\r
175 mock.ANY, self.tenant_name, self.tenant_description)
\r
176 args[1].assert_called_once_with()
\r
178 @mock.patch('functest.core.vnf.os_utils.get_keystone_client')
\r
179 @mock.patch('functest.core.vnf.os_utils.get_or_create_tenant_for_vnf',
\r
181 @mock.patch('functest.core.vnf.os_utils.get_or_create_user_for_vnf',
\r
182 side_effect=Exception)
\r
183 def test_prepare_user_creation_ko(self, *args):
\r
184 with self.assertRaises(vnf.VnfPreparationException):
\r
185 self.test.prepare()
\r
186 args[0].assert_called_once_with(mock.ANY, self.tenant_name)
\r
187 args[1].assert_called_once_with(
\r
188 mock.ANY, self.tenant_name, self.tenant_description)
\r
189 args[2].assert_called_once_with()
\r
192 if __name__ == "__main__":
\r
193 logging.disable(logging.CRITICAL)
\r
194 unittest.main(verbosity=2)
\r