Merge "Add vIPSEC VNF for running Crypto performance test case"
[yardstick.git] / yardstick / 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 division
17 import unittest
18 import mock
19 import subprocess
20
21 from yardstick.network_services.helpers.cpu import \
22     CpuSysCores
23
24
25 class TestCpuSysCores(unittest.TestCase):
26
27     def setUp(self):
28         self._mock_ssh = mock.patch("yardstick.ssh.SSH")
29         self.mock_ssh = self._mock_ssh.start()
30
31         self.addCleanup(self._cleanup)
32
33     def _cleanup(self):
34         self._mock_ssh.stop()
35
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)
41
42     def test__get_core_details(self):
43         with mock.patch("yardstick.ssh.SSH") as ssh:
44             ssh_mock = mock.Mock(autospec=ssh.SSH)
45             ssh_mock.execute = \
46                 mock.Mock(return_value=(1, "", ""))
47             ssh_mock.put = \
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))
54
55     def test_get_core_socket(self):
56         with mock.patch("yardstick.ssh.SSH") as ssh:
57             ssh_mock = mock.Mock(autospec=ssh.SSH)
58             ssh_mock.execute = \
59                 mock.Mock(return_value=(1, "cpu:1\ntest:2\n \n", ""))
60             ssh_mock.put = \
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())
70
71     def test_validate_cpu_cfg(self):
72         with mock.patch("yardstick.ssh.SSH") as ssh:
73             ssh_mock = mock.Mock(autospec=ssh.SSH)
74             ssh_mock.execute = \
75                 mock.Mock(return_value=(1, "cpu:1\ntest:2\n \n", ""))
76             ssh_mock.put = \
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'}]])
83             cpu_topo.core_map = \
84                 {'thread_per_core': '1', '2': ['1'], 'cores_per_socket': '2'}
85             self.assertEqual(-1, cpu_topo.validate_cpu_cfg())
86
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)
90             ssh_mock.execute = \
91                 mock.Mock(return_value=(1, "cpu:1\ntest:2\n \n", ""))
92             ssh_mock.put = \
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'}]])
99             cpu_topo.core_map = \
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))
104
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)
108             ssh_mock.execute = \
109                 mock.Mock(return_value=(1, "cpu:1\ntest:2\n \n", ""))
110             ssh_mock.put = \
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))
122
123     def test_get_cpu_layout(self):
124         with mock.patch("yardstick.ssh.SSH") as ssh:
125             ssh_mock = mock.Mock(autospec=ssh.SSH)
126             ssh_mock.execute = \
127                 mock.Mock(
128                     return_value=(1, "# CPU,Core,Socket,Node,,L1d,L1i,L2,L3\n'"
129                                      "0,0,0,0,,0,0,0,0\n"
130                                      "1,1,0,0,,1,1,1,0\n", ""))
131             ssh_mock.put = \
132                 mock.Mock(return_value=(1, "", ""))
133             cpu_topo = CpuSysCores(ssh_mock)
134             subprocess.check_output = mock.Mock(return_value=0)
135             self.assertEqual({'cpuinfo': [[0, 0, 0, 0, 0, 0, 0, 0, 0],
136                                           [1, 1, 0, 0, 0, 1, 1, 1, 0]]},
137                              cpu_topo.get_cpu_layout())
138
139     def test__str2int(self):
140         self.assertEqual(1, CpuSysCores._str2int("1"))
141
142     def test__str2int_error(self):
143         self.assertEqual(0, CpuSysCores._str2int("err"))