Merge "Add/Update tempest.conf via a specific file"
[functest.git] / functest / tests / unit / vnf / ims / test_orchestra_ims.py
1 #!/usr/bin/env python
2
3 # All rights reserved. This program and the accompanying materials
4 # are made available under the terms of the Apache License, Version 2.0
5 # which accompanies this distribution, and is available at
6 # http://www.apache.org/licenses/LICENSE-2.0
7
8 """Test module for orchestra_ims"""
9
10 import logging
11 import unittest
12
13 import mock
14
15 from functest.core import vnf
16 from functest.opnfv_tests.vnf.ims import orchestra_ims
17
18
19 class OrchestraImsTesting(unittest.TestCase):
20     """Test class for orchestra_ims"""
21     def setUp(self):
22
23         self.tenant = 'orchestra_ims'
24         self.creds = {'username': 'mocked_username',
25                       'password': 'mocked_password'}
26         self.tenant_images = {
27             'image1': 'mocked_image_url_1',
28             'image2': 'mocked_image_url_2'
29         }
30         self.orchestrator = {
31             'name': 'openbaton',
32             'version': '3.2.0',
33             'object': 'foo',
34             'requirements': {
35                 'flavor': {
36                     'name': 'mocked_flavor',
37                     'ram_min': 4096,
38                     'disk': 5,
39                     'vcpus': 2
40                 },
41                 'os_image': 'mocked_image'
42             },
43             'bootstrap': {
44                 'url': 'mocked_bootstrap_url',
45                 'config': {
46                     'url': 'mocked_config_url'}
47             },
48             'gvnfm': {
49                 'userdata': {
50                     'url': 'mocked_userdata_url'
51                 }
52             },
53             'credentials': {
54                 'username': 'mocked_username',
55                 'password': 'mocked_password'
56             }
57         }
58         self.vnf = {
59             'name': 'openims',
60             'descriptor': {
61                 'url': 'mocked_descriptor_url'
62             },
63             'requirements': {
64                 'flavor': {
65                     'name': 'mocked_flavor',
66                     'ram_min': 2048,
67                     'disk': 5,
68                     'vcpus': 2}
69             }
70         }
71         self.vIMS = {
72             'scscf': {
73                 'ports': [3870, 6060]
74             },
75             'pcscf': {
76                 'ports': [4060]
77             },
78             'icscf': {
79                 'ports': [3869, 5060]
80             },
81             'fhoss': {
82                 'ports': [3868]
83             },
84             'bind9': {
85                 'ports': []
86             }
87         }
88         with mock.patch('functest.opnfv_tests.vnf.ims.orchestra_ims.'
89                         'os.makedirs'),\
90             mock.patch('functest.opnfv_tests.vnf.ims.orchestra_ims.'
91                        'get_config', return_value={
92                            'orchestrator': self.orchestrator,
93                            'name': self.orchestrator['name'],
94                            'version': self.orchestrator['version'],
95                            'requirements': self.orchestrator['requirements'],
96                            'credentials': self.orchestrator['credentials'],
97                            'bootstrap': self.orchestrator['bootstrap'],
98                            'gvnfm': self.orchestrator['gvnfm'],
99                            'os_image':
100                                self.orchestrator['requirements']['os_image'],
101                            'flavor':
102                                self.orchestrator['requirements']['flavor'],
103                            'url': self.orchestrator['bootstrap']['url'],
104                            'config': self.orchestrator['bootstrap']['config'],
105                            'tenant_images': self.tenant_images,
106                            'vnf': self.vnf,
107                            'vIMS': self.vIMS}):
108             self.ims_vnf = orchestra_ims.ImsVnf()
109
110         self.details = {'orchestrator': {'status': 'PASS', 'duration': 120},
111                         'vnf': {},
112                         'test_vnf': {}}
113
114     @mock.patch('functest.core.vnf.os_utils.get_keystone_client',
115                 return_value='test')
116     @mock.patch('functest.core.vnf.os_utils.get_or_create_tenant_for_vnf',
117                 return_value=True)
118     @mock.patch('functest.core.vnf.os_utils.get_or_create_user_for_vnf',
119                 return_value=True)
120     @mock.patch('functest.core.vnf.os_utils.get_credentials',
121                 return_value={'auth_url': 'test/v1'})
122     @mock.patch(
123         'functest.utils.openstack_utils.get_tenant_id',
124         return_value={'mocked_tenant_id'})
125     @mock.patch(
126         'functest.utils.openstack_utils.get_floating_ips',
127         return_value=[])
128     @mock.patch('snaps.openstack.create_image.OpenStackImage.create')
129     @mock.patch('snaps.openstack.create_flavor.OpenStackFlavor.create')
130     @mock.patch(
131         'snaps.openstack.create_security_group.OpenStackSecurityGroup.create')
132     @mock.patch('snaps.openstack.create_network.OpenStackNetwork.create')
133     @mock.patch('snaps.openstack.create_router.OpenStackRouter.create')
134     @mock.patch(
135         'functest.opnfv_tests.openstack.snaps.snaps_utils.get_ext_net_name')
136     @mock.patch(
137         'functest.opnfv_tests.openstack.snaps.snaps_utils.'
138         'neutron_utils.create_floating_ip')
139     def test_prepare_default(self, *args):
140         """Testing prepare function without any exceptions expected"""
141         self.assertIsNone(self.ims_vnf.prepare())
142         args[4].assert_called_once_with()
143
144     @mock.patch('functest.core.vnf.os_utils.get_keystone_client',
145                 return_value='test')
146     @mock.patch('functest.core.vnf.os_utils.get_or_create_tenant_for_vnf',
147                 return_value=True)
148     @mock.patch('functest.core.vnf.os_utils.get_or_create_user_for_vnf',
149                 return_value=True)
150     @mock.patch('functest.core.vnf.os_utils.get_credentials',
151                 return_value={'auth_url': 'test/no_v'})
152     @mock.patch('snaps.openstack.create_image.OpenStackImage.create')
153     def test_prepare_bad_auth_url(self, *args):
154         """Testing prepare function with bad auth url"""
155         with self.assertRaises(Exception):
156             self.ims_vnf.prepare()
157             args[0].assert_not_called()
158
159     def test_prepare_missing_param(self):
160         """Testing prepare function with missing param"""
161         with self.assertRaises(vnf.VnfPreparationException):
162             self.ims_vnf.prepare()
163
164     @mock.patch('functest.core.vnf.os_utils.get_keystone_client',
165                 side_effect=Exception)
166     def test_prepare_keystone_exception(self, *args):
167         """Testing prepare function with keystone exception"""
168         with self.assertRaises(vnf.VnfPreparationException):
169             self.ims_vnf.prepare()
170         args[0].assert_called_once_with()
171
172     @mock.patch('functest.core.vnf.os_utils.get_keystone_client',
173                 return_value='test')
174     @mock.patch('functest.core.vnf.os_utils.get_or_create_tenant_for_vnf',
175                 side_effect=Exception)
176     def test_prepare_tenant_exception(self, *args):
177         """Testing prepare function with tenant exception"""
178         with self.assertRaises(vnf.VnfPreparationException):
179             self.ims_vnf.prepare()
180         args[1].assert_called_once_with()
181
182     @mock.patch('functest.core.vnf.os_utils.get_keystone_client',
183                 return_value='test')
184     @mock.patch('functest.core.vnf.os_utils.get_or_create_tenant_for_vnf',
185                 return_value=True)
186     @mock.patch('functest.core.vnf.os_utils.get_or_create_user_for_vnf',
187                 side_effect=Exception)
188     def test_prepare_user_exception(self, *args):
189         """Testing prepare function with user exception"""
190         with self.assertRaises(vnf.VnfPreparationException):
191             self.ims_vnf.prepare()
192         args[2].assert_called_once_with()
193
194     @mock.patch('functest.core.vnf.os_utils.get_keystone_client',
195                 return_value='test')
196     @mock.patch('functest.core.vnf.os_utils.get_or_create_tenant_for_vnf',
197                 return_value=True)
198     @mock.patch('functest.core.vnf.os_utils.get_or_create_user_for_vnf',
199                 return_value=True)
200     @mock.patch('functest.core.vnf.os_utils.get_credentials',
201                 side_effect=Exception)
202     def test_prepare_credentials_exception(self, *args):
203         """Testing prepare function with credentials exception"""
204         with self.assertRaises(vnf.VnfPreparationException):
205             self.ims_vnf.prepare()
206         args[0].assert_called_once_with()
207
208     # # @mock.patch('functest.opnfv_tests.vnf.ims.orchestra_ims.get_userdata')
209     # def test_deploy_orchestrator(self, *args):
210     #     floating_ip = FloatingIp
211     #     floating_ip.ip = 'mocked_ip'
212     #     details = {'fip':floating_ip,'flavor':{'name':'mocked_name'}}
213     #     self.orchestrator['details'] = details
214     #     with mock.patch.dict(self.orchestrator, {'details':
215     #     {'fip':floating_ip,'flavor':{'name':'mocked_name'}}}):
216     #     # with mock.patch.dict(self.orchestrator, details):
217     #         orchestra_ims.get_userdata(self.orchestrator)
218     #     self.assertIsNone(self.ims_vnf.deploy_orchestrator())
219     #     args[4].assert_called_once_with()
220
221
222 if __name__ == "__main__":
223     logging.disable(logging.CRITICAL)
224     unittest.main(verbosity=2)