Merge "Move tests: unit/network_services/{lib/,collector/,*.py}"
[yardstick.git] / yardstick / tests / unit / benchmark / scenarios / lib / test_attach_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 attach_volume
16
17
18 class AttachVolumeTestCase(unittest.TestCase):
19
20     def setUp(self):
21
22         self._mock_attach_volume_to_server = mock.patch.object(
23             openstack_utils, 'attach_volume_to_server')
24         self.mock_attach_volume_to_server = (
25             self._mock_attach_volume_to_server.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(attach_volume, 'LOG')
30         self.mock_log = self._mock_log.start()
31         _uuid = uuidutils.generate_uuid()
32         self.args = {'options': {'server_name_or_id': _uuid,
33                                  'volume_name_or_id': _uuid}}
34         self.result = {}
35         self.addCleanup(self._stop_mock)
36         self.attachvol_obj = attach_volume.AttachVolume(self.args, mock.ANY)
37
38     def _stop_mock(self):
39         self._mock_attach_volume_to_server.stop()
40         self._mock_get_shade_client.stop()
41         self._mock_log.stop()
42
43     def test_run(self):
44         self.mock_attach_volume_to_server.return_value = True
45         self.assertIsNone(self.attachvol_obj.run(self.result))
46         self.assertEqual({'attach_volume': 1}, self.result)
47         self.mock_log.info.asset_called_once_with(
48             'Attach volume to server successful!')
49
50     def test_run_fail(self):
51         self.mock_attach_volume_to_server.return_value = False
52         with self.assertRaises(exceptions.ScenarioAttachVolumeError):
53             self.attachvol_obj.run(self.result)
54         self.assertEqual({'attach_volume': 0}, self.result)
55         self.mock_log.error.assert_called_once_with(
56             'Attach volume to server failed!')