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
18 from yardstick.benchmark.scenarios.compute import cpuload
21 @mock.patch('yardstick.benchmark.scenarios.compute.cpuload.ssh')
22 class CPULoadTestCase(unittest.TestCase):
29 'key_filename': "mykey.key"
35 def test_setup_mpstat_installed(self, mock_ssh):
36 l = cpuload.CPULoad({}, self.ctx)
37 mock_ssh.SSH().execute.return_value = (0, '', '')
40 self.assertIsNotNone(l.client)
41 self.assertTrue(l.setup_done)
42 self.assertTrue(l.has_mpstat)
44 def test_setup_mpstat_not_installed(self, mock_ssh):
45 l = cpuload.CPULoad({}, self.ctx)
46 mock_ssh.SSH().execute.return_value = (127, '', '')
49 self.assertIsNotNone(l.client)
50 self.assertTrue(l.setup_done)
51 self.assertFalse(l.has_mpstat)
53 def test_execute_command_success(self, mock_ssh):
54 l = cpuload.CPULoad({}, self.ctx)
55 mock_ssh.SSH().execute.return_value = (0, '', '')
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)
63 def test_execute_command_failed(self, mock_ssh):
64 l = cpuload.CPULoad({}, self.ctx)
65 mock_ssh.SSH().execute.return_value = (0, '', '')
68 mock_ssh.SSH().execute.return_value = (127, '', 'abcdefg')
69 self.assertRaises(RuntimeError, l._execute_command,
72 def test_get_loadavg(self, mock_ssh):
73 l = cpuload.CPULoad({}, self.ctx)
74 mock_ssh.SSH().execute.return_value = (0, '', '')
77 mock_ssh.SSH().execute.return_value = \
78 (0, '1.50 1.45 1.51 3/813 14322', '')
79 result = l._get_loadavg()
81 {'loadavg': ['1.50', '1.45', '1.51', '3/813', '14322']}
82 self.assertEqual(result, expected_result)
84 def test_get_cpu_usage_mpstat(self, mock_ssh):
85 l = cpuload.CPULoad({}, self.ctx)
86 mock_ssh.SSH().execute.return_value = (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()
119 self.assertDictEqual(result, expected_result)
121 def test_get_cpu_usage(self, mock_ssh):
122 l = cpuload.CPULoad({}, self.ctx)
123 mock_ssh.SSH().execute.return_value = (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()
156 self.assertDictEqual(result, expected_result)
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, '', '')
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, '')]
169 'loadavg': ['1.50', '1.45', '1.51', '3/813', '14322'],
171 {'cpu': {'%gnice': '0.00',
181 'cpu0': {'%gnice': '0.00',
192 self.assertDictEqual(self.result, expected_result)
194 def test_run_proc_stat(self, mock_ssh):
195 l = cpuload.CPULoad({}, self.ctx)
196 mock_ssh.SSH().execute.return_value = (1, '', '')
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, '')]
206 'loadavg': ['1.50', '1.45', '1.51', '3/813', '14322'],
231 self.assertDictEqual(self.result, expected_result)
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()