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 constants.CONST.__setattr__("vnf_foo_tenant_name", self.tenant_name)
33 constants.CONST.__setattr__(
34 "vnf_foo_tenant_description", self.tenant_description)
35 self.test = vnf.VnfOnBoarding(project='functest', case_name='foo')
37 def test_run_deploy_orch_exc(self):
38 with mock.patch.object(self.test, 'prepare'), \
39 mock.patch.object(self.test, 'deploy_orchestrator',
40 side_effect=Exception) as mock_method, \
41 mock.patch.object(self.test, 'deploy_vnf',
43 mock.patch.object(self.test, 'test_vnf',
45 self.assertEqual(self.test.run(),
46 testcase.TestCase.EX_TESTCASE_FAILED)
47 mock_method.assert_called_with()
49 def test_run_deploy_vnf_exc(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 side_effect=Exception) as mock_method:
55 self.assertEqual(self.test.run(),
56 testcase.TestCase.EX_TESTCASE_FAILED)
57 mock_method.assert_called_with()
59 def test_run_test_vnf_exc(self):
60 with mock.patch.object(self.test, 'prepare'),\
61 mock.patch.object(self.test, 'deploy_orchestrator',
63 mock.patch.object(self.test, 'deploy_vnf', return_value=True), \
64 mock.patch.object(self.test, 'test_vnf',
65 side_effect=Exception) as mock_method:
66 self.assertEqual(self.test.run(),
67 testcase.TestCase.EX_TESTCASE_FAILED)
68 mock_method.assert_called_with()
70 def test_run_deploy_orch_ko(self):
71 with mock.patch.object(self.test, 'prepare'),\
72 mock.patch.object(self.test, 'deploy_orchestrator',
73 return_value=False), \
74 mock.patch.object(self.test, 'deploy_vnf',
76 mock.patch.object(self.test, 'test_vnf',
78 self.assertEqual(self.test.run(),
79 testcase.TestCase.EX_TESTCASE_FAILED)
81 def test_run_vnf_deploy_ko(self):
82 with mock.patch.object(self.test, 'prepare'),\
83 mock.patch.object(self.test, 'deploy_orchestrator',
85 mock.patch.object(self.test, 'deploy_vnf',
86 return_value=False), \
87 mock.patch.object(self.test, 'test_vnf',
89 self.assertEqual(self.test.run(),
90 testcase.TestCase.EX_TESTCASE_FAILED)
92 def test_run_vnf_test_ko(self):
93 with mock.patch.object(self.test, 'prepare'),\
94 mock.patch.object(self.test, 'deploy_orchestrator',
96 mock.patch.object(self.test, 'deploy_vnf',
98 mock.patch.object(self.test, 'test_vnf',
100 self.assertEqual(self.test.run(),
101 testcase.TestCase.EX_TESTCASE_FAILED)
103 def test_run_default(self):
104 with mock.patch.object(self.test, 'prepare'),\
105 mock.patch.object(self.test, 'deploy_orchestrator',
106 return_value=True), \
107 mock.patch.object(self.test, 'deploy_vnf',
108 return_value=True), \
109 mock.patch.object(self.test, 'test_vnf',
111 self.assertEqual(self.test.run(), testcase.TestCase.EX_OK)
113 @mock.patch('functest.core.vnf.OpenStackUser')
114 @mock.patch('functest.core.vnf.OpenStackProject')
115 @mock.patch('snaps.openstack.tests.openstack_tests.get_credentials',
116 side_effect=Exception)
117 def test_prepare_exc1(self, *args):
118 with self.assertRaises(Exception):
120 args[0].assert_called_with(
121 os_env_file=constants.CONST.__getattribute__('env_file'))
122 args[1].assert_not_called()
123 args[2].assert_not_called()
125 @mock.patch('functest.core.vnf.OpenStackUser')
126 @mock.patch('functest.core.vnf.OpenStackProject', side_effect=Exception)
127 @mock.patch('snaps.openstack.tests.openstack_tests.get_credentials')
128 def test_prepare_exc2(self, *args):
129 with self.assertRaises(Exception):
131 args[0].assert_called_with(
132 os_env_file=constants.CONST.__getattribute__('env_file'))
133 args[1].assert_called_with(mock.ANY, mock.ANY)
134 args[2].assert_not_called()
136 @mock.patch('functest.core.vnf.OpenStackUser', side_effect=Exception)
137 @mock.patch('functest.core.vnf.OpenStackProject')
138 @mock.patch('snaps.openstack.tests.openstack_tests.get_credentials')
139 def test_prepare_exc3(self, *args):
140 with self.assertRaises(Exception):
142 args[0].assert_called_with(
143 os_env_file=constants.CONST.__getattribute__('env_file'))
144 args[1].assert_called_with(mock.ANY, mock.ANY)
145 args[2].assert_called_with(mock.ANY, mock.ANY)
147 @mock.patch('functest.core.vnf.OpenStackUser')
148 @mock.patch('functest.core.vnf.OpenStackProject')
149 @mock.patch('snaps.openstack.tests.openstack_tests.get_credentials')
150 def test_prepare_default(self, *args):
151 self.assertEqual(self.test.prepare(), testcase.TestCase.EX_OK)
152 args[0].assert_called_with(
153 os_env_file=constants.CONST.__getattribute__('env_file'))
154 args[1].assert_called_with(mock.ANY, mock.ANY)
155 args[2].assert_called_with(mock.ANY, mock.ANY)
157 def test_deploy_vnf_unimplemented(self):
158 with self.assertRaises(vnf.VnfDeploymentException):
159 self.test.deploy_vnf()
161 def test_test_vnf_unimplemented(self):
162 with self.assertRaises(vnf.VnfTestException):
165 def test_deploy_orch_unimplemented(self):
166 self.assertTrue(self.test.deploy_orchestrator())
168 @mock.patch('snaps.openstack.tests.openstack_tests.get_credentials',
169 return_value=OSCreds(
170 username='user', password='pass',
171 auth_url='http://foo.com:5000/v3', project_name='bar'),
172 side_effect=Exception)
173 def test_prepare_keystone_client_ko(self, *args):
174 with self.assertRaises(vnf.VnfPreparationException):
176 args[0].assert_called_once()
178 def test_vnf_clean_exc(self):
180 obj.clean.side_effect = Exception
181 self.test.created_object = [obj]
183 obj.clean.assert_called_with()
185 def test_vnf_clean(self):
187 self.test.created_object = [obj]
189 obj.clean.assert_called_with()
192 if __name__ == "__main__":
193 logging.disable(logging.CRITICAL)
194 unittest.main(verbosity=2)