1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
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 ##############################################################################
10 from oslo_utils import uuidutils
15 from yardstick.common import openstack_utils
18 class GetCredentialsTestCase(unittest.TestCase):
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'},
24 openstack_utils.get_credentials()
27 class GetHeatApiVersionTestCase(unittest.TestCase):
29 def test_get_heat_api_version_check_result(self):
30 API = 'HEAT_API_VERSION'
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)
38 class DeleteNeutronNetTestCase(unittest.TestCase):
41 self.mock_shade_client = mock.Mock()
42 self.mock_shade_client.delete_network = mock.Mock()
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,
48 self.assertTrue(output)
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,
54 self.assertFalse(output)
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,
62 self.assertFalse(output)
63 mock_logger.error.assert_called_once()
66 class CreateNeutronNetTestCase(unittest.TestCase):
69 self.mock_shade_client = mock.Mock()
70 self.network_name = 'name'
71 self.mock_shade_client.create_network = mock.Mock()
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,
78 self.assertEqual(_uuid, output)
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'))
85 output = openstack_utils.create_neutron_net(self.mock_shade_client,
87 mock_logger.error.assert_called_once()
88 self.assertIsNone(output)
91 class CreateNeutronSubnetTestCase(unittest.TestCase):
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()
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)
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'))
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)
116 class DeleteNeutronRouterTestCase(unittest.TestCase):
119 self.mock_shade_client = mock.Mock()
120 self.mock_shade_client.delete_router = mock.Mock()
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,
126 self.assertTrue(output)
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,
132 self.assertFalse(output)
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,
140 mock_logger.error.assert_called_once()
141 self.assertFalse(output)
144 class CreateNeutronRouterTestCase(unittest.TestCase):
147 self.mock_shade_client = mock.Mock()
148 self.mock_shade_client.create_subnet = mock.Mock()
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)
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'))
162 output = openstack_utils.create_neutron_router(
163 self.mock_shade_client)
164 mock_logger.error.assert_called_once()
165 self.assertIsNone(output)
168 class RemoveRouterInterfaceTestCase(unittest.TestCase):
171 self.mock_shade_client = mock.Mock()
172 self.router = 'router'
173 self.mock_shade_client.remove_router_interface = mock.Mock()
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)
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)
191 class CreateFloatingIpTestCase(unittest.TestCase):
194 self.mock_shade_client = mock.Mock()
195 self.network_name_or_id = 'name'
196 self.mock_shade_client.create_floating_ip = mock.Mock()
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)
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)
215 class DeleteFloatingIpTestCase(unittest.TestCase):
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()
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,
226 self.assertTrue(output)
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,
232 self.assertFalse(output)
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,
240 mock_logger.error.assert_called_once()
241 self.assertFalse(output)
244 class CreateSecurityGroupRuleTestCase(unittest.TestCase):
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()
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)
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'))
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)