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