Merge "Replace glance create image with shade client."
[yardstick.git] / yardstick / tests / unit / benchmark / scenarios / lib / test_create_floating_ip.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 import unittest
11 import mock
12
13 from yardstick.benchmark.scenarios.lib import create_floating_ip
14 from yardstick.common import openstack_utils
15 from yardstick.common import exceptions
16
17
18 class CreateFloatingIpTestCase(unittest.TestCase):
19
20     def setUp(self):
21         self._mock_create_floating_ip = mock.patch.object(
22             openstack_utils, 'create_floating_ip')
23         self.mock_create_floating_ip = self._mock_create_floating_ip.start()
24         self._mock_get_shade_client = mock.patch.object(
25             openstack_utils, 'get_shade_client')
26         self.mock_get_shade_client = self._mock_get_shade_client.start()
27         self._mock_log = mock.patch.object(create_floating_ip, 'LOG')
28         self.mock_log = self._mock_log.start()
29         self.args = {'options': {'network_name_or_id': 'yardstick_net'}}
30         self.result = {}
31
32         self.fip_obj = create_floating_ip.CreateFloatingIp(self.args, mock.ANY)
33         self.fip_obj.scenario_cfg = {'output': 'key1\nkey2'}
34
35         self.addCleanup(self._stop_mock)
36
37     def _stop_mock(self):
38         self._mock_create_floating_ip.stop()
39         self._mock_get_shade_client.stop()
40         self._mock_log.stop()
41
42     def test_run(self):
43         self.mock_create_floating_ip.return_value = {'fip_id': 'value1',
44                                                      'fip_addr': 'value2'}
45         output = self.fip_obj.run(self.result)
46         self.assertEqual({'floating_ip_create': 1}, self.result)
47         self.assertEqual({'key1': 'value1', 'key2': 'value2'}, output)
48         self.mock_log.info.asset_called_once_with(
49             'Creating floating ip successful!')
50
51     def test_run_no_fip(self):
52         self.mock_create_floating_ip.return_value = None
53         with self.assertRaises(exceptions.ScenarioCreateFloatingIPError):
54             self.fip_obj.run(self.result)
55         self.assertEqual({'floating_ip_create': 0}, self.result)
56         self.mock_log.error.assert_called_once_with(
57             'Creating floating ip failed!')