54f493ee6245a07096f5ac618427108bcf99a7f4
[yardstick.git] / 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 import mock
15 import unittest
16 import json
17
18 from yardstick.benchmark.scenarios.storage import fio
19
20
21 @mock.patch('yardstick.benchmark.scenarios.storage.fio.ssh')
22 class FioTestCase(unittest.TestCase):
23
24     def setUp(self):
25         self.ctx = {
26             'host': '172.16.0.137',
27             'user': 'cirros',
28             'key_filename': "mykey.key"
29         }
30
31     def test_fio_successful_setup(self, mock_ssh):
32
33         p = fio.Fio(self.ctx)
34         options = {
35             'filename': "/home/ec2-user/data.raw",
36             'bs': "4k",
37             'rw': "write",
38             'ramp_time': 10
39         }
40         args = {'options': options}
41         p.setup()
42
43         mock_ssh.SSH().execute.return_value = (0, '', '')
44         self.assertIsNotNone(p.client)
45         self.assertEqual(p.setup_done, True)
46
47     def test_fio_successful_no_sla(self, mock_ssh):
48
49         p = fio.Fio(self.ctx)
50         options = {
51             'filename': "/home/ec2-user/data.raw",
52             'bs': "4k",
53             'rw': "write",
54             'ramp_time': 10
55         }
56         args = {'options': options}
57         p.client = mock_ssh.SSH()
58
59         sample_output = '{"read_bw": "N/A", "write_lat": "407.08usec", \
60             "read_iops": "N/A", "write_bw": "9507KB/s", \
61             "write_iops": "2376", "read_lat": "N/A"}'
62         mock_ssh.SSH().execute.return_value = (0, sample_output, '')
63
64         result = p.run(args)
65         expected_result = json.loads(sample_output)
66         self.assertEqual(result, expected_result)
67
68     def test_fio_unsuccessful_script_error(self, mock_ssh):
69
70         p = fio.Fio(self.ctx)
71         options = {
72             'filename': "/home/ec2-user/data.raw",
73             'bs': "4k",
74             'rw': "write",
75             'ramp_time': 10
76         }
77         args = {'options': options}
78         p.client = mock_ssh.SSH()
79
80         mock_ssh.SSH().execute.return_value = (1, '', 'FOOBAR')
81         self.assertRaises(RuntimeError, p.run, args)
82
83
84 def main():
85     unittest.main()
86
87 if __name__ == '__main__':
88     main()