[bugfix]tc006 failed due to volume attached to different location "/dev/vdc"
[yardstick.git] / yardstick / 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.from_node().execute.return_value = (0, '', '')
55         self.assertIsNotNone(p.client)
56         self.assertTrue(p.setup_done)
57
58     def test_fio_job_file_successful_setup(self, mock_ssh):
59
60         options = {
61             'job_file': 'job_file.ini',
62             'directory': '/FIO_Test'
63         }
64         args = {'options': options}
65         p = fio.Fio(args, self.ctx)
66         mock_ssh.SSH.from_node().execute.return_value = (0, '/dev/vdb', '')
67         p.setup()
68
69         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
70         self.assertIsNotNone(p.client)
71         self.assertTrue(p.setup_done)
72
73     def test_fio_job_file_no_disk__setup(self, mock_ssh):
74
75         options = {
76             'job_file': 'job_file.ini',
77             'directory': '/FIO_Test'
78         }
79         args = {'options': options}
80         p = fio.Fio(args, self.ctx)
81         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
82         p.setup()
83
84         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
85         self.assertIsNotNone(p.client)
86         self.assertTrue(p.setup_done)
87
88     def test_fio_successful_no_sla(self, mock_ssh):
89
90         options = {
91             'filename': '/home/ubuntu/data.raw',
92             'bs': '4k',
93             'rw': 'rw',
94             'ramp_time': 10
95         }
96         args = {'options': options}
97         p = fio.Fio(args, self.ctx)
98         result = {}
99
100         p.client = mock_ssh.SSH.from_node()
101
102         sample_output = self._read_sample_output(self.sample_output['rw'])
103         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
104
105         p.run(result)
106
107         expected_result = '{"read_bw": 83888, "read_iops": 20972,' \
108             '"read_lat": 236.8, "write_bw": 84182, "write_iops": 21045,'\
109             '"write_lat": 233.55}'
110         expected_result = jsonutils.loads(expected_result)
111         self.assertEqual(result, expected_result)
112
113     def test_fio_successful_read_no_sla(self, mock_ssh):
114
115         options = {
116             'filename': '/home/ubuntu/data.raw',
117             'bs': '4k',
118             'rw': "read",
119             'ramp_time': 10
120         }
121         args = {'options': options}
122         p = fio.Fio(args, self.ctx)
123         result = {}
124
125         p.client = mock_ssh.SSH.from_node()
126
127         sample_output = self._read_sample_output(self.sample_output['read'])
128         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
129
130         p.run(result)
131
132         expected_result = '{"read_bw": 36113, "read_iops": 9028,' \
133             '"read_lat": 108.7}'
134         expected_result = jsonutils.loads(expected_result)
135         self.assertEqual(result, expected_result)
136
137     def test_fio_successful_write_no_sla(self, mock_ssh):
138
139         options = {
140             'filename': '/home/ubuntu/data.raw',
141             'bs': '4k',
142             'rw': 'write',
143             'ramp_time': 10
144         }
145         args = {'options': options}
146         p = fio.Fio(args, self.ctx)
147         result = {}
148
149         p.client = mock_ssh.SSH.from_node()
150
151         sample_output = self._read_sample_output(self.sample_output['write'])
152         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
153
154         p.run(result)
155
156         expected_result = '{"write_bw": 35107, "write_iops": 8776,'\
157             '"write_lat": 111.74}'
158         expected_result = jsonutils.loads(expected_result)
159         self.assertEqual(result, expected_result)
160
161     def test_fio_successful_lat_sla(self, mock_ssh):
162
163         options = {
164             'filename': '/home/ubuntu/data.raw',
165             'bs': '4k',
166             'rw': 'rw',
167             'ramp_time': 10
168         }
169         args = {
170             'options': options,
171             'sla': {'write_lat': 300.1}
172         }
173         p = fio.Fio(args, self.ctx)
174         result = {}
175
176         p.client = mock_ssh.SSH.from_node()
177
178         sample_output = self._read_sample_output(self.sample_output['rw'])
179         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
180
181         p.run(result)
182
183         expected_result = '{"read_bw": 83888, "read_iops": 20972,' \
184             '"read_lat": 236.8, "write_bw": 84182, "write_iops": 21045,'\
185             '"write_lat": 233.55}'
186         expected_result = jsonutils.loads(expected_result)
187         self.assertEqual(result, expected_result)
188
189     def test_fio_unsuccessful_lat_sla(self, mock_ssh):
190
191         options = {
192             'filename': '/home/ubuntu/data.raw',
193             'bs': '4k',
194             'rw': 'rw',
195             'ramp_time': 10
196         }
197         args = {
198             'options': options,
199             'sla': {'write_lat': 200.1}
200         }
201         p = fio.Fio(args, self.ctx)
202         result = {}
203
204         p.client = mock_ssh.SSH.from_node()
205
206         sample_output = self._read_sample_output(self.sample_output['rw'])
207         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
208         self.assertRaises(AssertionError, p.run, result)
209
210     def test_fio_successful_bw_iops_sla(self, mock_ssh):
211
212         options = {
213             'filename': '/home/ubuntu/data.raw',
214             'bs': '4k',
215             'rw': 'rw',
216             'ramp_time': 10
217         }
218         args = {
219             'options': options,
220             'sla': {'read_iops': 20000}
221         }
222         p = fio.Fio(args, self.ctx)
223         result = {}
224
225         p.client = mock_ssh.SSH.from_node()
226
227         sample_output = self._read_sample_output(self.sample_output['rw'])
228         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
229
230         p.run(result)
231
232         expected_result = '{"read_bw": 83888, "read_iops": 20972,' \
233             '"read_lat": 236.8, "write_bw": 84182, "write_iops": 21045,'\
234             '"write_lat": 233.55}'
235         expected_result = jsonutils.loads(expected_result)
236         self.assertEqual(result, expected_result)
237
238     def test_fio_unsuccessful_bw_iops_sla(self, mock_ssh):
239
240         options = {
241             'filename': '/home/ubuntu/data.raw',
242             'bs': '4k',
243             'rw': 'rw',
244             'ramp_time': 10
245         }
246         args = {
247             'options': options,
248             'sla': {'read_iops': 30000}
249         }
250         p = fio.Fio(args, self.ctx)
251         result = {}
252
253         p.client = mock_ssh.SSH.from_node()
254
255         sample_output = self._read_sample_output(self.sample_output['rw'])
256         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
257         self.assertRaises(AssertionError, p.run, result)
258
259     def test_fio_unsuccessful_script_error(self, mock_ssh):
260
261         options = {
262             'filename': '/home/ubuntu/data.raw',
263             'bs': '4k',
264             'rw': 'rw',
265             'ramp_time': 10
266         }
267         args = {'options': options}
268         p = fio.Fio(args, self.ctx)
269         result = {}
270
271         p.client = mock_ssh.SSH.from_node()
272
273         mock_ssh.SSH.from_node().execute.return_value = (1, '', 'FOOBAR')
274         self.assertRaises(RuntimeError, p.run, result)
275
276     def _read_sample_output(self, file_name):
277         curr_path = os.path.dirname(os.path.abspath(__file__))
278         output = os.path.join(curr_path, file_name)
279         with open(output) as f:
280             sample_output = f.read()
281         return sample_output
282
283
284 def main():
285     unittest.main()
286
287
288 if __name__ == '__main__':
289     main()