close file handles opened with tempfile.mkstemp

Signed-off-by: Steven Armstrong <steven@icarus.ethz.ch>
This commit is contained in:
Steven Armstrong 2011-10-21 15:26:27 +02:00
parent 1e354387f9
commit d4e715f052
6 changed files with 17 additions and 18 deletions

View file

@ -37,6 +37,7 @@ class EmulatorTestCase(test.CdistTestCase):
os.environ = os.environ.copy() os.environ = os.environ.copy()
self.temp_dir = self.mkdtemp() self.temp_dir = self.mkdtemp()
handle, self.script = self.mkstemp(dir=self.temp_dir) handle, self.script = self.mkstemp(dir=self.temp_dir)
os.close(handle)
self.target_host = 'localhost' self.target_host = 'localhost'
out_path = self.temp_dir out_path = self.temp_dir
self.local = local.Local(self.target_host, local_base_path, out_path) self.local = local.Local(self.target_host, local_base_path, out_path)

View file

@ -90,23 +90,20 @@ class LocalTestCase(test.CdistTestCase):
def test_run_script_success(self): def test_run_script_success(self):
handle, script = self.mkstemp(dir=self.temp_dir) handle, script = self.mkstemp(dir=self.temp_dir)
fd = open(script, "w") with os.fdopen(handle, "w") as fd:
fd.writelines(["#!/bin/sh\n", "/bin/true"]) fd.writelines(["#!/bin/sh\n", "/bin/true"])
fd.close()
self.local.run_script(script) self.local.run_script(script)
def test_run_script_fail(self): def test_run_script_fail(self):
handle, script = self.mkstemp(dir=self.temp_dir) handle, script = self.mkstemp(dir=self.temp_dir)
fd = open(script, "w") with os.fdopen(handle, "w") as fd:
fd.writelines(["#!/bin/sh\n", "/bin/false"]) fd.writelines(["#!/bin/sh\n", "/bin/false"])
fd.close()
self.assertRaises(local.LocalScriptError, self.local.run_script, script) self.assertRaises(local.LocalScriptError, self.local.run_script, script)
def test_run_script_get_output(self): def test_run_script_get_output(self):
handle, script = self.mkstemp(dir=self.temp_dir) handle, script = self.mkstemp(dir=self.temp_dir)
fd = open(script, "w") with os.fdopen(handle, "w") as fd:
fd.writelines(["#!/bin/sh\n", "echo foobar"]) fd.writelines(["#!/bin/sh\n", "echo foobar"])
fd.close()
self.assertEqual(self.local.run_script(script, return_output=True), "foobar\n") self.assertEqual(self.local.run_script(script, return_output=True), "foobar\n")
def test_mkdir(self): def test_mkdir(self):

View file

@ -68,23 +68,20 @@ class RemoteTestCase(test.CdistTestCase):
def test_run_script_success(self): def test_run_script_success(self):
handle, script = self.mkstemp(dir=self.temp_dir) handle, script = self.mkstemp(dir=self.temp_dir)
fd = open(script, "w") with os.fdopen(handle, "w") as fd:
fd.writelines(["#!/bin/sh\n", "/bin/true"]) fd.writelines(["#!/bin/sh\n", "/bin/true"])
fd.close()
self.remote.run_script(script) self.remote.run_script(script)
def test_run_script_fail(self): def test_run_script_fail(self):
handle, script = self.mkstemp(dir=self.temp_dir) handle, script = self.mkstemp(dir=self.temp_dir)
fd = open(script, "w") with os.fdopen(handle, "w") as fd:
fd.writelines(["#!/bin/sh\n", "/bin/false"]) fd.writelines(["#!/bin/sh\n", "/bin/false"])
fd.close()
self.assertRaises(remote.RemoteScriptError, self.remote.run_script, script) self.assertRaises(remote.RemoteScriptError, self.remote.run_script, script)
def test_run_script_get_output(self): def test_run_script_get_output(self):
handle, script = self.mkstemp(dir=self.temp_dir) handle, script = self.mkstemp(dir=self.temp_dir)
fd = open(script, "w") with os.fdopen(handle, "w") as fd:
fd.writelines(["#!/bin/sh\n", "echo foobar"]) fd.writelines(["#!/bin/sh\n", "echo foobar"])
fd.close()
self.assertEqual(self.remote.run_script(script, return_output=True), "foobar\n") self.assertEqual(self.remote.run_script(script, return_output=True), "foobar\n")
def test_mkdir(self): def test_mkdir(self):
@ -100,6 +97,7 @@ class RemoteTestCase(test.CdistTestCase):
def test_transfer_file(self): def test_transfer_file(self):
handle, source = self.mkstemp(dir=self.temp_dir) handle, source = self.mkstemp(dir=self.temp_dir)
os.close(handle)
target = self.mkdtemp(dir=self.temp_dir) target = self.mkdtemp(dir=self.temp_dir)
self.remote.transfer(source, target) self.remote.transfer(source, target)
self.assertTrue(os.path.isfile(target)) self.assertTrue(os.path.isfile(target))
@ -108,6 +106,7 @@ class RemoteTestCase(test.CdistTestCase):
source = self.mkdtemp(dir=self.temp_dir) source = self.mkdtemp(dir=self.temp_dir)
# put a file in the directory as payload # put a file in the directory as payload
handle, source_file = self.mkstemp(dir=source) handle, source_file = self.mkstemp(dir=source)
os.close(handle)
source_file_name = os.path.split(source_file)[-1] source_file_name = os.path.split(source_file)[-1]
target = self.mkdtemp(dir=self.temp_dir) target = self.mkdtemp(dir=self.temp_dir)
self.remote.transfer(source, target) self.remote.transfer(source, target)

View file

@ -61,6 +61,7 @@ class ManifestTestCase(test.CdistTestCase):
def test_initial_manifest_environment(self): def test_initial_manifest_environment(self):
initial_manifest = os.path.join(self.local.manifest_path, "dump_environment") initial_manifest = os.path.join(self.local.manifest_path, "dump_environment")
handle, output_file = self.mkstemp(dir=self.temp_dir) handle, output_file = self.mkstemp(dir=self.temp_dir)
os.close(handle)
os.environ['__cdist_test_out'] = output_file os.environ['__cdist_test_out'] = output_file
self.manifest.run_initial_manifest(initial_manifest) 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_type = core.Type(self.local.type_path, '__dump_environment')
cdist_object = core.Object(cdist_type, self.local.object_path, 'whatever') cdist_object = core.Object(cdist_type, self.local.object_path, 'whatever')
handle, output_file = self.mkstemp(dir=self.temp_dir) handle, output_file = self.mkstemp(dir=self.temp_dir)
os.close(handle)
os.environ['__cdist_test_out'] = output_file os.environ['__cdist_test_out'] = output_file
self.manifest.run_type_manifest(cdist_object) self.manifest.run_type_manifest(cdist_object)

View file

View file