Merge "Enable https insecure for functest"
[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
22 class VnfBaseTesting(unittest.TestCase):
23     """The class testing VNF."""
24     # pylint: disable=missing-docstring,too-many-public-methods
25
26     tenant_name = 'test_tenant_name'
27     tenant_description = 'description'
28
29     def setUp(self):
30         constants.CONST.__setattr__("vnf_foo_tenant_name", self.tenant_name)
31         constants.CONST.__setattr__(
32             "vnf_foo_tenant_description", self.tenant_description)
33         self.test = vnf.VnfOnBoarding(project='functest', case_name='foo')
34
35     def test_run_deploy_vnf_exc(self):
36         with mock.patch.object(self.test, 'prepare'),\
37             mock.patch.object(self.test, 'deploy_orchestrator',
38                               return_value=None), \
39             mock.patch.object(self.test, 'deploy_vnf',
40                               side_effect=Exception):
41             self.assertEqual(self.test.run(),
42                              testcase.TestCase.EX_TESTCASE_FAILED)
43
44     def test_run_test_vnf_exc(self):
45         with mock.patch.object(self.test, 'prepare'),\
46             mock.patch.object(self.test, 'deploy_orchestrator',
47                               return_value=None), \
48             mock.patch.object(self.test, 'deploy_vnf'), \
49             mock.patch.object(self.test, 'test_vnf',
50                               side_effect=Exception):
51             self.assertEqual(self.test.run(),
52                              testcase.TestCase.EX_TESTCASE_FAILED)
53
54     def test_run_deploy_orch_ko(self):
55         with mock.patch.object(self.test, 'prepare'),\
56                 mock.patch.object(self.test, 'deploy_orchestrator',
57                                   return_value=False), \
58                 mock.patch.object(self.test, 'deploy_vnf',
59                                   return_value=True), \
60                 mock.patch.object(self.test, 'test_vnf',
61                                   return_value=True):
62             self.assertEqual(self.test.run(),
63                              testcase.TestCase.EX_TESTCASE_FAILED)
64
65     def test_run_vnf_deploy_ko(self):
66         with mock.patch.object(self.test, 'prepare'),\
67                 mock.patch.object(self.test, 'deploy_orchestrator',
68                                   return_value=True), \
69                 mock.patch.object(self.test, 'deploy_vnf',
70                                   return_value=False), \
71                 mock.patch.object(self.test, 'test_vnf',
72                                   return_value=True):
73             self.assertEqual(self.test.run(),
74                              testcase.TestCase.EX_TESTCASE_FAILED)
75
76     def test_run_vnf_test_ko(self):
77         with mock.patch.object(self.test, 'prepare'),\
78                 mock.patch.object(self.test, 'deploy_orchestrator',
79                                   return_value=True), \
80                 mock.patch.object(self.test, 'deploy_vnf',
81                                   return_value=True), \
82                 mock.patch.object(self.test, 'test_vnf',
83                                   return_value=False):
84             self.assertEqual(self.test.run(),
85                              testcase.TestCase.EX_TESTCASE_FAILED)
86
87     def test_run_default(self):
88         with mock.patch.object(self.test, 'prepare'),\
89                 mock.patch.object(self.test, 'deploy_orchestrator',
90                                   return_value=True), \
91                 mock.patch.object(self.test, 'deploy_vnf',
92                                   return_value=True), \
93                 mock.patch.object(self.test, 'test_vnf',
94                                   return_value=True):
95             self.assertEqual(self.test.run(), testcase.TestCase.EX_OK)
96
97     def test_deploy_vnf_unimplemented(self):
98         with self.assertRaises(vnf.VnfDeploymentException):
99             self.test.deploy_vnf()
100
101     def test_test_vnf_unimplemented(self):
102         with self.assertRaises(vnf.VnfTestException):
103             self.test.test_vnf()
104
105     @mock.patch('functest.core.vnf.os_utils.get_keystone_client')
106     @mock.patch('functest.core.vnf.os_utils.delete_user',
107                 return_value=True)
108     def test_clean_user_set(self, *args):
109         self.test.user_created = True
110         self.test.clean()
111         args[0].assert_called_once_with(mock.ANY, self.tenant_name)
112         args[1].assert_called_once_with()
113
114     @mock.patch('functest.core.vnf.os_utils.get_keystone_client')
115     @mock.patch('functest.core.vnf.os_utils.delete_user',
116                 return_value=True)
117     def test_clean_user_unset(self, *args):
118         self.test.user_created = False
119         self.test.clean()
120         args[0].assert_not_called()
121         args[1].assert_called_once_with()
122
123     @mock.patch('functest.core.vnf.os_utils.get_keystone_client')
124     @mock.patch('functest.core.vnf.os_utils.delete_tenant',
125                 return_value=True)
126     def test_clean_tenant_set(self, *args):
127         self.test.tenant_created = True
128         self.test.clean()
129         args[0].assert_called_once_with(mock.ANY, self.tenant_name)
130         args[1].assert_called_once_with()
131
132     @mock.patch('functest.core.vnf.os_utils.get_keystone_client')
133     @mock.patch('functest.core.vnf.os_utils.delete_tenant',
134                 return_value=True)
135     def test_clean_tenant_unset(self, *args):
136         self.test.tenant_created = False
137         self.test.clean()
138         args[0].assert_not_called()
139         args[1].assert_called_once_with()
140
141     def test_deploy_orch_unimplemented(self):
142         self.assertTrue(self.test.deploy_orchestrator())
143
144     @mock.patch('functest.core.vnf.os_utils.get_credentials',
145                 return_value={'creds': 'test'})
146     @mock.patch('functest.core.vnf.os_utils.get_keystone_client',
147                 return_value='test')
148     @mock.patch('functest.core.vnf.os_utils.get_or_create_tenant_for_vnf',
149                 return_value=0)
150     @mock.patch('functest.core.vnf.os_utils.get_or_create_user_for_vnf',
151                 return_value=0)
152     def test_prepare(self, *args):
153         self.assertEqual(self.test.prepare(),
154                          testcase.TestCase.EX_OK)
155         args[0].assert_called_once_with('test', self.tenant_name)
156         args[1].assert_called_once_with(
157             'test', self.tenant_name, self.tenant_description)
158         args[2].assert_called_once_with()
159         args[3].assert_called_once_with()
160
161     @mock.patch('functest.core.vnf.os_utils.get_credentials',
162                 side_effect=Exception)
163     def test_prepare_admin_creds_ko(self, *args):
164         with self.assertRaises(vnf.VnfPreparationException):
165             self.test.prepare()
166         args[0].assert_called_once_with()
167
168     @mock.patch('functest.core.vnf.os_utils.get_credentials',
169                 return_value='creds')
170     @mock.patch('functest.core.vnf.os_utils.get_keystone_client',
171                 side_effect=Exception)
172     def test_prepare_keystone_client_ko(self, *args):
173         with self.assertRaises(vnf.VnfPreparationException):
174             self.test.prepare()
175         args[0].assert_called_once_with()
176         args[1].assert_called_once_with()
177
178     @mock.patch('functest.core.vnf.os_utils.get_credentials',
179                 return_value='creds')
180     @mock.patch('functest.core.vnf.os_utils.get_keystone_client')
181     @mock.patch('functest.core.vnf.os_utils.get_or_create_tenant_for_vnf',
182                 side_effect=Exception)
183     def test_prepare_tenant_creation_ko(self, *args):
184         with self.assertRaises(vnf.VnfPreparationException):
185             self.test.prepare()
186         args[0].assert_called_once_with(
187             mock.ANY, self.tenant_name, self.tenant_description)
188         args[1].assert_called_once_with()
189         args[2].assert_called_once_with()
190
191     @mock.patch('functest.core.vnf.os_utils.get_credentials',
192                 return_value='creds')
193     @mock.patch('functest.core.vnf.os_utils.get_keystone_client')
194     @mock.patch('functest.core.vnf.os_utils.get_or_create_tenant_for_vnf',
195                 return_value=0)
196     @mock.patch('functest.core.vnf.os_utils.get_or_create_user_for_vnf',
197                 side_effect=Exception)
198     def test_prepare_user_creation_ko(self, *args):
199         with self.assertRaises(vnf.VnfPreparationException):
200             self.test.prepare()
201         args[0].assert_called_once_with(mock.ANY, self.tenant_name)
202         args[1].assert_called_once_with(
203             mock.ANY, self.tenant_name, self.tenant_description)
204         args[2].assert_called_once_with()
205         args[3].assert_called_once_with()
206
207
208 if __name__ == "__main__":
209     logging.disable(logging.CRITICAL)
210     unittest.main(verbosity=2)