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