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