Merge "Add test case description and task file for TC025"
[yardstick.git] / 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 import mock
15 import unittest
16 import json
17
18 from yardstick.benchmark.scenarios.compute import unixbench
19
20
21 @mock.patch('yardstick.benchmark.scenarios.compute.unixbench.ssh')
22 class UnixbenchTestCase(unittest.TestCase):
23
24     def setUp(self):
25         self.ctx = {
26             "host": {
27                 "ip": "192.168.50.28",
28                 "user": "root",
29                 "key_filename": "mykey.key"
30             }
31         }
32
33     def test_unixbench_successful_setup(self, mock_ssh):
34
35         u = unixbench.Unixbench({}, self.ctx)
36         u.setup()
37
38         mock_ssh.SSH().execute.return_value = (0, '', '')
39         self.assertIsNotNone(u.client)
40         self.assertEqual(u.setup_done, True)
41
42     def test_unixbench_successful_no_sla(self, mock_ssh):
43
44         options = {
45             "test_type": 'dhry2reg',
46             "run_mode": 'verbose'
47         }
48         args = {
49             "options": options,
50         }
51         u = unixbench.Unixbench(args, self.ctx)
52         result = {}
53
54         u.server = mock_ssh.SSH()
55
56         sample_output = '{"Score":"4425.4"}'
57         mock_ssh.SSH().execute.return_value = (0, sample_output, '')
58
59         u.run(result)
60         expected_result = json.loads(sample_output)
61         self.assertEqual(result, expected_result)
62
63     def test_unixbench_successful_in_quiet_mode(self, mock_ssh):
64
65         options = {
66             "test_type": 'dhry2reg',
67             "run_mode": 'quiet',
68             "copies":1
69         }
70         args = {
71             "options": options,
72         }
73         u = unixbench.Unixbench(args, self.ctx)
74         result = {}
75
76         u.server = mock_ssh.SSH()
77
78         sample_output = '{"Score":"4425.4"}'
79         mock_ssh.SSH().execute.return_value = (0, sample_output, '')
80
81         u.run(result)
82         expected_result = json.loads(sample_output)
83         self.assertEqual(result, expected_result)
84
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()
104
105         sample_output = '{"signle_score":"2251.7","parallel_score":"4395.9"}'
106         mock_ssh.SSH().execute.return_value = (0, sample_output, '')
107
108         u.run(result)
109         expected_result = json.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()
122         sample_output = '{"single_score":"200.7","parallel_score":"4395.9"}'
123
124         mock_ssh.SSH().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()
137         sample_output = '{"signle_score":"2251.7","parallel_score":"3395.9"}'
138
139         mock_ssh.SSH().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()
160
161         mock_ssh.SSH().execute.return_value = (1, '', 'FOOBAR')
162         self.assertRaises(RuntimeError, u.run, result)
163
164
165 def main():
166     unittest.main()
167
168 if __name__ == '__main__':
169     main()