Merge "Enable heat context to support existing network"
[yardstick.git] / tests / unit / network_services / helpers / test_cpu.py
1 # Copyright (c) 2016-2017 Intel Corporation
2 #
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
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14 #
15
16 from __future__ import absolute_import
17 from __future__ import division
18 import unittest
19 import mock
20 import subprocess
21
22 from yardstick.network_services.helpers.cpu import \
23     CpuSysCores
24
25
26 class TestCpuSysCores(unittest.TestCase):
27
28     def test___init__(self):
29         with mock.patch("yardstick.ssh.SSH") as ssh:
30             ssh_mock = mock.Mock(autospec=ssh.SSH)
31             ssh_mock.execute = \
32                 mock.Mock(return_value=(1, "", ""))
33             ssh_mock.put = \
34                 mock.Mock(return_value=(1, "", ""))
35             cpu_topo = CpuSysCores(ssh_mock)
36             self.assertIsNotNone(cpu_topo.connection)
37
38     def test__get_core_details(self):
39         with mock.patch("yardstick.ssh.SSH") as ssh:
40             ssh_mock = mock.Mock(autospec=ssh.SSH)
41             ssh_mock.execute = \
42                 mock.Mock(return_value=(1, "", ""))
43             ssh_mock.put = \
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))
50
51     def test_get_core_socket(self):
52         with mock.patch("yardstick.ssh.SSH") as ssh:
53             ssh_mock = mock.Mock(autospec=ssh.SSH)
54             ssh_mock.execute = \
55                     mock.Mock(return_value=(1, "cpu:1\ntest:2\n \n", ""))
56             ssh_mock.put = \
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())
66
67     def test_validate_cpu_cfg(self):
68         with mock.patch("yardstick.ssh.SSH") as ssh:
69             ssh_mock = mock.Mock(autospec=ssh.SSH)
70             ssh_mock.execute = \
71                     mock.Mock(return_value=(1, "cpu:1\ntest:2\n \n", ""))
72             ssh_mock.put = \
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'}]])
79             cpu_topo.core_map = \
80                 {'thread_per_core': '1', '2':['1'], 'cores_per_socket': '2'}
81             self.assertEqual(-1, cpu_topo.validate_cpu_cfg())
82
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)
86             ssh_mock.execute = \
87                     mock.Mock(return_value=(1, "cpu:1\ntest:2\n \n", ""))
88             ssh_mock.put = \
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'}]])
95             cpu_topo.core_map = \
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))
100
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)
104             ssh_mock.execute = \
105                     mock.Mock(return_value=(1, "cpu:1\ntest:2\n \n", ""))
106             ssh_mock.put = \
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))