6d38e9c5334dcbeafe58674a3c87b899b7dbfe36
[yardstick.git] / tests / unit / benchmark / scenarios / storage / test_fio.py
1 #!/usr/bin/env python
2
3 ##############################################################################
4 # Copyright (c) 2015 Ericsson AB 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 yardstick.benchmark.scenarios.storage.fio.Fio
13
14 import mock
15 import unittest
16 import json
17 import os
18
19 from yardstick.benchmark.scenarios.storage import fio
20
21
22 @mock.patch('yardstick.benchmark.scenarios.storage.fio.ssh')
23 class FioTestCase(unittest.TestCase):
24
25     def setUp(self):
26         self.ctx = {
27             'host': '172.16.0.137',
28             'user': 'cirros',
29             'key_filename': 'mykey.key'
30         }
31         self.sample_output = {
32             'read': 'fio_read_sample_output.json',
33             'write': 'fio_write_sample_output.json',
34             'rw': 'fio_rw_sample_output.json'
35         }
36
37     def test_fio_successful_setup(self, mock_ssh):
38
39         p = fio.Fio(self.ctx)
40         options = {
41             'filename': '/home/ec2-user/data.raw',
42             'bs': '4k',
43             'rw': 'rw',
44             'ramp_time': 10
45         }
46         args = {'options': options}
47         p.setup()
48
49         mock_ssh.SSH().execute.return_value = (0, '', '')
50         self.assertIsNotNone(p.client)
51         self.assertEqual(p.setup_done, True)
52
53     def test_fio_successful_no_sla(self, mock_ssh):
54
55         p = fio.Fio(self.ctx)
56         options = {
57             'filename': '/home/ec2-user/data.raw',
58             'bs': '4k',
59             'rw': 'rw',
60             'ramp_time': 10
61         }
62         args = {'options': options}
63         p.client = mock_ssh.SSH()
64
65         sample_output = self._read_sample_output(self.sample_output['rw'])
66         mock_ssh.SSH().execute.return_value = (0, sample_output, '')
67
68         result = p.run(args)
69
70         expected_result = '{"read_bw": 83888, "read_iops": 20972,' \
71             '"read_lat": 236.8, "write_bw": 84182, "write_iops": 21045,'\
72             '"write_lat": 233.55}'
73         expected_result = json.loads(expected_result)
74         self.assertEqual(result, expected_result)
75
76     def test_fio_successful_read_no_sla(self, mock_ssh):
77
78         p = fio.Fio(self.ctx)
79         options = {
80             'filename': '/home/ec2-user/data.raw',
81             'bs': '4k',
82             'rw': "read",
83             'ramp_time': 10
84         }
85         args = {'options': options}
86         p.client = mock_ssh.SSH()
87
88         sample_output = self._read_sample_output(self.sample_output['read'])
89         mock_ssh.SSH().execute.return_value = (0, sample_output, '')
90
91         result = p.run(args)
92
93         expected_result = '{"read_bw": 36113, "read_iops": 9028,' \
94             '"read_lat": 108.7}'
95         expected_result = json.loads(expected_result)
96         self.assertEqual(result, expected_result)
97
98     def test_fio_successful_write_no_sla(self, mock_ssh):
99
100         p = fio.Fio(self.ctx)
101         options = {
102             'filename': '/home/ec2-user/data.raw',
103             'bs': '4k',
104             'rw': 'write',
105             'ramp_time': 10
106         }
107         args = {'options': options}
108         p.client = mock_ssh.SSH()
109
110         sample_output = self._read_sample_output(self.sample_output['write'])
111         mock_ssh.SSH().execute.return_value = (0, sample_output, '')
112
113         result = p.run(args)
114
115         expected_result = '{"write_bw": 35107, "write_iops": 8776,'\
116             '"write_lat": 111.74}'
117         expected_result = json.loads(expected_result)
118         self.assertEqual(result, expected_result)
119
120     def test_fio_successful_lat_sla(self, mock_ssh):
121
122         p = fio.Fio(self.ctx)
123         options = {
124             'filename': '/home/ec2-user/data.raw',
125             'bs': '4k',
126             'rw': 'rw',
127             'ramp_time': 10
128         }
129         args = {
130             'options': options,
131             'sla': {'write_lat': 300.1}
132         }
133
134         p.client = mock_ssh.SSH()
135
136         sample_output = self._read_sample_output(self.sample_output['rw'])
137         mock_ssh.SSH().execute.return_value = (0, sample_output, '')
138
139         result = p.run(args)
140
141         expected_result = '{"read_bw": 83888, "read_iops": 20972,' \
142             '"read_lat": 236.8, "write_bw": 84182, "write_iops": 21045,'\
143             '"write_lat": 233.55}'
144         expected_result = json.loads(expected_result)
145         self.assertEqual(result, expected_result)
146
147
148     def test_fio_unsuccessful_lat_sla(self, mock_ssh):
149
150         p = fio.Fio(self.ctx)
151         options = {
152             'filename': '/home/ec2-user/data.raw',
153             'bs': '4k',
154             'rw': 'rw',
155             'ramp_time': 10
156         }
157         args = {
158             'options': options,
159             'sla': {'write_lat': 200.1}
160         }
161
162         p.client = mock_ssh.SSH()
163
164         sample_output = self._read_sample_output(self.sample_output['rw'])
165         mock_ssh.SSH().execute.return_value = (0, sample_output, '')
166         self.assertRaises(AssertionError, p.run, args)
167
168     def test_fio_successful_bw_iops_sla(self, mock_ssh):
169
170         p = fio.Fio(self.ctx)
171         options = {
172             'filename': '/home/ec2-user/data.raw',
173             'bs': '4k',
174             'rw': 'rw',
175             'ramp_time': 10
176         }
177         args = {
178             'options': options,
179             'sla': {'read_iops': 20000}
180         }
181
182         p.client = mock_ssh.SSH()
183
184         sample_output = self._read_sample_output(self.sample_output['rw'])
185         mock_ssh.SSH().execute.return_value = (0, sample_output, '')
186
187         result = p.run(args)
188
189         expected_result = '{"read_bw": 83888, "read_iops": 20972,' \
190             '"read_lat": 236.8, "write_bw": 84182, "write_iops": 21045,'\
191             '"write_lat": 233.55}'
192         expected_result = json.loads(expected_result)
193         self.assertEqual(result, expected_result)
194
195     def test_fio_unsuccessful_bw_iops_sla(self, mock_ssh):
196
197         p = fio.Fio(self.ctx)
198         options = {
199             'filename': '/home/ec2-user/data.raw',
200             'bs': '4k',
201             'rw': 'rw',
202             'ramp_time': 10
203         }
204         args = {
205             'options': options,
206             'sla': {'read_iops': 30000}
207         }
208
209         p.client = mock_ssh.SSH()
210
211         sample_output = self._read_sample_output(self.sample_output['rw'])
212         mock_ssh.SSH().execute.return_value = (0, sample_output, '')
213         self.assertRaises(AssertionError, p.run, args)
214
215     def test_fio_unsuccessful_script_error(self, mock_ssh):
216
217         p = fio.Fio(self.ctx)
218         options = {
219             'filename': '/home/ec2-user/data.raw',
220             'bs': '4k',
221             'rw': 'rw',
222             'ramp_time': 10
223         }
224         args = {'options': options}
225         p.client = mock_ssh.SSH()
226
227         mock_ssh.SSH().execute.return_value = (1, '', 'FOOBAR')
228         self.assertRaises(RuntimeError, p.run, args)
229
230     def _read_sample_output(self, file_name):
231         curr_path = os.path.dirname(os.path.abspath(__file__))
232         output = os.path.join(curr_path, file_name)
233         with open(output) as f:
234             sample_output = f.read()
235         return sample_output
236
237 def main():
238     unittest.main()
239
240 if __name__ == '__main__':
241     main()