Unlink vnf from constants
[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
20 from snaps.openstack.os_credentials import OSCreds
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=vnf.VnfOnBoarding.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=vnf.VnfOnBoarding.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('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):
135             self.test.prepare()
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)
139
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)
148
149     def test_deploy_vnf_unimplemented(self):
150         with self.assertRaises(vnf.VnfDeploymentException):
151             self.test.deploy_vnf()
152
153     def test_test_vnf_unimplemented(self):
154         with self.assertRaises(vnf.VnfTestException):
155             self.test.test_vnf()
156
157     def test_deploy_orch_unimplemented(self):
158         self.assertTrue(self.test.deploy_orchestrator())
159
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):
167             self.test.prepare()
168         args[0].assert_called_once()
169
170     def test_vnf_clean_exc(self):
171         obj = mock.Mock()
172         obj.clean.side_effect = Exception
173         self.test.created_object = [obj]
174         self.test.clean()
175         obj.clean.assert_called_with()
176
177     def test_vnf_clean(self):
178         obj = mock.Mock()
179         self.test.created_object = [obj]
180         self.test.clean()
181         obj.clean.assert_called_with()
182
183
184 if __name__ == "__main__":
185     logging.disable(logging.CRITICAL)
186     unittest.main(verbosity=2)