Replace nova delete keypair with shade client.
[yardstick.git] / yardstick / tests / unit / benchmark / scenarios / lib / test_create_network.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_network
17
18
19 class CreateNetworkTestCase(unittest.TestCase):
20
21     def setUp(self):
22
23         self._mock_create_neutron_net = mock.patch.object(
24             openstack_utils, 'create_neutron_net')
25         self.mock_create_neutron_net = self._mock_create_neutron_net.start()
26         self._mock_get_shade_client = mock.patch.object(
27             openstack_utils, 'get_shade_client')
28         self.mock_get_shade_client = self._mock_get_shade_client.start()
29         self._mock_log = mock.patch.object(create_network, 'LOG')
30         self.mock_log = self._mock_log.start()
31         self.args = {'options': {'network_name': 'yardstick_net'}}
32         self.result = {}
33
34         self._cnet_obj = create_network.CreateNetwork(self.args, mock.ANY)
35         self.addCleanup(self._stop_mock)
36
37     def _stop_mock(self):
38         self._mock_create_neutron_net.stop()
39         self._mock_get_shade_client.stop()
40         self._mock_log.stop()
41
42     def test_run(self):
43         _uuid = uuidutils.generate_uuid()
44         self._cnet_obj.scenario_cfg = {'output': 'id'}
45         self.mock_create_neutron_net.return_value = _uuid
46         output = self._cnet_obj.run(self.result)
47         self.assertEqual({"network_create": 1}, self.result)
48         self.assertEqual({'id': _uuid}, output)
49         self.mock_log.info.asset_called_once_with('Create network successful!')
50
51     def test_run_fail_exception(self):
52         self.mock_create_neutron_net.return_value = None
53         with self.assertRaises(exceptions.ScenarioCreateNetworkError):
54             self._cnet_obj.run(self.result)
55         self.assertEqual({"network_create": 0}, self.result)
56         self.mock_log.error.assert_called_once_with(
57             'Create network failed!')