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