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