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