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
23 from yardstick import ssh
24 import yardstick.error
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('yardstick.common.utils.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('yardstick.common.utils.configparser.ConfigParser')
931 def test_parse_ini_file_missing_section_header(self, mock_config_parser_type):
932 mock_config_parser = mock_config_parser_type()
933 mock_config_parser.read.side_effect = \
934 configparser.MissingSectionHeaderError(mock.Mock(), 321, mock.Mock())
936 with self.assertRaises(configparser.MissingSectionHeaderError):
937 utils.parse_ini_file('my_path')
939 @mock.patch('yardstick.common.utils.configparser.ConfigParser')
940 def test_parse_ini_file_no_file(self, mock_config_parser_type):
941 mock_config_parser = mock_config_parser_type()
942 mock_config_parser.read.return_value = False
943 with self.assertRaises(RuntimeError):
944 utils.parse_ini_file('my_path')
946 @mock.patch('yardstick.common.utils.configparser.ConfigParser')
947 def test_parse_ini_file_no_default_section_header(self, mock_config_parser_type):
957 mock_config_parser = mock_config_parser_type()
958 mock_config_parser.read.return_value = True
959 mock_config_parser.sections.return_value = ['s1', 's2']
960 mock_config_parser.items.side_effect = iter([
961 configparser.NoSectionError(mock.Mock()),
971 result = utils.parse_ini_file('my_path')
972 self.assertDictEqual(result, expected)
974 def test_join_non_strings(self):
975 self.assertEqual(utils.join_non_strings(':'), '')
976 self.assertEqual(utils.join_non_strings(':', 'a'), 'a')
977 self.assertEqual(utils.join_non_strings(':', 'a', 2, 'c'), 'a:2:c')
978 self.assertEqual(utils.join_non_strings(':', ['a', 2, 'c']), 'a:2:c')
979 self.assertEqual(utils.join_non_strings(':', 'abc'), 'abc')
981 def test_validate_non_string_sequence(self):
982 self.assertEqual(utils.validate_non_string_sequence([1, 2, 3]), [1, 2, 3])
983 self.assertIsNone(utils.validate_non_string_sequence('123'))
984 self.assertIsNone(utils.validate_non_string_sequence(1))
986 self.assertEqual(utils.validate_non_string_sequence(1, 2), 2)
987 self.assertEqual(utils.validate_non_string_sequence(1, default=2), 2)
989 with self.assertRaises(RuntimeError):
990 utils.validate_non_string_sequence(1, raise_exc=RuntimeError)
992 def test_error_class(self):
993 with self.assertRaises(RuntimeError):
994 yardstick.error.ErrorClass()
996 error_instance = yardstick.error.ErrorClass(test='')
997 with self.assertRaises(AttributeError):
998 error_instance.get_name()
1001 class TestUtilsIpAddrMethods(unittest.TestCase):
1003 GOOD_IP_V4_ADDRESS_STR_LIST = [
1013 GOOD_IP_V4_MASK_STR_LIST = [
1022 GOOD_IP_V6_ADDRESS_STR_LIST = [
1024 u'fe80::250:56ff:fe89:91ff',
1025 u'123:4567:89ab:cdef:123:4567:89ab:cdef',
1028 GOOD_IP_V6_MASK_STR_LIST = [
1037 INVALID_IP_ADDRESS_STR_LIST = [
1041 u'123:4567:89ab:cdef:123:4567:89ab:cdef/129',
1044 def test_safe_ip_address(self):
1045 addr_list = self.GOOD_IP_V4_ADDRESS_STR_LIST
1046 for addr in addr_list:
1048 expected = ipaddress.ip_address(addr)
1049 self.assertEqual(utils.safe_ip_address(addr), expected, addr)
1051 def test_safe_ip_address_v6_ip(self):
1052 addr_list = self.GOOD_IP_V6_ADDRESS_STR_LIST
1053 for addr in addr_list:
1055 expected = ipaddress.ip_address(addr)
1056 self.assertEqual(utils.safe_ip_address(addr), expected, addr)
1058 @mock.patch("yardstick.common.utils.logging")
1059 def test_safe_ip_address_negative(self, *args):
1060 # NOTE(ralonsoh): check the calls to mocked functions.
1061 for value in self.INVALID_IP_ADDRESS_STR_LIST:
1062 self.assertIsNone(utils.safe_ip_address(value), value)
1064 addr_list = self.GOOD_IP_V4_ADDRESS_STR_LIST
1065 mask_list = self.GOOD_IP_V4_MASK_STR_LIST
1066 for addr_mask_pair in product(addr_list, mask_list):
1067 value = ''.join(addr_mask_pair)
1068 self.assertIsNone(utils.safe_ip_address(value), value)
1070 addr_list = self.GOOD_IP_V6_ADDRESS_STR_LIST
1071 mask_list = self.GOOD_IP_V6_MASK_STR_LIST
1072 for addr_mask_pair in product(addr_list, mask_list):
1073 value = ''.join(addr_mask_pair)
1074 self.assertIsNone(utils.safe_ip_address(value), value)
1076 def test_get_ip_version(self):
1077 addr_list = self.GOOD_IP_V4_ADDRESS_STR_LIST
1078 for addr in addr_list:
1080 self.assertEqual(utils.get_ip_version(addr), 4, addr)
1082 def test_get_ip_version_v6_ip(self):
1083 addr_list = self.GOOD_IP_V6_ADDRESS_STR_LIST
1084 for addr in addr_list:
1086 self.assertEqual(utils.get_ip_version(addr), 6, addr)
1088 @mock.patch("yardstick.common.utils.logging")
1089 def test_get_ip_version_negative(self, *args):
1090 # NOTE(ralonsoh): check the calls to mocked functions.
1091 for value in self.INVALID_IP_ADDRESS_STR_LIST:
1092 self.assertIsNone(utils.get_ip_version(value), value)
1094 addr_list = self.GOOD_IP_V4_ADDRESS_STR_LIST
1095 mask_list = self.GOOD_IP_V4_MASK_STR_LIST
1096 for addr_mask_pair in product(addr_list, mask_list):
1097 value = ''.join(addr_mask_pair)
1098 self.assertIsNone(utils.get_ip_version(value), value)
1100 addr_list = self.GOOD_IP_V6_ADDRESS_STR_LIST
1101 mask_list = self.GOOD_IP_V6_MASK_STR_LIST
1102 for addr_mask_pair in product(addr_list, mask_list):
1103 value = ''.join(addr_mask_pair)
1104 self.assertIsNone(utils.get_ip_version(value), value)
1106 def test_ip_to_hex(self):
1107 self.assertEqual(utils.ip_to_hex('0.0.0.0'), '00000000')
1108 self.assertEqual(utils.ip_to_hex('10.20.30.40'), '0a141e28')
1109 self.assertEqual(utils.ip_to_hex('127.0.0.1'), '7f000001')
1110 self.assertEqual(utils.ip_to_hex('172.31.90.100'), 'ac1f5a64')
1111 self.assertEqual(utils.ip_to_hex('192.168.254.253'), 'c0a8fefd')
1112 self.assertEqual(utils.ip_to_hex('255.255.255.255'), 'ffffffff')
1114 def test_ip_to_hex_v6_ip(self):
1115 for value in self.GOOD_IP_V6_ADDRESS_STR_LIST:
1116 self.assertEqual(utils.ip_to_hex(value), value)
1118 @mock.patch("yardstick.common.utils.logging")
1119 def test_ip_to_hex_negative(self, *args):
1120 # NOTE(ralonsoh): check the calls to mocked functions.
1121 addr_list = self.GOOD_IP_V4_ADDRESS_STR_LIST
1122 mask_list = self.GOOD_IP_V4_MASK_STR_LIST
1123 value_iter = (''.join(pair) for pair in product(addr_list, mask_list))
1124 for value in chain(value_iter, self.INVALID_IP_ADDRESS_STR_LIST):
1125 self.assertEqual(utils.ip_to_hex(value), value)
1128 class SafeDecodeUtf8TestCase(unittest.TestCase):
1130 @unittest.skipIf(six.PY2,
1131 'This test should only be launched with Python 3.x')
1132 def test_safe_decode_utf8(self):
1133 _bytes = b'this is a byte array'
1134 out = utils.safe_decode_utf8(_bytes)
1135 self.assertIs(type(out), str)
1136 self.assertEqual('this is a byte array', out)
1139 class ReadMeminfoTestCase(unittest.TestCase):
1141 MEMINFO = (b'MemTotal: 65860500 kB\n'
1142 b'MemFree: 28690900 kB\n'
1143 b'MemAvailable: 52873764 kB\n'
1144 b'Active(anon): 3015676 kB\n'
1145 b'HugePages_Total: 8\n'
1146 b'Hugepagesize: 1048576 kB')
1147 MEMINFO_DICT = {'MemTotal': '65860500',
1148 'MemFree': '28690900',
1149 'MemAvailable': '52873764',
1150 'Active(anon)': '3015676',
1151 'HugePages_Total': '8',
1152 'Hugepagesize': '1048576'}
1154 def test_read_meminfo(self):
1155 ssh_client = ssh.SSH('user', 'host')
1156 with mock.patch.object(ssh_client, 'get_file_obj') as \
1158 mock.patch.object(six, 'BytesIO',
1159 return_value=six.BytesIO(self.MEMINFO)):
1160 output = utils.read_meminfo(ssh_client)
1161 mock_get_client.assert_called_once_with('/proc/meminfo', mock.ANY)
1162 self.assertEqual(self.MEMINFO_DICT, output)
1165 class TimerTestCase(unittest.TestCase):
1167 def test__getattr(self):
1168 with utils.Timer() as timer:
1170 self.assertEqual(1, round(timer.total_seconds(), 0))
1171 self.assertEqual(1, timer.delta.seconds)
1173 def test__enter_with_timeout(self):
1174 with utils.Timer(timeout=10) as timer:
1176 self.assertEqual(1, round(timer.total_seconds(), 0))
1178 def test__enter_with_timeout_exception(self):
1179 with self.assertRaises(exceptions.TimerTimeout):
1180 with utils.Timer(timeout=1):
1184 class WaitUntilTrueTestCase(unittest.TestCase):
1186 def test_no_timeout(self):
1187 self.assertIsNone(utils.wait_until_true(lambda: True,
1188 timeout=1, sleep=1))
1190 def test_timeout_generic_exception(self):
1191 with self.assertRaises(exceptions.WaitTimeout):
1192 self.assertIsNone(utils.wait_until_true(lambda: False,
1193 timeout=1, sleep=1))
1195 def test_timeout_given_exception(self):
1196 class MyTimeoutException(exceptions.YardstickException):
1197 message = 'My timeout exception'
1199 with self.assertRaises(MyTimeoutException):
1201 utils.wait_until_true(lambda: False, timeout=1, sleep=1,
1202 exception=MyTimeoutException))