Merge "testcase start service script fail on centos env"
[yardstick.git] / yardstick / tests / unit / benchmark / scenarios / compute / test_cpuload.py
1 ##############################################################################
2 # Copyright (c) 2015 Ericsson AB and others.
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.lmbench.Lmbench
11
12 from __future__ import absolute_import
13 import mock
14 import unittest
15 import os
16
17 from yardstick.benchmark.scenarios.compute import cpuload
18
19
20 @mock.patch('yardstick.benchmark.scenarios.compute.cpuload.ssh')
21 class CPULoadTestCase(unittest.TestCase):
22
23     def setUp(self):
24         self.ctx = {
25             'host': {
26                 'ip': '172.16.0.137',
27                 'user': 'cirros',
28                 'key_filename': "mykey.key"
29             }
30         }
31
32         self.result = {}
33
34     def test_setup_mpstat_installed(self, mock_ssh):
35         options = {
36             "interval": 1,
37             "count": 1
38         }
39
40         args = {'options': options}
41
42         l = cpuload.CPULoad(args, self.ctx)
43         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
44
45         l.setup()
46         self.assertIsNotNone(l.client)
47         self.assertTrue(l.setup_done)
48         self.assertTrue(l.has_mpstat)
49
50     def test_setup_mpstat_not_installed(self, mock_ssh):
51         options = {
52             "interval": 1,
53             "count": 1
54         }
55
56         args = {'options': options}
57
58         l = cpuload.CPULoad(args, self.ctx)
59         mock_ssh.SSH.from_node().execute.return_value = (127, '', '')
60
61         l.setup()
62         self.assertIsNotNone(l.client)
63         self.assertTrue(l.setup_done)
64         self.assertFalse(l.has_mpstat)
65
66     def test_execute_command_success(self, mock_ssh):
67         options = {
68             "interval": 1,
69             "count": 1
70         }
71
72         args = {'options': options}
73
74         l = cpuload.CPULoad(args, self.ctx)
75         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
76         l.setup()
77
78         expected_result = 'abcdefg'
79         mock_ssh.SSH.from_node().execute.return_value = (0, expected_result, '')
80         result = l._execute_command("foo")
81         self.assertEqual(result, expected_result)
82
83     def test_execute_command_failed(self, mock_ssh):
84         options = {
85             "interval": 1,
86             "count": 1
87         }
88
89         args = {'options': options}
90
91         l = cpuload.CPULoad(args, self.ctx)
92         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
93         l.setup()
94
95         mock_ssh.SSH.from_node().execute.return_value = (127, '', 'abcdefg')
96         self.assertRaises(RuntimeError, l._execute_command,
97                           "cat /proc/loadavg")
98
99     def test_get_loadavg(self, mock_ssh):
100         options = {
101             "interval": 1,
102             "count": 1
103         }
104
105         args = {'options': options}
106
107         l = cpuload.CPULoad(args, self.ctx)
108         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
109         l.setup()
110
111         mock_ssh.SSH.from_node().execute.return_value = \
112             (0, '1.50 1.45 1.51 3/813 14322', '')
113         result = l._get_loadavg()
114         expected_result = \
115             {'loadavg': ['1.50', '1.45', '1.51', '3/813', '14322']}
116         self.assertEqual(result, expected_result)
117
118     def test_get_cpu_usage_mpstat(self, mock_ssh):
119         options = {
120             "interval": 1,
121             "count": 1
122         }
123
124         args = {'options': options}
125
126         l = cpuload.CPULoad(args, self.ctx)
127         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
128         l.setup()
129
130         l.interval = 1
131         l.count = 1
132         mpstat_output = self._read_file("cpuload_sample_output1.txt")
133         mock_ssh.SSH.from_node().execute.return_value = (0, mpstat_output, '')
134         result = l._get_cpu_usage_mpstat()
135
136         expected_result = \
137             {"mpstat_minimum":
138                 {"cpu": {"%steal": "0.00", "%usr": "0.00", "%gnice": "0.00",
139                          "%idle": "100.00", "%guest": "0.00",
140                          "%iowait": "0.00", "%sys": "0.00", "%soft": "0.00",
141                          "%irq": "0.00", "%nice": "0.00"},
142                  "cpu0": {"%steal": "0.00", "%usr": "0.00", "%gnice": "0.00",
143                           "%idle": "100.00", "%guest": "0.00",
144                           "%iowait": "0.00", "%sys": "0.00", "%soft": "0.00",
145                           "%irq": "0.00", "%nice": "0.00"}},
146              "mpstat_average":
147                 {"cpu": {"%steal": "0.00", "%usr": "0.00", "%gnice": "0.00",
148                          "%idle": "100.00", "%guest": "0.00",
149                          "%iowait": "0.00", "%sys": "0.00", "%soft": "0.00",
150                          "%irq": "0.00", "%nice": "0.00"},
151                  "cpu0": {"%steal": "0.00", "%usr": "0.00", "%gnice": "0.00",
152                           "%idle": "100.00", "%guest": "0.00",
153                           "%iowait": "0.00", "%sys": "0.00", "%soft": "0.00",
154                           "%irq": "0.00", "%nice": "0.00"}},
155              "mpstat_maximun":
156                 {"cpu": {"%steal": "0.00", "%usr": "0.00", "%gnice": "0.00",
157                          "%idle": "100.00", "%guest": "0.00",
158                          "%iowait": "0.00", "%sys": "0.00", "%soft": "0.00",
159                          "%irq": "0.00", "%nice": "0.00"},
160                  "cpu0": {"%steal": "0.00", "%usr": "0.00", "%gnice": "0.00",
161                           "%idle": "100.00", "%guest": "0.00",
162                           "%iowait": "0.00", "%sys": "0.00", "%soft": "0.00",
163                           "%irq": "0.00", "%nice": "0.00"}}}
164
165         self.assertDictEqual(result, expected_result)
166
167     def test_get_cpu_usage(self, mock_ssh):
168         options = {
169             "interval": 0,
170             "count": 1
171         }
172
173         args = {'options': options}
174
175         l = cpuload.CPULoad(args, self.ctx)
176         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
177         l.setup()
178
179         l.interval = 0
180         output = self._read_file("cpuload_sample_output2.txt")
181         mock_ssh.SSH.from_node().execute.return_value = (0, output, '')
182         result = l._get_cpu_usage()
183
184         expected_result = \
185             {'mpstat':
186                 {'cpu':
187                     {'%steal': '0.00',
188                      '%usr': '11.31',
189                      '%gnice': '0.00',
190                      '%idle': '81.78',
191                      '%iowait': '0.18',
192                      '%guest': '5.50',
193                      '%sys': '1.19',
194                      '%soft': '0.01',
195                      '%irq': '0.00',
196                      '%nice': '0.03'},
197                  'cpu0':
198                     {'%steal': '0.00',
199                      '%usr': '20.00',
200                      '%gnice': '0.00',
201                      '%idle': '71.60',
202                      '%iowait': '0.33',
203                      '%guest': '6.61',
204                      '%sys': '1.37',
205                      '%soft': '0.06',
206                      '%irq': '0.00',
207                      '%nice': '0.03'}}}
208
209         self.assertDictEqual(result, expected_result)
210
211     def test_run_proc_stat(self, mock_ssh):
212         options = {
213             "interval": 1,
214             "count": 1
215         }
216
217         args = {'options': options}
218
219         l = cpuload.CPULoad(args, self.ctx)
220         mock_ssh.SSH.from_node().execute.return_value = (1, '', '')
221         l.setup()
222
223         l.interval = 0
224         stat_output = self._read_file("cpuload_sample_output2.txt")
225         mock_ssh.SSH.from_node().execute.side_effect = \
226             [(0, '1.50 1.45 1.51 3/813 14322', ''), (0, stat_output, '')]
227
228         l.run(self.result)
229         expected_result = {
230             'loadavg': ['1.50', '1.45', '1.51', '3/813', '14322'],
231             'mpstat':
232                 {'cpu':
233                     {'%steal': '0.00',
234                      '%usr': '11.31',
235                      '%gnice': '0.00',
236                      '%idle': '81.78',
237                      '%iowait': '0.18',
238                      '%guest': '5.50',
239                      '%sys': '1.19',
240                      '%soft': '0.01',
241                      '%irq': '0.00',
242                      '%nice': '0.03'},
243                  'cpu0':
244                     {'%steal': '0.00',
245                      '%usr': '20.00',
246                      '%gnice': '0.00',
247                      '%idle': '71.60',
248                      '%iowait': '0.33',
249                      '%guest': '6.61',
250                      '%sys': '1.37',
251                      '%soft': '0.06',
252                      '%irq': '0.00',
253                      '%nice': '0.03'}}}
254
255         self.assertDictEqual(self.result, expected_result)
256
257     def _read_file(self, filename):
258         curr_path = os.path.dirname(os.path.abspath(__file__))
259         output = os.path.join(curr_path, filename)
260         with open(output) as f:
261             sample_output = f.read()
262         return sample_output