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