Merge "nsb_setup: parametrize docker image"
[yardstick.git] / yardstick / tests / unit / benchmark / scenarios / lib / test_create_keypair.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 from oslo_utils import uuidutils
10 import unittest
11 import mock
12
13 from yardstick.common import openstack_utils
14 from yardstick.common import exceptions
15 from yardstick.benchmark.scenarios.lib import create_keypair
16
17
18 class CreateKeypairTestCase(unittest.TestCase):
19
20     def setUp(self):
21
22         self._mock_create_keypair = mock.patch.object(
23             openstack_utils, 'create_keypair')
24         self.mock_create_keypair = (
25             self._mock_create_keypair.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_keypair, 'LOG')
30         self.mock_log = self._mock_log.start()
31         self.args = {'options': {'key_name': 'yardstick_key'}}
32         self.result = {}
33
34         self.ckeypair_obj = create_keypair.CreateKeypair(self.args, mock.ANY)
35         self.addCleanup(self._stop_mock)
36
37     def _stop_mock(self):
38         self._mock_create_keypair.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.ckeypair_obj.scenario_cfg = {'output': 'id'}
45         self.mock_create_keypair.return_value = {
46             'name': 'key-name', 'type': 'ssh', 'id': _uuid}
47         output = self.ckeypair_obj.run(self.result)
48         self.assertDictEqual({'keypair_create': 1}, self.result)
49         self.assertDictEqual({'id': _uuid}, output)
50         self.mock_log.info.asset_called_once_with('Create keypair successful!')
51
52     def test_run_fail(self):
53         self.mock_create_keypair.return_value = None
54         with self.assertRaises(exceptions.ScenarioCreateKeypairError):
55             self.ckeypair_obj.run(self.result)
56         self.assertDictEqual({'keypair_create': 0}, self.result)
57         self.mock_log.error.assert_called_once_with('Create keypair failed!')