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