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