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