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('functest.core.vnf.OpenStackProject')
133 @mock.patch('snaps.openstack.tests.openstack_tests.get_credentials')
134 def test_prepare_exc3(self, *args):
135 with self.assertRaises(Exception):
137 args[0].assert_called_with(os_env_file=constants.ENV_FILE)
138 args[1].assert_called_with(mock.ANY, mock.ANY)
139 args[2].assert_called_with(mock.ANY, mock.ANY)
141 @mock.patch('functest.core.vnf.OpenStackUser')
142 @mock.patch('functest.core.vnf.OpenStackProject')
143 @mock.patch('snaps.openstack.tests.openstack_tests.get_credentials')
144 def test_prepare_default(self, *args):
145 self.assertEqual(self.test.prepare(), testcase.TestCase.EX_OK)
146 args[0].assert_called_with(os_env_file=constants.ENV_FILE)
147 args[1].assert_called_with(mock.ANY, mock.ANY)
148 args[2].assert_called_with(mock.ANY, mock.ANY)
150 def test_deploy_vnf_unimplemented(self):
151 with self.assertRaises(vnf.VnfDeploymentException):
152 self.test.deploy_vnf()
154 def test_test_vnf_unimplemented(self):
155 with self.assertRaises(vnf.VnfTestException):
158 def test_deploy_orch_unimplemented(self):
159 self.assertTrue(self.test.deploy_orchestrator())
161 @mock.patch('snaps.openstack.tests.openstack_tests.get_credentials',
162 return_value=OSCreds(
163 username='user', password='pass',
164 auth_url='http://foo.com:5000/v3', project_name='bar'),
165 side_effect=Exception)
166 def test_prepare_keystone_client_ko(self, *args):
167 with self.assertRaises(vnf.VnfPreparationException):
169 args[0].assert_called_once()
171 def test_vnf_clean_exc(self):
173 obj.clean.side_effect = Exception
174 self.test.created_object = [obj]
176 obj.clean.assert_called_with()
178 def test_vnf_clean(self):
180 self.test.created_object = [obj]
182 obj.clean.assert_called_with()
185 if __name__ == "__main__":
186 logging.disable(logging.CRITICAL)
187 unittest.main(verbosity=2)