Merge "Improve "get_server" function in Kubernetes context"
[yardstick.git] / yardstick / tests / unit / benchmark / scenarios / storage / test_storagecapacity.py
1 ##############################################################################
2 # Copyright (c) 2016 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
10 # Unittest for
11 # yardstick.benchmark.scenarios.storage.storagecapacity.StorageCapacity
12
13 from __future__ import absolute_import
14
15 import unittest
16
17 import mock
18 from oslo_serialization import jsonutils
19
20 from yardstick.benchmark.scenarios.storage import storagecapacity
21
22 DISK_SIZE_SAMPLE_OUTPUT = \
23     '{"Numberf of devides": "2", "Total disk size in bytes": "1024000000"}'
24 BLOCK_SIZE_SAMPLE_OUTPUT = '{"/dev/sda": 1024, "/dev/sdb": 4096}'
25 DISK_UTIL_RAW_OUTPUT = "vda 10.00\nvda 0.00"
26 DISK_UTIL_SAMPLE_OUTPUT = \
27     '{"vda": {"avg_util": 5.0, "max_util": 10.0, "min_util": 0.0}}'
28
29
30 @mock.patch('yardstick.benchmark.scenarios.storage.storagecapacity.ssh')
31 class StorageCapacityTestCase(unittest.TestCase):
32
33     def setUp(self):
34         self.scn = {
35             "options": {
36                 'test_type': 'disk_size'
37             }
38         }
39         self.ctx = {
40             "host": {
41                 'ip': '172.16.0.137',
42                 'user': 'cirros',
43                 'password': "root"
44             }
45         }
46         self.result = {}
47
48     def test_capacity_successful_setup(self, mock_ssh):
49         c = storagecapacity.StorageCapacity(self.scn, self.ctx)
50
51         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
52         c.setup()
53         self.assertIsNotNone(c.client)
54         self.assertTrue(c.setup_done)
55
56     def test_capacity_disk_size_successful(self, mock_ssh):
57         c = storagecapacity.StorageCapacity(self.scn, self.ctx)
58
59         mock_ssh.SSH.from_node().execute.return_value = (0, DISK_SIZE_SAMPLE_OUTPUT, '')
60         c.run(self.result)
61         expected_result = jsonutils.loads(
62             DISK_SIZE_SAMPLE_OUTPUT)
63         self.assertEqual(self.result, expected_result)
64
65     def test_capacity_block_size_successful(self, mock_ssh):
66         args = {
67             "options": {
68                 'test_type': 'block_size'
69             }
70         }
71         c = storagecapacity.StorageCapacity(args, self.ctx)
72
73         mock_ssh.SSH.from_node().execute.return_value = (0, BLOCK_SIZE_SAMPLE_OUTPUT, '')
74         c.run(self.result)
75         expected_result = jsonutils.loads(
76             BLOCK_SIZE_SAMPLE_OUTPUT)
77         self.assertEqual(self.result, expected_result)
78
79     def test_capacity_disk_utilization_successful(self, mock_ssh):
80         args = {
81             "options": {
82                 'test_type': 'disk_utilization',
83                 'interval': 1,
84                 'count': 2
85             }
86         }
87         c = storagecapacity.StorageCapacity(args, self.ctx)
88
89         mock_ssh.SSH.from_node().execute.return_value = (0, DISK_UTIL_RAW_OUTPUT, '')
90         c.run(self.result)
91         expected_result = jsonutils.loads(
92             DISK_UTIL_SAMPLE_OUTPUT)
93         self.assertEqual(self.result, expected_result)
94
95     def test_capacity_unsuccessful_script_error(self, mock_ssh):
96         c = storagecapacity.StorageCapacity(self.scn, self.ctx)
97
98         mock_ssh.SSH.from_node().execute.return_value = (1, '', 'FOOBAR')
99         self.assertRaises(RuntimeError, c.run, self.result)