1 ##############################################################################
2 # Copyright (c) 2015 Ericsson AB and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
10 from copy import deepcopy
14 from itertools import product, chain
18 from six.moves import configparser
24 from yardstick import ssh
25 from yardstick.common import constants
26 from yardstick.common import utils
27 from yardstick.common import exceptions
28 from yardstick.tests.unit import base as ut_base
31 class IterSubclassesTestCase(ut_base.BaseUnitTestCase):
32 # Disclaimer: this class is a modified copy from
33 # rally/tests/unit/common/plugin/test_discover.py
34 # Copyright 2015: Mirantis Inc.
36 def test_itersubclasses(self):
49 self.assertEqual([B, C, D], list(utils.itersubclasses(A)))
52 class ImportModulesFromPackageTestCase(ut_base.BaseUnitTestCase):
54 @mock.patch('yardstick.common.utils.os.walk')
55 def test_import_modules_from_package_no_mod(self, mock_walk):
56 yardstick_root = os.path.dirname(os.path.dirname(yardstick.__file__))
57 mock_walk.return_value = ([
58 (os.path.join(yardstick_root, 'foo'), ['bar'], ['__init__.py']),
59 (os.path.join(yardstick_root, 'foo', 'bar'), [], ['baz.txt', 'qux.rst'])
62 utils.import_modules_from_package('foo.bar')
64 @mock.patch('yardstick.common.utils.os.walk')
65 @mock.patch.object(importlib, 'import_module')
66 def test_import_modules_from_package(self, mock_import_module, mock_walk):
68 yardstick_root = os.path.dirname(os.path.dirname(yardstick.__file__))
69 mock_walk.return_value = ([
70 (os.path.join(yardstick_root, 'foo', os.pardir, 'bar'), [], ['baz.py'])
73 utils.import_modules_from_package('foo.bar')
74 mock_import_module.assert_called_once_with('bar.baz')
77 class GetParaFromYaml(ut_base.BaseUnitTestCase):
79 @mock.patch('yardstick.common.utils.os.environ.get')
80 def test_get_param_para_not_found(self, get_env):
81 file_path = 'config_sample.yaml'
82 get_env.return_value = self._get_file_abspath(file_path)
85 self.assertTrue(constants.get_param(args, default), default)
87 @mock.patch('yardstick.common.utils.os.environ.get')
88 def test_get_param_para_exists(self, get_env):
89 file_path = 'config_sample.yaml'
90 get_env.return_value = self._get_file_abspath(file_path)
92 para = '/home/opnfv/repos/releng'
93 self.assertEqual(para, constants.get_param(args))
95 def _get_file_abspath(self, filename):
96 curr_path = os.path.dirname(os.path.abspath(__file__))
97 file_path = os.path.join(curr_path, filename)
101 class CommonUtilTestCase(ut_base.BaseUnitTestCase):
122 def test__dict_key_flatten(self):
123 line = 'mpstat.loadavg1=0.29,rtt=1.03,mpstat.loadavg0=1.09,' \
124 'mpstat.cpu0.%idle=99.00,mpstat.cpu0.%sys=0.00'
125 # need to sort for assert to work
126 line = ",".join(sorted(line.split(',')))
127 flattened_data = utils.flatten_dict_key(
128 self.data['benchmark']['data'])
130 ("=".join(item) for item in sorted(flattened_data.items())))
131 self.assertEqual(result, line)
133 def test_get_key_with_default_negative(self):
134 with self.assertRaises(KeyError):
135 utils.get_key_with_default({}, 'key1')
137 @mock.patch('yardstick.common.utils.open', create=True)
138 def test_(self, mock_open):
139 mock_open.side_effect = IOError
141 with self.assertRaises(IOError):
142 utils.find_relative_file('my/path', 'task/path')
144 self.assertEqual(mock_open.call_count, 2)
146 @mock.patch('yardstick.common.utils.open', create=True)
147 def test_open_relative_path(self, mock_open):
148 mock_open_result = mock_open()
149 mock_open_call_count = 1 # initial call to get result
151 self.assertEqual(utils.open_relative_file('foo', 'bar'), mock_open_result)
153 mock_open_call_count += 1 # one more call expected
154 self.assertEqual(mock_open.call_count, mock_open_call_count)
155 self.assertIn('foo', mock_open.call_args_list[-1][0][0])
156 self.assertNotIn('bar', mock_open.call_args_list[-1][0][0])
158 def open_effect(*args, **kwargs):
159 if kwargs.get('name', args[0]) == os.path.join('bar', 'foo'):
160 return mock_open_result
161 raise IOError(errno.ENOENT, 'not found')
163 mock_open.side_effect = open_effect
164 self.assertEqual(utils.open_relative_file('foo', 'bar'), mock_open_result)
166 mock_open_call_count += 2 # two more calls expected
167 self.assertEqual(mock_open.call_count, mock_open_call_count)
168 self.assertIn('foo', mock_open.call_args_list[-1][0][0])
169 self.assertIn('bar', mock_open.call_args_list[-1][0][0])
171 # test an IOError of type ENOENT
172 mock_open.side_effect = IOError(errno.ENOENT, 'not found')
173 with self.assertRaises(IOError):
174 # the second call still raises
175 utils.open_relative_file('foo', 'bar')
177 mock_open_call_count += 2 # two more calls expected
178 self.assertEqual(mock_open.call_count, mock_open_call_count)
179 self.assertIn('foo', mock_open.call_args_list[-1][0][0])
180 self.assertIn('bar', mock_open.call_args_list[-1][0][0])
182 # test an IOError other than ENOENT
183 mock_open.side_effect = IOError(errno.EBUSY, 'busy')
184 with self.assertRaises(IOError):
185 utils.open_relative_file('foo', 'bar')
187 mock_open_call_count += 1 # one more call expected
188 self.assertEqual(mock_open.call_count, mock_open_call_count)
191 class TestMacAddressToHex(ut_base.BaseUnitTestCase):
193 def test_mac_address_to_hex_list(self):
194 self.assertEqual(utils.mac_address_to_hex_list("ea:3e:e1:9a:99:e8"),
195 ['0xea', '0x3e', '0xe1', '0x9a', '0x99', '0xe8'])
198 class TranslateToStrTestCase(ut_base.BaseUnitTestCase):
200 def test_translate_to_str_unicode(self):
202 output_str = utils.translate_to_str(input_str)
205 self.assertEqual(result, output_str)
207 def test_translate_to_str_dict_list_unicode(self):
209 u'hello': {u'hello': [u'world']}
211 output_str = utils.translate_to_str(input_str)
214 'hello': {'hello': ['world']}
216 self.assertEqual(result, output_str)
218 def test_translate_to_str_non_string(self):
219 input_value = object()
220 result = utils.translate_to_str(input_value)
221 self.assertIs(input_value, result)
224 class TestParseCpuInfo(ut_base.BaseUnitTestCase):
226 def test_single_socket_no_hyperthread(self):
229 vendor_id : GenuineIntel
232 model name : Intel Core Processor (Haswell, no TSX)
247 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
252 address sizes : 46 bits physical, 48 bits virtual
256 vendor_id : GenuineIntel
259 model name : Intel Core Processor (Haswell, no TSX)
274 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
279 address sizes : 46 bits physical, 48 bits virtual
283 vendor_id : GenuineIntel
286 model name : Intel Core Processor (Haswell, no TSX)
301 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
306 address sizes : 46 bits physical, 48 bits virtual
310 socket_map = utils.SocketTopology.parse_cpuinfo(cpuinfo)
311 self.assertEqual(sorted(socket_map.keys()), [0])
312 self.assertEqual(sorted(socket_map[0].keys()), [2, 3, 4])
314 def test_single_socket_hyperthread(self):
317 vendor_id : GenuineIntel
320 model name : Intel(R) Xeon(R) CPU E3-1275 v3 @ 3.50GHz
335 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
340 address sizes : 39 bits physical, 48 bits virtual
344 vendor_id : GenuineIntel
347 model name : Intel(R) Xeon(R) CPU E3-1275 v3 @ 3.50GHz
362 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
367 address sizes : 39 bits physical, 48 bits virtual
371 vendor_id : GenuineIntel
374 model name : Intel(R) Xeon(R) CPU E3-1275 v3 @ 3.50GHz
389 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
394 address sizes : 39 bits physical, 48 bits virtual
398 socket_map = utils.SocketTopology.parse_cpuinfo(cpuinfo)
399 self.assertEqual(sorted(socket_map.keys()), [0])
400 self.assertEqual(sorted(socket_map[0].keys()), [1, 2, 3])
401 self.assertEqual(sorted(socket_map[0][1]), [5])
402 self.assertEqual(sorted(socket_map[0][2]), [6])
403 self.assertEqual(sorted(socket_map[0][3]), [7])
405 def test_dual_socket_hyperthread(self):
408 vendor_id : GenuineIntel
411 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
413 microcode : 0xb00001f
415 cache size : 56320 KB
426 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
431 address sizes : 46 bits physical, 48 bits virtual
435 vendor_id : GenuineIntel
438 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
440 microcode : 0xb00001f
442 cache size : 56320 KB
453 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
458 address sizes : 46 bits physical, 48 bits virtual
462 vendor_id : GenuineIntel
465 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
467 microcode : 0xb00001f
469 cache size : 56320 KB
480 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
485 address sizes : 46 bits physical, 48 bits virtual
489 vendor_id : GenuineIntel
492 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
494 microcode : 0xb00001f
496 cache size : 56320 KB
507 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
512 address sizes : 46 bits physical, 48 bits virtual
516 vendor_id : GenuineIntel
519 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
521 microcode : 0xb00001f
523 cache size : 56320 KB
534 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
539 address sizes : 46 bits physical, 48 bits virtual
543 vendor_id : GenuineIntel
546 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
548 microcode : 0xb00001f
550 cache size : 56320 KB
561 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
566 address sizes : 46 bits physical, 48 bits virtual
570 vendor_id : GenuineIntel
573 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
575 microcode : 0xb00001f
577 cache size : 56320 KB
588 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
593 address sizes : 46 bits physical, 48 bits virtual
597 socket_map = utils.SocketTopology.parse_cpuinfo(cpuinfo)
598 self.assertEqual(sorted(socket_map.keys()), [0, 1])
599 self.assertEqual(sorted(socket_map[0].keys()), [0, 1, 2])
600 self.assertEqual(sorted(socket_map[1].keys()), [26, 27, 28])
601 self.assertEqual(sorted(socket_map[0][0]), [44])
602 self.assertEqual(sorted(socket_map[0][1]), [1])
603 self.assertEqual(sorted(socket_map[0][2]), [2])
604 self.assertEqual(sorted(socket_map[1][26]), [85])
605 self.assertEqual(sorted(socket_map[1][27]), [86])
606 self.assertEqual(sorted(socket_map[1][28]), [43, 87])
608 def test_dual_socket_no_hyperthread(self):
611 vendor_id : GenuineIntel
614 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
616 microcode : 0xb00001f
618 cache size : 56320 KB
629 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
634 address sizes : 46 bits physical, 48 bits virtual
638 vendor_id : GenuineIntel
641 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
643 microcode : 0xb00001f
645 cache size : 56320 KB
656 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
661 address sizes : 46 bits physical, 48 bits virtual
665 vendor_id : GenuineIntel
668 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
670 microcode : 0xb00001f
672 cache size : 56320 KB
683 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
688 address sizes : 46 bits physical, 48 bits virtual
692 vendor_id : GenuineIntel
695 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
697 microcode : 0xb00001f
699 cache size : 56320 KB
710 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
715 address sizes : 46 bits physical, 48 bits virtual
719 vendor_id : GenuineIntel
722 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
724 microcode : 0xb00001f
726 cache size : 56320 KB
737 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
742 address sizes : 46 bits physical, 48 bits virtual
746 vendor_id : GenuineIntel
749 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
751 microcode : 0xb00001f
753 cache size : 56320 KB
764 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
769 address sizes : 46 bits physical, 48 bits virtual
773 vendor_id : GenuineIntel
776 model name : Intel(R) Xeon(R) CPU E5-2699 v4 @ 2.20GHz
778 microcode : 0xb00001f
780 cache size : 56320 KB
791 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
796 address sizes : 46 bits physical, 48 bits virtual
800 socket_map = utils.SocketTopology.parse_cpuinfo(cpuinfo)
801 processors = socket_map.processors()
802 self.assertEqual(processors, [1, 2, 43, 44, 85, 86, 87])
803 cores = socket_map.cores()
804 self.assertEqual(cores, [0, 1, 2, 26, 27, 28])
805 sockets = socket_map.sockets()
806 self.assertEqual(sockets, [0, 1])
809 class ChangeObjToDictTestCase(ut_base.BaseUnitTestCase):
811 def test_change_obj_to_dict(self):
814 self.name = 'yardstick'
817 obj_r = utils.change_obj_to_dict(obj)
818 obj_s = {'name': 'yardstick'}
819 self.assertEqual(obj_r, obj_s)
822 class SetDictValueTestCase(ut_base.BaseUnitTestCase):
824 def test_set_dict_value(self):
828 output_dic = utils.set_dict_value(input_dic, 'welcome.to', 'yardstick')
829 self.assertEqual(output_dic.get('welcome', {}).get('to'), 'yardstick')
832 class RemoveFileTestCase(ut_base.BaseUnitTestCase):
834 def test_remove_file(self):
836 utils.remove_file('notexistfile.txt')
837 except Exception as e: # pylint: disable=broad-except
838 # NOTE(ralonsoh): to narrow the scope of this exception.
839 self.assertTrue(isinstance(e, OSError))
842 class ParseIniFileTestCase(ut_base.BaseUnitTestCase):
845 self._mock_config_parser_type = mock.patch.object(configparser,
847 self.mock_config_parser_type = self._mock_config_parser_type.start()
848 self.addCleanup(self._stop_mocks)
850 def _stop_mocks(self):
851 self._mock_config_parser_type.stop()
853 def test_parse_ini_file(self):
854 defaults = {'default1': 'value1',
855 'default2': 'value2'}
856 s1 = {'key1': 'value11',
858 s2 = {'key1': 'value123',
861 mock_config_parser = mock.Mock()
862 self.mock_config_parser_type.return_value = mock_config_parser
863 mock_config_parser.read.return_value = True
864 mock_config_parser.sections.return_value = ['s1', 's2']
865 mock_config_parser.items.side_effect = iter([
871 expected = {'DEFAULT': defaults,
874 result = utils.parse_ini_file('my_path')
875 self.assertDictEqual(expected, result)
877 @mock.patch.object(utils, 'logger')
878 def test_parse_ini_file_missing_section_header(self, *args):
879 mock_config_parser = mock.Mock()
880 self.mock_config_parser_type.return_value = mock_config_parser
881 mock_config_parser.read.side_effect = (
882 configparser.MissingSectionHeaderError(
883 mock.Mock(), 321, mock.Mock()))
885 with self.assertRaises(configparser.MissingSectionHeaderError):
886 utils.parse_ini_file('my_path')
888 def test_parse_ini_file_no_file(self):
889 mock_config_parser = mock.Mock()
890 self.mock_config_parser_type.return_value = mock_config_parser
891 mock_config_parser.read.return_value = False
892 with self.assertRaises(RuntimeError):
893 utils.parse_ini_file('my_path')
895 def test_parse_ini_file_no_default_section_header(self):
896 s1 = {'key1': 'value11',
898 s2 = {'key1': 'value123',
901 mock_config_parser = mock.Mock()
902 self.mock_config_parser_type.return_value = mock_config_parser
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()),
911 expected = {'DEFAULT': {},
914 result = utils.parse_ini_file('my_path')
915 self.assertDictEqual(expected, result)
918 class TestUtils(ut_base.BaseUnitTestCase):
920 @mock.patch('yardstick.common.utils.os.makedirs')
921 def test_makedirs(self, *_):
922 self.assertIsNone(utils.makedirs('a/b/c/d'))
924 @mock.patch('yardstick.common.utils.os.makedirs')
925 def test_makedirs_exists(self, mock_os_makedirs):
926 mock_os_makedirs.side_effect = OSError(errno.EEXIST, 'exists')
927 self.assertIsNone(utils.makedirs('a/b/c/d'))
929 @mock.patch('yardstick.common.utils.os.makedirs')
930 def test_makedirs_busy(self, mock_os_makedirs):
931 mock_os_makedirs.side_effect = OSError(errno.EBUSY, 'busy')
932 with self.assertRaises(OSError):
933 utils.makedirs('a/b/c/d')
935 @mock.patch('yardstick.common.utils.jsonify')
936 def test_result_handler(self, mock_jsonify):
937 mock_jsonify.return_value = 432
939 self.assertEqual(utils.result_handler('x', 234), 432)
940 mock_jsonify.assert_called_once_with({'status': 'x', 'result': 234})
942 @mock.patch('random.randint')
943 @mock.patch('socket.socket')
944 def test_get_free_port(self, mock_socket, mock_randint):
945 mock_randint.return_value = 7777
946 s = mock_socket('x', 'y')
947 s.connect_ex.side_effect = iter([0, 1])
948 result = utils.get_free_port('10.20.30.40')
949 self.assertEqual(result, 7777)
950 self.assertEqual(s.connect_ex.call_count, 2)
952 @mock.patch('subprocess.check_output')
953 def test_execute_command(self, mock_check_output):
954 expected = ['hello world', '1234']
955 mock_check_output.return_value = os.linesep.join(expected)
956 result = utils.execute_command('my_command arg1 arg2')
957 self.assertEqual(result, expected)
959 @mock.patch('subprocess.Popen')
960 def test_source_env(self, mock_popen):
961 base_env = deepcopy(os.environ)
962 mock_process = mock_popen()
964 'garbage line before',
966 'garbage line after',
968 mock_process.communicate.return_value = os.linesep.join(output_list), '', 0
969 expected = {'NEW_ENV_VALUE': '234'}
970 result = utils.source_env('my_file')
971 self.assertDictEqual(result, expected)
973 os.environ.update(base_env)
975 @mock.patch.object(configparser, 'ConfigParser')
976 def test_parse_ini_file(self, mock_config_parser_type):
978 'default1': 'value1',
979 'default2': 'value2',
990 mock_config_parser = mock_config_parser_type()
991 mock_config_parser.read.return_value = True
992 mock_config_parser.sections.return_value = ['s1', 's2']
993 mock_config_parser.items.side_effect = iter([
1000 'DEFAULT': defaults,
1004 result = utils.parse_ini_file('my_path')
1005 self.assertDictEqual(result, expected)
1007 @mock.patch.object(utils, 'logger')
1008 @mock.patch.object(configparser, 'ConfigParser')
1009 def test_parse_ini_file_missing_section_header(
1010 self, mock_config_parser_type, *args):
1011 mock_config_parser = mock_config_parser_type()
1012 mock_config_parser.read.side_effect = (
1013 configparser.MissingSectionHeaderError(mock.Mock(), 321,
1016 with self.assertRaises(configparser.MissingSectionHeaderError):
1017 utils.parse_ini_file('my_path')
1019 @mock.patch.object(configparser, 'ConfigParser')
1020 def test_parse_ini_file_no_file(self, mock_config_parser_type):
1021 mock_config_parser = mock_config_parser_type()
1022 mock_config_parser.read.return_value = False
1023 with self.assertRaises(RuntimeError):
1024 utils.parse_ini_file('my_path')
1026 @mock.patch.object(configparser, 'ConfigParser')
1027 def test_parse_ini_file_no_default_section_header(self, mock_config_parser_type):
1037 mock_config_parser = mock_config_parser_type()
1038 mock_config_parser.read.return_value = True
1039 mock_config_parser.sections.return_value = ['s1', 's2']
1040 mock_config_parser.items.side_effect = iter([
1041 configparser.NoSectionError(mock.Mock()),
1051 result = utils.parse_ini_file('my_path')
1052 self.assertDictEqual(result, expected)
1054 def test_join_non_strings(self):
1055 self.assertEqual(utils.join_non_strings(':'), '')
1056 self.assertEqual(utils.join_non_strings(':', 'a'), 'a')
1057 self.assertEqual(utils.join_non_strings(':', 'a', 2, 'c'), 'a:2:c')
1058 self.assertEqual(utils.join_non_strings(':', ['a', 2, 'c']), 'a:2:c')
1059 self.assertEqual(utils.join_non_strings(':', 'abc'), 'abc')
1061 def test_validate_non_string_sequence(self):
1062 self.assertEqual(utils.validate_non_string_sequence([1, 2, 3]), [1, 2, 3])
1063 self.assertIsNone(utils.validate_non_string_sequence('123'))
1064 self.assertIsNone(utils.validate_non_string_sequence(1))
1066 self.assertEqual(utils.validate_non_string_sequence(1, 2), 2)
1067 self.assertEqual(utils.validate_non_string_sequence(1, default=2), 2)
1069 with self.assertRaises(RuntimeError):
1070 utils.validate_non_string_sequence(1, raise_exc=RuntimeError)
1073 class TestUtilsIpAddrMethods(ut_base.BaseUnitTestCase):
1075 GOOD_IP_V4_ADDRESS_STR_LIST = [
1085 GOOD_IP_V4_MASK_STR_LIST = [
1094 GOOD_IP_V6_ADDRESS_STR_LIST = [
1096 u'fe80::250:56ff:fe89:91ff',
1097 u'123:4567:89ab:cdef:123:4567:89ab:cdef',
1100 GOOD_IP_V6_MASK_STR_LIST = [
1109 INVALID_IP_ADDRESS_STR_LIST = [
1113 u'123:4567:89ab:cdef:123:4567:89ab:cdef/129',
1116 def test_safe_ip_address(self):
1117 addr_list = self.GOOD_IP_V4_ADDRESS_STR_LIST
1118 for addr in addr_list:
1120 expected = ipaddress.ip_address(addr)
1121 self.assertEqual(utils.safe_ip_address(addr), expected, addr)
1123 def test_safe_ip_address_v6_ip(self):
1124 addr_list = self.GOOD_IP_V6_ADDRESS_STR_LIST
1125 for addr in addr_list:
1127 expected = ipaddress.ip_address(addr)
1128 self.assertEqual(utils.safe_ip_address(addr), expected, addr)
1130 @mock.patch("yardstick.common.utils.logging")
1131 def test_safe_ip_address_negative(self, *args):
1132 # NOTE(ralonsoh): check the calls to mocked functions.
1133 for value in self.INVALID_IP_ADDRESS_STR_LIST:
1134 self.assertIsNone(utils.safe_ip_address(value), value)
1136 addr_list = self.GOOD_IP_V4_ADDRESS_STR_LIST
1137 mask_list = self.GOOD_IP_V4_MASK_STR_LIST
1138 for addr_mask_pair in product(addr_list, mask_list):
1139 value = ''.join(addr_mask_pair)
1140 self.assertIsNone(utils.safe_ip_address(value), value)
1142 addr_list = self.GOOD_IP_V6_ADDRESS_STR_LIST
1143 mask_list = self.GOOD_IP_V6_MASK_STR_LIST
1144 for addr_mask_pair in product(addr_list, mask_list):
1145 value = ''.join(addr_mask_pair)
1146 self.assertIsNone(utils.safe_ip_address(value), value)
1148 def test_get_ip_version(self):
1149 addr_list = self.GOOD_IP_V4_ADDRESS_STR_LIST
1150 for addr in addr_list:
1152 self.assertEqual(utils.get_ip_version(addr), 4, addr)
1154 def test_get_ip_version_v6_ip(self):
1155 addr_list = self.GOOD_IP_V6_ADDRESS_STR_LIST
1156 for addr in addr_list:
1158 self.assertEqual(utils.get_ip_version(addr), 6, addr)
1160 @mock.patch("yardstick.common.utils.logging")
1161 def test_get_ip_version_negative(self, *args):
1162 # NOTE(ralonsoh): check the calls to mocked functions.
1163 for value in self.INVALID_IP_ADDRESS_STR_LIST:
1164 self.assertIsNone(utils.get_ip_version(value), value)
1166 addr_list = self.GOOD_IP_V4_ADDRESS_STR_LIST
1167 mask_list = self.GOOD_IP_V4_MASK_STR_LIST
1168 for addr_mask_pair in product(addr_list, mask_list):
1169 value = ''.join(addr_mask_pair)
1170 self.assertIsNone(utils.get_ip_version(value), value)
1172 addr_list = self.GOOD_IP_V6_ADDRESS_STR_LIST
1173 mask_list = self.GOOD_IP_V6_MASK_STR_LIST
1174 for addr_mask_pair in product(addr_list, mask_list):
1175 value = ''.join(addr_mask_pair)
1176 self.assertIsNone(utils.get_ip_version(value), value)
1178 def test_ip_to_hex(self):
1179 self.assertEqual(utils.ip_to_hex('0.0.0.0'), '00000000')
1180 self.assertEqual(utils.ip_to_hex('10.20.30.40'), '0a141e28')
1181 self.assertEqual(utils.ip_to_hex('127.0.0.1'), '7f000001')
1182 self.assertEqual(utils.ip_to_hex('172.31.90.100'), 'ac1f5a64')
1183 self.assertEqual(utils.ip_to_hex('192.168.254.253'), 'c0a8fefd')
1184 self.assertEqual(utils.ip_to_hex('255.255.255.255'), 'ffffffff')
1186 def test_ip_to_hex_v6_ip(self):
1187 for value in self.GOOD_IP_V6_ADDRESS_STR_LIST:
1188 self.assertEqual(utils.ip_to_hex(value), value)
1190 @mock.patch("yardstick.common.utils.logging")
1191 def test_ip_to_hex_negative(self, *args):
1192 # NOTE(ralonsoh): check the calls to mocked functions.
1193 addr_list = self.GOOD_IP_V4_ADDRESS_STR_LIST
1194 mask_list = self.GOOD_IP_V4_MASK_STR_LIST
1195 value_iter = (''.join(pair) for pair in product(addr_list, mask_list))
1196 for value in chain(value_iter, self.INVALID_IP_ADDRESS_STR_LIST):
1197 self.assertEqual(utils.ip_to_hex(value), value)
1199 def test_get_mask_from_ip_range_ipv4(self):
1201 for mask in range(8, 30):
1202 ip = ipaddress.ip_network(ip_str + '/' + str(mask), strict=False)
1203 result = utils.get_mask_from_ip_range(ip[2], ip[-2])
1204 self.assertEqual(mask, result)
1206 def test_get_mask_from_ip_range_ipv6(self):
1208 for mask in range(8, 120):
1209 ip = ipaddress.ip_network(ip_str + '/' + str(mask), strict=False)
1210 result = utils.get_mask_from_ip_range(ip[2], ip[-2])
1211 self.assertEqual(mask, result)
1214 class SafeDecodeUtf8TestCase(ut_base.BaseUnitTestCase):
1216 @unittest.skipIf(six.PY2,
1217 'This test should only be launched with Python 3.x')
1218 def test_safe_decode_utf8(self):
1219 _bytes = b'this is a byte array'
1220 out = utils.safe_decode_utf8(_bytes)
1221 self.assertIs(type(out), str)
1222 self.assertEqual('this is a byte array', out)
1225 class ReadMeminfoTestCase(ut_base.BaseUnitTestCase):
1227 MEMINFO = (b'MemTotal: 65860500 kB\n'
1228 b'MemFree: 28690900 kB\n'
1229 b'MemAvailable: 52873764 kB\n'
1230 b'Active(anon): 3015676 kB\n'
1231 b'HugePages_Total: 8\n'
1232 b'Hugepagesize: 1048576 kB')
1233 MEMINFO_DICT = {'MemTotal': '65860500',
1234 'MemFree': '28690900',
1235 'MemAvailable': '52873764',
1236 'Active(anon)': '3015676',
1237 'HugePages_Total': '8',
1238 'Hugepagesize': '1048576'}
1240 def test_read_meminfo(self):
1241 ssh_client = ssh.SSH('user', 'host')
1242 with mock.patch.object(ssh_client, 'get_file_obj') as \
1244 mock.patch.object(six, 'BytesIO',
1245 return_value=six.BytesIO(self.MEMINFO)):
1246 output = utils.read_meminfo(ssh_client)
1247 mock_get_client.assert_called_once_with('/proc/meminfo', mock.ANY)
1248 self.assertEqual(self.MEMINFO_DICT, output)
1251 class TimerTestCase(ut_base.BaseUnitTestCase):
1253 def test__getattr(self):
1254 with utils.Timer() as timer:
1256 self.assertEqual(1, round(timer.total_seconds(), 0))
1257 self.assertEqual(1, timer.delta.seconds)
1259 def test__enter_with_timeout(self):
1260 with utils.Timer(timeout=10) as timer:
1262 self.assertEqual(1, round(timer.total_seconds(), 0))
1264 def test__enter_with_timeout_exception(self):
1265 with self.assertRaises(exceptions.TimerTimeout):
1266 with utils.Timer(timeout=1):
1269 def test__enter_with_timeout_no_exception(self):
1270 with utils.Timer(timeout=1, raise_exception=False):
1273 def test__iter(self):
1275 for i in utils.Timer(timeout=2):
1276 iterations.append(i)
1278 self.assertEqual(2, len(iterations))
1281 class WaitUntilTrueTestCase(ut_base.BaseUnitTestCase):
1283 def test_no_timeout(self):
1284 self.assertIsNone(utils.wait_until_true(lambda: True,
1285 timeout=1, sleep=1))
1287 def test_timeout_generic_exception(self):
1288 with self.assertRaises(exceptions.WaitTimeout):
1289 self.assertIsNone(utils.wait_until_true(lambda: False,
1290 timeout=1, sleep=1))
1292 def test_timeout_given_exception(self):
1293 class MyTimeoutException(exceptions.YardstickException):
1294 message = 'My timeout exception'
1296 with self.assertRaises(MyTimeoutException):
1298 utils.wait_until_true(lambda: False, timeout=1, sleep=1,
1299 exception=MyTimeoutException))
1302 class SendSocketCommandTestCase(unittest.TestCase):
1304 @mock.patch.object(socket, 'socket')
1305 def test_execute_correct(self, mock_socket):
1306 mock_socket_obj = mock.Mock()
1307 mock_socket_obj.connect_ex.return_value = 0
1308 mock_socket.return_value = mock_socket_obj
1309 self.assertEqual(0, utils.send_socket_command('host', 22, 'command'))
1310 mock_socket.assert_called_once_with(socket.AF_INET, socket.SOCK_STREAM)
1311 mock_socket_obj.connect_ex.assert_called_once_with(('host', 22))
1312 mock_socket_obj.sendall.assert_called_once_with(six.b('command'))
1313 mock_socket_obj.close.assert_called_once()
1315 @mock.patch.object(socket, 'socket')
1316 def test_execute_exception(self, mock_socket):
1317 mock_socket_obj = mock.Mock()
1318 mock_socket_obj.connect_ex.return_value = 0
1319 mock_socket.return_value = mock_socket_obj
1320 mock_socket_obj.sendall.side_effect = socket.error
1321 self.assertEqual(1, utils.send_socket_command('host', 22, 'command'))
1322 mock_socket.assert_called_once_with(socket.AF_INET, socket.SOCK_STREAM)
1323 mock_socket_obj.connect_ex.assert_called_once_with(('host', 22))
1324 mock_socket_obj.sendall.assert_called_once_with(six.b('command'))
1325 mock_socket_obj.close.assert_called_once()