3 # Copyright (c) 2016-2017 Intel Corporation
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
18 from __future__ import absolute_import
19 from __future__ import division
24 from yardstick.network_services.helpers.cpu import \
28 class TestCpuSysCores(unittest.TestCase):
30 def test___init__(self):
31 with mock.patch("yardstick.ssh.SSH") as ssh:
32 ssh_mock = mock.Mock(autospec=ssh.SSH)
34 mock.Mock(return_value=(1, "", ""))
36 mock.Mock(return_value=(1, "", ""))
37 cpu_topo = CpuSysCores(ssh_mock)
38 self.assertIsNotNone(cpu_topo.connection)
40 def test__get_core_details(self):
41 with mock.patch("yardstick.ssh.SSH") as ssh:
42 ssh_mock = mock.Mock(autospec=ssh.SSH)
44 mock.Mock(return_value=(1, "", ""))
46 mock.Mock(return_value=(1, "", ""))
47 cpu_topo = CpuSysCores(ssh_mock)
48 subprocess.check_output = mock.Mock(return_value=0)
49 lines = ["cpu:1", "topo:2", ""]
50 self.assertEqual([{'topo': '2', 'cpu': '1'}],
51 cpu_topo._get_core_details(lines))
53 def test_get_core_socket(self):
54 with mock.patch("yardstick.ssh.SSH") as ssh:
55 ssh_mock = mock.Mock(autospec=ssh.SSH)
57 mock.Mock(return_value=(1, "cpu:1\ntest:2\n \n", ""))
59 mock.Mock(return_value=(1, "", ""))
60 cpu_topo = CpuSysCores(ssh_mock)
61 subprocess.check_output = mock.Mock(return_value=0)
62 cpu_topo._get_core_details = \
63 mock.Mock(side_effect=[[{'Core(s) per socket': '2', 'Thread(s) per core': '1'}],
64 [{'physical id': '2', 'processor': '1'}]])
65 self.assertEqual({'thread_per_core': '1', '2': ['1'],
66 'cores_per_socket': '2'},
67 cpu_topo.get_core_socket())
69 def test_validate_cpu_cfg(self):
70 with mock.patch("yardstick.ssh.SSH") as ssh:
71 ssh_mock = mock.Mock(autospec=ssh.SSH)
73 mock.Mock(return_value=(1, "cpu:1\ntest:2\n \n", ""))
75 mock.Mock(return_value=(1, "", ""))
76 cpu_topo = CpuSysCores(ssh_mock)
77 subprocess.check_output = mock.Mock(return_value=0)
78 cpu_topo._get_core_details = \
79 mock.Mock(side_effect=[[{'Core(s) per socket': '2', 'Thread(s) per core': '1'}],
80 [{'physical id': '2', 'processor': '1'}]])
82 {'thread_per_core': '1', '2':['1'], 'cores_per_socket': '2'}
83 self.assertEqual(-1, cpu_topo.validate_cpu_cfg())
85 def test_validate_cpu_cfg_2t(self):
86 with mock.patch("yardstick.ssh.SSH") as ssh:
87 ssh_mock = mock.Mock(autospec=ssh.SSH)
89 mock.Mock(return_value=(1, "cpu:1\ntest:2\n \n", ""))
91 mock.Mock(return_value=(1, "", ""))
92 cpu_topo = CpuSysCores(ssh_mock)
93 subprocess.check_output = mock.Mock(return_value=0)
94 cpu_topo._get_core_details = \
95 mock.Mock(side_effect=[[{'Core(s) per socket': '2', 'Thread(s) per core': '1'}],
96 [{'physical id': '2', 'processor': '1'}]])
98 {'thread_per_core': 1, '2':['1'], 'cores_per_socket': '2'}
99 vnf_cfg = {'lb_config': 'SW', 'lb_count': 1, 'worker_config':
100 '1C/2T', 'worker_threads': 1}
101 self.assertEqual(-1, cpu_topo.validate_cpu_cfg(vnf_cfg))
103 def test_validate_cpu_cfg_fail(self):
104 with mock.patch("yardstick.ssh.SSH") as ssh:
105 ssh_mock = mock.Mock(autospec=ssh.SSH)
107 mock.Mock(return_value=(1, "cpu:1\ntest:2\n \n", ""))
109 mock.Mock(return_value=(1, "", ""))
110 cpu_topo = CpuSysCores(ssh_mock)
111 subprocess.check_output = mock.Mock(return_value=0)
112 cpu_topo._get_core_details = \
113 mock.Mock(side_effect=[[{'Core(s) per socket': '2', 'Thread(s) per core': '1'}],
114 [{'physical id': '2', 'processor': '1'}]])
115 cpu_topo.core_map = \
116 {'thread_per_core': 1, '2':[1], 'cores_per_socket': 2}
117 vnf_cfg = {'lb_config': 'SW', 'lb_count': 1, 'worker_config':
118 '1C/1T', 'worker_threads': 1}
119 self.assertEqual(-1, cpu_topo.validate_cpu_cfg(vnf_cfg))