c6b0f46b25bf3569747fa002fe4905cfd5e29359
[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()
86
87
88 class CreateNeutronNetTestCase(unittest.TestCase):
89
90     def setUp(self):
91         self.mock_shade_client = mock.Mock()
92         self.network_name = 'name'
93         self.mock_shade_client.create_network = mock.Mock()
94
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,
99                                                     self.network_name)
100         self.assertEqual(_uuid, output)
101
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'))
106
107         output = openstack_utils.create_neutron_net(self.mock_shade_client,
108                                                     self.network_name)
109         mock_logger.error.assert_called_once()
110         self.assertIsNone(output)
111
112
113 class CreateNeutronSubnetTestCase(unittest.TestCase):
114
115     def setUp(self):
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()
119
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)
126
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'))
131
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)
136
137
138 class DeleteNeutronRouterTestCase(unittest.TestCase):
139
140     def setUp(self):
141         self.mock_shade_client = mock.Mock()
142         self.mock_shade_client.delete_router = mock.Mock()
143
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,
147                                                        'router_id')
148         self.assertTrue(output)
149
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,
153                                                        'router_id')
154         self.assertFalse(output)
155
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,
161                                                        'router_id')
162         mock_logger.error.assert_called_once()
163         self.assertFalse(output)