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 GetNetworkIdTestCase(unittest.TestCase):
40 def test_get_network_id(self):
41 _uuid = uuidutils.generate_uuid()
42 mock_shade_client = mock.Mock()
43 mock_shade_client.list_networks = mock.Mock()
44 mock_shade_client.list_networks.return_value = [{'id': _uuid}]
46 output = openstack_utils.get_network_id(mock_shade_client,
48 self.assertEqual(_uuid, output)
50 def test_get_network_id_no_network(self):
51 mock_shade_client = mock.Mock()
52 mock_shade_client.list_networks = mock.Mock()
53 mock_shade_client.list_networks.return_value = None
55 output = openstack_utils.get_network_id(mock_shade_client,
57 self.assertIsNone(output)
60 class DeleteNeutronNetTestCase(unittest.TestCase):
63 self.mock_shade_client = mock.Mock()
64 self.mock_shade_client.delete_network = mock.Mock()
66 def test_delete_neutron_net(self):
67 self.mock_shade_client.delete_network.return_value = True
68 output = openstack_utils.delete_neutron_net(self.mock_shade_client,
70 self.assertTrue(output)
72 def test_delete_neutron_net_fail(self):
73 self.mock_shade_client.delete_network.return_value = False
74 output = openstack_utils.delete_neutron_net(self.mock_shade_client,
76 self.assertFalse(output)
78 @mock.patch.object(openstack_utils, 'log')
79 def test_delete_neutron_net_exception(self, mock_logger):
80 self.mock_shade_client.delete_network.side_effect = (
81 exc.OpenStackCloudException('error message'))
82 output = openstack_utils.delete_neutron_net(self.mock_shade_client,
84 self.assertFalse(output)
85 mock_logger.error.assert_called_once()
88 class CreateNeutronNetTestCase(unittest.TestCase):
91 self.mock_shade_client = mock.Mock()
92 self.network_name = 'name'
93 self.mock_shade_client.create_network = mock.Mock()
95 def test_create_neutron_net(self):
96 _uuid = uuidutils.generate_uuid()
97 self.mock_shade_client.create_network.return_value = {'id': _uuid}
98 output = openstack_utils.create_neutron_net(self.mock_shade_client,
100 self.assertEqual(_uuid, output)
102 @mock.patch.object(openstack_utils, 'log')
103 def test_create_neutron_net_exception(self, mock_logger):
104 self.mock_shade_client.create_network.side_effect = (
105 exc.OpenStackCloudException('error message'))
107 output = openstack_utils.create_neutron_net(self.mock_shade_client,
109 mock_logger.error.assert_called_once()
110 self.assertIsNone(output)
113 class CreateNeutronSubnetTestCase(unittest.TestCase):
116 self.mock_shade_client = mock.Mock()
117 self.network_name_or_id = 'name_or_id'
118 self.mock_shade_client.create_subnet = mock.Mock()
120 def test_create_neutron_subnet(self):
121 _uuid = uuidutils.generate_uuid()
122 self.mock_shade_client.create_subnet.return_value = {'id': _uuid}
123 output = openstack_utils.create_neutron_subnet(
124 self.mock_shade_client, self.network_name_or_id)
125 self.assertEqual(_uuid, output)
127 @mock.patch.object(openstack_utils, 'log')
128 def test_create_neutron_subnet_exception(self, mock_logger):
129 self.mock_shade_client.create_subnet.side_effect = (
130 exc.OpenStackCloudException('error message'))
132 output = openstack_utils.create_neutron_subnet(
133 self.mock_shade_client, self.network_name_or_id)
134 mock_logger.error.assert_called_once()
135 self.assertIsNone(output)
138 class DeleteNeutronRouterTestCase(unittest.TestCase):
141 self.mock_shade_client = mock.Mock()
142 self.mock_shade_client.delete_router = mock.Mock()
144 def test_delete_neutron_router(self):
145 self.mock_shade_client.delete_router.return_value = True
146 output = openstack_utils.delete_neutron_router(self.mock_shade_client,
148 self.assertTrue(output)
150 def test_delete_neutron_router_fail(self):
151 self.mock_shade_client.delete_router.return_value = False
152 output = openstack_utils.delete_neutron_router(self.mock_shade_client,
154 self.assertFalse(output)
156 @mock.patch.object(openstack_utils, 'log')
157 def test_delete_neutron_router_exception(self, mock_logger):
158 self.mock_shade_client.delete_router.side_effect = (
159 exc.OpenStackCloudException('error message'))
160 output = openstack_utils.delete_neutron_router(self.mock_shade_client,
162 mock_logger.error.assert_called_once()
163 self.assertFalse(output)
166 class CreateNeutronRouterTestCase(unittest.TestCase):
169 self.mock_shade_client = mock.Mock()
170 self.mock_shade_client.create_subnet = mock.Mock()
172 def test_create_neutron_router(self):
173 _uuid = uuidutils.generate_uuid()
174 self.mock_shade_client.create_router.return_value = {'id': _uuid}
175 output = openstack_utils.create_neutron_router(
176 self.mock_shade_client)
177 self.assertEqual(_uuid, output)
179 @mock.patch.object(openstack_utils, 'log')
180 def test_create_neutron_subnet_exception(self, mock_logger):
181 self.mock_shade_client.create_router.side_effect = (
182 exc.OpenStackCloudException('error message'))
184 output = openstack_utils.create_neutron_router(
185 self.mock_shade_client)
186 mock_logger.error.assert_called_once()
187 self.assertIsNone(output)
190 class RemoveRouterInterfaceTestCase(unittest.TestCase):
193 self.mock_shade_client = mock.Mock()
194 self.router = 'router'
195 self.mock_shade_client.remove_router_interface = mock.Mock()
197 def test_remove_router_interface(self):
198 self.mock_shade_client.remove_router_interface.return_value = True
199 output = openstack_utils.remove_router_interface(
200 self.mock_shade_client, self.router)
201 self.assertTrue(output)
203 @mock.patch.object(openstack_utils, 'log')
204 def test_remove_router_interface_exception(self, mock_logger):
205 self.mock_shade_client.remove_router_interface.side_effect = (
206 exc.OpenStackCloudException('error message'))
207 output = openstack_utils.remove_router_interface(
208 self.mock_shade_client, self.router)
209 mock_logger.error.assert_called_once()
210 self.assertFalse(output)