Merge "Fixed document for Grafana Port usage"
[yardstick.git] / yardstick / tests / unit / benchmark / scenarios / storage / test_storagecapacity.py
1 #!/usr/bin/env python
2
3 ##############################################################################
4 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
11
12 # Unittest for
13 # yardstick.benchmark.scenarios.storage.storagecapacity.StorageCapacity
14
15 from __future__ import absolute_import
16
17 import unittest
18
19 import mock
20 from oslo_serialization import jsonutils
21
22 from yardstick.benchmark.scenarios.storage import storagecapacity
23
24 DISK_SIZE_SAMPLE_OUTPUT = \
25     '{"Numberf of devides": "2", "Total disk size in bytes": "1024000000"}'
26 BLOCK_SIZE_SAMPLE_OUTPUT = '{"/dev/sda": 1024, "/dev/sdb": 4096}'
27 DISK_UTIL_RAW_OUTPUT = "vda 10.00\nvda 0.00"
28 DISK_UTIL_SAMPLE_OUTPUT = \
29     '{"vda": {"avg_util": 5.0, "max_util": 10.0, "min_util": 0.0}}'
30
31
32 @mock.patch('yardstick.benchmark.scenarios.storage.storagecapacity.ssh')
33 class StorageCapacityTestCase(unittest.TestCase):
34
35     def setUp(self):
36         self.scn = {
37             "options": {
38                 'test_type': 'disk_size'
39             }
40         }
41         self.ctx = {
42             "host": {
43                 'ip': '172.16.0.137',
44                 'user': 'cirros',
45                 'password': "root"
46             }
47         }
48         self.result = {}
49
50     def test_capacity_successful_setup(self, mock_ssh):
51         c = storagecapacity.StorageCapacity(self.scn, self.ctx)
52
53         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
54         c.setup()
55         self.assertIsNotNone(c.client)
56         self.assertTrue(c.setup_done)
57
58     def test_capacity_disk_size_successful(self, mock_ssh):
59         c = storagecapacity.StorageCapacity(self.scn, self.ctx)
60
61         mock_ssh.SSH.from_node().execute.return_value = (0, DISK_SIZE_SAMPLE_OUTPUT, '')
62         c.run(self.result)
63         expected_result = jsonutils.loads(
64             DISK_SIZE_SAMPLE_OUTPUT)
65         self.assertEqual(self.result, expected_result)
66
67     def test_capacity_block_size_successful(self, mock_ssh):
68         args = {
69             "options": {
70                 'test_type': 'block_size'
71             }
72         }
73         c = storagecapacity.StorageCapacity(args, self.ctx)
74
75         mock_ssh.SSH.from_node().execute.return_value = (0, BLOCK_SIZE_SAMPLE_OUTPUT, '')
76         c.run(self.result)
77         expected_result = jsonutils.loads(
78             BLOCK_SIZE_SAMPLE_OUTPUT)
79         self.assertEqual(self.result, expected_result)
80
81     def test_capacity_disk_utilization_successful(self, mock_ssh):
82         args = {
83             "options": {
84                 'test_type': 'disk_utilization',
85                 'interval': 1,
86                 'count': 2
87             }
88         }
89         c = storagecapacity.StorageCapacity(args, self.ctx)
90
91         mock_ssh.SSH.from_node().execute.return_value = (0, DISK_UTIL_RAW_OUTPUT, '')
92         c.run(self.result)
93         expected_result = jsonutils.loads(
94             DISK_UTIL_SAMPLE_OUTPUT)
95         self.assertEqual(self.result, expected_result)
96
97     def test_capacity_unsuccessful_script_error(self, mock_ssh):
98         c = storagecapacity.StorageCapacity(self.scn, self.ctx)
99
100         mock_ssh.SSH.from_node().execute.return_value = (1, '', 'FOOBAR')
101         self.assertRaises(RuntimeError, c.run, self.result)
102
103
104 def main():
105     unittest.main()
106
107 if __name__ == '__main__':
108     main()