Merge "Integrate Patrole as a new testcase"
[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
17 from functest.core import vnf
18 from functest.core import testcase
19 from functest.utils import constants
20
21 from snaps.openstack.os_credentials import OSCreds
22
23
24 class VnfBaseTesting(unittest.TestCase):
25     """The class testing VNF."""
26     # pylint: disable=missing-docstring,too-many-public-methods
27
28     tenant_name = 'test_tenant_name'
29     tenant_description = 'description'
30
31     def setUp(self):
32         self.test = vnf.VnfOnBoarding(project='functest', case_name='foo')
33
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',
39                                   return_value=True), \
40                 mock.patch.object(self.test, 'test_vnf',
41                                   return_value=True):
42             self.assertEqual(self.test.run(),
43                              testcase.TestCase.EX_TESTCASE_FAILED)
44             mock_method.assert_called_with()
45
46     def test_run_deploy_vnf_exc(self):
47         with mock.patch.object(self.test, 'prepare'),\
48             mock.patch.object(self.test, 'deploy_orchestrator',
49                               return_value=True), \
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()
55
56     def test_run_test_vnf_exc(self):
57         with mock.patch.object(self.test, 'prepare'),\
58             mock.patch.object(self.test, 'deploy_orchestrator',
59                               return_value=True), \
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()
66
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',
72                                   return_value=True), \
73                 mock.patch.object(self.test, 'test_vnf',
74                                   return_value=True):
75             self.assertEqual(self.test.run(),
76                              testcase.TestCase.EX_TESTCASE_FAILED)
77
78     def test_run_vnf_deploy_ko(self):
79         with mock.patch.object(self.test, 'prepare'),\
80                 mock.patch.object(self.test, 'deploy_orchestrator',
81                                   return_value=True), \
82                 mock.patch.object(self.test, 'deploy_vnf',
83                                   return_value=False), \
84                 mock.patch.object(self.test, 'test_vnf',
85                                   return_value=True):
86             self.assertEqual(self.test.run(),
87                              testcase.TestCase.EX_TESTCASE_FAILED)
88
89     def test_run_vnf_test_ko(self):
90         with mock.patch.object(self.test, 'prepare'),\
91                 mock.patch.object(self.test, 'deploy_orchestrator',
92                                   return_value=True), \
93                 mock.patch.object(self.test, 'deploy_vnf',
94                                   return_value=True), \
95                 mock.patch.object(self.test, 'test_vnf',
96                                   return_value=False):
97             self.assertEqual(self.test.run(),
98                              testcase.TestCase.EX_TESTCASE_FAILED)
99
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',
107                                   return_value=True):
108             self.assertEqual(self.test.run(), testcase.TestCase.EX_OK)
109
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):
116             self.test.prepare()
117         args[0].assert_called_with(os_env_file=constants.ENV_FILE)
118         args[1].assert_not_called()
119         args[2].assert_not_called()
120
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):
126             self.test.prepare()
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()
130
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):
139             self.test.prepare()
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)
145
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)
159
160     def test_deploy_vnf_unimplemented(self):
161         with self.assertRaises(vnf.VnfDeploymentException):
162             self.test.deploy_vnf()
163
164     def test_test_vnf_unimplemented(self):
165         with self.assertRaises(vnf.VnfTestException):
166             self.test.test_vnf()
167
168     def test_deploy_orch_unimplemented(self):
169         self.assertTrue(self.test.deploy_orchestrator())
170
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):
178             self.test.prepare()
179         args[0].assert_called_once()
180
181     def test_vnf_clean_exc(self):
182         obj = mock.Mock()
183         obj.clean.side_effect = Exception
184         self.test.created_object = [obj]
185         self.test.clean()
186         obj.clean.assert_called_with()
187
188     def test_vnf_clean(self):
189         obj = mock.Mock()
190         self.test.created_object = [obj]
191         self.test.clean()
192         obj.clean.assert_called_with()
193
194
195 if __name__ == "__main__":
196     logging.disable(logging.CRITICAL)
197     unittest.main(verbosity=2)