Fully cover vnf
[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         constants.CONST.__setattr__("vnf_foo_tenant_name", self.tenant_name)
33         constants.CONST.__setattr__(
34             "vnf_foo_tenant_description", self.tenant_description)
35         self.test = vnf.VnfOnBoarding(project='functest', case_name='foo')
36
37     def test_run_deploy_vnf_exc(self):
38         with mock.patch.object(self.test, 'prepare'),\
39             mock.patch.object(self.test, 'deploy_orchestrator',
40                               return_value=None), \
41             mock.patch.object(self.test, 'deploy_vnf',
42                               side_effect=Exception):
43             self.assertEqual(self.test.run(),
44                              testcase.TestCase.EX_TESTCASE_FAILED)
45
46     def test_run_test_vnf_exc(self):
47         with mock.patch.object(self.test, 'prepare'),\
48             mock.patch.object(self.test, 'deploy_orchestrator',
49                               return_value=None), \
50             mock.patch.object(self.test, 'deploy_vnf'), \
51             mock.patch.object(self.test, 'test_vnf',
52                               side_effect=Exception):
53             self.assertEqual(self.test.run(),
54                              testcase.TestCase.EX_TESTCASE_FAILED)
55
56     def test_run_deploy_orch_ko(self):
57         with mock.patch.object(self.test, 'prepare'),\
58                 mock.patch.object(self.test, 'deploy_orchestrator',
59                                   return_value=False), \
60                 mock.patch.object(self.test, 'deploy_vnf',
61                                   return_value=True), \
62                 mock.patch.object(self.test, 'test_vnf',
63                                   return_value=True):
64             self.assertEqual(self.test.run(),
65                              testcase.TestCase.EX_TESTCASE_FAILED)
66
67     def test_run_vnf_deploy_ko(self):
68         with mock.patch.object(self.test, 'prepare'),\
69                 mock.patch.object(self.test, 'deploy_orchestrator',
70                                   return_value=True), \
71                 mock.patch.object(self.test, 'deploy_vnf',
72                                   return_value=False), \
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_test_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=True), \
84                 mock.patch.object(self.test, 'test_vnf',
85                                   return_value=False):
86             self.assertEqual(self.test.run(),
87                              testcase.TestCase.EX_TESTCASE_FAILED)
88
89     def test_run_default(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=True):
97             self.assertEqual(self.test.run(), testcase.TestCase.EX_OK)
98
99     def test_deploy_vnf_unimplemented(self):
100         with self.assertRaises(vnf.VnfDeploymentException):
101             self.test.deploy_vnf()
102
103     def test_test_vnf_unimplemented(self):
104         with self.assertRaises(vnf.VnfTestException):
105             self.test.test_vnf()
106
107     def test_deploy_orch_unimplemented(self):
108         self.assertTrue(self.test.deploy_orchestrator())
109
110     @mock.patch('snaps.openstack.tests.openstack_tests.get_credentials',
111                 return_value=OSCreds(
112                     username='user', password='pass',
113                     auth_url='http://foo.com:5000/v3', project_name='bar'),
114                 side_effect=Exception)
115     def test_prepare_keystone_client_ko(self, *args):
116         with self.assertRaises(vnf.VnfPreparationException):
117             self.test.prepare()
118         args[0].assert_called_once()
119
120     def test_vnf_clean_exc(self):
121         obj = mock.Mock()
122         obj.clean.side_effect = Exception
123         self.test.created_object = [obj]
124         self.test.clean()
125         obj.clean.assert_called_with()
126
127     def test_vnf_clean(self):
128         obj = mock.Mock()
129         self.test.created_object = [obj]
130         self.test.clean()
131         obj.clean.assert_called_with()
132
133
134 if __name__ == "__main__":
135     logging.disable(logging.CRITICAL)
136     unittest.main(verbosity=2)