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