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