aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/benchmark/scenarios/lib/test_create_volume.py
blob: ef2c0ccaf96b0054a898bf0f61c1a8f3d15e3e84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
##############################################################################
# Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
import unittest
import mock

import yardstick.benchmark.scenarios.lib.create_volume


class CreateVolumeTestCase(unittest.TestCase):

    def setUp(self):
        self._mock_cinder_client = mock.patch(
            'yardstick.common.openstack_utils.get_cinder_client')
        self.mock_cinder_client = self._mock_cinder_client.start()
        self._mock_glance_client = mock.patch(
            'yardstick.common.openstack_utils.get_glance_client')
        self.mock_glance_client = self._mock_glance_client.start()
        self.addCleanup(self._stop_mock)

        self.scenario_cfg = {
            "options" :
                {
                    'volume_name': 'yardstick_test_volume_01',
                    'size': '256',
                    'image': 'cirros-0.3.5'
                }
            }

        self.scenario = (
            yardstick.benchmark.scenarios.lib.create_volume.CreateVolume(
                scenario_cfg=self.scenario_cfg,
                context_cfg={}))

    def _stop_mock(self):
        self._mock_cinder_client.stop()
        self._mock_glance_client.stop()

    def test_init(self):
        self.mock_cinder_client.return_value = "All volumes are equal"
        self.mock_glance_client.return_value = "Images are more equal"

        expected_vol_name = self.scenario_cfg["options"]["volume_name"]
        expected_vol_size = self.scenario_cfg["options"]["size"]
        expected_im_name = self.scenario_cfg["options"]["image"]
        expected_im_id = None

        scenario = (
            yardstick.benchmark.scenarios.lib.create_volume.CreateVolume(
                scenario_cfg=self.scenario_cfg,
                context_cfg={}))

        self.assertEqual(expected_vol_name, scenario.volume_name)
        self.assertEqual(expected_vol_size, scenario.volume_size)
        self.assertEqual(expected_im_name, scenario.image_name)
        self.assertEqual(expected_im_id, scenario.image_id)
        self.assertEqual("All volumes are equal", scenario.cinder_client)
        self.assertEqual("Images are more equal", scenario.glance_client)

    def test_setup(self):
        self.assertFalse(self.scenario.setup_done)
        self.scenario.setup()
        self.assertTrue(self.scenario.setup_done)

    @mock.patch('yardstick.common.openstack_utils.create_volume')
    @mock.patch('yardstick.common.openstack_utils.get_image_id')
    def test_run(self, mock_image_id, mock_create_volume):
        self.scenario.run()

        mock_image_id.assert_called_once()
        mock_create_volume.assert_called_once()

    @mock.patch.object(
        yardstick.benchmark.scenarios.lib.create_volume.CreateVolume, 'setup')
    def test_run_no_setup(self, scenario_setup):
        self.scenario.setup_done = False
        self.scenario.run()
        scenario_setup.assert_called_once()


def main():
    unittest.main()


if __name__ == '__main__':
    main()