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