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 absolute_import
17 from __future__ import division
22 from yardstick.network_services.helpers.cpu import \
26 class TestCpuSysCores(unittest.TestCase):
28 def test___init__(self):
29 with mock.patch("yardstick.ssh.SSH") as ssh:
30 ssh_mock = mock.Mock(autospec=ssh.SSH)
32 mock.Mock(return_value=(1, "", ""))
34 mock.Mock(return_value=(1, "", ""))
35 cpu_topo = CpuSysCores(ssh_mock)
36 self.assertIsNotNone(cpu_topo.connection)
38 def test__get_core_details(self):
39 with mock.patch("yardstick.ssh.SSH") as ssh:
40 ssh_mock = mock.Mock(autospec=ssh.SSH)
42 mock.Mock(return_value=(1, "", ""))
44 mock.Mock(return_value=(1, "", ""))
45 cpu_topo = CpuSysCores(ssh_mock)
46 subprocess.check_output = mock.Mock(return_value=0)
47 lines = ["cpu:1", "topo:2", ""]
48 self.assertEqual([{'topo': '2', 'cpu': '1'}],
49 cpu_topo._get_core_details(lines))
51 def test_get_core_socket(self):
52 with mock.patch("yardstick.ssh.SSH") as ssh:
53 ssh_mock = mock.Mock(autospec=ssh.SSH)
55 mock.Mock(return_value=(1, "cpu:1\ntest:2\n \n", ""))
57 mock.Mock(return_value=(1, "", ""))
58 cpu_topo = CpuSysCores(ssh_mock)
59 subprocess.check_output = mock.Mock(return_value=0)
60 cpu_topo._get_core_details = \
61 mock.Mock(side_effect=[[{'Core(s) per socket': '2', 'Thread(s) per core': '1'}],
62 [{'physical id': '2', 'processor': '1'}]])
63 self.assertEqual({'thread_per_core': '1', '2': ['1'],
64 'cores_per_socket': '2'},
65 cpu_topo.get_core_socket())
67 def test_validate_cpu_cfg(self):
68 with mock.patch("yardstick.ssh.SSH") as ssh:
69 ssh_mock = mock.Mock(autospec=ssh.SSH)
71 mock.Mock(return_value=(1, "cpu:1\ntest:2\n \n", ""))
73 mock.Mock(return_value=(1, "", ""))
74 cpu_topo = CpuSysCores(ssh_mock)
75 subprocess.check_output = mock.Mock(return_value=0)
76 cpu_topo._get_core_details = \
77 mock.Mock(side_effect=[[{'Core(s) per socket': '2', 'Thread(s) per core': '1'}],
78 [{'physical id': '2', 'processor': '1'}]])
80 {'thread_per_core': '1', '2':['1'], 'cores_per_socket': '2'}
81 self.assertEqual(-1, cpu_topo.validate_cpu_cfg())
83 def test_validate_cpu_cfg_2t(self):
84 with mock.patch("yardstick.ssh.SSH") as ssh:
85 ssh_mock = mock.Mock(autospec=ssh.SSH)
87 mock.Mock(return_value=(1, "cpu:1\ntest:2\n \n", ""))
89 mock.Mock(return_value=(1, "", ""))
90 cpu_topo = CpuSysCores(ssh_mock)
91 subprocess.check_output = mock.Mock(return_value=0)
92 cpu_topo._get_core_details = \
93 mock.Mock(side_effect=[[{'Core(s) per socket': '2', 'Thread(s) per core': '1'}],
94 [{'physical id': '2', 'processor': '1'}]])
96 {'thread_per_core': 1, '2':['1'], 'cores_per_socket': '2'}
97 vnf_cfg = {'lb_config': 'SW', 'lb_count': 1, 'worker_config':
98 '1C/2T', 'worker_threads': 1}
99 self.assertEqual(-1, cpu_topo.validate_cpu_cfg(vnf_cfg))
101 def test_validate_cpu_cfg_fail(self):
102 with mock.patch("yardstick.ssh.SSH") as ssh:
103 ssh_mock = mock.Mock(autospec=ssh.SSH)
105 mock.Mock(return_value=(1, "cpu:1\ntest:2\n \n", ""))
107 mock.Mock(return_value=(1, "", ""))
108 cpu_topo = CpuSysCores(ssh_mock)
109 subprocess.check_output = mock.Mock(return_value=0)
110 cpu_topo._get_core_details = \
111 mock.Mock(side_effect=[[{'Core(s) per socket': '2', 'Thread(s) per core': '1'}],
112 [{'physical id': '2', 'processor': '1'}]])
113 cpu_topo.core_map = \
114 {'thread_per_core': 1, '2':[1], 'cores_per_socket': 2}
115 vnf_cfg = {'lb_config': 'SW', 'lb_count': 1, 'worker_config':
116 '1C/1T', 'worker_threads': 1}
117 self.assertEqual(-1, cpu_topo.validate_cpu_cfg(vnf_cfg))