3b7e8eaa11d00fac7ebf6e4173c020476b5cbb3f
[yardstick.git] / yardstick / tests / unit / common / test_openstack_utils.py
1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 from oslo_utils import uuidutils
11 import unittest
12 import mock
13
14 from shade import exc
15 from yardstick.common import openstack_utils
16
17
18 class GetCredentialsTestCase(unittest.TestCase):
19
20     @mock.patch('yardstick.common.openstack_utils.os')
21     def test_get_credentials(self, _):
22         with mock.patch.dict('os.environ', {'OS_IDENTITY_API_VERSION': '2'},
23                              clear=True):
24             openstack_utils.get_credentials()
25
26
27 class GetHeatApiVersionTestCase(unittest.TestCase):
28
29     def test_get_heat_api_version_check_result(self):
30         API = 'HEAT_API_VERSION'
31         expected_result = '2'
32
33         with mock.patch.dict('os.environ', {API: '2'}, clear=True):
34             api_version = openstack_utils.get_heat_api_version()
35             self.assertEqual(api_version, expected_result)
36
37
38 class DeleteNeutronNetTestCase(unittest.TestCase):
39
40     def setUp(self):
41         self.mock_shade_client = mock.Mock()
42         self.mock_shade_client.delete_network = mock.Mock()
43
44     def test_delete_neutron_net(self):
45         self.mock_shade_client.delete_network.return_value = True
46         output = openstack_utils.delete_neutron_net(self.mock_shade_client,
47                                                     'network_id')
48         self.assertTrue(output)
49
50     def test_delete_neutron_net_fail(self):
51         self.mock_shade_client.delete_network.return_value = False
52         output = openstack_utils.delete_neutron_net(self.mock_shade_client,
53                                                     'network_id')
54         self.assertFalse(output)
55
56     @mock.patch.object(openstack_utils, 'log')
57     def test_delete_neutron_net_exception(self, mock_logger):
58         self.mock_shade_client.delete_network.side_effect = (
59             exc.OpenStackCloudException('error message'))
60         output = openstack_utils.delete_neutron_net(self.mock_shade_client,
61                                                     'network_id')
62         self.assertFalse(output)
63         mock_logger.error.assert_called_once()
64
65
66 class CreateNeutronNetTestCase(unittest.TestCase):
67
68     def setUp(self):
69         self.mock_shade_client = mock.Mock()
70         self.network_name = 'name'
71         self.mock_shade_client.create_network = mock.Mock()
72
73     def test_create_neutron_net(self):
74         _uuid = uuidutils.generate_uuid()
75         self.mock_shade_client.create_network.return_value = {'id': _uuid}
76         output = openstack_utils.create_neutron_net(self.mock_shade_client,
77                                                     self.network_name)
78         self.assertEqual(_uuid, output)
79
80     @mock.patch.object(openstack_utils, 'log')
81     def test_create_neutron_net_exception(self, mock_logger):
82         self.mock_shade_client.create_network.side_effect = (
83             exc.OpenStackCloudException('error message'))
84
85         output = openstack_utils.create_neutron_net(self.mock_shade_client,
86                                                     self.network_name)
87         mock_logger.error.assert_called_once()
88         self.assertIsNone(output)
89
90
91 class CreateNeutronSubnetTestCase(unittest.TestCase):
92
93     def setUp(self):
94         self.mock_shade_client = mock.Mock()
95         self.network_name_or_id = 'name_or_id'
96         self.mock_shade_client.create_subnet = mock.Mock()
97
98     def test_create_neutron_subnet(self):
99         _uuid = uuidutils.generate_uuid()
100         self.mock_shade_client.create_subnet.return_value = {'id': _uuid}
101         output = openstack_utils.create_neutron_subnet(
102             self.mock_shade_client, self.network_name_or_id)
103         self.assertEqual(_uuid, output)
104
105     @mock.patch.object(openstack_utils, 'log')
106     def test_create_neutron_subnet_exception(self, mock_logger):
107         self.mock_shade_client.create_subnet.side_effect = (
108             exc.OpenStackCloudException('error message'))
109
110         output = openstack_utils.create_neutron_subnet(
111             self.mock_shade_client, self.network_name_or_id)
112         mock_logger.error.assert_called_once()
113         self.assertIsNone(output)
114
115
116 class DeleteNeutronRouterTestCase(unittest.TestCase):
117
118     def setUp(self):
119         self.mock_shade_client = mock.Mock()
120         self.mock_shade_client.delete_router = mock.Mock()
121
122     def test_delete_neutron_router(self):
123         self.mock_shade_client.delete_router.return_value = True
124         output = openstack_utils.delete_neutron_router(self.mock_shade_client,
125                                                        'router_id')
126         self.assertTrue(output)
127
128     def test_delete_neutron_router_fail(self):
129         self.mock_shade_client.delete_router.return_value = False
130         output = openstack_utils.delete_neutron_router(self.mock_shade_client,
131                                                        'router_id')
132         self.assertFalse(output)
133
134     @mock.patch.object(openstack_utils, 'log')
135     def test_delete_neutron_router_exception(self, mock_logger):
136         self.mock_shade_client.delete_router.side_effect = (
137             exc.OpenStackCloudException('error message'))
138         output = openstack_utils.delete_neutron_router(self.mock_shade_client,
139                                                        'router_id')
140         mock_logger.error.assert_called_once()
141         self.assertFalse(output)
142
143
144 class CreateNeutronRouterTestCase(unittest.TestCase):
145
146     def setUp(self):
147         self.mock_shade_client = mock.Mock()
148         self.mock_shade_client.create_subnet = mock.Mock()
149
150     def test_create_neutron_router(self):
151         _uuid = uuidutils.generate_uuid()
152         self.mock_shade_client.create_router.return_value = {'id': _uuid}
153         output = openstack_utils.create_neutron_router(
154             self.mock_shade_client)
155         self.assertEqual(_uuid, output)
156
157     @mock.patch.object(openstack_utils, 'log')
158     def test_create_neutron_subnet_exception(self, mock_logger):
159         self.mock_shade_client.create_router.side_effect = (
160             exc.OpenStackCloudException('error message'))
161
162         output = openstack_utils.create_neutron_router(
163             self.mock_shade_client)
164         mock_logger.error.assert_called_once()
165         self.assertIsNone(output)
166
167
168 class RemoveRouterInterfaceTestCase(unittest.TestCase):
169
170     def setUp(self):
171         self.mock_shade_client = mock.Mock()
172         self.router = 'router'
173         self.mock_shade_client.remove_router_interface = mock.Mock()
174
175     def test_remove_router_interface(self):
176         self.mock_shade_client.remove_router_interface.return_value = True
177         output = openstack_utils.remove_router_interface(
178             self.mock_shade_client, self.router)
179         self.assertTrue(output)
180
181     @mock.patch.object(openstack_utils, 'log')
182     def test_remove_router_interface_exception(self, mock_logger):
183         self.mock_shade_client.remove_router_interface.side_effect = (
184             exc.OpenStackCloudException('error message'))
185         output = openstack_utils.remove_router_interface(
186             self.mock_shade_client, self.router)
187         mock_logger.error.assert_called_once()
188         self.assertFalse(output)
189
190
191 class CreateFloatingIpTestCase(unittest.TestCase):
192
193     def setUp(self):
194         self.mock_shade_client = mock.Mock()
195         self.network_name_or_id = 'name'
196         self.mock_shade_client.create_floating_ip = mock.Mock()
197
198     def test_create_floating_ip(self):
199         self.mock_shade_client.create_floating_ip.return_value = \
200             {'floating_ip_address': 'value1', 'id': 'value2'}
201         output = openstack_utils.create_floating_ip(self.mock_shade_client,
202                                                     self.network_name_or_id)
203         self.assertEqual({'fip_addr': 'value1', 'fip_id': 'value2'}, output)
204
205     @mock.patch.object(openstack_utils, 'log')
206     def test_create_floating_ip_exception(self, mock_logger):
207         self.mock_shade_client.create_floating_ip.side_effect = (
208             exc.OpenStackCloudException('error message'))
209         output = openstack_utils.create_floating_ip(
210             self.mock_shade_client, self.network_name_or_id)
211         mock_logger.error.assert_called_once()
212         self.assertIsNone(output)
213
214
215 class DeleteFloatingIpTestCase(unittest.TestCase):
216
217     def setUp(self):
218         self.mock_shade_client = mock.Mock()
219         self.floating_ip_id = 'floating_ip_id'
220         self.mock_shade_client.delete_floating_ip = mock.Mock()
221
222     def test_delete_floating_ip(self):
223         self.mock_shade_client.delete_floating_ip.return_value = True
224         output = openstack_utils.delete_floating_ip(self.mock_shade_client,
225                                                     'floating_ip_id')
226         self.assertTrue(output)
227
228     def test_delete_floating_ip_fail(self):
229         self.mock_shade_client.delete_floating_ip.return_value = False
230         output = openstack_utils.delete_floating_ip(self.mock_shade_client,
231                                                     'floating_ip_id')
232         self.assertFalse(output)
233
234     @mock.patch.object(openstack_utils, 'log')
235     def test_delete_floating_ip_exception(self, mock_logger):
236         self.mock_shade_client.delete_floating_ip.side_effect = (
237             exc.OpenStackCloudException('error message'))
238         output = openstack_utils.delete_floating_ip(self.mock_shade_client,
239                                                     'floating_ip_id')
240         mock_logger.error.assert_called_once()
241         self.assertFalse(output)
242
243
244 class CreateSecurityGroupRuleTestCase(unittest.TestCase):
245
246     def setUp(self):
247         self.mock_shade_client = mock.Mock()
248         self.secgroup_name_or_id = 'sg_name_id'
249         self.mock_shade_client.create_security_group_rule = mock.Mock()
250
251     def test_create_security_group_rule(self):
252         self.mock_shade_client.create_security_group_rule.return_value = (
253             {'security_group_rule'})
254         output = openstack_utils.create_security_group_rule(
255             self.mock_shade_client, self.secgroup_name_or_id)
256         self.assertTrue(output)
257
258     @mock.patch.object(openstack_utils, 'log')
259     def test_create_security_group_rule_exception(self, mock_logger):
260         self.mock_shade_client.create_security_group_rule.side_effect = (
261             exc.OpenStackCloudException('error message'))
262
263         output = openstack_utils.create_security_group_rule(
264             self.mock_shade_client, self.secgroup_name_or_id)
265         mock_logger.error.assert_called_once()
266         self.assertFalse(output)