Merge "bugfix: tc037/38 multiprocessing failed sometimes"
[yardstick.git] / tests / unit / network_services / helpers / test_cpu.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2016-2017 Intel Corporation
4 #
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
8 #
9 #      http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16 #
17
18 from __future__ import absolute_import
19 from __future__ import division
20 import unittest
21 import mock
22 import subprocess
23
24 from yardstick.network_services.helpers.cpu import \
25     CpuSysCores
26
27
28 class TestCpuSysCores(unittest.TestCase):
29
30     def test___init__(self):
31         with mock.patch("yardstick.ssh.SSH") as ssh:
32             ssh_mock = mock.Mock(autospec=ssh.SSH)
33             ssh_mock.execute = \
34                 mock.Mock(return_value=(1, "", ""))
35             ssh_mock.put = \
36                 mock.Mock(return_value=(1, "", ""))
37             cpu_topo = CpuSysCores(ssh_mock)
38             self.assertIsNotNone(cpu_topo.connection)
39
40     def test__get_core_details(self):
41         with mock.patch("yardstick.ssh.SSH") as ssh:
42             ssh_mock = mock.Mock(autospec=ssh.SSH)
43             ssh_mock.execute = \
44                 mock.Mock(return_value=(1, "", ""))
45             ssh_mock.put = \
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))
52
53     def test_get_core_socket(self):
54         with mock.patch("yardstick.ssh.SSH") as ssh:
55             ssh_mock = mock.Mock(autospec=ssh.SSH)
56             ssh_mock.execute = \
57                     mock.Mock(return_value=(1, "cpu:1\ntest:2\n \n", ""))
58             ssh_mock.put = \
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())
68
69     def test_validate_cpu_cfg(self):
70         with mock.patch("yardstick.ssh.SSH") as ssh:
71             ssh_mock = mock.Mock(autospec=ssh.SSH)
72             ssh_mock.execute = \
73                     mock.Mock(return_value=(1, "cpu:1\ntest:2\n \n", ""))
74             ssh_mock.put = \
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'}]])
81             cpu_topo.core_map = \
82                 {'thread_per_core': '1', '2':['1'], 'cores_per_socket': '2'}
83             self.assertEqual(-1, cpu_topo.validate_cpu_cfg())
84
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)
88             ssh_mock.execute = \
89                     mock.Mock(return_value=(1, "cpu:1\ntest:2\n \n", ""))
90             ssh_mock.put = \
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'}]])
97             cpu_topo.core_map = \
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))
102
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)
106             ssh_mock.execute = \
107                     mock.Mock(return_value=(1, "cpu:1\ntest:2\n \n", ""))
108             ssh_mock.put = \
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))