From d4e715f0520c8699fe5bebc8cf85d7c6a5830a98 Mon Sep 17 00:00:00 2001 From: Steven Armstrong Date: Fri, 21 Oct 2011 15:26:27 +0200 Subject: [PATCH] close file handles opened with tempfile.mkstemp Signed-off-by: Steven Armstrong --- lib/cdist/test/emulator/__init__.py | 1 + lib/cdist/test/exec/local.py | 15 ++++++--------- lib/cdist/test/exec/remote.py | 17 ++++++++--------- lib/cdist/test/manifest/__init__.py | 2 ++ lib/cdist/test/util/__init__.py | 0 lib/cdist/test/util/fsproperty.py | 0 6 files changed, 17 insertions(+), 18 deletions(-) create mode 100644 lib/cdist/test/util/__init__.py create mode 100644 lib/cdist/test/util/fsproperty.py diff --git a/lib/cdist/test/emulator/__init__.py b/lib/cdist/test/emulator/__init__.py index 386969e5..91150334 100644 --- a/lib/cdist/test/emulator/__init__.py +++ b/lib/cdist/test/emulator/__init__.py @@ -37,6 +37,7 @@ class EmulatorTestCase(test.CdistTestCase): os.environ = os.environ.copy() self.temp_dir = self.mkdtemp() handle, self.script = self.mkstemp(dir=self.temp_dir) + os.close(handle) self.target_host = 'localhost' out_path = self.temp_dir self.local = local.Local(self.target_host, local_base_path, out_path) diff --git a/lib/cdist/test/exec/local.py b/lib/cdist/test/exec/local.py index d73aa079..e6f2c2b0 100644 --- a/lib/cdist/test/exec/local.py +++ b/lib/cdist/test/exec/local.py @@ -90,23 +90,20 @@ class LocalTestCase(test.CdistTestCase): def test_run_script_success(self): handle, script = self.mkstemp(dir=self.temp_dir) - fd = open(script, "w") - fd.writelines(["#!/bin/sh\n", "/bin/true"]) - fd.close() + with os.fdopen(handle, "w") as fd: + fd.writelines(["#!/bin/sh\n", "/bin/true"]) self.local.run_script(script) def test_run_script_fail(self): handle, script = self.mkstemp(dir=self.temp_dir) - fd = open(script, "w") - fd.writelines(["#!/bin/sh\n", "/bin/false"]) - fd.close() + with os.fdopen(handle, "w") as fd: + fd.writelines(["#!/bin/sh\n", "/bin/false"]) self.assertRaises(local.LocalScriptError, self.local.run_script, script) def test_run_script_get_output(self): handle, script = self.mkstemp(dir=self.temp_dir) - fd = open(script, "w") - fd.writelines(["#!/bin/sh\n", "echo foobar"]) - fd.close() + with os.fdopen(handle, "w") as fd: + fd.writelines(["#!/bin/sh\n", "echo foobar"]) self.assertEqual(self.local.run_script(script, return_output=True), "foobar\n") def test_mkdir(self): diff --git a/lib/cdist/test/exec/remote.py b/lib/cdist/test/exec/remote.py index 92b68d06..e111a302 100644 --- a/lib/cdist/test/exec/remote.py +++ b/lib/cdist/test/exec/remote.py @@ -68,23 +68,20 @@ class RemoteTestCase(test.CdistTestCase): def test_run_script_success(self): handle, script = self.mkstemp(dir=self.temp_dir) - fd = open(script, "w") - fd.writelines(["#!/bin/sh\n", "/bin/true"]) - fd.close() + with os.fdopen(handle, "w") as fd: + fd.writelines(["#!/bin/sh\n", "/bin/true"]) self.remote.run_script(script) def test_run_script_fail(self): handle, script = self.mkstemp(dir=self.temp_dir) - fd = open(script, "w") - fd.writelines(["#!/bin/sh\n", "/bin/false"]) - fd.close() + with os.fdopen(handle, "w") as fd: + fd.writelines(["#!/bin/sh\n", "/bin/false"]) self.assertRaises(remote.RemoteScriptError, self.remote.run_script, script) def test_run_script_get_output(self): handle, script = self.mkstemp(dir=self.temp_dir) - fd = open(script, "w") - fd.writelines(["#!/bin/sh\n", "echo foobar"]) - fd.close() + with os.fdopen(handle, "w") as fd: + fd.writelines(["#!/bin/sh\n", "echo foobar"]) self.assertEqual(self.remote.run_script(script, return_output=True), "foobar\n") def test_mkdir(self): @@ -100,6 +97,7 @@ class RemoteTestCase(test.CdistTestCase): def test_transfer_file(self): handle, source = self.mkstemp(dir=self.temp_dir) + os.close(handle) target = self.mkdtemp(dir=self.temp_dir) self.remote.transfer(source, target) self.assertTrue(os.path.isfile(target)) @@ -108,6 +106,7 @@ class RemoteTestCase(test.CdistTestCase): source = self.mkdtemp(dir=self.temp_dir) # put a file in the directory as payload handle, source_file = self.mkstemp(dir=source) + os.close(handle) source_file_name = os.path.split(source_file)[-1] target = self.mkdtemp(dir=self.temp_dir) self.remote.transfer(source, target) diff --git a/lib/cdist/test/manifest/__init__.py b/lib/cdist/test/manifest/__init__.py index 00789aec..2383adf7 100644 --- a/lib/cdist/test/manifest/__init__.py +++ b/lib/cdist/test/manifest/__init__.py @@ -61,6 +61,7 @@ class ManifestTestCase(test.CdistTestCase): def test_initial_manifest_environment(self): initial_manifest = os.path.join(self.local.manifest_path, "dump_environment") handle, output_file = self.mkstemp(dir=self.temp_dir) + os.close(handle) os.environ['__cdist_test_out'] = output_file self.manifest.run_initial_manifest(initial_manifest) @@ -81,6 +82,7 @@ class ManifestTestCase(test.CdistTestCase): cdist_type = core.Type(self.local.type_path, '__dump_environment') cdist_object = core.Object(cdist_type, self.local.object_path, 'whatever') handle, output_file = self.mkstemp(dir=self.temp_dir) + os.close(handle) os.environ['__cdist_test_out'] = output_file self.manifest.run_type_manifest(cdist_object) diff --git a/lib/cdist/test/util/__init__.py b/lib/cdist/test/util/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/lib/cdist/test/util/fsproperty.py b/lib/cdist/test/util/fsproperty.py new file mode 100644 index 00000000..e69de29b