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