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