3 ##############################################################################
4 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and other.
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 ##############################################################################
12 # Unittest for yardstick.benchmark.scenarios.compute.cyclictest.Cyclictest
18 from yardstick.benchmark.scenarios.compute import cyclictest
21 @mock.patch('yardstick.benchmark.scenarios.compute.cyclictest.ssh')
22 class CyclictestTestCase(unittest.TestCase):
28 "rpm_dir": "/opt/rpm",
34 "script_dir": "/opt/scripts",
35 "image_dir": "/opt/image",
43 "max_min_latency": 50,
44 "max_avg_latency": 100,
45 "max_max_latency": 1000
58 "ip": "10.229.43.154",
59 "key_filename": "/yardstick/resources/files/yardstick_key",
66 def test_cyclictest_successful_setup(self, mock_ssh):
68 c = cyclictest.Cyclictest(self.scenario_cfg, self.context_cfg)
69 mock_ssh.SSH().execute.return_value = (0, '', '')
72 self.assertIsNotNone(c.guest)
73 self.assertIsNotNone(c.host)
74 self.assertEqual(c.setup_done, True)
76 def test_cyclictest_successful_no_sla(self, mock_ssh):
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, '', '')
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, '')
88 expected_result = json.loads(sample_output)
89 self.assertEqual(result, expected_result)
91 def test_cyclictest_successful_sla(self, mock_ssh):
93 self.scenario_cfg.update({"sla": {
95 "max_min_latency": 100,
96 "max_avg_latency": 500,
97 "max_max_latency": 1000
100 c = cyclictest.Cyclictest(self.scenario_cfg, self.context_cfg)
101 mock_ssh.SSH().execute.return_value = (0, '', '')
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, '')
109 expected_result = json.loads(sample_output)
110 self.assertEqual(result, expected_result)
112 def test_cyclictest_unsuccessful_sla_min_latency(self, mock_ssh):
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, '', '')
120 c.guest = mock_ssh.SSH()
121 sample_output = '{"min": 100, "avg": 500, "max": 1000}'
123 mock_ssh.SSH().execute.return_value = (0, sample_output, '')
124 self.assertRaises(AssertionError, c.run, result)
126 def test_cyclictest_unsuccessful_sla_avg_latency(self, mock_ssh):
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, '', '')
134 c.guest = mock_ssh.SSH()
135 sample_output = '{"min": 100, "avg": 500, "max": 1000}'
137 mock_ssh.SSH().execute.return_value = (0, sample_output, '')
138 self.assertRaises(AssertionError, c.run, result)
140 def test_cyclictest_unsuccessful_sla_max_latency(self, mock_ssh):
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, '', '')
148 c.guest = mock_ssh.SSH()
149 sample_output = '{"min": 100, "avg": 500, "max": 1000}'
151 mock_ssh.SSH().execute.return_value = (0, sample_output, '')
152 self.assertRaises(AssertionError, c.run, result)
154 def test_cyclictest_unsuccessful_script_error(self, mock_ssh):
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, '', '')
162 c.guest = mock_ssh.SSH()
164 mock_ssh.SSH().execute.return_value = (1, '', 'FOOBAR')
165 self.assertRaises(RuntimeError, c.run, result)
171 if __name__ == '__main__':