Merge "Replace glance create image with shade client."
[yardstick.git] / yardstick / tests / unit / benchmark / scenarios / lib / test_delete_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 import unittest
10 import mock
11
12 from yardstick.common import openstack_utils
13 from yardstick.common import exceptions
14 from yardstick.benchmark.scenarios.lib import delete_keypair
15
16
17 class DeleteKeypairTestCase(unittest.TestCase):
18
19     def setUp(self):
20         self._mock_delete_keypair = mock.patch.object(
21             openstack_utils, 'delete_keypair')
22         self.mock_delete_keypair = self._mock_delete_keypair.start()
23         self._mock_get_shade_client = mock.patch.object(
24             openstack_utils, 'get_shade_client')
25         self.mock_get_shade_client = self._mock_get_shade_client.start()
26         self._mock_log = mock.patch.object(delete_keypair, 'LOG')
27         self.mock_log = self._mock_log.start()
28         self.args = {'options': {'key_name': 'yardstick_key'}}
29         self.result = {}
30         self.delkey_obj = delete_keypair.DeleteKeypair(self.args, mock.ANY)
31
32         self.addCleanup(self._stop_mock)
33
34     def _stop_mock(self):
35         self._mock_delete_keypair.stop()
36         self._mock_get_shade_client.stop()
37         self._mock_log.stop()
38
39     def test_run(self):
40         self.mock_delete_keypair.return_value = True
41         self.assertIsNone(self.delkey_obj.run(self.result))
42         self.assertEqual({'delete_keypair': 1}, self.result)
43         self.mock_log.info.assert_called_once_with(
44             'Delete keypair successful!')
45
46     def test_run_fail(self):
47         self.mock_delete_keypair.return_value = False
48         with self.assertRaises(exceptions.ScenarioDeleteKeypairError):
49             self.delkey_obj.run(self.result)
50         self.assertEqual({'delete_keypair': 0}, self.result)
51         self.mock_log.error.assert_called_once_with("Delete keypair failed!")