Replace nova delete keypair with shade client.
[yardstick.git] / yardstick / tests / unit / benchmark / scenarios / lib / test_create_subnet.py
1 ##############################################################################
2 # Copyright (c) 2017 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 yardstick.common import openstack_utils
15 from yardstick.common import exceptions
16 from yardstick.benchmark.scenarios.lib import create_subnet
17
18
19 class CreateSubnetTestCase(unittest.TestCase):
20
21     def setUp(self):
22
23         self._mock_create_neutron_subnet = mock.patch.object(
24             openstack_utils, 'create_neutron_subnet')
25         self.mock_create_neutron_subnet = (
26             self._mock_create_neutron_subnet.start())
27         self._mock_get_shade_client = mock.patch.object(
28             openstack_utils, 'get_shade_client')
29         self.mock_get_shade_client = self._mock_get_shade_client.start()
30         self._mock_log = mock.patch.object(create_subnet, 'LOG')
31         self.mock_log = self._mock_log.start()
32         self.args = {'options': {'network_name_or_id': 'yardstick_net'}}
33         self.result = {"subnet_create": 0}
34
35         self._csubnet_obj = create_subnet.CreateSubnet(self.args, mock.ANY)
36         self.addCleanup(self._stop_mock)
37
38     def _stop_mock(self):
39         self._mock_create_neutron_subnet.stop()
40         self._mock_get_shade_client.stop()
41         self._mock_log.stop()
42
43     def test_run(self):
44         _uuid = uuidutils.generate_uuid()
45         self._csubnet_obj.scenario_cfg = {'output': 'id'}
46         self.mock_create_neutron_subnet.return_value = _uuid
47         output = self._csubnet_obj.run(self.result)
48         self.assertDictEqual({"subnet_create": 1}, self.result)
49         self.assertDictEqual({'id': _uuid}, output)
50         self.mock_log.info.asset_called_once_with('Create subnet successful!')
51
52     def test_run_fail(self):
53         self._csubnet_obj.scenario_cfg = {'output': 'id'}
54         self.mock_create_neutron_subnet.return_value = None
55         with self.assertRaises(exceptions.ScenarioCreateSubnetError):
56             self._csubnet_obj.run(self.result)
57         self.assertDictEqual({"subnet_create": 0}, self.result)
58         self.mock_log.error.assert_called_once_with('Create subnet failed!')