Create RobotFramework class
[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='test')
112 #    @mock.patch('snaps.openstack.create_project.OpenStackProject',
113 #                return_value=True)
114 #    @mock.patch('snaps.openstack.create_user.OpenStackUser',
115 #                return_value=True)
116 #    def test_prepare(self, *args):
117 #        self.assertEqual(self.test.prepare(),
118 #                         testcase.TestCase.EX_OK)
119 #        args[0].assert_called_once_with()
120 #        args[1].assert_called_once_with('test', self.tenant_name)
121 #        args[2].assert_called_once_with(
122 #            'test', self.tenant_name, self.tenant_description)
123 #        args[3].assert_called_once_with()
124
125     @mock.patch('snaps.openstack.tests.openstack_tests.get_credentials',
126                 return_value=OSCreds(
127                     username='user', password='pass',
128                     auth_url='http://foo.com:5000/v3', project_name='bar'),
129                 side_effect=Exception)
130     def test_prepare_keystone_client_ko(self, *args):
131         with self.assertRaises(vnf.VnfPreparationException):
132             self.test.prepare()
133         args[0].assert_called_once()
134
135 #    @mock.patch('snaps.openstack.tests.openstack_tests.get_credentials',
136 #                return_value=OS_CREDS)
137 #    @mock.patch('snaps.openstack.create_project.OpenStackProject')
138 #    @mock.patch('snaps.openstack.create_project.OpenStackProject.create',
139 #                side_effect=Exception)
140 #    def test_prepare_tenant_creation_ko(self, *args):
141 #        with self.assertRaises(vnf.VnfPreparationException):
142 #            self.test.prepare()
143 #        args[2].assert_called_once()
144 #        args[1].assert_called_once_with(OS_CREDS,
145 #                                        ProjectSettings(
146 #                                           name=self.tenant_name,
147 #                                           description=self.tenant_description,
148 #                                        ))
149 #        args[0].assert_called_once()
150
151 #    @mock.patch('snaps.openstack.tests.openstack_tests.get_credentials')
152 #    @mock.patch('snaps.openstack.create_project.OpenStackProject',
153 #                return_value=0)
154 #    @mock.patch('snaps.openstack.create_user.OpenStackUser',
155 #                side_effect=Exception)
156 #    def test_prepare_user_creation_ko(self, *args):
157 #        with self.assertRaises(vnf.VnfPreparationException):
158 #            self.test.prepare()
159 #        args[0].assert_called_once_with(mock.ANY, self.tenant_name)
160 #        args[1].assert_called_once_with(
161 #            mock.ANY, self.tenant_name, self.tenant_description)
162 #        args[2].assert_called_once_with()
163
164
165 if __name__ == "__main__":
166     logging.disable(logging.CRITICAL)
167     unittest.main(verbosity=2)