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
26 class IterSubclassesTestCase(unittest.TestCase):
27 # Disclaimer: this class is a modified copy from
28 # rally/tests/unit/common/plugin/test_discover.py
29 # Copyright 2015: Mirantis Inc.
31 def test_itersubclasses(self):
44 self.assertEqual([B, C, D], list(utils.itersubclasses(A)))
47 class TryAppendModuleTestCase(unittest.TestCase):
49 @mock.patch('yardstick.common.utils.importutils')
50 def test_try_append_module_not_in_modules(self, mock_importutils):
54 utils.try_append_module(name, modules)
55 mock_importutils.import_module.assert_called_with(name)
57 @mock.patch('yardstick.common.utils.importutils')
58 def test_try_append_module_already_in_modules(self, mock_importutils):
62 utils.try_append_module(name, modules)
63 self.assertFalse(mock_importutils.import_module.called)
66 class ImportModulesFromPackageTestCase(unittest.TestCase):
68 @mock.patch('yardstick.common.utils.os.walk')
69 @mock.patch('yardstick.common.utils.try_append_module')
70 def test_import_modules_from_package_no_mod(self, mock_append, mock_walk):
73 mock_walk.return_value = ([
74 ('..' + sep + 'foo', ['bar'], ['__init__.py']),
75 ('..' + sep + 'foo' + sep + 'bar', [], ['baz.txt', 'qux.rst'])
78 utils.import_modules_from_package('foo.bar')
79 self.assertFalse(mock_append.called)
81 @mock.patch('yardstick.common.utils.os.walk')
82 @mock.patch('yardstick.common.utils.importutils')
83 def test_import_modules_from_package(self, mock_importutils, mock_walk):
86 mock_walk.return_value = ([
87 ('foo' + sep + '..' + sep + 'bar', [], ['baz.py'])
90 utils.import_modules_from_package('foo.bar')
91 mock_importutils.import_module.assert_called_with('bar.baz')
94 class GetParaFromYaml(unittest.TestCase):
96 @mock.patch('yardstick.common.utils.os.environ.get')
97 def test_get_param_para_not_found(self, get_env):
98 file_path = 'config_sample.yaml'
99 get_env.return_value = self._get_file_abspath(file_path)
102 self.assertTrue(utils.get_param(args, default), default)
104 @mock.patch('yardstick.common.utils.os.environ.get')
105 def test_get_param_para_exists(self, get_env):
106 file_path = 'config_sample.yaml'
107 get_env.return_value = self._get_file_abspath(file_path)
109 para = '/home/opnfv/repos/releng'
110 self.assertEqual(para, utils.get_param(args))
112 def _get_file_abspath(self, filename):
113 curr_path = os.path.dirname(os.path.abspath(__file__))
114 file_path = os.path.join(curr_path, filename)
118 class CommonUtilTestCase(unittest.TestCase):
139 def test__dict_key_flatten(self):
140 line = 'mpstat.loadavg1=0.29,rtt=1.03,mpstat.loadavg0=1.09,' \
141 'mpstat.cpu0.%idle=99.00,mpstat.cpu0.%sys=0.00'
142 # need to sort for assert to work
143 line = ",".join(sorted(line.split(',')))
144 flattened_data = utils.flatten_dict_key(
145 self.data['benchmark']['data'])
147 ("=".join(item) for item in sorted(flattened_data.items())))
148 self.assertEqual(result, line)
151 class TestMacAddressToHex(unittest.TestCase):
153 def test_mac_address_to_hex_list(self):
154 self.assertEqual(utils.mac_address_to_hex_list("ea:3e:e1:9a:99:e8"),
155 ['0xea', '0x3e', '0xe1', '0x9a', '0x99', '0xe8'])
158 class TranslateToStrTestCase(unittest.TestCase):
160 def test_translate_to_str_unicode(self):
162 output_str = utils.translate_to_str(input_str)
165 self.assertEqual(result, output_str)
167 def test_translate_to_str_dict_list_unicode(self):
169 u'hello': {u'hello': [u'world']}
171 output_str = utils.translate_to_str(input_str)
174 'hello': {'hello': ['world']}
176 self.assertEqual(result, output_str)
178 def test_translate_to_str_non_string(self):
179 input_value = object()
180 result = utils.translate_to_str(input_value)
181 self.assertIs(input_value, result)
184 class TestParseCpuInfo(unittest.TestCase):
186 def test_single_socket_no_hyperthread(self):
189 vendor_id : GenuineIntel
192 model name : Intel Core Processor (Haswell, no TSX)
207 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
212 address sizes : 46 bits physical, 48 bits virtual
216 vendor_id : GenuineIntel
219 model name : Intel Core Processor (Haswell, no TSX)
234 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
239 address sizes : 46 bits physical, 48 bits virtual
243 vendor_id : GenuineIntel
246 model name : Intel Core Processor (Haswell, no TSX)
261 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
266 address sizes : 46 bits physical, 48 bits virtual
270 socket_map = utils.parse_cpuinfo(cpuinfo)
271 assert sorted(socket_map.keys()) == [0]
272 assert sorted(socket_map[0].keys()) == [2, 3, 4]
274 def test_single_socket_hyperthread(self):
277 vendor_id : GenuineIntel
280 model name : Intel(R) Xeon(R) CPU E3-1275 v3 @ 3.50GHz
295 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
300 address sizes : 39 bits physical, 48 bits virtual
304 vendor_id : GenuineIntel
307 model name : Intel(R) Xeon(R) CPU E3-1275 v3 @ 3.50GHz
322 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
327 address sizes : 39 bits physical, 48 bits virtual
331 vendor_id : GenuineIntel
334 model name : Intel(R) Xeon(R) CPU E3-1275 v3 @ 3.50GHz
349 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
354 address sizes : 39 bits physical, 48 bits virtual
358 socket_map = utils.parse_cpuinfo(cpuinfo)
359 assert sorted(socket_map.keys()) == [0]
360 assert sorted(socket_map[0].keys()) == [1, 2, 3]
361 assert sorted(socket_map[0][1]) == [5]
362 assert sorted(socket_map[0][2]) == [6]
363 assert sorted(socket_map[0][3]) == [7]
365 def test_dual_socket_hyperthread(self):
368 vendor_id : GenuineIntel
371 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
373 microcode : 0xb00001f
375 cache size : 56320 KB
386 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
391 address sizes : 46 bits physical, 48 bits virtual
395 vendor_id : GenuineIntel
398 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
400 microcode : 0xb00001f
402 cache size : 56320 KB
413 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
418 address sizes : 46 bits physical, 48 bits virtual
422 vendor_id : GenuineIntel
425 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
427 microcode : 0xb00001f
429 cache size : 56320 KB
440 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
445 address sizes : 46 bits physical, 48 bits virtual
449 vendor_id : GenuineIntel
452 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
454 microcode : 0xb00001f
456 cache size : 56320 KB
467 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
472 address sizes : 46 bits physical, 48 bits virtual
476 vendor_id : GenuineIntel
479 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
481 microcode : 0xb00001f
483 cache size : 56320 KB
494 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
499 address sizes : 46 bits physical, 48 bits virtual
503 vendor_id : GenuineIntel
506 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
508 microcode : 0xb00001f
510 cache size : 56320 KB
521 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
526 address sizes : 46 bits physical, 48 bits virtual
530 vendor_id : GenuineIntel
533 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
535 microcode : 0xb00001f
537 cache size : 56320 KB
548 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
553 address sizes : 46 bits physical, 48 bits virtual
557 socket_map = utils.parse_cpuinfo(cpuinfo)
558 assert sorted(socket_map.keys()) == [0, 1]
559 assert sorted(socket_map[0].keys()) == [0, 1, 2]
560 assert sorted(socket_map[1].keys()) == [26, 27, 28]
561 assert sorted(socket_map[0][0]) == [44]
562 assert sorted(socket_map[0][1]) == [1]
563 assert sorted(socket_map[0][2]) == [2]
564 assert sorted(socket_map[1][26]) == [85]
565 assert sorted(socket_map[1][27]) == [86]
566 assert sorted(socket_map[1][28]) == [43, 87]
568 def test_dual_socket_no_hyperthread(self):
571 vendor_id : GenuineIntel
574 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
576 microcode : 0xb00001f
578 cache size : 56320 KB
589 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
594 address sizes : 46 bits physical, 48 bits virtual
598 vendor_id : GenuineIntel
601 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
603 microcode : 0xb00001f
605 cache size : 56320 KB
616 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
621 address sizes : 46 bits physical, 48 bits virtual
625 vendor_id : GenuineIntel
628 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
630 microcode : 0xb00001f
632 cache size : 56320 KB
643 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
648 address sizes : 46 bits physical, 48 bits virtual
652 vendor_id : GenuineIntel
655 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
657 microcode : 0xb00001f
659 cache size : 56320 KB
670 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
675 address sizes : 46 bits physical, 48 bits virtual
679 vendor_id : GenuineIntel
682 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
684 microcode : 0xb00001f
686 cache size : 56320 KB
697 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
702 address sizes : 46 bits physical, 48 bits virtual
706 vendor_id : GenuineIntel
709 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
711 microcode : 0xb00001f
713 cache size : 56320 KB
724 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
729 address sizes : 46 bits physical, 48 bits virtual
733 vendor_id : GenuineIntel
736 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
738 microcode : 0xb00001f
740 cache size : 56320 KB
751 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
756 address sizes : 46 bits physical, 48 bits virtual
760 socket_map = utils.parse_cpuinfo(cpuinfo)
761 processors = socket_map.processors()
762 assert processors == [1, 2, 43, 44, 85, 86, 87]
763 cores = socket_map.cores()
764 assert cores == [0, 1, 2, 26, 27, 28]
765 sockets = socket_map.sockets()
766 assert sockets == [0, 1]
769 class ChangeObjToDictTestCase(unittest.TestCase):
771 def test_change_obj_to_dict(self):
774 self.name = 'yardstick'
777 obj_r = utils.change_obj_to_dict(obj)
778 obj_s = {'name': 'yardstick'}
779 self.assertEqual(obj_r, obj_s)
782 class SetDictValueTestCase(unittest.TestCase):
784 def test_set_dict_value(self):
788 output_dic = utils.set_dict_value(input_dic, 'welcome.to', 'yardstick')
789 self.assertEqual(output_dic.get('welcome', {}).get('to'), 'yardstick')
792 class RemoveFileTestCase(unittest.TestCase):
794 def test_remove_file(self):
796 utils.remove_file('notexistfile.txt')
797 except Exception as e:
798 self.assertTrue(isinstance(e, OSError))
801 class TestUtils(unittest.TestCase):
803 @mock.patch('yardstick.common.utils.jsonify')
804 def test_result_handler(self, mock_jsonify):
805 mock_jsonify.return_value = 432
807 self.assertEqual(utils.result_handler('x', 234), 432)
808 mock_jsonify.assert_called_once_with({'status': 'x', 'result': 234})
810 @mock.patch('random.randint')
811 @mock.patch('socket.socket')
812 def test_get_free_port(self, mock_socket, mock_randint):
813 mock_randint.return_value = 7777
814 s = mock_socket('x', 'y')
815 s.connect_ex.side_effect = iter([0, 1])
816 result = utils.get_free_port('10.20.30.40')
817 self.assertEqual(result, 7777)
818 self.assertEqual(s.connect_ex.call_count, 2)
820 @mock.patch('subprocess.check_output')
821 def test_execute_command(self, mock_check_output):
822 expected = ['hello world', '1234']
823 mock_check_output.return_value = os.linesep.join(expected)
824 result = utils.execute_command('my_command arg1 arg2')
825 self.assertEqual(result, expected)
827 @mock.patch('subprocess.Popen')
828 def test_source_env(self, mock_popen):
829 base_env = deepcopy(os.environ)
830 mock_process = mock_popen()
832 'garbage line before',
834 'garbage line after',
836 mock_process.communicate.return_value = os.linesep.join(output_list), '', 0
837 expected = {'NEW_ENV_VALUE': '234'}
838 result = utils.source_env('my_file')
839 self.assertDictEqual(result, expected)
841 os.environ.update(base_env)
843 @mock.patch('yardstick.common.utils.configparser.ConfigParser')
844 def test_parse_ini_file(self, mock_config_parser_type):
846 'default1': 'value1',
847 'default2': 'value2',
858 mock_config_parser = mock_config_parser_type()
859 mock_config_parser.read.return_value = True
860 mock_config_parser.sections.return_value = ['s1', 's2']
861 mock_config_parser.items.side_effect = iter([
872 result = utils.parse_ini_file('my_path')
873 self.assertDictEqual(result, expected)
875 @mock.patch('yardstick.common.utils.configparser.ConfigParser')
876 def test_parse_ini_file_missing_section_header(self, mock_config_parser_type):
877 mock_config_parser = mock_config_parser_type()
878 mock_config_parser.read.side_effect = \
879 configparser.MissingSectionHeaderError(mock.Mock(), 321, mock.Mock())
881 with self.assertRaises(configparser.MissingSectionHeaderError):
882 utils.parse_ini_file('my_path')
884 @mock.patch('yardstick.common.utils.configparser.ConfigParser')
885 def test_parse_ini_file_no_file(self, mock_config_parser_type):
886 mock_config_parser = mock_config_parser_type()
887 mock_config_parser.read.return_value = False
888 with self.assertRaises(RuntimeError):
889 utils.parse_ini_file('my_path')
891 @mock.patch('yardstick.common.utils.configparser.ConfigParser')
892 def test_parse_ini_file_no_default_section_header(self, mock_config_parser_type):
902 mock_config_parser = mock_config_parser_type()
903 mock_config_parser.read.return_value = True
904 mock_config_parser.sections.return_value = ['s1', 's2']
905 mock_config_parser.items.side_effect = iter([
906 configparser.NoSectionError(mock.Mock()),
916 result = utils.parse_ini_file('my_path')
917 self.assertDictEqual(result, expected)
920 class TestUtilsIpAddrMethods(unittest.TestCase):
922 GOOD_IP_V4_ADDRESS_STR_LIST = [
932 GOOD_IP_V4_MASK_STR_LIST = [
941 GOOD_IP_V6_ADDRESS_STR_LIST = [
943 u'fe80::250:56ff:fe89:91ff',
944 u'123:4567:89ab:cdef:123:4567:89ab:cdef',
947 GOOD_IP_V6_MASK_STR_LIST = [
956 INVALID_IP_ADDRESS_STR_LIST = [
960 u'123:4567:89ab:cdef:123:4567:89ab:cdef/129',
963 def test_safe_ip_address(self):
964 addr_list = self.GOOD_IP_V4_ADDRESS_STR_LIST
965 for addr in addr_list:
967 expected = ipaddress.ip_address(addr)
968 self.assertEqual(utils.safe_ip_address(addr), expected, addr)
970 def test_safe_ip_address_v6_ip(self):
971 addr_list = self.GOOD_IP_V6_ADDRESS_STR_LIST
972 for addr in addr_list:
974 expected = ipaddress.ip_address(addr)
975 self.assertEqual(utils.safe_ip_address(addr), expected, addr)
977 @mock.patch("yardstick.common.utils.logging")
978 def test_safe_ip_address_negative(self, mock_logging):
979 for value in self.INVALID_IP_ADDRESS_STR_LIST:
980 self.assertIsNone(utils.safe_ip_address(value), value)
982 addr_list = self.GOOD_IP_V4_ADDRESS_STR_LIST
983 mask_list = self.GOOD_IP_V4_MASK_STR_LIST
984 for addr_mask_pair in product(addr_list, mask_list):
985 value = ''.join(addr_mask_pair)
986 self.assertIsNone(utils.safe_ip_address(value), value)
988 addr_list = self.GOOD_IP_V6_ADDRESS_STR_LIST
989 mask_list = self.GOOD_IP_V6_MASK_STR_LIST
990 for addr_mask_pair in product(addr_list, mask_list):
991 value = ''.join(addr_mask_pair)
992 self.assertIsNone(utils.safe_ip_address(value), value)
994 def test_get_ip_version(self):
995 addr_list = self.GOOD_IP_V4_ADDRESS_STR_LIST
996 for addr in addr_list:
998 self.assertEqual(utils.get_ip_version(addr), 4, addr)
1000 def test_get_ip_version_v6_ip(self):
1001 addr_list = self.GOOD_IP_V6_ADDRESS_STR_LIST
1002 for addr in addr_list:
1004 self.assertEqual(utils.get_ip_version(addr), 6, addr)
1006 @mock.patch("yardstick.common.utils.logging")
1007 def test_get_ip_version_negative(self, mock_logging):
1008 for value in self.INVALID_IP_ADDRESS_STR_LIST:
1009 self.assertIsNone(utils.get_ip_version(value), value)
1011 addr_list = self.GOOD_IP_V4_ADDRESS_STR_LIST
1012 mask_list = self.GOOD_IP_V4_MASK_STR_LIST
1013 for addr_mask_pair in product(addr_list, mask_list):
1014 value = ''.join(addr_mask_pair)
1015 self.assertIsNone(utils.get_ip_version(value), value)
1017 addr_list = self.GOOD_IP_V6_ADDRESS_STR_LIST
1018 mask_list = self.GOOD_IP_V6_MASK_STR_LIST
1019 for addr_mask_pair in product(addr_list, mask_list):
1020 value = ''.join(addr_mask_pair)
1021 self.assertIsNone(utils.get_ip_version(value), value)
1023 def test_ip_to_hex(self):
1024 self.assertEqual(utils.ip_to_hex('0.0.0.0'), '00000000')
1025 self.assertEqual(utils.ip_to_hex('10.20.30.40'), '0a141e28')
1026 self.assertEqual(utils.ip_to_hex('127.0.0.1'), '7f000001')
1027 self.assertEqual(utils.ip_to_hex('172.31.90.100'), 'ac1f5a64')
1028 self.assertEqual(utils.ip_to_hex('192.168.254.253'), 'c0a8fefd')
1029 self.assertEqual(utils.ip_to_hex('255.255.255.255'), 'ffffffff')
1031 def test_ip_to_hex_v6_ip(self):
1032 for value in self.GOOD_IP_V6_ADDRESS_STR_LIST:
1033 self.assertEqual(utils.ip_to_hex(value), value)
1035 @mock.patch("yardstick.common.utils.logging")
1036 def test_ip_to_hex_negative(self, mock_logging):
1037 addr_list = self.GOOD_IP_V4_ADDRESS_STR_LIST
1038 mask_list = self.GOOD_IP_V4_MASK_STR_LIST
1039 value_iter = (''.join(pair) for pair in product(addr_list, mask_list))
1040 for value in chain(value_iter, self.INVALID_IP_ADDRESS_STR_LIST):
1041 self.assertEqual(utils.ip_to_hex(value), value)
1047 if __name__ == '__main__':