Prohibit the importation of a list of libraries
[yardstick.git] / yardstick / tests / unit / benchmark / scenarios / lib / test_create_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 import mock
10 import unittest
11
12 from yardstick.benchmark.scenarios.lib import create_volume
13
14
15 class CreateVolumeTestCase(unittest.TestCase):
16
17     def setUp(self):
18         self._mock_cinder_client = mock.patch(
19             'yardstick.common.openstack_utils.get_cinder_client')
20         self.mock_cinder_client = self._mock_cinder_client.start()
21         self._mock_glance_client = mock.patch(
22             'yardstick.common.openstack_utils.get_glance_client')
23         self.mock_glance_client = self._mock_glance_client.start()
24         self.addCleanup(self._stop_mock)
25
26         self.scenario_cfg = {
27             "options" :
28                 {
29                     'volume_name': 'yardstick_test_volume_01',
30                     'size': '256',
31                     'image': 'cirros-0.3.5'
32                 }
33             }
34
35         self.scenario = create_volume.CreateVolume(
36             scenario_cfg=self.scenario_cfg,
37             context_cfg={})
38
39     def _stop_mock(self):
40         self._mock_cinder_client.stop()
41         self._mock_glance_client.stop()
42
43     def test_init(self):
44         self.mock_cinder_client.return_value = "All volumes are equal"
45         self.mock_glance_client.return_value = "Images are more equal"
46
47         expected_vol_name = self.scenario_cfg["options"]["volume_name"]
48         expected_vol_size = self.scenario_cfg["options"]["size"]
49         expected_im_name = self.scenario_cfg["options"]["image"]
50         expected_im_id = None
51
52         scenario = create_volume.CreateVolume(
53             scenario_cfg=self.scenario_cfg,
54             context_cfg={})
55
56         self.assertEqual(expected_vol_name, scenario.volume_name)
57         self.assertEqual(expected_vol_size, scenario.volume_size)
58         self.assertEqual(expected_im_name, scenario.image_name)
59         self.assertEqual(expected_im_id, scenario.image_id)
60         self.assertEqual("All volumes are equal", scenario.cinder_client)
61         self.assertEqual("Images are more equal", scenario.glance_client)
62
63     def test_setup(self):
64         self.assertFalse(self.scenario.setup_done)
65         self.scenario.setup()
66         self.assertTrue(self.scenario.setup_done)
67
68     @mock.patch('yardstick.common.openstack_utils.create_volume')
69     @mock.patch('yardstick.common.openstack_utils.get_image_id')
70     def test_run(self, mock_image_id, mock_create_volume):
71         self.scenario.run()
72
73         mock_image_id.assert_called_once()
74         mock_create_volume.assert_called_once()
75
76     @mock.patch.object(create_volume.CreateVolume, 'setup')
77     def test_run_no_setup(self, scenario_setup):
78         self.scenario.setup_done = False
79         self.scenario.run()
80         scenario_setup.assert_called_once()
81
82     @mock.patch('yardstick.common.openstack_utils.create_volume')
83     @mock.patch('yardstick.common.openstack_utils.get_image_id')
84     @mock.patch('yardstick.common.openstack_utils.get_cinder_client')
85     @mock.patch('yardstick.common.openstack_utils.get_glance_client')
86     def test_create_volume(self, mock_get_glance_client,
87                            mock_get_cinder_client, mock_image_id,
88                            mock_create_volume):
89         options = {
90             'volume_name': 'yardstick_test_volume_01',
91             'size': '256',
92             'image': 'cirros-0.3.5'
93         }
94         args = {"options": options}
95         scenario = create_volume.CreateVolume(args, {})
96         scenario.run()
97         self.assertTrue(mock_create_volume.called)
98         self.assertTrue(mock_image_id.called)
99         self.assertTrue(mock_get_glance_client.called)
100         self.assertTrue(mock_get_cinder_client.called)
101
102
103 def main():
104     unittest.main()
105
106
107 if __name__ == '__main__':
108     main()