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()
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)

View file

@ -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):

View file

@ -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)

View file

@ -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)

View file

View file