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
16 from snaps.openstack.os_credentials import OSCreds
17 from xtesting.core import testcase
19 from functest.core import vnf
20 from functest.utils import constants
23 class VnfBaseTesting(unittest.TestCase):
24 """The class testing VNF."""
25 # pylint: disable=missing-docstring,too-many-public-methods
27 tenant_name = 'test_tenant_name'
28 tenant_description = 'description'
31 self.test = vnf.VnfOnBoarding(project='functest', case_name='foo')
33 def test_run_deploy_orch_exc(self):
34 with mock.patch.object(self.test, 'prepare'), \
35 mock.patch.object(self.test, 'deploy_orchestrator',
36 side_effect=Exception) as mock_method, \
37 mock.patch.object(self.test, 'deploy_vnf',
39 mock.patch.object(self.test, 'test_vnf',
41 self.assertEqual(self.test.run(),
42 testcase.TestCase.EX_TESTCASE_FAILED)
43 mock_method.assert_called_with()
45 def test_run_deploy_vnf_exc(self):
46 with mock.patch.object(self.test, 'prepare'),\
47 mock.patch.object(self.test, 'deploy_orchestrator',
49 mock.patch.object(self.test, 'deploy_vnf',
50 side_effect=Exception) as mock_method:
51 self.assertEqual(self.test.run(),
52 testcase.TestCase.EX_TESTCASE_FAILED)
53 mock_method.assert_called_with()
55 def test_run_test_vnf_exc(self):
56 with mock.patch.object(self.test, 'prepare'),\
57 mock.patch.object(self.test, 'deploy_orchestrator',
59 mock.patch.object(self.test, 'deploy_vnf', return_value=True), \
60 mock.patch.object(self.test, 'test_vnf',
61 side_effect=Exception) as mock_method:
62 self.assertEqual(self.test.run(),
63 testcase.TestCase.EX_TESTCASE_FAILED)
64 mock_method.assert_called_with()
66 def test_run_deploy_orch_ko(self):
67 with mock.patch.object(self.test, 'prepare'),\
68 mock.patch.object(self.test, 'deploy_orchestrator',
69 return_value=False), \
70 mock.patch.object(self.test, 'deploy_vnf',
72 mock.patch.object(self.test, 'test_vnf',
74 self.assertEqual(self.test.run(),
75 testcase.TestCase.EX_TESTCASE_FAILED)
77 def test_run_vnf_deploy_ko(self):
78 with mock.patch.object(self.test, 'prepare'),\
79 mock.patch.object(self.test, 'deploy_orchestrator',
81 mock.patch.object(self.test, 'deploy_vnf',
82 return_value=False), \
83 mock.patch.object(self.test, 'test_vnf',
85 self.assertEqual(self.test.run(),
86 testcase.TestCase.EX_TESTCASE_FAILED)
88 def test_run_vnf_test_ko(self):
89 with mock.patch.object(self.test, 'prepare'),\
90 mock.patch.object(self.test, 'deploy_orchestrator',
92 mock.patch.object(self.test, 'deploy_vnf',
94 mock.patch.object(self.test, 'test_vnf',
96 self.assertEqual(self.test.run(),
97 testcase.TestCase.EX_TESTCASE_FAILED)
99 def test_run_default(self):
100 with mock.patch.object(self.test, 'prepare'),\
101 mock.patch.object(self.test, 'deploy_orchestrator',
102 return_value=True), \
103 mock.patch.object(self.test, 'deploy_vnf',
104 return_value=True), \
105 mock.patch.object(self.test, 'test_vnf',
107 self.assertEqual(self.test.run(), testcase.TestCase.EX_OK)
109 @mock.patch('functest.core.vnf.OpenStackUser')
110 @mock.patch('functest.core.vnf.OpenStackProject')
111 @mock.patch('snaps.openstack.tests.openstack_tests.get_credentials',
112 side_effect=Exception)
113 def test_prepare_exc1(self, *args):
114 with self.assertRaises(Exception):
116 args[0].assert_called_with(os_env_file=constants.ENV_FILE)
117 args[1].assert_not_called()
118 args[2].assert_not_called()
120 @mock.patch('functest.core.vnf.OpenStackUser')
121 @mock.patch('functest.core.vnf.OpenStackProject', side_effect=Exception)
122 @mock.patch('snaps.openstack.tests.openstack_tests.get_credentials')
123 def test_prepare_exc2(self, *args):
124 with self.assertRaises(Exception):
126 args[0].assert_called_with(os_env_file=constants.ENV_FILE)
127 args[1].assert_called_with(mock.ANY, mock.ANY)
128 args[2].assert_not_called()
130 @mock.patch('functest.core.vnf.OpenStackUser', side_effect=Exception)
131 @mock.patch('snaps.openstack.utils.keystone_utils.get_role_by_name',
132 return_value="admin")
133 @mock.patch('snaps.openstack.utils.keystone_utils.keystone_client')
134 @mock.patch('functest.core.vnf.OpenStackProject')
135 @mock.patch('snaps.openstack.tests.openstack_tests.get_credentials')
136 def test_prepare_exc3(self, *args):
137 with self.assertRaises(Exception):
139 args[0].assert_called_with(os_env_file=constants.ENV_FILE)
140 args[1].assert_called_with(mock.ANY, mock.ANY)
141 args[2].assert_called_with(mock.ANY)
142 args[3].assert_called_with(mock.ANY, mock.ANY)
143 args[4].assert_called_with(mock.ANY, mock.ANY)
145 @mock.patch('functest.core.vnf.OpenStackUser')
146 @mock.patch('snaps.openstack.utils.keystone_utils.get_role_by_name',
147 return_value="admin")
148 @mock.patch('snaps.openstack.utils.keystone_utils.keystone_client')
149 @mock.patch('functest.core.vnf.OpenStackProject')
150 @mock.patch('snaps.openstack.tests.openstack_tests.get_credentials')
151 def test_prepare_default(self, *args):
152 self.assertEqual(self.test.prepare(), testcase.TestCase.EX_OK)
153 args[0].assert_called_with(os_env_file=constants.ENV_FILE)
154 args[1].assert_called_with(mock.ANY, mock.ANY)
155 args[2].assert_called_with(mock.ANY)
156 args[3].assert_called_with(mock.ANY, mock.ANY)
157 args[4].assert_called_with(mock.ANY, mock.ANY)
159 def test_deploy_vnf_unimplemented(self):
160 with self.assertRaises(vnf.VnfDeploymentException):
161 self.test.deploy_vnf()
163 def test_test_vnf_unimplemented(self):
164 with self.assertRaises(vnf.VnfTestException):
167 def test_deploy_orch_unimplemented(self):
168 self.assertTrue(self.test.deploy_orchestrator())
170 @mock.patch('snaps.openstack.tests.openstack_tests.get_credentials',
171 return_value=OSCreds(
172 username='user', password='pass',
173 auth_url='http://foo.com:5000/v3', project_name='bar'),
174 side_effect=Exception)
175 def test_prepare_keystone_client_ko(self, *args):
176 with self.assertRaises(vnf.VnfPreparationException):
178 args[0].assert_called_once()
180 def test_vnf_clean_exc(self):
182 obj.clean.side_effect = Exception
183 self.test.created_object = [obj]
185 obj.clean.assert_called_with()
187 def test_vnf_clean(self):
189 self.test.created_object = [obj]
191 obj.clean.assert_called_with()
194 if __name__ == "__main__":
195 logging.disable(logging.CRITICAL)
196 unittest.main(verbosity=2)