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