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