3 ##############################################################################
4 # Copyright (c) 2015 Ericsson AB and others.
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 ##############################################################################
12 # Unittest for yardstick.benchmark.scenarios.compute.lmbench.Lmbench
14 from __future__ import absolute_import
19 from yardstick.benchmark.scenarios.compute import cpuload
22 @mock.patch('yardstick.benchmark.scenarios.compute.cpuload.ssh')
23 class CPULoadTestCase(unittest.TestCase):
30 'key_filename': "mykey.key"
36 def test_setup_mpstat_installed(self, mock_ssh):
42 args = {'options': options}
44 l = cpuload.CPULoad(args, self.ctx)
45 mock_ssh.SSH().execute.return_value = (0, '', '')
48 self.assertIsNotNone(l.client)
49 self.assertTrue(l.setup_done)
50 self.assertTrue(l.has_mpstat)
52 def test_setup_mpstat_not_installed(self, mock_ssh):
58 args = {'options': options}
60 l = cpuload.CPULoad(args, self.ctx)
61 mock_ssh.SSH().execute.return_value = (127, '', '')
64 self.assertIsNotNone(l.client)
65 self.assertTrue(l.setup_done)
66 self.assertFalse(l.has_mpstat)
68 def test_execute_command_success(self, mock_ssh):
74 args = {'options': options}
76 l = cpuload.CPULoad(args, self.ctx)
77 mock_ssh.SSH().execute.return_value = (0, '', '')
80 expected_result = 'abcdefg'
81 mock_ssh.SSH().execute.return_value = (0, expected_result, '')
82 result = l._execute_command("foo")
83 self.assertEqual(result, expected_result)
85 def test_execute_command_failed(self, mock_ssh):
91 args = {'options': options}
93 l = cpuload.CPULoad(args, self.ctx)
94 mock_ssh.SSH().execute.return_value = (0, '', '')
97 mock_ssh.SSH().execute.return_value = (127, '', 'abcdefg')
98 self.assertRaises(RuntimeError, l._execute_command,
101 def test_get_loadavg(self, mock_ssh):
107 args = {'options': options}
109 l = cpuload.CPULoad(args, self.ctx)
110 mock_ssh.SSH().execute.return_value = (0, '', '')
113 mock_ssh.SSH().execute.return_value = \
114 (0, '1.50 1.45 1.51 3/813 14322', '')
115 result = l._get_loadavg()
117 {'loadavg': ['1.50', '1.45', '1.51', '3/813', '14322']}
118 self.assertEqual(result, expected_result)
120 def test_get_cpu_usage_mpstat(self, mock_ssh):
126 args = {'options': options}
128 l = cpuload.CPULoad(args, self.ctx)
129 mock_ssh.SSH().execute.return_value = (0, '', '')
134 mpstat_output = self._read_file("cpuload_sample_output1.txt")
135 mock_ssh.SSH().execute.return_value = (0, mpstat_output, '')
136 result = l._get_cpu_usage_mpstat()
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"}},
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"}},
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"}}}
167 self.assertDictEqual(result, expected_result)
169 def test_get_cpu_usage(self, mock_ssh):
175 args = {'options': options}
177 l = cpuload.CPULoad(args, self.ctx)
178 mock_ssh.SSH().execute.return_value = (0, '', '')
182 output = self._read_file("cpuload_sample_output2.txt")
183 mock_ssh.SSH().execute.return_value = (0, output, '')
184 result = l._get_cpu_usage()
211 self.assertDictEqual(result, expected_result)
213 def test_run_proc_stat(self, mock_ssh):
219 args = {'options': options}
221 l = cpuload.CPULoad(args, self.ctx)
222 mock_ssh.SSH().execute.return_value = (1, '', '')
226 stat_output = self._read_file("cpuload_sample_output2.txt")
227 mock_ssh.SSH().execute.side_effect = \
228 [(0, '1.50 1.45 1.51 3/813 14322', ''), (0, stat_output, '')]
232 'loadavg': ['1.50', '1.45', '1.51', '3/813', '14322'],
257 self.assertDictEqual(self.result, expected_result)
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()