Merge "Document for Fraser test case results"
[yardstick.git] / yardstick / tests / unit / benchmark / scenarios / lib / test_detach_volume.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 detach_volume
16
17
18 class DetachVolumeTestCase(unittest.TestCase):
19
20     def setUp(self):
21         self._mock_detach_volume = mock.patch.object(
22             openstack_utils, 'detach_volume')
23         self.mock_detach_volume = (
24             self._mock_detach_volume.start())
25         self._mock_get_shade_client = mock.patch.object(
26             openstack_utils, 'get_shade_client')
27         self.mock_get_shade_client = self._mock_get_shade_client.start()
28         self._mock_log = mock.patch.object(detach_volume, 'LOG')
29         self.mock_log = self._mock_log.start()
30         _uuid = uuidutils.generate_uuid()
31         self.args = {'options': {'server_name_or_id':  _uuid,
32                                  'volume_name_or_id': _uuid}}
33         self.result = {}
34
35         self.detachvol_obj = detach_volume.DetachVolume(self.args, mock.ANY)
36
37         self.addCleanup(self._stop_mock)
38
39     def _stop_mock(self):
40         self._mock_detach_volume.stop()
41         self._mock_get_shade_client.stop()
42         self._mock_log.stop()
43
44     def test_run(self):
45         self.mock_detach_volume.return_value = True
46         self.assertIsNone(self.detachvol_obj.run(self.result))
47         self.assertEqual({'detach_volume': 1}, self.result)
48         self.mock_log.info.assert_called_once_with(
49             'Detach volume from server successful!')
50
51     def test_run_fail(self):
52         self.mock_detach_volume.return_value = False
53         with self.assertRaises(exceptions.ScenarioDetachVolumeError):
54             self.detachvol_obj.run(self.result)
55         self.assertEqual({'detach_volume': 0}, self.result)
56         self.mock_log.error.assert_called_once_with(
57             'Detach volume from server failed!')