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         l = cpuload.CPULoad({}, self.ctx)
37         mock_ssh.SSH().execute.return_value = (0, '', '')
38
39         l.setup()
40         self.assertIsNotNone(l.client)
41         self.assertTrue(l.setup_done)
42         self.assertTrue(l.has_mpstat)
43
44     def test_setup_mpstat_not_installed(self, mock_ssh):
45         l = cpuload.CPULoad({}, self.ctx)
46         mock_ssh.SSH().execute.return_value = (127, '', '')
47
48         l.setup()
49         self.assertIsNotNone(l.client)
50         self.assertTrue(l.setup_done)
51         self.assertFalse(l.has_mpstat)
52
53     def test_execute_command_success(self, mock_ssh):
54         l = cpuload.CPULoad({}, self.ctx)
55         mock_ssh.SSH().execute.return_value = (0, '', '')
56         l.setup()
57
58         expected_result = 'abcdefg'
59         mock_ssh.SSH().execute.return_value = (0, expected_result, '')
60         result = l._execute_command("foo")
61         self.assertEqual(result, expected_result)
62
63     def test_execute_command_failed(self, mock_ssh):
64         l = cpuload.CPULoad({}, self.ctx)
65         mock_ssh.SSH().execute.return_value = (0, '', '')
66         l.setup()
67
68         mock_ssh.SSH().execute.return_value = (127, '', 'abcdefg')
69         self.assertRaises(RuntimeError, l._execute_command,
70                           "cat /proc/loadavg")
71
72     def test_get_loadavg(self, mock_ssh):
73         l = cpuload.CPULoad({}, self.ctx)
74         mock_ssh.SSH().execute.return_value = (0, '', '')
75         l.setup()
76
77         mock_ssh.SSH().execute.return_value = \
78             (0, '1.50 1.45 1.51 3/813 14322', '')
79         result = l._get_loadavg()
80         expected_result = \
81             {'loadavg': ['1.50', '1.45', '1.51', '3/813', '14322']}
82         self.assertEqual(result, expected_result)
83
84     def test_get_cpu_usage_mpstat(self, mock_ssh):
85         l = cpuload.CPULoad({}, self.ctx)
86         mock_ssh.SSH().execute.return_value = (0, '', '')
87         l.setup()
88
89         l.interval = 0
90         mpstat_output = self._read_file("cpuload_sample_output1.txt")
91         mock_ssh.SSH().execute.return_value = (0, mpstat_output, '')
92         result = l._get_cpu_usage_mpstat()
93
94         expected_result = \
95             {'mpstat':
96                 {'cpu':
97                     {'%gnice': '0.00',
98                      '%guest': '5.51',
99                      '%idle': '81.77',
100                      '%iowait': '0.18',
101                      '%irq': '0.00',
102                      '%nice': '0.03',
103                      '%soft': '0.01',
104                      '%steal': '0.00',
105                      '%sys': '1.19',
106                      '%usr': '11.31'},
107                  'cpu0':
108                      {'%gnice': '0.00',
109                       '%guest': '6.62',
110                       '%idle': '71.56',
111                       '%iowait': '0.33',
112                       '%irq': '0.00',
113                       '%nice': '0.03',
114                       '%soft': '0.06',
115                       '%steal': '0.00',
116                       '%sys': '1.36',
117                       '%usr': '20.03'}}}
118
119         self.assertDictEqual(result, expected_result)
120
121     def test_get_cpu_usage(self, mock_ssh):
122         l = cpuload.CPULoad({}, self.ctx)
123         mock_ssh.SSH().execute.return_value = (0, '', '')
124         l.setup()
125
126         l.interval = 0
127         output = self._read_file("cpuload_sample_output2.txt")
128         mock_ssh.SSH().execute.return_value = (0, output, '')
129         result = l._get_cpu_usage()
130
131         expected_result = \
132             {'mpstat':
133                 {'cpu':
134                     {'%steal': '0.00',
135                      '%usr': '11.31',
136                      '%gnice': '0.00',
137                      '%idle': '81.78',
138                      '%iowait': '0.18',
139                      '%guest': '5.50',
140                      '%sys': '1.19',
141                      '%soft': '0.01',
142                      '%irq': '0.00',
143                      '%nice': '0.03'},
144                  'cpu0':
145                     {'%steal': '0.00',
146                      '%usr': '20.00',
147                      '%gnice': '0.00',
148                      '%idle': '71.60',
149                      '%iowait': '0.33',
150                      '%guest': '6.61',
151                      '%sys': '1.37',
152                      '%soft': '0.06',
153                      '%irq': '0.00',
154                      '%nice': '0.03'}}}
155
156         self.assertDictEqual(result, expected_result)
157
158     def test_run_mpstat(self, mock_ssh):
159         l = cpuload.CPULoad({'options': {'interval': 1}}, self.ctx)
160         mock_ssh.SSH().execute.return_value = (0, '', '')
161
162         mpstat_output = self._read_file("cpuload_sample_output1.txt")
163         mock_ssh.SSH().execute.side_effect = \
164             [(0, '', ''), (0, '1.50 1.45 1.51 3/813 14322', ''), (0, mpstat_output, '')]
165
166         l.run(self.result)
167
168         expected_result = {
169             'loadavg': ['1.50', '1.45', '1.51', '3/813', '14322'],
170             'mpstat':
171             {'cpu': {'%gnice': '0.00',
172                      '%guest': '5.51',
173                      '%idle': '81.77',
174                      '%iowait': '0.18',
175                      '%irq': '0.00',
176                      '%nice': '0.03',
177                      '%soft': '0.01',
178                      '%steal': '0.00',
179                      '%sys': '1.19',
180                      '%usr': '11.31'},
181              'cpu0': {'%gnice': '0.00',
182                       '%guest': '6.62',
183                       '%idle': '71.56',
184                       '%iowait': '0.33',
185                       '%irq': '0.00',
186                       '%nice': '0.03',
187                       '%soft': '0.06',
188                       '%steal': '0.00',
189                       '%sys': '1.36',
190                       '%usr': '20.03'}}}
191
192         self.assertDictEqual(self.result, expected_result)
193
194     def test_run_proc_stat(self, mock_ssh):
195         l = cpuload.CPULoad({}, self.ctx)
196         mock_ssh.SSH().execute.return_value = (1, '', '')
197         l.setup()
198
199         l.interval = 0
200         stat_output = self._read_file("cpuload_sample_output2.txt")
201         mock_ssh.SSH().execute.side_effect = \
202             [(0, '1.50 1.45 1.51 3/813 14322', ''), (0, stat_output, '')]
203
204         l.run(self.result)
205         expected_result = {
206             'loadavg': ['1.50', '1.45', '1.51', '3/813', '14322'],
207             'mpstat':
208                 {'cpu':
209                     {'%steal': '0.00',
210                      '%usr': '11.31',
211                      '%gnice': '0.00',
212                      '%idle': '81.78',
213                      '%iowait': '0.18',
214                      '%guest': '5.50',
215                      '%sys': '1.19',
216                      '%soft': '0.01',
217                      '%irq': '0.00',
218                      '%nice': '0.03'},
219                  'cpu0':
220                     {'%steal': '0.00',
221                      '%usr': '20.00',
222                      '%gnice': '0.00',
223                      '%idle': '71.60',
224                      '%iowait': '0.33',
225                      '%guest': '6.61',
226                      '%sys': '1.37',
227                      '%soft': '0.06',
228                      '%irq': '0.00',
229                      '%nice': '0.03'}}}
230
231         self.assertDictEqual(self.result, expected_result)
232
233     def _read_file(self, filename):
234         curr_path = os.path.dirname(os.path.abspath(__file__))
235         output = os.path.join(curr_path, filename)
236         with open(output) as f:
237             sample_output = f.read()
238         return sample_output