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
20 from snaps.openstack.os_credentials import OSCreds
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=vnf.VnfOnBoarding.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=vnf.VnfOnBoarding.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('functest.core.vnf.OpenStackProject')
132 @mock.patch('snaps.openstack.tests.openstack_tests.get_credentials')
133 def test_prepare_exc3(self, *args):
134 with self.assertRaises(Exception):
136 args[0].assert_called_with(os_env_file=vnf.VnfOnBoarding.env_file)
137 args[1].assert_called_with(mock.ANY, mock.ANY)
138 args[2].assert_called_with(mock.ANY, mock.ANY)
140 @mock.patch('functest.core.vnf.OpenStackUser')
141 @mock.patch('functest.core.vnf.OpenStackProject')
142 @mock.patch('snaps.openstack.tests.openstack_tests.get_credentials')
143 def test_prepare_default(self, *args):
144 self.assertEqual(self.test.prepare(), testcase.TestCase.EX_OK)
145 args[0].assert_called_with(os_env_file=vnf.VnfOnBoarding.env_file)
146 args[1].assert_called_with(mock.ANY, mock.ANY)
147 args[2].assert_called_with(mock.ANY, mock.ANY)
149 def test_deploy_vnf_unimplemented(self):
150 with self.assertRaises(vnf.VnfDeploymentException):
151 self.test.deploy_vnf()
153 def test_test_vnf_unimplemented(self):
154 with self.assertRaises(vnf.VnfTestException):
157 def test_deploy_orch_unimplemented(self):
158 self.assertTrue(self.test.deploy_orchestrator())
160 @mock.patch('snaps.openstack.tests.openstack_tests.get_credentials',
161 return_value=OSCreds(
162 username='user', password='pass',
163 auth_url='http://foo.com:5000/v3', project_name='bar'),
164 side_effect=Exception)
165 def test_prepare_keystone_client_ko(self, *args):
166 with self.assertRaises(vnf.VnfPreparationException):
168 args[0].assert_called_once()
170 def test_vnf_clean_exc(self):
172 obj.clean.side_effect = Exception
173 self.test.created_object = [obj]
175 obj.clean.assert_called_with()
177 def test_vnf_clean(self):
179 self.test.created_object = [obj]
181 obj.clean.assert_called_with()
184 if __name__ == "__main__":
185 logging.disable(logging.CRITICAL)
186 unittest.main(verbosity=2)