1 ##############################################################################
2 # Copyright (c) 2015 Ericsson AB and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
10 # Unittest for yardstick.benchmark.scenarios.compute.lmbench.Lmbench
12 from __future__ import absolute_import
17 from yardstick.benchmark.scenarios.compute import cpuload
20 @mock.patch('yardstick.benchmark.scenarios.compute.cpuload.ssh')
21 class CPULoadTestCase(unittest.TestCase):
28 'key_filename': "mykey.key"
34 def test_setup_mpstat_installed(self, mock_ssh):
40 args = {'options': options}
42 l = cpuload.CPULoad(args, self.ctx)
43 mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
46 self.assertIsNotNone(l.client)
47 self.assertTrue(l.setup_done)
48 self.assertTrue(l.has_mpstat)
50 def test_setup_mpstat_not_installed(self, mock_ssh):
56 args = {'options': options}
58 l = cpuload.CPULoad(args, self.ctx)
59 mock_ssh.SSH.from_node().execute.return_value = (127, '', '')
62 self.assertIsNotNone(l.client)
63 self.assertTrue(l.setup_done)
64 self.assertFalse(l.has_mpstat)
66 def test_execute_command_success(self, mock_ssh):
72 args = {'options': options}
74 l = cpuload.CPULoad(args, self.ctx)
75 mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
78 expected_result = 'abcdefg'
79 mock_ssh.SSH.from_node().execute.return_value = (0, expected_result, '')
80 result = l._execute_command("foo")
81 self.assertEqual(result, expected_result)
83 def test_execute_command_failed(self, mock_ssh):
89 args = {'options': options}
91 l = cpuload.CPULoad(args, self.ctx)
92 mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
95 mock_ssh.SSH.from_node().execute.return_value = (127, '', 'abcdefg')
96 self.assertRaises(RuntimeError, l._execute_command,
99 def test_get_loadavg(self, mock_ssh):
105 args = {'options': options}
107 l = cpuload.CPULoad(args, self.ctx)
108 mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
111 mock_ssh.SSH.from_node().execute.return_value = \
112 (0, '1.50 1.45 1.51 3/813 14322', '')
113 result = l._get_loadavg()
115 {'loadavg': ['1.50', '1.45', '1.51', '3/813', '14322']}
116 self.assertEqual(result, expected_result)
118 def test_get_cpu_usage_mpstat(self, mock_ssh):
124 args = {'options': options}
126 l = cpuload.CPULoad(args, self.ctx)
127 mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
132 mpstat_output = self._read_file("cpuload_sample_output1.txt")
133 mock_ssh.SSH.from_node().execute.return_value = (0, mpstat_output, '')
134 result = l._get_cpu_usage_mpstat()
138 {"cpu": {"%steal": "0.00", "%usr": "0.00", "%gnice": "0.00",
139 "%idle": "100.00", "%guest": "0.00",
140 "%iowait": "0.00", "%sys": "0.00", "%soft": "0.00",
141 "%irq": "0.00", "%nice": "0.00"},
142 "cpu0": {"%steal": "0.00", "%usr": "0.00", "%gnice": "0.00",
143 "%idle": "100.00", "%guest": "0.00",
144 "%iowait": "0.00", "%sys": "0.00", "%soft": "0.00",
145 "%irq": "0.00", "%nice": "0.00"}},
147 {"cpu": {"%steal": "0.00", "%usr": "0.00", "%gnice": "0.00",
148 "%idle": "100.00", "%guest": "0.00",
149 "%iowait": "0.00", "%sys": "0.00", "%soft": "0.00",
150 "%irq": "0.00", "%nice": "0.00"},
151 "cpu0": {"%steal": "0.00", "%usr": "0.00", "%gnice": "0.00",
152 "%idle": "100.00", "%guest": "0.00",
153 "%iowait": "0.00", "%sys": "0.00", "%soft": "0.00",
154 "%irq": "0.00", "%nice": "0.00"}},
156 {"cpu": {"%steal": "0.00", "%usr": "0.00", "%gnice": "0.00",
157 "%idle": "100.00", "%guest": "0.00",
158 "%iowait": "0.00", "%sys": "0.00", "%soft": "0.00",
159 "%irq": "0.00", "%nice": "0.00"},
160 "cpu0": {"%steal": "0.00", "%usr": "0.00", "%gnice": "0.00",
161 "%idle": "100.00", "%guest": "0.00",
162 "%iowait": "0.00", "%sys": "0.00", "%soft": "0.00",
163 "%irq": "0.00", "%nice": "0.00"}}}
165 self.assertDictEqual(result, expected_result)
167 def test_get_cpu_usage(self, mock_ssh):
173 args = {'options': options}
175 l = cpuload.CPULoad(args, self.ctx)
176 mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
180 output = self._read_file("cpuload_sample_output2.txt")
181 mock_ssh.SSH.from_node().execute.return_value = (0, output, '')
182 result = l._get_cpu_usage()
209 self.assertDictEqual(result, expected_result)
211 def test_run_proc_stat(self, mock_ssh):
217 args = {'options': options}
219 l = cpuload.CPULoad(args, self.ctx)
220 mock_ssh.SSH.from_node().execute.return_value = (1, '', '')
224 stat_output = self._read_file("cpuload_sample_output2.txt")
225 mock_ssh.SSH.from_node().execute.side_effect = \
226 [(0, '1.50 1.45 1.51 3/813 14322', ''), (0, stat_output, '')]
230 'loadavg': ['1.50', '1.45', '1.51', '3/813', '14322'],
255 self.assertDictEqual(self.result, expected_result)
257 def _read_file(self, filename):
258 curr_path = os.path.dirname(os.path.abspath(__file__))
259 output = os.path.join(curr_path, filename)
260 with open(output) as f:
261 sample_output = f.read()