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 from yardstick.common import constants
25 from yardstick.common import utils
26 from yardstick.common import exceptions
27 from yardstick.tests.unit import base as ut_base
30 class IterSubclassesTestCase(ut_base.BaseUnitTestCase):
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(ut_base.BaseUnitTestCase):
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(ut_base.BaseUnitTestCase):
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(ut_base.BaseUnitTestCase):
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(ut_base.BaseUnitTestCase):
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(ut_base.BaseUnitTestCase):
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(ut_base.BaseUnitTestCase):
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(ut_base.BaseUnitTestCase):
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(ut_base.BaseUnitTestCase):
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(ut_base.BaseUnitTestCase):
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 ParseIniFileTestCase(ut_base.BaseUnitTestCase):
844 self._mock_config_parser_type = mock.patch.object(configparser,
846 self.mock_config_parser_type = self._mock_config_parser_type.start()
847 self.addCleanup(self._stop_mocks)
849 def _stop_mocks(self):
850 self._mock_config_parser_type.stop()
852 def test_parse_ini_file(self):
853 defaults = {'default1': 'value1',
854 'default2': 'value2'}
855 s1 = {'key1': 'value11',
857 s2 = {'key1': 'value123',
860 mock_config_parser = mock.Mock()
861 self.mock_config_parser_type.return_value = mock_config_parser
862 mock_config_parser.read.return_value = True
863 mock_config_parser.sections.return_value = ['s1', 's2']
864 mock_config_parser.items.side_effect = iter([
870 expected = {'DEFAULT': defaults,
873 result = utils.parse_ini_file('my_path')
874 self.assertDictEqual(expected, result)
876 @mock.patch.object(utils, 'logger')
877 def test_parse_ini_file_missing_section_header(self, *args):
878 mock_config_parser = mock.Mock()
879 self.mock_config_parser_type.return_value = mock_config_parser
880 mock_config_parser.read.side_effect = (
881 configparser.MissingSectionHeaderError(
882 mock.Mock(), 321, mock.Mock()))
884 with self.assertRaises(configparser.MissingSectionHeaderError):
885 utils.parse_ini_file('my_path')
887 def test_parse_ini_file_no_file(self):
888 mock_config_parser = mock.Mock()
889 self.mock_config_parser_type.return_value = mock_config_parser
890 mock_config_parser.read.return_value = False
891 with self.assertRaises(RuntimeError):
892 utils.parse_ini_file('my_path')
894 def test_parse_ini_file_no_default_section_header(self):
895 s1 = {'key1': 'value11',
897 s2 = {'key1': 'value123',
900 mock_config_parser = mock.Mock()
901 self.mock_config_parser_type.return_value = mock_config_parser
902 mock_config_parser.read.return_value = True
903 mock_config_parser.sections.return_value = ['s1', 's2']
904 mock_config_parser.items.side_effect = iter([
905 configparser.NoSectionError(mock.Mock()),
910 expected = {'DEFAULT': {},
913 result = utils.parse_ini_file('my_path')
914 self.assertDictEqual(expected, result)
917 class TestUtils(ut_base.BaseUnitTestCase):
919 @mock.patch('yardstick.common.utils.os.makedirs')
920 def test_makedirs(self, *_):
921 self.assertIsNone(utils.makedirs('a/b/c/d'))
923 @mock.patch('yardstick.common.utils.os.makedirs')
924 def test_makedirs_exists(self, mock_os_makedirs):
925 mock_os_makedirs.side_effect = OSError(errno.EEXIST, 'exists')
926 self.assertIsNone(utils.makedirs('a/b/c/d'))
928 @mock.patch('yardstick.common.utils.os.makedirs')
929 def test_makedirs_busy(self, mock_os_makedirs):
930 mock_os_makedirs.side_effect = OSError(errno.EBUSY, 'busy')
931 with self.assertRaises(OSError):
932 utils.makedirs('a/b/c/d')
934 @mock.patch('yardstick.common.utils.jsonify')
935 def test_result_handler(self, mock_jsonify):
936 mock_jsonify.return_value = 432
938 self.assertEqual(utils.result_handler('x', 234), 432)
939 mock_jsonify.assert_called_once_with({'status': 'x', 'result': 234})
941 @mock.patch('random.randint')
942 @mock.patch('socket.socket')
943 def test_get_free_port(self, mock_socket, mock_randint):
944 mock_randint.return_value = 7777
945 s = mock_socket('x', 'y')
946 s.connect_ex.side_effect = iter([0, 1])
947 result = utils.get_free_port('10.20.30.40')
948 self.assertEqual(result, 7777)
949 self.assertEqual(s.connect_ex.call_count, 2)
951 @mock.patch('subprocess.check_output')
952 def test_execute_command(self, mock_check_output):
953 expected = ['hello world', '1234']
954 mock_check_output.return_value = os.linesep.join(expected)
955 result = utils.execute_command('my_command arg1 arg2')
956 self.assertEqual(result, expected)
958 @mock.patch('subprocess.Popen')
959 def test_source_env(self, mock_popen):
960 base_env = deepcopy(os.environ)
961 mock_process = mock_popen()
963 'garbage line before',
965 'garbage line after',
967 mock_process.communicate.return_value = os.linesep.join(output_list), '', 0
968 expected = {'NEW_ENV_VALUE': '234'}
969 result = utils.source_env('my_file')
970 self.assertDictEqual(result, expected)
972 os.environ.update(base_env)
974 @mock.patch.object(configparser, 'ConfigParser')
975 def test_parse_ini_file(self, mock_config_parser_type):
977 'default1': 'value1',
978 'default2': 'value2',
989 mock_config_parser = mock_config_parser_type()
990 mock_config_parser.read.return_value = True
991 mock_config_parser.sections.return_value = ['s1', 's2']
992 mock_config_parser.items.side_effect = iter([
1003 result = utils.parse_ini_file('my_path')
1004 self.assertDictEqual(result, expected)
1006 @mock.patch.object(utils, 'logger')
1007 @mock.patch.object(configparser, 'ConfigParser')
1008 def test_parse_ini_file_missing_section_header(
1009 self, mock_config_parser_type, *args):
1010 mock_config_parser = mock_config_parser_type()
1011 mock_config_parser.read.side_effect = (
1012 configparser.MissingSectionHeaderError(mock.Mock(), 321,
1015 with self.assertRaises(configparser.MissingSectionHeaderError):
1016 utils.parse_ini_file('my_path')
1018 @mock.patch.object(configparser, 'ConfigParser')
1019 def test_parse_ini_file_no_file(self, mock_config_parser_type):
1020 mock_config_parser = mock_config_parser_type()
1021 mock_config_parser.read.return_value = False
1022 with self.assertRaises(RuntimeError):
1023 utils.parse_ini_file('my_path')
1025 @mock.patch.object(configparser, 'ConfigParser')
1026 def test_parse_ini_file_no_default_section_header(self, mock_config_parser_type):
1036 mock_config_parser = mock_config_parser_type()
1037 mock_config_parser.read.return_value = True
1038 mock_config_parser.sections.return_value = ['s1', 's2']
1039 mock_config_parser.items.side_effect = iter([
1040 configparser.NoSectionError(mock.Mock()),
1050 result = utils.parse_ini_file('my_path')
1051 self.assertDictEqual(result, expected)
1053 def test_join_non_strings(self):
1054 self.assertEqual(utils.join_non_strings(':'), '')
1055 self.assertEqual(utils.join_non_strings(':', 'a'), 'a')
1056 self.assertEqual(utils.join_non_strings(':', 'a', 2, 'c'), 'a:2:c')
1057 self.assertEqual(utils.join_non_strings(':', ['a', 2, 'c']), 'a:2:c')
1058 self.assertEqual(utils.join_non_strings(':', 'abc'), 'abc')
1060 def test_validate_non_string_sequence(self):
1061 self.assertEqual(utils.validate_non_string_sequence([1, 2, 3]), [1, 2, 3])
1062 self.assertIsNone(utils.validate_non_string_sequence('123'))
1063 self.assertIsNone(utils.validate_non_string_sequence(1))
1065 self.assertEqual(utils.validate_non_string_sequence(1, 2), 2)
1066 self.assertEqual(utils.validate_non_string_sequence(1, default=2), 2)
1068 with self.assertRaises(RuntimeError):
1069 utils.validate_non_string_sequence(1, raise_exc=RuntimeError)
1072 class TestUtilsIpAddrMethods(ut_base.BaseUnitTestCase):
1074 GOOD_IP_V4_ADDRESS_STR_LIST = [
1084 GOOD_IP_V4_MASK_STR_LIST = [
1093 GOOD_IP_V6_ADDRESS_STR_LIST = [
1095 u'fe80::250:56ff:fe89:91ff',
1096 u'123:4567:89ab:cdef:123:4567:89ab:cdef',
1099 GOOD_IP_V6_MASK_STR_LIST = [
1108 INVALID_IP_ADDRESS_STR_LIST = [
1112 u'123:4567:89ab:cdef:123:4567:89ab:cdef/129',
1115 def test_safe_ip_address(self):
1116 addr_list = self.GOOD_IP_V4_ADDRESS_STR_LIST
1117 for addr in addr_list:
1119 expected = ipaddress.ip_address(addr)
1120 self.assertEqual(utils.safe_ip_address(addr), expected, addr)
1122 def test_safe_ip_address_v6_ip(self):
1123 addr_list = self.GOOD_IP_V6_ADDRESS_STR_LIST
1124 for addr in addr_list:
1126 expected = ipaddress.ip_address(addr)
1127 self.assertEqual(utils.safe_ip_address(addr), expected, addr)
1129 @mock.patch("yardstick.common.utils.logging")
1130 def test_safe_ip_address_negative(self, *args):
1131 # NOTE(ralonsoh): check the calls to mocked functions.
1132 for value in self.INVALID_IP_ADDRESS_STR_LIST:
1133 self.assertIsNone(utils.safe_ip_address(value), value)
1135 addr_list = self.GOOD_IP_V4_ADDRESS_STR_LIST
1136 mask_list = self.GOOD_IP_V4_MASK_STR_LIST
1137 for addr_mask_pair in product(addr_list, mask_list):
1138 value = ''.join(addr_mask_pair)
1139 self.assertIsNone(utils.safe_ip_address(value), value)
1141 addr_list = self.GOOD_IP_V6_ADDRESS_STR_LIST
1142 mask_list = self.GOOD_IP_V6_MASK_STR_LIST
1143 for addr_mask_pair in product(addr_list, mask_list):
1144 value = ''.join(addr_mask_pair)
1145 self.assertIsNone(utils.safe_ip_address(value), value)
1147 def test_get_ip_version(self):
1148 addr_list = self.GOOD_IP_V4_ADDRESS_STR_LIST
1149 for addr in addr_list:
1151 self.assertEqual(utils.get_ip_version(addr), 4, addr)
1153 def test_get_ip_version_v6_ip(self):
1154 addr_list = self.GOOD_IP_V6_ADDRESS_STR_LIST
1155 for addr in addr_list:
1157 self.assertEqual(utils.get_ip_version(addr), 6, addr)
1159 @mock.patch("yardstick.common.utils.logging")
1160 def test_get_ip_version_negative(self, *args):
1161 # NOTE(ralonsoh): check the calls to mocked functions.
1162 for value in self.INVALID_IP_ADDRESS_STR_LIST:
1163 self.assertIsNone(utils.get_ip_version(value), value)
1165 addr_list = self.GOOD_IP_V4_ADDRESS_STR_LIST
1166 mask_list = self.GOOD_IP_V4_MASK_STR_LIST
1167 for addr_mask_pair in product(addr_list, mask_list):
1168 value = ''.join(addr_mask_pair)
1169 self.assertIsNone(utils.get_ip_version(value), value)
1171 addr_list = self.GOOD_IP_V6_ADDRESS_STR_LIST
1172 mask_list = self.GOOD_IP_V6_MASK_STR_LIST
1173 for addr_mask_pair in product(addr_list, mask_list):
1174 value = ''.join(addr_mask_pair)
1175 self.assertIsNone(utils.get_ip_version(value), value)
1177 def test_ip_to_hex(self):
1178 self.assertEqual(utils.ip_to_hex('0.0.0.0'), '00000000')
1179 self.assertEqual(utils.ip_to_hex('10.20.30.40'), '0a141e28')
1180 self.assertEqual(utils.ip_to_hex('127.0.0.1'), '7f000001')
1181 self.assertEqual(utils.ip_to_hex('172.31.90.100'), 'ac1f5a64')
1182 self.assertEqual(utils.ip_to_hex('192.168.254.253'), 'c0a8fefd')
1183 self.assertEqual(utils.ip_to_hex('255.255.255.255'), 'ffffffff')
1185 def test_ip_to_hex_v6_ip(self):
1186 for value in self.GOOD_IP_V6_ADDRESS_STR_LIST:
1187 self.assertEqual(utils.ip_to_hex(value), value)
1189 @mock.patch("yardstick.common.utils.logging")
1190 def test_ip_to_hex_negative(self, *args):
1191 # NOTE(ralonsoh): check the calls to mocked functions.
1192 addr_list = self.GOOD_IP_V4_ADDRESS_STR_LIST
1193 mask_list = self.GOOD_IP_V4_MASK_STR_LIST
1194 value_iter = (''.join(pair) for pair in product(addr_list, mask_list))
1195 for value in chain(value_iter, self.INVALID_IP_ADDRESS_STR_LIST):
1196 self.assertEqual(utils.ip_to_hex(value), value)
1199 class SafeDecodeUtf8TestCase(ut_base.BaseUnitTestCase):
1201 @unittest.skipIf(six.PY2,
1202 'This test should only be launched with Python 3.x')
1203 def test_safe_decode_utf8(self):
1204 _bytes = b'this is a byte array'
1205 out = utils.safe_decode_utf8(_bytes)
1206 self.assertIs(type(out), str)
1207 self.assertEqual('this is a byte array', out)
1210 class ReadMeminfoTestCase(ut_base.BaseUnitTestCase):
1212 MEMINFO = (b'MemTotal: 65860500 kB\n'
1213 b'MemFree: 28690900 kB\n'
1214 b'MemAvailable: 52873764 kB\n'
1215 b'Active(anon): 3015676 kB\n'
1216 b'HugePages_Total: 8\n'
1217 b'Hugepagesize: 1048576 kB')
1218 MEMINFO_DICT = {'MemTotal': '65860500',
1219 'MemFree': '28690900',
1220 'MemAvailable': '52873764',
1221 'Active(anon)': '3015676',
1222 'HugePages_Total': '8',
1223 'Hugepagesize': '1048576'}
1225 def test_read_meminfo(self):
1226 ssh_client = ssh.SSH('user', 'host')
1227 with mock.patch.object(ssh_client, 'get_file_obj') as \
1229 mock.patch.object(six, 'BytesIO',
1230 return_value=six.BytesIO(self.MEMINFO)):
1231 output = utils.read_meminfo(ssh_client)
1232 mock_get_client.assert_called_once_with('/proc/meminfo', mock.ANY)
1233 self.assertEqual(self.MEMINFO_DICT, output)
1236 class TimerTestCase(ut_base.BaseUnitTestCase):
1238 def test__getattr(self):
1239 with utils.Timer() as timer:
1241 self.assertEqual(1, round(timer.total_seconds(), 0))
1242 self.assertEqual(1, timer.delta.seconds)
1244 def test__enter_with_timeout(self):
1245 with utils.Timer(timeout=10) as timer:
1247 self.assertEqual(1, round(timer.total_seconds(), 0))
1249 def test__enter_with_timeout_exception(self):
1250 with self.assertRaises(exceptions.TimerTimeout):
1251 with utils.Timer(timeout=1):
1254 def test__enter_with_timeout_no_exception(self):
1255 with utils.Timer(timeout=1, raise_exception=False):
1258 def test__iter(self):
1260 for i in utils.Timer(timeout=2):
1261 iterations.append(i)
1263 self.assertEqual(2, len(iterations))
1266 class WaitUntilTrueTestCase(ut_base.BaseUnitTestCase):
1268 def test_no_timeout(self):
1269 self.assertIsNone(utils.wait_until_true(lambda: True,
1270 timeout=1, sleep=1))
1272 def test_timeout_generic_exception(self):
1273 with self.assertRaises(exceptions.WaitTimeout):
1274 self.assertIsNone(utils.wait_until_true(lambda: False,
1275 timeout=1, sleep=1))
1277 def test_timeout_given_exception(self):
1278 class MyTimeoutException(exceptions.YardstickException):
1279 message = 'My timeout exception'
1281 with self.assertRaises(MyTimeoutException):
1283 utils.wait_until_true(lambda: False, timeout=1, sleep=1,
1284 exception=MyTimeoutException))