1 ##############################################################################
2 # Copyright (c) 2015 Ericsson AB and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
10 from copy import deepcopy
14 from itertools import product, chain
18 from six.moves import configparser
24 from yardstick import ssh
25 from yardstick.common import constants
26 from yardstick.common import utils
27 from yardstick.common import exceptions
30 class IterSubclassesTestCase(unittest.TestCase):
31 # Disclaimer: this class is a modified copy from
32 # rally/tests/unit/common/plugin/test_discover.py
33 # Copyright 2015: Mirantis Inc.
35 def test_itersubclasses(self):
48 self.assertEqual([B, C, D], list(utils.itersubclasses(A)))
51 class ImportModulesFromPackageTestCase(unittest.TestCase):
53 @mock.patch('yardstick.common.utils.os.walk')
54 def test_import_modules_from_package_no_mod(self, mock_walk):
55 yardstick_root = os.path.dirname(os.path.dirname(yardstick.__file__))
56 mock_walk.return_value = ([
57 (os.path.join(yardstick_root, 'foo'), ['bar'], ['__init__.py']),
58 (os.path.join(yardstick_root, 'foo', 'bar'), [], ['baz.txt', 'qux.rst'])
61 utils.import_modules_from_package('foo.bar')
63 @mock.patch('yardstick.common.utils.os.walk')
64 @mock.patch.object(importlib, 'import_module')
65 def test_import_modules_from_package(self, mock_import_module, mock_walk):
67 yardstick_root = os.path.dirname(os.path.dirname(yardstick.__file__))
68 mock_walk.return_value = ([
69 (os.path.join(yardstick_root, 'foo', os.pardir, 'bar'), [], ['baz.py'])
72 utils.import_modules_from_package('foo.bar')
73 mock_import_module.assert_called_once_with('bar.baz')
76 class GetParaFromYaml(unittest.TestCase):
78 @mock.patch('yardstick.common.utils.os.environ.get')
79 def test_get_param_para_not_found(self, get_env):
80 file_path = 'config_sample.yaml'
81 get_env.return_value = self._get_file_abspath(file_path)
84 self.assertTrue(constants.get_param(args, default), default)
86 @mock.patch('yardstick.common.utils.os.environ.get')
87 def test_get_param_para_exists(self, get_env):
88 file_path = 'config_sample.yaml'
89 get_env.return_value = self._get_file_abspath(file_path)
91 para = '/home/opnfv/repos/releng'
92 self.assertEqual(para, constants.get_param(args))
94 def _get_file_abspath(self, filename):
95 curr_path = os.path.dirname(os.path.abspath(__file__))
96 file_path = os.path.join(curr_path, filename)
100 class CommonUtilTestCase(unittest.TestCase):
121 def test__dict_key_flatten(self):
122 line = 'mpstat.loadavg1=0.29,rtt=1.03,mpstat.loadavg0=1.09,' \
123 'mpstat.cpu0.%idle=99.00,mpstat.cpu0.%sys=0.00'
124 # need to sort for assert to work
125 line = ",".join(sorted(line.split(',')))
126 flattened_data = utils.flatten_dict_key(
127 self.data['benchmark']['data'])
129 ("=".join(item) for item in sorted(flattened_data.items())))
130 self.assertEqual(result, line)
132 def test_get_key_with_default_negative(self):
133 with self.assertRaises(KeyError):
134 utils.get_key_with_default({}, 'key1')
136 @mock.patch('yardstick.common.utils.open', create=True)
137 def test_(self, mock_open):
138 mock_open.side_effect = IOError
140 with self.assertRaises(IOError):
141 utils.find_relative_file('my/path', 'task/path')
143 self.assertEqual(mock_open.call_count, 2)
145 @mock.patch('yardstick.common.utils.open', create=True)
146 def test_open_relative_path(self, mock_open):
147 mock_open_result = mock_open()
148 mock_open_call_count = 1 # initial call to get result
150 self.assertEqual(utils.open_relative_file('foo', 'bar'), mock_open_result)
152 mock_open_call_count += 1 # one more call expected
153 self.assertEqual(mock_open.call_count, mock_open_call_count)
154 self.assertIn('foo', mock_open.call_args_list[-1][0][0])
155 self.assertNotIn('bar', mock_open.call_args_list[-1][0][0])
157 def open_effect(*args, **kwargs):
158 if kwargs.get('name', args[0]) == os.path.join('bar', 'foo'):
159 return mock_open_result
160 raise IOError(errno.ENOENT, 'not found')
162 mock_open.side_effect = open_effect
163 self.assertEqual(utils.open_relative_file('foo', 'bar'), mock_open_result)
165 mock_open_call_count += 2 # two more calls expected
166 self.assertEqual(mock_open.call_count, mock_open_call_count)
167 self.assertIn('foo', mock_open.call_args_list[-1][0][0])
168 self.assertIn('bar', mock_open.call_args_list[-1][0][0])
170 # test an IOError of type ENOENT
171 mock_open.side_effect = IOError(errno.ENOENT, 'not found')
172 with self.assertRaises(IOError):
173 # the second call still raises
174 utils.open_relative_file('foo', 'bar')
176 mock_open_call_count += 2 # two more calls expected
177 self.assertEqual(mock_open.call_count, mock_open_call_count)
178 self.assertIn('foo', mock_open.call_args_list[-1][0][0])
179 self.assertIn('bar', mock_open.call_args_list[-1][0][0])
181 # test an IOError other than ENOENT
182 mock_open.side_effect = IOError(errno.EBUSY, 'busy')
183 with self.assertRaises(IOError):
184 utils.open_relative_file('foo', 'bar')
186 mock_open_call_count += 1 # one more call expected
187 self.assertEqual(mock_open.call_count, mock_open_call_count)
190 class TestMacAddressToHex(unittest.TestCase):
192 def test_mac_address_to_hex_list(self):
193 self.assertEqual(utils.mac_address_to_hex_list("ea:3e:e1:9a:99:e8"),
194 ['0xea', '0x3e', '0xe1', '0x9a', '0x99', '0xe8'])
197 class TranslateToStrTestCase(unittest.TestCase):
199 def test_translate_to_str_unicode(self):
201 output_str = utils.translate_to_str(input_str)
204 self.assertEqual(result, output_str)
206 def test_translate_to_str_dict_list_unicode(self):
208 u'hello': {u'hello': [u'world']}
210 output_str = utils.translate_to_str(input_str)
213 'hello': {'hello': ['world']}
215 self.assertEqual(result, output_str)
217 def test_translate_to_str_non_string(self):
218 input_value = object()
219 result = utils.translate_to_str(input_value)
220 self.assertIs(input_value, result)
223 class TestParseCpuInfo(unittest.TestCase):
225 def test_single_socket_no_hyperthread(self):
228 vendor_id : GenuineIntel
231 model name : Intel Core Processor (Haswell, no TSX)
246 flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt arat
251 address sizes : 46 bits physical, 48 bits virtual
255 vendor_id : GenuineIntel
258 model name : Intel Core Processor (Haswell, no TSX)
273 flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt arat
278 address sizes : 46 bits physical, 48 bits virtual
282 vendor_id : GenuineIntel
285 model name : Intel Core Processor (Haswell, no TSX)
300 flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt arat
305 address sizes : 46 bits physical, 48 bits virtual
309 socket_map = utils.SocketTopology.parse_cpuinfo(cpuinfo)
310 self.assertEqual(sorted(socket_map.keys()), [0])
311 self.assertEqual(sorted(socket_map[0].keys()), [2, 3, 4])
313 def test_single_socket_hyperthread(self):
316 vendor_id : GenuineIntel
319 model name : Intel(R) Xeon(R) CPU E3-1275 v3 @ 3.50GHz
334 flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm epb tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt dtherm ida arat pln pts
339 address sizes : 39 bits physical, 48 bits virtual
343 vendor_id : GenuineIntel
346 model name : Intel(R) Xeon(R) CPU E3-1275 v3 @ 3.50GHz
361 flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm epb tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt dtherm ida arat pln pts
366 address sizes : 39 bits physical, 48 bits virtual
370 vendor_id : GenuineIntel
373 model name : Intel(R) Xeon(R) CPU E3-1275 v3 @ 3.50GHz
388 flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm epb tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt dtherm ida arat pln pts
393 address sizes : 39 bits physical, 48 bits virtual
397 socket_map = utils.SocketTopology.parse_cpuinfo(cpuinfo)
398 self.assertEqual(sorted(socket_map.keys()), [0])
399 self.assertEqual(sorted(socket_map[0].keys()), [1, 2, 3])
400 self.assertEqual(sorted(socket_map[0][1]), [5])
401 self.assertEqual(sorted(socket_map[0][2]), [6])
402 self.assertEqual(sorted(socket_map[0][3]), [7])
404 def test_dual_socket_hyperthread(self):
407 vendor_id : GenuineIntel
410 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
412 microcode : 0xb00001f
414 cache size : 56320 KB
425 flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch epb cat_l3 cdp_l3 intel_ppin intel_pt tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm rdt_a rdseed adx smap xsaveopt cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts
430 address sizes : 46 bits physical, 48 bits virtual
434 vendor_id : GenuineIntel
437 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
439 microcode : 0xb00001f
441 cache size : 56320 KB
452 flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch epb cat_l3 cdp_l3 intel_ppin intel_pt tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm rdt_a rdseed adx smap xsaveopt cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts
457 address sizes : 46 bits physical, 48 bits virtual
461 vendor_id : GenuineIntel
464 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
466 microcode : 0xb00001f
468 cache size : 56320 KB
479 flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch epb cat_l3 cdp_l3 intel_ppin intel_pt tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm rdt_a rdseed adx smap xsaveopt cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts
484 address sizes : 46 bits physical, 48 bits virtual
488 vendor_id : GenuineIntel
491 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
493 microcode : 0xb00001f
495 cache size : 56320 KB
506 flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch epb cat_l3 cdp_l3 intel_ppin intel_pt tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm rdt_a rdseed adx smap xsaveopt cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts
511 address sizes : 46 bits physical, 48 bits virtual
515 vendor_id : GenuineIntel
518 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
520 microcode : 0xb00001f
522 cache size : 56320 KB
533 flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch epb cat_l3 cdp_l3 intel_ppin intel_pt tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm rdt_a rdseed adx smap xsaveopt cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts
538 address sizes : 46 bits physical, 48 bits virtual
542 vendor_id : GenuineIntel
545 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
547 microcode : 0xb00001f
549 cache size : 56320 KB
560 flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch epb cat_l3 cdp_l3 intel_ppin intel_pt tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm rdt_a rdseed adx smap xsaveopt cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts
565 address sizes : 46 bits physical, 48 bits virtual
569 vendor_id : GenuineIntel
572 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
574 microcode : 0xb00001f
576 cache size : 56320 KB
587 flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch epb cat_l3 cdp_l3 intel_ppin intel_pt tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm rdt_a rdseed adx smap xsaveopt cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts
592 address sizes : 46 bits physical, 48 bits virtual
596 socket_map = utils.SocketTopology.parse_cpuinfo(cpuinfo)
597 self.assertEqual(sorted(socket_map.keys()), [0, 1])
598 self.assertEqual(sorted(socket_map[0].keys()), [0, 1, 2])
599 self.assertEqual(sorted(socket_map[1].keys()), [26, 27, 28])
600 self.assertEqual(sorted(socket_map[0][0]), [44])
601 self.assertEqual(sorted(socket_map[0][1]), [1])
602 self.assertEqual(sorted(socket_map[0][2]), [2])
603 self.assertEqual(sorted(socket_map[1][26]), [85])
604 self.assertEqual(sorted(socket_map[1][27]), [86])
605 self.assertEqual(sorted(socket_map[1][28]), [43, 87])
607 def test_dual_socket_no_hyperthread(self):
610 vendor_id : GenuineIntel
613 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
615 microcode : 0xb00001f
617 cache size : 56320 KB
628 flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch epb cat_l3 cdp_l3 intel_ppin intel_pt tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm rdt_a rdseed adx smap xsaveopt cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts
633 address sizes : 46 bits physical, 48 bits virtual
637 vendor_id : GenuineIntel
640 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
642 microcode : 0xb00001f
644 cache size : 56320 KB
655 flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch epb cat_l3 cdp_l3 intel_ppin intel_pt tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm rdt_a rdseed adx smap xsaveopt cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts
660 address sizes : 46 bits physical, 48 bits virtual
664 vendor_id : GenuineIntel
667 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
669 microcode : 0xb00001f
671 cache size : 56320 KB
682 flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch epb cat_l3 cdp_l3 intel_ppin intel_pt tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm rdt_a rdseed adx smap xsaveopt cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts
687 address sizes : 46 bits physical, 48 bits virtual
691 vendor_id : GenuineIntel
694 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
696 microcode : 0xb00001f
698 cache size : 56320 KB
709 flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch epb cat_l3 cdp_l3 intel_ppin intel_pt tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm rdt_a rdseed adx smap xsaveopt cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts
714 address sizes : 46 bits physical, 48 bits virtual
718 vendor_id : GenuineIntel
721 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
723 microcode : 0xb00001f
725 cache size : 56320 KB
736 flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch epb cat_l3 cdp_l3 intel_ppin intel_pt tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm rdt_a rdseed adx smap xsaveopt cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts
741 address sizes : 46 bits physical, 48 bits virtual
745 vendor_id : GenuineIntel
748 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
750 microcode : 0xb00001f
752 cache size : 56320 KB
763 flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch epb cat_l3 cdp_l3 intel_ppin intel_pt tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm rdt_a rdseed adx smap xsaveopt cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts
768 address sizes : 46 bits physical, 48 bits virtual
772 vendor_id : GenuineIntel
775 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
777 microcode : 0xb00001f
779 cache size : 56320 KB
790 flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch epb cat_l3 cdp_l3 intel_ppin intel_pt tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm rdt_a rdseed adx smap xsaveopt cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts
795 address sizes : 46 bits physical, 48 bits virtual
799 socket_map = utils.SocketTopology.parse_cpuinfo(cpuinfo)
800 processors = socket_map.processors()
801 self.assertEqual(processors, [1, 2, 43, 44, 85, 86, 87])
802 cores = socket_map.cores()
803 self.assertEqual(cores, [0, 1, 2, 26, 27, 28])
804 sockets = socket_map.sockets()
805 self.assertEqual(sockets, [0, 1])
808 class ChangeObjToDictTestCase(unittest.TestCase):
810 def test_change_obj_to_dict(self):
813 self.name = 'yardstick'
816 obj_r = utils.change_obj_to_dict(obj)
817 obj_s = {'name': 'yardstick'}
818 self.assertEqual(obj_r, obj_s)
821 class SetDictValueTestCase(unittest.TestCase):
823 def test_set_dict_value(self):
827 output_dic = utils.set_dict_value(input_dic, 'welcome.to', 'yardstick')
828 self.assertEqual(output_dic.get('welcome', {}).get('to'), 'yardstick')
831 class RemoveFileTestCase(unittest.TestCase):
833 def test_remove_file(self):
835 utils.remove_file('notexistfile.txt')
836 except Exception as e: # pylint: disable=broad-except
837 # NOTE(ralonsoh): to narrow the scope of this exception.
838 self.assertTrue(isinstance(e, OSError))
841 class TestUtils(unittest.TestCase):
843 @mock.patch('yardstick.common.utils.os.makedirs')
844 def test_makedirs(self, *_):
845 self.assertIsNone(utils.makedirs('a/b/c/d'))
847 @mock.patch('yardstick.common.utils.os.makedirs')
848 def test_makedirs_exists(self, mock_os_makedirs):
849 mock_os_makedirs.side_effect = OSError(errno.EEXIST, 'exists')
850 self.assertIsNone(utils.makedirs('a/b/c/d'))
852 @mock.patch('yardstick.common.utils.os.makedirs')
853 def test_makedirs_busy(self, mock_os_makedirs):
854 mock_os_makedirs.side_effect = OSError(errno.EBUSY, 'busy')
855 with self.assertRaises(OSError):
856 utils.makedirs('a/b/c/d')
858 @mock.patch('yardstick.common.utils.jsonify')
859 def test_result_handler(self, mock_jsonify):
860 mock_jsonify.return_value = 432
862 self.assertEqual(utils.result_handler('x', 234), 432)
863 mock_jsonify.assert_called_once_with({'status': 'x', 'result': 234})
865 @mock.patch('random.randint')
866 @mock.patch('socket.socket')
867 def test_get_free_port(self, mock_socket, mock_randint):
868 mock_randint.return_value = 7777
869 s = mock_socket('x', 'y')
870 s.connect_ex.side_effect = iter([0, 1])
871 result = utils.get_free_port('10.20.30.40')
872 self.assertEqual(result, 7777)
873 self.assertEqual(s.connect_ex.call_count, 2)
875 @mock.patch('subprocess.check_output')
876 def test_execute_command(self, mock_check_output):
877 expected = ['hello world', '1234']
878 mock_check_output.return_value = os.linesep.join(expected)
879 result = utils.execute_command('my_command arg1 arg2')
880 self.assertEqual(result, expected)
882 @mock.patch('subprocess.Popen')
883 def test_source_env(self, mock_popen):
884 base_env = deepcopy(os.environ)
885 mock_process = mock_popen()
887 'garbage line before',
889 'garbage line after',
891 mock_process.communicate.return_value = os.linesep.join(output_list), '', 0
892 expected = {'NEW_ENV_VALUE': '234'}
893 result = utils.source_env('my_file')
894 self.assertDictEqual(result, expected)
896 os.environ.update(base_env)
898 @mock.patch.object(configparser, 'ConfigParser')
899 def test_parse_ini_file(self, mock_config_parser_type):
901 'default1': 'value1',
902 'default2': 'value2',
913 mock_config_parser = mock_config_parser_type()
914 mock_config_parser.read.return_value = True
915 mock_config_parser.sections.return_value = ['s1', 's2']
916 mock_config_parser.items.side_effect = iter([
927 result = utils.parse_ini_file('my_path')
928 self.assertDictEqual(result, expected)
930 @mock.patch.object(utils, 'logger')
931 @mock.patch.object(configparser, 'ConfigParser')
932 def test_parse_ini_file_missing_section_header(
933 self, mock_config_parser_type, *args):
934 mock_config_parser = mock_config_parser_type()
935 mock_config_parser.read.side_effect = (
936 configparser.MissingSectionHeaderError(mock.Mock(), 321,
939 with self.assertRaises(configparser.MissingSectionHeaderError):
940 utils.parse_ini_file('my_path')
942 @mock.patch.object(configparser, 'ConfigParser')
943 def test_parse_ini_file_no_file(self, mock_config_parser_type):
944 mock_config_parser = mock_config_parser_type()
945 mock_config_parser.read.return_value = False
946 with self.assertRaises(RuntimeError):
947 utils.parse_ini_file('my_path')
949 @mock.patch.object(configparser, 'ConfigParser')
950 def test_parse_ini_file_no_default_section_header(self, mock_config_parser_type):
960 mock_config_parser = mock_config_parser_type()
961 mock_config_parser.read.return_value = True
962 mock_config_parser.sections.return_value = ['s1', 's2']
963 mock_config_parser.items.side_effect = iter([
964 configparser.NoSectionError(mock.Mock()),
974 result = utils.parse_ini_file('my_path')
975 self.assertDictEqual(result, expected)
977 def test_join_non_strings(self):
978 self.assertEqual(utils.join_non_strings(':'), '')
979 self.assertEqual(utils.join_non_strings(':', 'a'), 'a')
980 self.assertEqual(utils.join_non_strings(':', 'a', 2, 'c'), 'a:2:c')
981 self.assertEqual(utils.join_non_strings(':', ['a', 2, 'c']), 'a:2:c')
982 self.assertEqual(utils.join_non_strings(':', 'abc'), 'abc')
984 def test_validate_non_string_sequence(self):
985 self.assertEqual(utils.validate_non_string_sequence([1, 2, 3]), [1, 2, 3])
986 self.assertIsNone(utils.validate_non_string_sequence('123'))
987 self.assertIsNone(utils.validate_non_string_sequence(1))
989 self.assertEqual(utils.validate_non_string_sequence(1, 2), 2)
990 self.assertEqual(utils.validate_non_string_sequence(1, default=2), 2)
992 with self.assertRaises(RuntimeError):
993 utils.validate_non_string_sequence(1, raise_exc=RuntimeError)
996 class TestUtilsIpAddrMethods(unittest.TestCase):
998 GOOD_IP_V4_ADDRESS_STR_LIST = [
1008 GOOD_IP_V4_MASK_STR_LIST = [
1017 GOOD_IP_V6_ADDRESS_STR_LIST = [
1019 u'fe80::250:56ff:fe89:91ff',
1020 u'123:4567:89ab:cdef:123:4567:89ab:cdef',
1023 GOOD_IP_V6_MASK_STR_LIST = [
1032 INVALID_IP_ADDRESS_STR_LIST = [
1036 u'123:4567:89ab:cdef:123:4567:89ab:cdef/129',
1039 def test_safe_ip_address(self):
1040 addr_list = self.GOOD_IP_V4_ADDRESS_STR_LIST
1041 for addr in addr_list:
1043 expected = ipaddress.ip_address(addr)
1044 self.assertEqual(utils.safe_ip_address(addr), expected, addr)
1046 def test_safe_ip_address_v6_ip(self):
1047 addr_list = self.GOOD_IP_V6_ADDRESS_STR_LIST
1048 for addr in addr_list:
1050 expected = ipaddress.ip_address(addr)
1051 self.assertEqual(utils.safe_ip_address(addr), expected, addr)
1053 @mock.patch("yardstick.common.utils.logging")
1054 def test_safe_ip_address_negative(self, *args):
1055 # NOTE(ralonsoh): check the calls to mocked functions.
1056 for value in self.INVALID_IP_ADDRESS_STR_LIST:
1057 self.assertIsNone(utils.safe_ip_address(value), value)
1059 addr_list = self.GOOD_IP_V4_ADDRESS_STR_LIST
1060 mask_list = self.GOOD_IP_V4_MASK_STR_LIST
1061 for addr_mask_pair in product(addr_list, mask_list):
1062 value = ''.join(addr_mask_pair)
1063 self.assertIsNone(utils.safe_ip_address(value), value)
1065 addr_list = self.GOOD_IP_V6_ADDRESS_STR_LIST
1066 mask_list = self.GOOD_IP_V6_MASK_STR_LIST
1067 for addr_mask_pair in product(addr_list, mask_list):
1068 value = ''.join(addr_mask_pair)
1069 self.assertIsNone(utils.safe_ip_address(value), value)
1071 def test_get_ip_version(self):
1072 addr_list = self.GOOD_IP_V4_ADDRESS_STR_LIST
1073 for addr in addr_list:
1075 self.assertEqual(utils.get_ip_version(addr), 4, addr)
1077 def test_get_ip_version_v6_ip(self):
1078 addr_list = self.GOOD_IP_V6_ADDRESS_STR_LIST
1079 for addr in addr_list:
1081 self.assertEqual(utils.get_ip_version(addr), 6, addr)
1083 @mock.patch("yardstick.common.utils.logging")
1084 def test_get_ip_version_negative(self, *args):
1085 # NOTE(ralonsoh): check the calls to mocked functions.
1086 for value in self.INVALID_IP_ADDRESS_STR_LIST:
1087 self.assertIsNone(utils.get_ip_version(value), value)
1089 addr_list = self.GOOD_IP_V4_ADDRESS_STR_LIST
1090 mask_list = self.GOOD_IP_V4_MASK_STR_LIST
1091 for addr_mask_pair in product(addr_list, mask_list):
1092 value = ''.join(addr_mask_pair)
1093 self.assertIsNone(utils.get_ip_version(value), value)
1095 addr_list = self.GOOD_IP_V6_ADDRESS_STR_LIST
1096 mask_list = self.GOOD_IP_V6_MASK_STR_LIST
1097 for addr_mask_pair in product(addr_list, mask_list):
1098 value = ''.join(addr_mask_pair)
1099 self.assertIsNone(utils.get_ip_version(value), value)
1101 def test_ip_to_hex(self):
1102 self.assertEqual(utils.ip_to_hex('0.0.0.0'), '00000000')
1103 self.assertEqual(utils.ip_to_hex('10.20.30.40'), '0a141e28')
1104 self.assertEqual(utils.ip_to_hex('127.0.0.1'), '7f000001')
1105 self.assertEqual(utils.ip_to_hex('172.31.90.100'), 'ac1f5a64')
1106 self.assertEqual(utils.ip_to_hex('192.168.254.253'), 'c0a8fefd')
1107 self.assertEqual(utils.ip_to_hex('255.255.255.255'), 'ffffffff')
1109 def test_ip_to_hex_v6_ip(self):
1110 for value in self.GOOD_IP_V6_ADDRESS_STR_LIST:
1111 self.assertEqual(utils.ip_to_hex(value), value)
1113 @mock.patch("yardstick.common.utils.logging")
1114 def test_ip_to_hex_negative(self, *args):
1115 # NOTE(ralonsoh): check the calls to mocked functions.
1116 addr_list = self.GOOD_IP_V4_ADDRESS_STR_LIST
1117 mask_list = self.GOOD_IP_V4_MASK_STR_LIST
1118 value_iter = (''.join(pair) for pair in product(addr_list, mask_list))
1119 for value in chain(value_iter, self.INVALID_IP_ADDRESS_STR_LIST):
1120 self.assertEqual(utils.ip_to_hex(value), value)
1123 class SafeDecodeUtf8TestCase(unittest.TestCase):
1125 @unittest.skipIf(six.PY2,
1126 'This test should only be launched with Python 3.x')
1127 def test_safe_decode_utf8(self):
1128 _bytes = b'this is a byte array'
1129 out = utils.safe_decode_utf8(_bytes)
1130 self.assertIs(type(out), str)
1131 self.assertEqual('this is a byte array', out)
1134 class ReadMeminfoTestCase(unittest.TestCase):
1136 MEMINFO = (b'MemTotal: 65860500 kB\n'
1137 b'MemFree: 28690900 kB\n'
1138 b'MemAvailable: 52873764 kB\n'
1139 b'Active(anon): 3015676 kB\n'
1140 b'HugePages_Total: 8\n'
1141 b'Hugepagesize: 1048576 kB')
1142 MEMINFO_DICT = {'MemTotal': '65860500',
1143 'MemFree': '28690900',
1144 'MemAvailable': '52873764',
1145 'Active(anon)': '3015676',
1146 'HugePages_Total': '8',
1147 'Hugepagesize': '1048576'}
1149 def test_read_meminfo(self):
1150 ssh_client = ssh.SSH('user', 'host')
1151 with mock.patch.object(ssh_client, 'get_file_obj') as \
1153 mock.patch.object(six, 'BytesIO',
1154 return_value=six.BytesIO(self.MEMINFO)):
1155 output = utils.read_meminfo(ssh_client)
1156 mock_get_client.assert_called_once_with('/proc/meminfo', mock.ANY)
1157 self.assertEqual(self.MEMINFO_DICT, output)
1160 class TimerTestCase(unittest.TestCase):
1162 def test__getattr(self):
1163 with utils.Timer() as timer:
1165 self.assertEqual(1, round(timer.total_seconds(), 0))
1166 self.assertEqual(1, timer.delta.seconds)
1168 def test__enter_with_timeout(self):
1169 with utils.Timer(timeout=10) as timer:
1171 self.assertEqual(1, round(timer.total_seconds(), 0))
1173 def test__enter_with_timeout_exception(self):
1174 with self.assertRaises(exceptions.TimerTimeout):
1175 with utils.Timer(timeout=1):
1179 class WaitUntilTrueTestCase(unittest.TestCase):
1181 def test_no_timeout(self):
1182 self.assertIsNone(utils.wait_until_true(lambda: True,
1183 timeout=1, sleep=1))
1185 def test_timeout_generic_exception(self):
1186 with self.assertRaises(exceptions.WaitTimeout):
1187 self.assertIsNone(utils.wait_until_true(lambda: False,
1188 timeout=1, sleep=1))
1190 def test_timeout_given_exception(self):
1191 class MyTimeoutException(exceptions.YardstickException):
1192 message = 'My timeout exception'
1194 with self.assertRaises(MyTimeoutException):
1196 utils.wait_until_true(lambda: False, timeout=1, sleep=1,
1197 exception=MyTimeoutException))
1200 class SendSocketCommandTestCase(unittest.TestCase):
1202 @mock.patch.object(socket, 'socket')
1203 def test_execute_correct(self, mock_socket):
1204 mock_socket_obj = mock.Mock()
1205 mock_socket_obj.connect_ex.return_value = 0
1206 mock_socket.return_value = mock_socket_obj
1207 self.assertEqual(0, utils.send_socket_command('host', 22, 'command'))
1208 mock_socket.assert_called_once_with(socket.AF_INET, socket.SOCK_STREAM)
1209 mock_socket_obj.connect_ex.assert_called_once_with(('host', 22))
1210 mock_socket_obj.sendall.assert_called_once_with(six.b('command'))
1211 mock_socket_obj.close.assert_called_once()
1213 @mock.patch.object(socket, 'socket')
1214 def test_execute_exception(self, mock_socket):
1215 mock_socket_obj = mock.Mock()
1216 mock_socket_obj.connect_ex.return_value = 0
1217 mock_socket.return_value = mock_socket_obj
1218 mock_socket_obj.sendall.side_effect = socket.error
1219 self.assertEqual(1, utils.send_socket_command('host', 22, 'command'))
1220 mock_socket.assert_called_once_with(socket.AF_INET, socket.SOCK_STREAM)
1221 mock_socket_obj.connect_ex.assert_called_once_with(('host', 22))
1222 mock_socket_obj.sendall.assert_called_once_with(six.b('command'))
1223 mock_socket_obj.close.assert_called_once()