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
21 from snaps.openstack.os_credentials import OSCreds
24 class VnfBaseTesting(unittest.TestCase):
25 """The class testing VNF."""
26 # pylint: disable=missing-docstring,too-many-public-methods
28 tenant_name = 'test_tenant_name'
29 tenant_description = 'description'
32 self.test = vnf.VnfOnBoarding(project='functest', case_name='foo')
34 def test_run_deploy_orch_exc(self):
35 with mock.patch.object(self.test, 'prepare'), \
36 mock.patch.object(self.test, 'deploy_orchestrator',
37 side_effect=Exception) as mock_method, \
38 mock.patch.object(self.test, 'deploy_vnf',
40 mock.patch.object(self.test, 'test_vnf',
42 self.assertEqual(self.test.run(),
43 testcase.TestCase.EX_TESTCASE_FAILED)
44 mock_method.assert_called_with()
46 def test_run_deploy_vnf_exc(self):
47 with mock.patch.object(self.test, 'prepare'),\
48 mock.patch.object(self.test, 'deploy_orchestrator',
50 mock.patch.object(self.test, 'deploy_vnf',
51 side_effect=Exception) as mock_method:
52 self.assertEqual(self.test.run(),
53 testcase.TestCase.EX_TESTCASE_FAILED)
54 mock_method.assert_called_with()
56 def test_run_test_vnf_exc(self):
57 with mock.patch.object(self.test, 'prepare'),\
58 mock.patch.object(self.test, 'deploy_orchestrator',
60 mock.patch.object(self.test, 'deploy_vnf', return_value=True), \
61 mock.patch.object(self.test, 'test_vnf',
62 side_effect=Exception) as mock_method:
63 self.assertEqual(self.test.run(),
64 testcase.TestCase.EX_TESTCASE_FAILED)
65 mock_method.assert_called_with()
67 def test_run_deploy_orch_ko(self):
68 with mock.patch.object(self.test, 'prepare'),\
69 mock.patch.object(self.test, 'deploy_orchestrator',
70 return_value=False), \
71 mock.patch.object(self.test, 'deploy_vnf',
73 mock.patch.object(self.test, 'test_vnf',
75 self.assertEqual(self.test.run(),
76 testcase.TestCase.EX_TESTCASE_FAILED)
78 def test_run_vnf_deploy_ko(self):
79 with mock.patch.object(self.test, 'prepare'),\
80 mock.patch.object(self.test, 'deploy_orchestrator',
82 mock.patch.object(self.test, 'deploy_vnf',
83 return_value=False), \
84 mock.patch.object(self.test, 'test_vnf',
86 self.assertEqual(self.test.run(),
87 testcase.TestCase.EX_TESTCASE_FAILED)
89 def test_run_vnf_test_ko(self):
90 with mock.patch.object(self.test, 'prepare'),\
91 mock.patch.object(self.test, 'deploy_orchestrator',
93 mock.patch.object(self.test, 'deploy_vnf',
95 mock.patch.object(self.test, 'test_vnf',
97 self.assertEqual(self.test.run(),
98 testcase.TestCase.EX_TESTCASE_FAILED)
100 def test_run_default(self):
101 with mock.patch.object(self.test, 'prepare'),\
102 mock.patch.object(self.test, 'deploy_orchestrator',
103 return_value=True), \
104 mock.patch.object(self.test, 'deploy_vnf',
105 return_value=True), \
106 mock.patch.object(self.test, 'test_vnf',
108 self.assertEqual(self.test.run(), testcase.TestCase.EX_OK)
110 @mock.patch('functest.core.vnf.OpenStackUser')
111 @mock.patch('functest.core.vnf.OpenStackProject')
112 @mock.patch('snaps.openstack.tests.openstack_tests.get_credentials',
113 side_effect=Exception)
114 def test_prepare_exc1(self, *args):
115 with self.assertRaises(Exception):
117 args[0].assert_called_with(os_env_file=constants.ENV_FILE)
118 args[1].assert_not_called()
119 args[2].assert_not_called()
121 @mock.patch('functest.core.vnf.OpenStackUser')
122 @mock.patch('functest.core.vnf.OpenStackProject', side_effect=Exception)
123 @mock.patch('snaps.openstack.tests.openstack_tests.get_credentials')
124 def test_prepare_exc2(self, *args):
125 with self.assertRaises(Exception):
127 args[0].assert_called_with(os_env_file=constants.ENV_FILE)
128 args[1].assert_called_with(mock.ANY, mock.ANY)
129 args[2].assert_not_called()
131 @mock.patch('functest.core.vnf.OpenStackUser', side_effect=Exception)
132 @mock.patch('snaps.openstack.utils.keystone_utils.get_role_by_name',
133 return_value="admin")
134 @mock.patch('snaps.openstack.utils.keystone_utils.keystone_client')
135 @mock.patch('functest.core.vnf.OpenStackProject')
136 @mock.patch('snaps.openstack.tests.openstack_tests.get_credentials')
137 def test_prepare_exc3(self, *args):
138 with self.assertRaises(Exception):
140 args[0].assert_called_with(os_env_file=constants.ENV_FILE)
141 args[1].assert_called_with(mock.ANY, mock.ANY)
142 args[2].assert_called_with(mock.ANY)
143 args[3].assert_called_with(mock.ANY, mock.ANY)
144 args[4].assert_called_with(mock.ANY, mock.ANY)
146 @mock.patch('functest.core.vnf.OpenStackUser')
147 @mock.patch('snaps.openstack.utils.keystone_utils.get_role_by_name',
148 return_value="admin")
149 @mock.patch('snaps.openstack.utils.keystone_utils.keystone_client')
150 @mock.patch('functest.core.vnf.OpenStackProject')
151 @mock.patch('snaps.openstack.tests.openstack_tests.get_credentials')
152 def test_prepare_default(self, *args):
153 self.assertEqual(self.test.prepare(), testcase.TestCase.EX_OK)
154 args[0].assert_called_with(os_env_file=constants.ENV_FILE)
155 args[1].assert_called_with(mock.ANY, mock.ANY)
156 args[2].assert_called_with(mock.ANY)
157 args[3].assert_called_with(mock.ANY, mock.ANY)
158 args[4].assert_called_with(mock.ANY, mock.ANY)
160 def test_deploy_vnf_unimplemented(self):
161 with self.assertRaises(vnf.VnfDeploymentException):
162 self.test.deploy_vnf()
164 def test_test_vnf_unimplemented(self):
165 with self.assertRaises(vnf.VnfTestException):
168 def test_deploy_orch_unimplemented(self):
169 self.assertTrue(self.test.deploy_orchestrator())
171 @mock.patch('snaps.openstack.tests.openstack_tests.get_credentials',
172 return_value=OSCreds(
173 username='user', password='pass',
174 auth_url='http://foo.com:5000/v3', project_name='bar'),
175 side_effect=Exception)
176 def test_prepare_keystone_client_ko(self, *args):
177 with self.assertRaises(vnf.VnfPreparationException):
179 args[0].assert_called_once()
181 def test_vnf_clean_exc(self):
183 obj.clean.side_effect = Exception
184 self.test.created_object = [obj]
186 obj.clean.assert_called_with()
188 def test_vnf_clean(self):
190 self.test.created_object = [obj]
192 obj.clean.assert_called_with()
195 if __name__ == "__main__":
196 logging.disable(logging.CRITICAL)
197 unittest.main(verbosity=2)