Merge "Bugfix: Abnormally stopped background scenario causes test data lost"
[yardstick.git] / tests / unit / benchmark / scenarios / compute / test_lmbench.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.compute.lmbench.Lmbench
13
14 from __future__ import absolute_import
15
16 import unittest
17
18 import mock
19 from oslo_serialization import jsonutils
20
21 from yardstick.benchmark.scenarios.compute import lmbench
22
23
24 @mock.patch('yardstick.benchmark.scenarios.compute.lmbench.ssh')
25 class LmbenchTestCase(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
36         self.result = {}
37
38     def test_successful_setup(self, mock_ssh):
39
40         l = lmbench.Lmbench({}, self.ctx)
41         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
42
43         l.setup()
44         self.assertIsNotNone(l.client)
45         self.assertTrue(l.setup_done)
46
47     def test_unsuccessful_unknown_type_run(self, mock_ssh):
48
49         options = {
50             "test_type": "foo"
51         }
52         args = {'options': options}
53
54         l = lmbench.Lmbench(args, self.ctx)
55
56         self.assertRaises(RuntimeError, l.run, self.result)
57
58     def test_successful_latency_run_no_sla(self, mock_ssh):
59
60         options = {
61             "test_type": "latency",
62             "stride": 64,
63             "stop_size": 16
64         }
65         args = {'options': options}
66         l = lmbench.Lmbench(args, self.ctx)
67
68         sample_output = '[{"latency": 4.944, "size": 0.00049}]'
69         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
70         l.run(self.result)
71         expected_result = {"latencies0.latency": 4.944, "latencies0.size": 0.00049}
72         self.assertEqual(self.result, expected_result)
73
74     def test_successful_bandwidth_run_no_sla(self, mock_ssh):
75
76         options = {
77             "test_type": "bandwidth",
78             "size": 500,
79             "benchmark": "rd",
80             "warmup": 0
81         }
82         args = {"options": options}
83         l = lmbench.Lmbench(args, self.ctx)
84
85         sample_output = '{"size(MB)": 0.262144, "bandwidth(MBps)": 11025.5}'
86         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
87         l.run(self.result)
88         expected_result = jsonutils.loads(sample_output)
89         self.assertEqual(self.result, expected_result)
90
91     def test_successful_latency_run_sla(self, mock_ssh):
92
93         options = {
94             "test_type": "latency",
95             "stride": 64,
96             "stop_size": 16
97         }
98         args = {
99             "options": options,
100             "sla": {"max_latency": 35}
101         }
102         l = lmbench.Lmbench(args, self.ctx)
103
104         sample_output = '[{"latency": 4.944, "size": 0.00049}]'
105         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
106         l.run(self.result)
107         expected_result = {"latencies0.latency": 4.944, "latencies0.size": 0.00049}
108         self.assertEqual(self.result, expected_result)
109
110     def test_successful_bandwidth_run_sla(self, mock_ssh):
111
112         options = {
113             "test_type": "bandwidth",
114             "size": 500,
115             "benchmark": "rd",
116             "warmup": 0
117         }
118         args = {
119             "options": options,
120             "sla": {"min_bandwidth": 10000}
121         }
122         l = lmbench.Lmbench(args, self.ctx)
123
124         sample_output = '{"size(MB)": 0.262144, "bandwidth(MBps)": 11025.5}'
125         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
126         l.run(self.result)
127         expected_result = jsonutils.loads(sample_output)
128         self.assertEqual(self.result, expected_result)
129
130     def test_unsuccessful_latency_run_sla(self, mock_ssh):
131
132         options = {
133             "test_type": "latency",
134             "stride": 64,
135             "stop_size": 16
136         }
137         args = {
138             "options": options,
139             "sla": {"max_latency": 35}
140         }
141         l = lmbench.Lmbench(args, self.ctx)
142
143         sample_output = '[{"latency": 37.5, "size": 0.00049}]'
144         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
145         self.assertRaises(AssertionError, l.run, self.result)
146
147     def test_unsuccessful_bandwidth_run_sla(self, mock_ssh):
148
149         options = {
150             "test_type": "bandwidth",
151             "size": 500,
152             "benchmark": "rd",
153             "warmup": 0
154         }
155         args = {
156             "options": options,
157             "sla": {"min_bandwidth": 10000}
158         }
159         l = lmbench.Lmbench(args, self.ctx)
160
161         sample_output = '{"size(MB)": 0.262144, "bandwidth(MBps)": 9925.5}'
162         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
163         self.assertRaises(AssertionError, l.run, self.result)
164
165     def test_successful_latency_for_cache_run_sla(self, mock_ssh):
166
167         options = {
168             "test_type": "latency_for_cache",
169             "repetition": 1,
170             "warmup": 0
171         }
172         args = {
173             "options": options,
174             "sla": {"max_latency": 35}
175         }
176         l = lmbench.Lmbench(args, self.ctx)
177
178         sample_output = "{\"L1cache\": 1.6}"
179         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
180         l.run(self.result)
181         expected_result = jsonutils.loads(sample_output)
182         self.assertEqual(self.result, expected_result)
183
184     def test_unsuccessful_script_error(self, mock_ssh):
185
186         options = {"test_type": "bandwidth"}
187         args = {"options": options}
188         l = lmbench.Lmbench(args, self.ctx)
189
190         mock_ssh.SSH.from_node().execute.return_value = (1, '', 'FOOBAR')
191         self.assertRaises(RuntimeError, l.run, self.result)
192
193
194 def main():
195     unittest.main()
196
197 if __name__ == '__main__':
198     main()