Leverage on Xtesting
[functest.git] / functest / tests / unit / core / test_vnf.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2016 Orange and others.
4 #
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
9
10 # pylint: disable=missing-docstring
11
12 import logging
13 import unittest
14
15 import mock
16 from snaps.openstack.os_credentials import OSCreds
17 from xtesting.core import testcase
18
19 from functest.core import vnf
20 from functest.utils import constants
21
22
23 class VnfBaseTesting(unittest.TestCase):
24     """The class testing VNF."""
25     # pylint: disable=missing-docstring,too-many-public-methods
26
27     tenant_name = 'test_tenant_name'
28     tenant_description = 'description'
29
30     def setUp(self):
31         self.test = vnf.VnfOnBoarding(project='functest', case_name='foo')
32
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',
38                                   return_value=True), \
39                 mock.patch.object(self.test, 'test_vnf',
40                                   return_value=True):
41             self.assertEqual(self.test.run(),
42                              testcase.TestCase.EX_TESTCASE_FAILED)
43             mock_method.assert_called_with()
44
45     def test_run_deploy_vnf_exc(self):
46         with mock.patch.object(self.test, 'prepare'),\
47             mock.patch.object(self.test, 'deploy_orchestrator',
48                               return_value=True), \
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()
54
55     def test_run_test_vnf_exc(self):
56         with mock.patch.object(self.test, 'prepare'),\
57             mock.patch.object(self.test, 'deploy_orchestrator',
58                               return_value=True), \
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()
65
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',
71                                   return_value=True), \
72                 mock.patch.object(self.test, 'test_vnf',
73                                   return_value=True):
74             self.assertEqual(self.test.run(),
75                              testcase.TestCase.EX_TESTCASE_FAILED)
76
77     def test_run_vnf_deploy_ko(self):
78         with mock.patch.object(self.test, 'prepare'),\
79                 mock.patch.object(self.test, 'deploy_orchestrator',
80                                   return_value=True), \
81                 mock.patch.object(self.test, 'deploy_vnf',
82                                   return_value=False), \
83                 mock.patch.object(self.test, 'test_vnf',
84                                   return_value=True):
85             self.assertEqual(self.test.run(),
86                              testcase.TestCase.EX_TESTCASE_FAILED)
87
88     def test_run_vnf_test_ko(self):
89         with mock.patch.object(self.test, 'prepare'),\
90                 mock.patch.object(self.test, 'deploy_orchestrator',
91                                   return_value=True), \
92                 mock.patch.object(self.test, 'deploy_vnf',
93                                   return_value=True), \
94                 mock.patch.object(self.test, 'test_vnf',
95                                   return_value=False):
96             self.assertEqual(self.test.run(),
97                              testcase.TestCase.EX_TESTCASE_FAILED)
98
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',
106                                   return_value=True):
107             self.assertEqual(self.test.run(), testcase.TestCase.EX_OK)
108
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):
115             self.test.prepare()
116         args[0].assert_called_with(os_env_file=constants.ENV_FILE)
117         args[1].assert_not_called()
118         args[2].assert_not_called()
119
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):
125             self.test.prepare()
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()
129
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):
138             self.test.prepare()
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)
144
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)
158
159     def test_deploy_vnf_unimplemented(self):
160         with self.assertRaises(vnf.VnfDeploymentException):
161             self.test.deploy_vnf()
162
163     def test_test_vnf_unimplemented(self):
164         with self.assertRaises(vnf.VnfTestException):
165             self.test.test_vnf()
166
167     def test_deploy_orch_unimplemented(self):
168         self.assertTrue(self.test.deploy_orchestrator())
169
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):
177             self.test.prepare()
178         args[0].assert_called_once()
179
180     def test_vnf_clean_exc(self):
181         obj = mock.Mock()
182         obj.clean.side_effect = Exception
183         self.test.created_object = [obj]
184         self.test.clean()
185         obj.clean.assert_called_with()
186
187     def test_vnf_clean(self):
188         obj = mock.Mock()
189         self.test.created_object = [obj]
190         self.test.clean()
191         obj.clean.assert_called_with()
192
193
194 if __name__ == "__main__":
195     logging.disable(logging.CRITICAL)
196     unittest.main(verbosity=2)