4863f05c03e382ed01d0b158ea7dd1994ec0609b
[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 GetNetworkIdTestCase(unittest.TestCase):
39
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}]
45
46         output = openstack_utils.get_network_id(mock_shade_client,
47                                                 'network_name')
48         self.assertEqual(_uuid, output)
49
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
54
55         output = openstack_utils.get_network_id(mock_shade_client,
56                                                 'network_name')
57         self.assertIsNone(output)
58
59
60 class DeleteNeutronNetTestCase(unittest.TestCase):
61
62     def setUp(self):
63         self.mock_shade_client = mock.Mock()
64         self.mock_shade_client.delete_network = mock.Mock()
65
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,
69                                                     'network_id')
70         self.assertTrue(output)
71
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,
75                                                     'network_id')
76         self.assertFalse(output)
77
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,
83                                                     'network_id')
84         self.assertFalse(output)
85         mock_logger.error.assert_called_once()