fec355b45fe15339bc3de893793ff9007cd17305
[yardstick.git] / yardstick / tests / unit / benchmark / scenarios / compute / test_unixbench.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.unixbench.Unixbench
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 unixbench
22
23
24 @mock.patch('yardstick.benchmark.scenarios.compute.unixbench.ssh')
25 class UnixbenchTestCase(unittest.TestCase):
26
27     def setUp(self):
28         self.ctx = {
29             "host": {
30                 "ip": "192.168.50.28",
31                 "user": "root",
32                 "key_filename": "mykey.key"
33             }
34         }
35
36     def test_unixbench_successful_setup(self, mock_ssh):
37
38         u = unixbench.Unixbench({}, self.ctx)
39         u.setup()
40
41         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
42         self.assertIsNotNone(u.client)
43         self.assertTrue(u.setup_done)
44
45     def test_unixbench_successful_no_sla(self, mock_ssh):
46
47         options = {
48             "test_type": 'dhry2reg',
49             "run_mode": 'verbose'
50         }
51         args = {
52             "options": options,
53         }
54         u = unixbench.Unixbench(args, self.ctx)
55         result = {}
56
57         u.server = mock_ssh.SSH.from_node()
58
59         sample_output = '{"Score":"4425.4"}'
60         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
61
62         u.run(result)
63         expected_result = jsonutils.loads(sample_output)
64         self.assertEqual(result, expected_result)
65
66     def test_unixbench_successful_in_quiet_mode(self, mock_ssh):
67
68         options = {
69             "test_type": 'dhry2reg',
70             "run_mode": 'quiet',
71             "copies": 1
72         }
73         args = {
74             "options": options,
75         }
76         u = unixbench.Unixbench(args, self.ctx)
77         result = {}
78
79         u.server = mock_ssh.SSH.from_node()
80
81         sample_output = '{"Score":"4425.4"}'
82         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
83
84         u.run(result)
85         expected_result = jsonutils.loads(sample_output)
86         self.assertEqual(result, expected_result)
87
88     def test_unixbench_successful_sla(self, mock_ssh):
89
90         options = {
91             "test_type": 'dhry2reg',
92             "run_mode": 'verbose'
93         }
94         sla = {
95             "single_score": '100',
96             "parallel_score": '500'
97         }
98         args = {
99             "options": options,
100             "sla": sla
101         }
102         u = unixbench.Unixbench(args, self.ctx)
103         result = {}
104
105         u.server = mock_ssh.SSH.from_node()
106
107         sample_output = '{"signle_score":"2251.7","parallel_score":"4395.9"}'
108         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
109
110         u.run(result)
111         expected_result = jsonutils.loads(sample_output)
112         self.assertEqual(result, expected_result)
113
114     def test_unixbench_unsuccessful_sla_single_score(self, mock_ssh):
115
116         args = {
117             "options": {},
118             "sla": {"single_score": "500"}
119         }
120         u = unixbench.Unixbench(args, self.ctx)
121         result = {}
122
123         u.server = mock_ssh.SSH.from_node()
124         sample_output = '{"single_score":"200.7","parallel_score":"4395.9"}'
125
126         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
127         self.assertRaises(AssertionError, u.run, result)
128
129     def test_unixbench_unsuccessful_sla_parallel_score(self, mock_ssh):
130
131         args = {
132             "options": {},
133             "sla": {"parallel_score": "4000"}
134         }
135         u = unixbench.Unixbench(args, self.ctx)
136         result = {}
137
138         u.server = mock_ssh.SSH.from_node()
139         sample_output = '{"signle_score":"2251.7","parallel_score":"3395.9"}'
140
141         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
142         self.assertRaises(AssertionError, u.run, result)
143
144     def test_unixbench_unsuccessful_script_error(self, mock_ssh):
145
146         options = {
147             "test_type": 'dhry2reg',
148             "run_mode": 'verbose'
149         }
150         sla = {
151             "single_score": '100',
152             "parallel_score": '500'
153         }
154         args = {
155             "options": options,
156             "sla": sla
157         }
158         u = unixbench.Unixbench(args, self.ctx)
159         result = {}
160
161         u.server = mock_ssh.SSH.from_node()
162
163         mock_ssh.SSH.from_node().execute.return_value = (1, '', 'FOOBAR')
164         self.assertRaises(RuntimeError, u.run, result)
165
166
167 def main():
168     unittest.main()
169
170 if __name__ == '__main__':
171     main()