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))