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):
41 args = {'options': options}
43 l = cpuload.CPULoad(args, self.ctx)
44 mock_ssh.SSH().execute.return_value = (0, '', '')
47 self.assertIsNotNone(l.client)
48 self.assertTrue(l.setup_done)
49 self.assertTrue(l.has_mpstat)
51 def test_setup_mpstat_not_installed(self, mock_ssh):
57 args = {'options': options}
59 l = cpuload.CPULoad(args, self.ctx)
60 mock_ssh.SSH().execute.return_value = (127, '', '')
63 self.assertIsNotNone(l.client)
64 self.assertTrue(l.setup_done)
65 self.assertFalse(l.has_mpstat)
67 def test_execute_command_success(self, mock_ssh):
73 args = {'options': options}
75 l = cpuload.CPULoad(args, self.ctx)
76 mock_ssh.SSH().execute.return_value = (0, '', '')
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)
84 def test_execute_command_failed(self, mock_ssh):
90 args = {'options': options}
92 l = cpuload.CPULoad(args, self.ctx)
93 mock_ssh.SSH().execute.return_value = (0, '', '')
96 mock_ssh.SSH().execute.return_value = (127, '', 'abcdefg')
97 self.assertRaises(RuntimeError, l._execute_command,
100 def test_get_loadavg(self, mock_ssh):
106 args = {'options': options}
108 l = cpuload.CPULoad(args, self.ctx)
109 mock_ssh.SSH().execute.return_value = (0, '', '')
112 mock_ssh.SSH().execute.return_value = \
113 (0, '1.50 1.45 1.51 3/813 14322', '')
114 result = l._get_loadavg()
116 {'loadavg': ['1.50', '1.45', '1.51', '3/813', '14322']}
117 self.assertEqual(result, expected_result)
119 def test_get_cpu_usage_mpstat(self, mock_ssh):
125 args = {'options': options}
127 l = cpuload.CPULoad(args, self.ctx)
128 mock_ssh.SSH().execute.return_value = (0, '', '')
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()
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"}},
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"}},
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"}}}
166 self.assertDictEqual(result, expected_result)
168 def test_get_cpu_usage(self, mock_ssh):
174 args = {'options': options}
176 l = cpuload.CPULoad(args, self.ctx)
177 mock_ssh.SSH().execute.return_value = (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()
210 self.assertDictEqual(result, expected_result)
212 def test_run_proc_stat(self, mock_ssh):
218 args = {'options': options}
220 l = cpuload.CPULoad(args, self.ctx)
221 mock_ssh.SSH().execute.return_value = (1, '', '')
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, '')]
231 'loadavg': ['1.50', '1.45', '1.51', '3/813', '14322'],
256 self.assertDictEqual(self.result, expected_result)
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()