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 # Unittest for yardstick.common.utils
12 from __future__ import absolute_import
17 from copy import deepcopy
18 from itertools import product, chain
21 from six.moves import configparser
23 from yardstick.common import utils
24 from yardstick.common import constants
27 class IterSubclassesTestCase(unittest.TestCase):
28 # Disclaimer: this class is a modified copy from
29 # rally/tests/unit/common/plugin/test_discover.py
30 # Copyright 2015: Mirantis Inc.
32 def test_itersubclasses(self):
45 self.assertEqual([B, C, D], list(utils.itersubclasses(A)))
48 class TryAppendModuleTestCase(unittest.TestCase):
50 @mock.patch('yardstick.common.utils.importutils')
51 def test_try_append_module_not_in_modules(self, mock_importutils):
55 utils.try_append_module(name, modules)
56 mock_importutils.import_module.assert_called_with(name)
58 @mock.patch('yardstick.common.utils.importutils')
59 def test_try_append_module_already_in_modules(self, mock_importutils):
63 utils.try_append_module(name, modules)
64 self.assertFalse(mock_importutils.import_module.called)
67 class ImportModulesFromPackageTestCase(unittest.TestCase):
69 @mock.patch('yardstick.common.utils.os.walk')
70 @mock.patch('yardstick.common.utils.try_append_module')
71 def test_import_modules_from_package_no_mod(self, mock_append, mock_walk):
74 mock_walk.return_value = ([
75 ('..' + sep + 'foo', ['bar'], ['__init__.py']),
76 ('..' + sep + 'foo' + sep + 'bar', [], ['baz.txt', 'qux.rst'])
79 utils.import_modules_from_package('foo.bar')
80 self.assertFalse(mock_append.called)
82 @mock.patch('yardstick.common.utils.os.walk')
83 @mock.patch('yardstick.common.utils.importutils')
84 def test_import_modules_from_package(self, mock_importutils, mock_walk):
87 mock_walk.return_value = ([
88 ('foo' + sep + '..' + sep + 'bar', [], ['baz.py'])
91 utils.import_modules_from_package('foo.bar')
92 mock_importutils.import_module.assert_called_with('bar.baz')
95 class GetParaFromYaml(unittest.TestCase):
97 @mock.patch('yardstick.common.utils.os.environ.get')
98 def test_get_param_para_not_found(self, get_env):
99 file_path = 'config_sample.yaml'
100 get_env.return_value = self._get_file_abspath(file_path)
103 self.assertTrue(constants.get_param(args, default), default)
105 @mock.patch('yardstick.common.utils.os.environ.get')
106 def test_get_param_para_exists(self, get_env):
107 file_path = 'config_sample.yaml'
108 get_env.return_value = self._get_file_abspath(file_path)
110 para = '/home/opnfv/repos/releng'
111 self.assertEqual(para, constants.get_param(args))
113 def _get_file_abspath(self, filename):
114 curr_path = os.path.dirname(os.path.abspath(__file__))
115 file_path = os.path.join(curr_path, filename)
119 class CommonUtilTestCase(unittest.TestCase):
140 def test__dict_key_flatten(self):
141 line = 'mpstat.loadavg1=0.29,rtt=1.03,mpstat.loadavg0=1.09,' \
142 'mpstat.cpu0.%idle=99.00,mpstat.cpu0.%sys=0.00'
143 # need to sort for assert to work
144 line = ",".join(sorted(line.split(',')))
145 flattened_data = utils.flatten_dict_key(
146 self.data['benchmark']['data'])
148 ("=".join(item) for item in sorted(flattened_data.items())))
149 self.assertEqual(result, line)
152 class TestMacAddressToHex(unittest.TestCase):
154 def test_mac_address_to_hex_list(self):
155 self.assertEqual(utils.mac_address_to_hex_list("ea:3e:e1:9a:99:e8"),
156 ['0xea', '0x3e', '0xe1', '0x9a', '0x99', '0xe8'])
159 class TranslateToStrTestCase(unittest.TestCase):
161 def test_translate_to_str_unicode(self):
163 output_str = utils.translate_to_str(input_str)
166 self.assertEqual(result, output_str)
168 def test_translate_to_str_dict_list_unicode(self):
170 u'hello': {u'hello': [u'world']}
172 output_str = utils.translate_to_str(input_str)
175 'hello': {'hello': ['world']}
177 self.assertEqual(result, output_str)
179 def test_translate_to_str_non_string(self):
180 input_value = object()
181 result = utils.translate_to_str(input_value)
182 self.assertIs(input_value, result)
185 class TestParseCpuInfo(unittest.TestCase):
187 def test_single_socket_no_hyperthread(self):
190 vendor_id : GenuineIntel
193 model name : Intel Core Processor (Haswell, no TSX)
208 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
213 address sizes : 46 bits physical, 48 bits virtual
217 vendor_id : GenuineIntel
220 model name : Intel Core Processor (Haswell, no TSX)
235 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
240 address sizes : 46 bits physical, 48 bits virtual
244 vendor_id : GenuineIntel
247 model name : Intel Core Processor (Haswell, no TSX)
262 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
267 address sizes : 46 bits physical, 48 bits virtual
271 socket_map = utils.parse_cpuinfo(cpuinfo)
272 assert sorted(socket_map.keys()) == [0]
273 assert sorted(socket_map[0].keys()) == [2, 3, 4]
275 def test_single_socket_hyperthread(self):
278 vendor_id : GenuineIntel
281 model name : Intel(R) Xeon(R) CPU E3-1275 v3 @ 3.50GHz
296 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
301 address sizes : 39 bits physical, 48 bits virtual
305 vendor_id : GenuineIntel
308 model name : Intel(R) Xeon(R) CPU E3-1275 v3 @ 3.50GHz
323 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
328 address sizes : 39 bits physical, 48 bits virtual
332 vendor_id : GenuineIntel
335 model name : Intel(R) Xeon(R) CPU E3-1275 v3 @ 3.50GHz
350 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
355 address sizes : 39 bits physical, 48 bits virtual
359 socket_map = utils.parse_cpuinfo(cpuinfo)
360 assert sorted(socket_map.keys()) == [0]
361 assert sorted(socket_map[0].keys()) == [1, 2, 3]
362 assert sorted(socket_map[0][1]) == [5]
363 assert sorted(socket_map[0][2]) == [6]
364 assert sorted(socket_map[0][3]) == [7]
366 def test_dual_socket_hyperthread(self):
369 vendor_id : GenuineIntel
372 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
374 microcode : 0xb00001f
376 cache size : 56320 KB
387 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
392 address sizes : 46 bits physical, 48 bits virtual
396 vendor_id : GenuineIntel
399 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
401 microcode : 0xb00001f
403 cache size : 56320 KB
414 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
419 address sizes : 46 bits physical, 48 bits virtual
423 vendor_id : GenuineIntel
426 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
428 microcode : 0xb00001f
430 cache size : 56320 KB
441 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
446 address sizes : 46 bits physical, 48 bits virtual
450 vendor_id : GenuineIntel
453 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
455 microcode : 0xb00001f
457 cache size : 56320 KB
468 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
473 address sizes : 46 bits physical, 48 bits virtual
477 vendor_id : GenuineIntel
480 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
482 microcode : 0xb00001f
484 cache size : 56320 KB
495 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
500 address sizes : 46 bits physical, 48 bits virtual
504 vendor_id : GenuineIntel
507 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
509 microcode : 0xb00001f
511 cache size : 56320 KB
522 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
527 address sizes : 46 bits physical, 48 bits virtual
531 vendor_id : GenuineIntel
534 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
536 microcode : 0xb00001f
538 cache size : 56320 KB
549 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
554 address sizes : 46 bits physical, 48 bits virtual
558 socket_map = utils.parse_cpuinfo(cpuinfo)
559 assert sorted(socket_map.keys()) == [0, 1]
560 assert sorted(socket_map[0].keys()) == [0, 1, 2]
561 assert sorted(socket_map[1].keys()) == [26, 27, 28]
562 assert sorted(socket_map[0][0]) == [44]
563 assert sorted(socket_map[0][1]) == [1]
564 assert sorted(socket_map[0][2]) == [2]
565 assert sorted(socket_map[1][26]) == [85]
566 assert sorted(socket_map[1][27]) == [86]
567 assert sorted(socket_map[1][28]) == [43, 87]
569 def test_dual_socket_no_hyperthread(self):
572 vendor_id : GenuineIntel
575 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
577 microcode : 0xb00001f
579 cache size : 56320 KB
590 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
595 address sizes : 46 bits physical, 48 bits virtual
599 vendor_id : GenuineIntel
602 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
604 microcode : 0xb00001f
606 cache size : 56320 KB
617 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
622 address sizes : 46 bits physical, 48 bits virtual
626 vendor_id : GenuineIntel
629 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
631 microcode : 0xb00001f
633 cache size : 56320 KB
644 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
649 address sizes : 46 bits physical, 48 bits virtual
653 vendor_id : GenuineIntel
656 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
658 microcode : 0xb00001f
660 cache size : 56320 KB
671 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
676 address sizes : 46 bits physical, 48 bits virtual
680 vendor_id : GenuineIntel
683 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
685 microcode : 0xb00001f
687 cache size : 56320 KB
698 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
703 address sizes : 46 bits physical, 48 bits virtual
707 vendor_id : GenuineIntel
710 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
712 microcode : 0xb00001f
714 cache size : 56320 KB
725 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
730 address sizes : 46 bits physical, 48 bits virtual
734 vendor_id : GenuineIntel
737 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
739 microcode : 0xb00001f
741 cache size : 56320 KB
752 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
757 address sizes : 46 bits physical, 48 bits virtual
761 socket_map = utils.parse_cpuinfo(cpuinfo)
762 processors = socket_map.processors()
763 assert processors == [1, 2, 43, 44, 85, 86, 87]
764 cores = socket_map.cores()
765 assert cores == [0, 1, 2, 26, 27, 28]
766 sockets = socket_map.sockets()
767 assert sockets == [0, 1]
770 class ChangeObjToDictTestCase(unittest.TestCase):
772 def test_change_obj_to_dict(self):
775 self.name = 'yardstick'
778 obj_r = utils.change_obj_to_dict(obj)
779 obj_s = {'name': 'yardstick'}
780 self.assertEqual(obj_r, obj_s)
783 class SetDictValueTestCase(unittest.TestCase):
785 def test_set_dict_value(self):
789 output_dic = utils.set_dict_value(input_dic, 'welcome.to', 'yardstick')
790 self.assertEqual(output_dic.get('welcome', {}).get('to'), 'yardstick')
793 class RemoveFileTestCase(unittest.TestCase):
795 def test_remove_file(self):
797 utils.remove_file('notexistfile.txt')
798 except Exception as e:
799 self.assertTrue(isinstance(e, OSError))
802 class TestUtils(unittest.TestCase):
804 @mock.patch('yardstick.common.utils.jsonify')
805 def test_result_handler(self, mock_jsonify):
806 mock_jsonify.return_value = 432
808 self.assertEqual(utils.result_handler('x', 234), 432)
809 mock_jsonify.assert_called_once_with({'status': 'x', 'result': 234})
811 @mock.patch('random.randint')
812 @mock.patch('socket.socket')
813 def test_get_free_port(self, mock_socket, mock_randint):
814 mock_randint.return_value = 7777
815 s = mock_socket('x', 'y')
816 s.connect_ex.side_effect = iter([0, 1])
817 result = utils.get_free_port('10.20.30.40')
818 self.assertEqual(result, 7777)
819 self.assertEqual(s.connect_ex.call_count, 2)
821 @mock.patch('subprocess.check_output')
822 def test_execute_command(self, mock_check_output):
823 expected = ['hello world', '1234']
824 mock_check_output.return_value = os.linesep.join(expected)
825 result = utils.execute_command('my_command arg1 arg2')
826 self.assertEqual(result, expected)
828 @mock.patch('subprocess.Popen')
829 def test_source_env(self, mock_popen):
830 base_env = deepcopy(os.environ)
831 mock_process = mock_popen()
833 'garbage line before',
835 'garbage line after',
837 mock_process.communicate.return_value = os.linesep.join(output_list), '', 0
838 expected = {'NEW_ENV_VALUE': '234'}
839 result = utils.source_env('my_file')
840 self.assertDictEqual(result, expected)
842 os.environ.update(base_env)
844 @mock.patch('yardstick.common.utils.configparser.ConfigParser')
845 def test_parse_ini_file(self, mock_config_parser_type):
847 'default1': 'value1',
848 'default2': 'value2',
859 mock_config_parser = mock_config_parser_type()
860 mock_config_parser.read.return_value = True
861 mock_config_parser.sections.return_value = ['s1', 's2']
862 mock_config_parser.items.side_effect = iter([
873 result = utils.parse_ini_file('my_path')
874 self.assertDictEqual(result, expected)
876 @mock.patch('yardstick.common.utils.configparser.ConfigParser')
877 def test_parse_ini_file_missing_section_header(self, mock_config_parser_type):
878 mock_config_parser = mock_config_parser_type()
879 mock_config_parser.read.side_effect = \
880 configparser.MissingSectionHeaderError(mock.Mock(), 321, mock.Mock())
882 with self.assertRaises(configparser.MissingSectionHeaderError):
883 utils.parse_ini_file('my_path')
885 @mock.patch('yardstick.common.utils.configparser.ConfigParser')
886 def test_parse_ini_file_no_file(self, mock_config_parser_type):
887 mock_config_parser = mock_config_parser_type()
888 mock_config_parser.read.return_value = False
889 with self.assertRaises(RuntimeError):
890 utils.parse_ini_file('my_path')
892 @mock.patch('yardstick.common.utils.configparser.ConfigParser')
893 def test_parse_ini_file_no_default_section_header(self, mock_config_parser_type):
903 mock_config_parser = mock_config_parser_type()
904 mock_config_parser.read.return_value = True
905 mock_config_parser.sections.return_value = ['s1', 's2']
906 mock_config_parser.items.side_effect = iter([
907 configparser.NoSectionError(mock.Mock()),
917 result = utils.parse_ini_file('my_path')
918 self.assertDictEqual(result, expected)
920 def test_join_non_strings(self):
921 self.assertEqual(utils.join_non_strings(':'), '')
922 self.assertEqual(utils.join_non_strings(':', 'a'), 'a')
923 self.assertEqual(utils.join_non_strings(':', 'a', 2, 'c'), 'a:2:c')
924 self.assertEqual(utils.join_non_strings(':', ['a', 2, 'c']), 'a:2:c')
925 self.assertEqual(utils.join_non_strings(':', 'abc'), 'abc')
927 def test_validate_non_string_sequence(self):
928 self.assertEqual(utils.validate_non_string_sequence([1, 2, 3]), [1, 2, 3])
929 self.assertIsNone(utils.validate_non_string_sequence('123'))
930 self.assertIsNone(utils.validate_non_string_sequence(1))
932 self.assertEqual(utils.validate_non_string_sequence(1, 2), 2)
933 self.assertEqual(utils.validate_non_string_sequence(1, default=2), 2)
935 with self.assertRaises(RuntimeError):
936 utils.validate_non_string_sequence(1, raise_exc=RuntimeError)
938 def test_error_class(self):
939 with self.assertRaises(RuntimeError):
942 error_instance = utils.ErrorClass(test='')
943 with self.assertRaises(AttributeError):
944 error_instance.get_name()
947 class TestUtilsIpAddrMethods(unittest.TestCase):
949 GOOD_IP_V4_ADDRESS_STR_LIST = [
959 GOOD_IP_V4_MASK_STR_LIST = [
968 GOOD_IP_V6_ADDRESS_STR_LIST = [
970 u'fe80::250:56ff:fe89:91ff',
971 u'123:4567:89ab:cdef:123:4567:89ab:cdef',
974 GOOD_IP_V6_MASK_STR_LIST = [
983 INVALID_IP_ADDRESS_STR_LIST = [
987 u'123:4567:89ab:cdef:123:4567:89ab:cdef/129',
990 def test_safe_ip_address(self):
991 addr_list = self.GOOD_IP_V4_ADDRESS_STR_LIST
992 for addr in addr_list:
994 expected = ipaddress.ip_address(addr)
995 self.assertEqual(utils.safe_ip_address(addr), expected, addr)
997 def test_safe_ip_address_v6_ip(self):
998 addr_list = self.GOOD_IP_V6_ADDRESS_STR_LIST
999 for addr in addr_list:
1001 expected = ipaddress.ip_address(addr)
1002 self.assertEqual(utils.safe_ip_address(addr), expected, addr)
1004 @mock.patch("yardstick.common.utils.logging")
1005 def test_safe_ip_address_negative(self, mock_logging):
1006 for value in self.INVALID_IP_ADDRESS_STR_LIST:
1007 self.assertIsNone(utils.safe_ip_address(value), value)
1009 addr_list = self.GOOD_IP_V4_ADDRESS_STR_LIST
1010 mask_list = self.GOOD_IP_V4_MASK_STR_LIST
1011 for addr_mask_pair in product(addr_list, mask_list):
1012 value = ''.join(addr_mask_pair)
1013 self.assertIsNone(utils.safe_ip_address(value), value)
1015 addr_list = self.GOOD_IP_V6_ADDRESS_STR_LIST
1016 mask_list = self.GOOD_IP_V6_MASK_STR_LIST
1017 for addr_mask_pair in product(addr_list, mask_list):
1018 value = ''.join(addr_mask_pair)
1019 self.assertIsNone(utils.safe_ip_address(value), value)
1021 def test_get_ip_version(self):
1022 addr_list = self.GOOD_IP_V4_ADDRESS_STR_LIST
1023 for addr in addr_list:
1025 self.assertEqual(utils.get_ip_version(addr), 4, addr)
1027 def test_get_ip_version_v6_ip(self):
1028 addr_list = self.GOOD_IP_V6_ADDRESS_STR_LIST
1029 for addr in addr_list:
1031 self.assertEqual(utils.get_ip_version(addr), 6, addr)
1033 @mock.patch("yardstick.common.utils.logging")
1034 def test_get_ip_version_negative(self, mock_logging):
1035 for value in self.INVALID_IP_ADDRESS_STR_LIST:
1036 self.assertIsNone(utils.get_ip_version(value), value)
1038 addr_list = self.GOOD_IP_V4_ADDRESS_STR_LIST
1039 mask_list = self.GOOD_IP_V4_MASK_STR_LIST
1040 for addr_mask_pair in product(addr_list, mask_list):
1041 value = ''.join(addr_mask_pair)
1042 self.assertIsNone(utils.get_ip_version(value), value)
1044 addr_list = self.GOOD_IP_V6_ADDRESS_STR_LIST
1045 mask_list = self.GOOD_IP_V6_MASK_STR_LIST
1046 for addr_mask_pair in product(addr_list, mask_list):
1047 value = ''.join(addr_mask_pair)
1048 self.assertIsNone(utils.get_ip_version(value), value)
1050 def test_ip_to_hex(self):
1051 self.assertEqual(utils.ip_to_hex('0.0.0.0'), '00000000')
1052 self.assertEqual(utils.ip_to_hex('10.20.30.40'), '0a141e28')
1053 self.assertEqual(utils.ip_to_hex('127.0.0.1'), '7f000001')
1054 self.assertEqual(utils.ip_to_hex('172.31.90.100'), 'ac1f5a64')
1055 self.assertEqual(utils.ip_to_hex('192.168.254.253'), 'c0a8fefd')
1056 self.assertEqual(utils.ip_to_hex('255.255.255.255'), 'ffffffff')
1058 def test_ip_to_hex_v6_ip(self):
1059 for value in self.GOOD_IP_V6_ADDRESS_STR_LIST:
1060 self.assertEqual(utils.ip_to_hex(value), value)
1062 @mock.patch("yardstick.common.utils.logging")
1063 def test_ip_to_hex_negative(self, mock_logging):
1064 addr_list = self.GOOD_IP_V4_ADDRESS_STR_LIST
1065 mask_list = self.GOOD_IP_V4_MASK_STR_LIST
1066 value_iter = (''.join(pair) for pair in product(addr_list, mask_list))
1067 for value in chain(value_iter, self.INVALID_IP_ADDRESS_STR_LIST):
1068 self.assertEqual(utils.ip_to_hex(value), value)
1074 if __name__ == '__main__':