diff options
Diffstat (limited to 'yardstick/tests/unit/common/test_openstack_utils.py')
-rw-r--r-- | yardstick/tests/unit/common/test_openstack_utils.py | 57 |
1 files changed, 48 insertions, 9 deletions
diff --git a/yardstick/tests/unit/common/test_openstack_utils.py b/yardstick/tests/unit/common/test_openstack_utils.py index bf468489e..8a2f5f95b 100644 --- a/yardstick/tests/unit/common/test_openstack_utils.py +++ b/yardstick/tests/unit/common/test_openstack_utils.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python - ############################################################################## # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others. # @@ -9,12 +7,11 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## -# Unittest for yardstick.common.openstack_utils - -from __future__ import absolute_import +from oslo_utils import uuidutils import unittest import mock +from shade import exc from yardstick.common import openstack_utils @@ -38,9 +35,51 @@ class GetHeatApiVersionTestCase(unittest.TestCase): self.assertEqual(api_version, expected_result) -def main(): - unittest.main() +class GetNetworkIdTestCase(unittest.TestCase): + + def test_get_network_id(self): + _uuid = uuidutils.generate_uuid() + mock_shade_client = mock.Mock() + mock_shade_client.list_networks = mock.Mock() + mock_shade_client.list_networks.return_value = [{'id': _uuid}] + + output = openstack_utils.get_network_id(mock_shade_client, + 'network_name') + self.assertEqual(_uuid, output) + + def test_get_network_id_no_network(self): + mock_shade_client = mock.Mock() + mock_shade_client.list_networks = mock.Mock() + mock_shade_client.list_networks.return_value = None + + output = openstack_utils.get_network_id(mock_shade_client, + 'network_name') + self.assertEqual(None, output) + + +class DeleteNeutronNetTestCase(unittest.TestCase): + + def setUp(self): + self.mock_shade_client = mock.Mock() + self.mock_shade_client.delete_network = mock.Mock() + + def test_delete_neutron_net(self): + self.mock_shade_client.delete_network.return_value = True + output = openstack_utils.delete_neutron_net(self.mock_shade_client, + 'network_id') + self.assertTrue(output) + def test_delete_neutron_net_fail(self): + self.mock_shade_client.delete_network.return_value = False + output = openstack_utils.delete_neutron_net(self.mock_shade_client, + 'network_id') + self.assertFalse(output) -if __name__ == '__main__': - main() + @mock.patch.object(openstack_utils, 'log') + def test_delete_neutron_net_exception(self, mock_logger): + self.mock_shade_client.delete_network.side_effect = ( + exc.OpenStackCloudException('error message')) + output = openstack_utils.delete_neutron_net(self.mock_shade_client, + 'network_id') + self.assertFalse(output) + mock_logger.error.assert_called_once() |