From: Jelte Jansen Date: Thu, 9 Aug 2012 12:34:28 +0000 (+0200) Subject: [2172] save and restore syscalls in setup/teardown X-Git-Tag: trac2351_base~65^2~34^2~2^2^2~10 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=cf59b08af0e9db877300b8dd81fb3679d4dc2b93;p=thirdparty%2Fkea.git [2172] save and restore syscalls in setup/teardown --- diff --git a/src/lib/python/isc/sysinfo/tests/sysinfo_test.py b/src/lib/python/isc/sysinfo/tests/sysinfo_test.py index a7fa365bcb..d9e3cc680c 100644 --- a/src/lib/python/isc/sysinfo/tests/sysinfo_test.py +++ b/src/lib/python/isc/sysinfo/tests/sysinfo_test.py @@ -226,6 +226,22 @@ def _my_osx_subprocess_check_output(command): assert False, 'Unhandled command' class SysInfoTest(unittest.TestCase): + + def setUp(self): + # Save existing implementations of library functions + # (they are replaced in the tests) + self.old_platform_system = platform.system + self.old_os_sysconf = os.sysconf + self.old_open = __builtins__.open + self.old_subprocess_check_output = subprocess.check_output + + def tearDown(self): + # Restore the library functions + platform.system = self.old_platform_system + os.sysconf = self.old_os_sysconf + __builtins__.open = self.old_open + subprocess.check_output = self.old_subprocess_check_output + def test_sysinfo(self): """Test that the various methods on SysInfo exist and return data.""" @@ -287,15 +303,11 @@ class SysInfoTest(unittest.TestCase): tests deep into the implementation, and not just the interfaces.""" - # Save and replace existing implementations of library functions + # Replace existing implementations of library functions # with mock ones for testing. - old_platform_system = platform.system platform.system = _my_linux_platform_system - old_os_sysconf = os.sysconf os.sysconf = _my_linux_os_sysconf - old_open = __builtins__.open __builtins__.open = _my_linux_open - old_subprocess_check_output = subprocess.check_output subprocess.check_output = _my_linux_subprocess_check_output s = SysInfoFromFactory() @@ -320,26 +332,16 @@ class SysInfoTest(unittest.TestCase): self.assertEqual('osuxbrcc1g9VgaF4yf3FrtfodrfATrbSnjhqhuQSAs8=\n', s.get_net_stats()) self.assertEqual('Z+w0lwa02/T+5+EIio84rrst/Dtizoz/aL9Im7J7ESA=\n', s.get_net_connections()) - # Restore original implementations. - platform.system = old_platform_system - os.sysconf = old_os_sysconf - __builtins__.open = old_open - subprocess.check_output = old_subprocess_check_output - def test_sysinfo_openbsd(self): """Tests the OpenBSD implementation of SysInfo. Note that this tests deep into the implementation, and not just the interfaces.""" - # Save and replace existing implementations of library functions + # Replace existing implementations of library functions # with mock ones for testing. - old_platform_system = platform.system platform.system = _my_openbsd_platform_system - old_os_sysconf = os.sysconf os.sysconf = _my_openbsd_os_sysconf - old_subprocess_check_output = subprocess.check_output subprocess.check_output = _my_openbsd_subprocess_check_output - old_os_uname = os.uname os.uname = _my_openbsd_platform_uname s = SysInfoFromFactory() @@ -370,25 +372,16 @@ class SysInfoTest(unittest.TestCase): self.assertEqual('osuxbrcc1g9VgaF4yf3FrtfodrfATrbSnjhqhuQSAs8=\n', s.get_net_stats()) self.assertEqual('Z+w0lwa02/T+5+EIio84rrst/Dtizoz/aL9Im7J7ESA=\n', s.get_net_connections()) - # Restore original implementations. - platform.system = old_platform_system - os.sysconf = old_os_sysconf - subprocess.check_output = old_subprocess_check_output - def test_sysinfo_freebsd(self): """Tests the FreeBSD implementation of SysInfo. Note that this tests deep into the implementation, and not just the interfaces.""" - # Save and replace existing implementations of library functions + # Replace existing implementations of library functions # with mock ones for testing. - old_platform_system = platform.system platform.system = _my_freebsd_platform_system - old_os_sysconf = os.sysconf os.sysconf = _my_freebsd_os_sysconf - old_subprocess_check_output = subprocess.check_output subprocess.check_output = _my_freebsd_subprocess_check_output - old_os_uname = os.uname os.uname = _my_freebsd_platform_uname s = SysInfoFromFactory() @@ -419,25 +412,16 @@ class SysInfoTest(unittest.TestCase): self.assertEqual('osuxbrcc1g9VgaF4yf3FrtfodrfATrbSnjhqhuQSAs8=\n', s.get_net_stats()) self.assertEqual('Z+w0lwa02/T+5+EIio84rrst/Dtizoz/aL9Im7J7ESA=\n', s.get_net_connections()) - # Restore original implementations. - platform.system = old_platform_system - os.sysconf = old_os_sysconf - subprocess.check_output = old_subprocess_check_output - def test_sysinfo_osx(self): """Tests the OS X implementation of SysInfo. Note that this tests deep into the implementation, and not just the interfaces.""" - # Save and replace existing implementations of library functions + # Replace existing implementations of library functions # with mock ones for testing. - old_platform_system = platform.system platform.system = _my_osx_platform_system - old_os_sysconf = os.sysconf os.sysconf = _my_osx_os_sysconf - old_subprocess_check_output = subprocess.check_output subprocess.check_output = _my_osx_subprocess_check_output - old_os_uname = os.uname os.uname = _my_osx_platform_uname s = SysInfoFromFactory() @@ -468,10 +452,5 @@ class SysInfoTest(unittest.TestCase): self.assertEqual('osuxbrcc1g9VgaF4yf3FrtfodrfATrbSnjhqhuQSAs8=\n', s.get_net_stats()) self.assertEqual('Z+w0lwa02/T+5+EIio84rrst/Dtizoz/aL9Im7J7ESA=\n', s.get_net_connections()) - # Restore original implementations. - platform.system = old_platform_system - os.sysconf = old_os_sysconf - subprocess.check_output = old_subprocess_check_output - if __name__ == "__main__": unittest.main()