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()