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