Support run cyclictest on BareMetal
[yardstick.git] / tests / unit / benchmark / scenarios / compute / test_cyclictest.py
1 #!/usr/bin/env python
2
3 ##############################################################################
4 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and other.
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.cyclictest.Cyclictest
13
14 import mock
15 import unittest
16 import json
17
18 from yardstick.benchmark.scenarios.compute import cyclictest
19
20
21 @mock.patch('yardstick.benchmark.scenarios.compute.cyclictest.ssh')
22 class CyclictestTestCase(unittest.TestCase):
23
24     def setUp(self):
25         self.scenario_cfg = {
26             "host": "kvm.LF",
27             "setup_options": {
28                 "rpm_dir": "/opt/rpm",
29                 "host_setup_seqs": [
30                     "host-setup0.sh",
31                     "host-setup1.sh",
32                     "host-run-qemu.sh"
33                 ],
34                 "script_dir": "/opt/scripts",
35                 "image_dir": "/opt/image",
36                 "guest_setup_seqs": [
37                     "guest-setup0.sh",
38                     "guest-setup1.sh"
39                 ]
40             },
41             "sla": {
42                 "action": "monitor",
43                 "max_min_latency": 50,
44                 "max_avg_latency": 100,
45                 "max_max_latency": 1000
46             },
47             "options": {
48                 "priority": 99,
49                 "threads": 1,
50                 "loops": 1000,
51                 "affinity": 1,
52                 "interval": 1000,
53                 "histogram": 90
54             }
55         }
56         self.context_cfg = {
57             "host": {
58                 "ip": "10.229.43.154",
59                 "key_filename": "/yardstick/resources/files/yardstick_key",
60                 "role": "BareMetal",
61                 "name": "kvm.LF",
62                 "user": "root"
63             }
64         }
65
66     def test_cyclictest_successful_setup(self, mock_ssh):
67
68         c = cyclictest.Cyclictest(self.scenario_cfg, self.context_cfg)
69         mock_ssh.SSH().execute.return_value = (0, '', '')
70
71         c.setup()
72         self.assertIsNotNone(c.guest)
73         self.assertIsNotNone(c.host)
74         self.assertEqual(c.setup_done, True)
75
76     def test_cyclictest_successful_no_sla(self, mock_ssh):
77         result = {}
78         self.scenario_cfg.pop("sla", None)
79         c = cyclictest.Cyclictest(self.scenario_cfg, self.context_cfg)
80         mock_ssh.SSH().execute.return_value = (0, '', '')
81         c.setup()
82
83         c.guest = mock_ssh.SSH()
84         sample_output = '{"min": 100, "avg": 500, "max": 1000}'
85         mock_ssh.SSH().execute.return_value = (0, sample_output, '')
86
87         c.run(result)
88         expected_result = json.loads(sample_output)
89         self.assertEqual(result, expected_result)
90
91     def test_cyclictest_successful_sla(self, mock_ssh):
92         result = {}
93         self.scenario_cfg.update({"sla": {
94                 "action": "monitor",
95                 "max_min_latency": 100,
96                 "max_avg_latency": 500,
97                 "max_max_latency": 1000
98             }
99         })
100         c = cyclictest.Cyclictest(self.scenario_cfg, self.context_cfg)
101         mock_ssh.SSH().execute.return_value = (0, '', '')
102         c.setup()
103
104         c.guest = mock_ssh.SSH()
105         sample_output = '{"min": 100, "avg": 500, "max": 1000}'
106         mock_ssh.SSH().execute.return_value = (0, sample_output, '')
107
108         c.run(result)
109         expected_result = json.loads(sample_output)
110         self.assertEqual(result, expected_result)
111
112     def test_cyclictest_unsuccessful_sla_min_latency(self, mock_ssh):
113
114         result = {}
115         self.scenario_cfg.update({"sla": {"max_min_latency": 10}})
116         c = cyclictest.Cyclictest(self.scenario_cfg, self.context_cfg)
117         mock_ssh.SSH().execute.return_value = (0, '', '')
118         c.setup()
119
120         c.guest = mock_ssh.SSH()
121         sample_output = '{"min": 100, "avg": 500, "max": 1000}'
122
123         mock_ssh.SSH().execute.return_value = (0, sample_output, '')
124         self.assertRaises(AssertionError, c.run, result)
125
126     def test_cyclictest_unsuccessful_sla_avg_latency(self, mock_ssh):
127
128         result = {}
129         self.scenario_cfg.update({"sla": {"max_avg_latency": 10}})
130         c = cyclictest.Cyclictest(self.scenario_cfg, self.context_cfg)
131         mock_ssh.SSH().execute.return_value = (0, '', '')
132         c.setup()
133
134         c.guest = mock_ssh.SSH()
135         sample_output = '{"min": 100, "avg": 500, "max": 1000}'
136
137         mock_ssh.SSH().execute.return_value = (0, sample_output, '')
138         self.assertRaises(AssertionError, c.run, result)
139
140     def test_cyclictest_unsuccessful_sla_max_latency(self, mock_ssh):
141
142         result = {}
143         self.scenario_cfg.update({"sla": {"max_max_latency": 10}})
144         c = cyclictest.Cyclictest(self.scenario_cfg, self.context_cfg)
145         mock_ssh.SSH().execute.return_value = (0, '', '')
146         c.setup()
147
148         c.guest = mock_ssh.SSH()
149         sample_output = '{"min": 100, "avg": 500, "max": 1000}'
150
151         mock_ssh.SSH().execute.return_value = (0, sample_output, '')
152         self.assertRaises(AssertionError, c.run, result)
153
154     def test_cyclictest_unsuccessful_script_error(self, mock_ssh):
155
156         result = {}
157         self.scenario_cfg.update({"sla": {"max_max_latency": 10}})
158         c = cyclictest.Cyclictest(self.scenario_cfg, self.context_cfg)
159         mock_ssh.SSH().execute.return_value = (0, '', '')
160         c.setup()
161
162         c.guest = mock_ssh.SSH()
163
164         mock_ssh.SSH().execute.return_value = (1, '', 'FOOBAR')
165         self.assertRaises(RuntimeError, c.run, result)
166
167
168 def main():
169     unittest.main()
170
171 if __name__ == '__main__':
172     main()