1 # Copyright (c) 2016-2017 Intel Corporation
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
16 from __future__ import division
21 from yardstick.network_services.helpers.cpu import \
25 class TestCpuSysCores(unittest.TestCase):
28 self._mock_ssh = mock.patch("yardstick.ssh.SSH")
29 self.mock_ssh = self._mock_ssh.start()
31 self.addCleanup(self._cleanup)
36 def test___init__(self):
37 self.mock_ssh.execute.return_value = 1, "", ""
38 self.mock_ssh.put.return_value = 1, "", ""
39 cpu_topo = CpuSysCores(self.mock_ssh)
40 self.assertIsNotNone(cpu_topo.connection)
42 def test__get_core_details(self):
43 with mock.patch("yardstick.ssh.SSH") as ssh:
44 ssh_mock = mock.Mock(autospec=ssh.SSH)
46 mock.Mock(return_value=(1, "", ""))
48 mock.Mock(return_value=(1, "", ""))
49 cpu_topo = CpuSysCores(ssh_mock)
50 subprocess.check_output = mock.Mock(return_value=0)
51 lines = ["cpu:1", "topo:2", ""]
52 self.assertEqual([{'topo': '2', 'cpu': '1'}],
53 cpu_topo._get_core_details(lines))
55 def test_get_core_socket(self):
56 with mock.patch("yardstick.ssh.SSH") as ssh:
57 ssh_mock = mock.Mock(autospec=ssh.SSH)
59 mock.Mock(return_value=(1, "cpu:1\ntest:2\n \n", ""))
61 mock.Mock(return_value=(1, "", ""))
62 cpu_topo = CpuSysCores(ssh_mock)
63 subprocess.check_output = mock.Mock(return_value=0)
64 cpu_topo._get_core_details = \
65 mock.Mock(side_effect=[[{'Core(s) per socket': '2', 'Thread(s) per core': '1'}],
66 [{'physical id': '2', 'processor': '1'}]])
67 self.assertEqual({'thread_per_core': '1', '2': ['1'],
68 'cores_per_socket': '2'},
69 cpu_topo.get_core_socket())
71 def test_validate_cpu_cfg(self):
72 with mock.patch("yardstick.ssh.SSH") as ssh:
73 ssh_mock = mock.Mock(autospec=ssh.SSH)
75 mock.Mock(return_value=(1, "cpu:1\ntest:2\n \n", ""))
77 mock.Mock(return_value=(1, "", ""))
78 cpu_topo = CpuSysCores(ssh_mock)
79 subprocess.check_output = mock.Mock(return_value=0)
80 cpu_topo._get_core_details = \
81 mock.Mock(side_effect=[[{'Core(s) per socket': '2', 'Thread(s) per core': '1'}],
82 [{'physical id': '2', 'processor': '1'}]])
84 {'thread_per_core': '1', '2': ['1'], 'cores_per_socket': '2'}
85 self.assertEqual(-1, cpu_topo.validate_cpu_cfg())
87 def test_validate_cpu_cfg_2t(self):
88 with mock.patch("yardstick.ssh.SSH") as ssh:
89 ssh_mock = mock.Mock(autospec=ssh.SSH)
91 mock.Mock(return_value=(1, "cpu:1\ntest:2\n \n", ""))
93 mock.Mock(return_value=(1, "", ""))
94 cpu_topo = CpuSysCores(ssh_mock)
95 subprocess.check_output = mock.Mock(return_value=0)
96 cpu_topo._get_core_details = \
97 mock.Mock(side_effect=[[{'Core(s) per socket': '2', 'Thread(s) per core': '1'}],
98 [{'physical id': '2', 'processor': '1'}]])
100 {'thread_per_core': 1, '2': ['1'], 'cores_per_socket': '2'}
101 vnf_cfg = {'lb_config': 'SW', 'lb_count': 1, 'worker_config':
102 '1C/2T', 'worker_threads': 1}
103 self.assertEqual(-1, cpu_topo.validate_cpu_cfg(vnf_cfg))
105 def test_validate_cpu_cfg_fail(self):
106 with mock.patch("yardstick.ssh.SSH") as ssh:
107 ssh_mock = mock.Mock(autospec=ssh.SSH)
109 mock.Mock(return_value=(1, "cpu:1\ntest:2\n \n", ""))
111 mock.Mock(return_value=(1, "", ""))
112 cpu_topo = CpuSysCores(ssh_mock)
113 subprocess.check_output = mock.Mock(return_value=0)
114 cpu_topo._get_core_details = \
115 mock.Mock(side_effect=[[{'Core(s) per socket': '2', 'Thread(s) per core': '1'}],
116 [{'physical id': '2', 'processor': '1'}]])
117 cpu_topo.core_map = \
118 {'thread_per_core': 1, '2': [1], 'cores_per_socket': 2}
119 vnf_cfg = {'lb_config': 'SW', 'lb_count': 1, 'worker_config':
120 '1C/1T', 'worker_threads': 1}
121 self.assertEqual(-1, cpu_topo.validate_cpu_cfg(vnf_cfg))
123 def test_get_cpu_layout(self):
124 with mock.patch("yardstick.ssh.SSH") as ssh:
125 ssh_mock = mock.Mock(autospec=ssh.SSH)
128 return_value=(1, "# CPU,Core,Socket,Node,,L1d,L1i,L2,L3\n'"
130 "1,1,0,0,,1,1,1,0\n", ""))
132 mock.Mock(return_value=(1, "", ""))
133 cpu_topo = CpuSysCores(ssh_mock)
134 subprocess.check_output = mock.Mock(return_value=0)
135 self.assertEqual({'cpuinfo': [[0, 0, 0, 0, 0, 0, 0, 0, 0],
136 [1, 1, 0, 0, 0, 1, 1, 1, 0]]},
137 cpu_topo.get_cpu_layout())
139 def test__str2int(self):
140 self.assertEqual(1, CpuSysCores._str2int("1"))
142 def test__str2int_error(self):
143 self.assertEqual(0, CpuSysCores._str2int("err"))
145 def test_smt_enabled(self):
146 self.assertEqual(False, CpuSysCores.smt_enabled(
147 {'cpuinfo': [[0, 0, 0, 0, 0, 0, 0, 0, 0],
148 [1, 1, 0, 0, 0, 1, 1, 1, 0]]}))
150 def test_is_smt_enabled(self):
151 with mock.patch("yardstick.ssh.SSH") as ssh:
152 ssh_mock = mock.Mock(autospec=ssh.SSH)
153 cpu_topo = CpuSysCores(ssh_mock)
154 cpu_topo.cpuinfo = {'cpuinfo': [[0, 0, 0, 0, 0, 0, 0, 0, 0],
155 [1, 1, 0, 0, 0, 1, 1, 1, 0]]}
156 self.assertEqual(False, cpu_topo.is_smt_enabled())
158 def test_cpu_list_per_node(self):
159 with mock.patch("yardstick.ssh.SSH") as ssh:
160 ssh_mock = mock.Mock(autospec=ssh.SSH)
161 cpu_topo = CpuSysCores(ssh_mock)
162 cpu_topo.cpuinfo = {'cpuinfo': [[0, 0, 0, 0, 0, 0, 0, 0, 0],
163 [1, 1, 0, 0, 0, 1, 1, 1, 0]]}
164 self.assertEqual([0, 1], cpu_topo.cpu_list_per_node(0, False))
166 def test_cpu_list_per_node_error(self):
167 with mock.patch("yardstick.ssh.SSH") as ssh:
168 ssh_mock = mock.Mock(autospec=ssh.SSH)
169 cpu_topo = CpuSysCores(ssh_mock)
170 cpu_topo.cpuinfo = {'err': [[0, 0, 0, 0, 0, 0, 0, 0, 0],
171 [1, 1, 0, 0, 0, 1, 1, 1, 0]]}
172 with self.assertRaises(RuntimeError) as raised:
173 cpu_topo.cpu_list_per_node(0, False)
174 self.assertIn('Node cpuinfo not available.', str(raised.exception))
176 def test_cpu_list_per_node_smt_error(self):
177 with mock.patch("yardstick.ssh.SSH") as ssh:
178 ssh_mock = mock.Mock(autospec=ssh.SSH)
179 cpu_topo = CpuSysCores(ssh_mock)
180 cpu_topo.cpuinfo = {'cpuinfo': [[0, 0, 0, 0, 0, 0, 0, 0, 0],
181 [1, 1, 0, 0, 0, 1, 1, 1, 0]]}
182 with self.assertRaises(RuntimeError) as raised:
183 cpu_topo.cpu_list_per_node(0, True)
184 self.assertIn('SMT is not enabled.', str(raised.exception))
186 def test_cpu_slice_of_list_per_node(self):
187 with mock.patch("yardstick.ssh.SSH") as ssh:
188 ssh_mock = mock.Mock(autospec=ssh.SSH)
189 cpu_topo = CpuSysCores(ssh_mock)
190 cpu_topo.cpuinfo = {'cpuinfo': [[0, 0, 0, 0, 0, 0, 0, 0, 0],
191 [1, 1, 0, 0, 0, 1, 1, 1, 0]]}
192 self.assertEqual([1],
193 cpu_topo.cpu_slice_of_list_per_node(0, 1, 0,
196 def test_cpu_slice_of_list_per_node_error(self):
197 with mock.patch("yardstick.ssh.SSH") as ssh:
198 ssh_mock = mock.Mock(autospec=ssh.SSH)
199 cpu_topo = CpuSysCores(ssh_mock)
200 cpu_topo.cpuinfo = {'cpuinfo': [[0, 0, 0, 0, 0, 0, 0, 0, 0],
201 [1, 1, 0, 0, 0, 1, 1, 1, 0]]}
202 with self.assertRaises(RuntimeError) as raised:
203 cpu_topo.cpu_slice_of_list_per_node(1, 1, 1, False)
204 self.assertIn('cpu_cnt + skip_cnt > length(cpu list).',
205 str(raised.exception))
207 def test_cpu_list_per_node_str(self):
208 with mock.patch("yardstick.ssh.SSH") as ssh:
209 ssh_mock = mock.Mock(autospec=ssh.SSH)
210 cpu_topo = CpuSysCores(ssh_mock)
211 cpu_topo.cpuinfo = {'cpuinfo': [[0, 0, 0, 0, 0, 0, 0, 0, 0],
212 [1, 1, 0, 0, 0, 1, 1, 1, 0]]}
213 self.assertEqual("1",
214 cpu_topo.cpu_list_per_node_str(0, 1, 1, ',',