From c984a09fe769ba4f15b041d64d6020cd467e5822 Mon Sep 17 00:00:00 2001 From: david-sarah Date: Fri, 5 Feb 2010 17:37:27 -0800 Subject: [PATCH] Fix race conditions and missing callback in allmydata.test.test_cli.Cp.test_copy_using_filecap, add utilities for one-liner reading and writing of files, and fix cases in test_cli where files were not being closed after writing. --- src/allmydata/test/test_cli.py | 21 ++++++++++++--------- src/allmydata/util/fileutil.py | 14 ++++++++++++++ 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/allmydata/test/test_cli.py b/src/allmydata/test/test_cli.py index 7d308248..cc909715 100644 --- a/src/allmydata/test/test_cli.py +++ b/src/allmydata/test/test_cli.py @@ -34,11 +34,11 @@ class CLI(unittest.TestCase): fileutil.rm_dir("cli/test_options") fileutil.make_dirs("cli/test_options") fileutil.make_dirs("cli/test_options/private") - open("cli/test_options/node.url","w").write("http://localhost:8080/\n") + fileutil.write("cli/test_options/node.url", "http://localhost:8080/\n") filenode_uri = uri.WriteableSSKFileURI(writekey="\x00"*16, fingerprint="\x00"*32) private_uri = uri.DirectoryURI(filenode_uri).to_string() - open("cli/test_options/private/root_dir.cap", "w").write(private_uri + "\n") + fileutil.write("cli/test_options/private/root_dir.cap", private_uri + "\n") o = cli.ListOptions() o.parseOptions(["--node-directory", "cli/test_options"]) self.failUnlessEqual(o['node-url'], "http://localhost:8080/") @@ -968,10 +968,10 @@ class Mv(GridTestMixin, CLITestMixin, unittest.TestCase): self.set_up_grid() fn1 = os.path.join(self.basedir, "file1") DATA1 = "Nuclear launch codes" - open(fn1, "wb").write(DATA1) + fileutil.write(fn1, DATA1) fn2 = os.path.join(self.basedir, "file2") DATA2 = "UML diagrams" - open(fn2, "wb").write(DATA2) + fileutil.write(fn2, DATA2) # copy both files to the grid d = self.do_cli("create-alias", "tahoe") d.addCallback(lambda res: @@ -1127,11 +1127,11 @@ class Cp(GridTestMixin, CLITestMixin, unittest.TestCase): fn1 = os.path.join(self.basedir, "Ärtonwall") DATA1 = "unicode file content" - open(fn1, "wb").write(DATA1) + fileutil.write(fn1, DATA1) fn2 = os.path.join(self.basedir, "Metallica") DATA2 = "non-unicode file content" - open(fn2, "wb").write(DATA2) + fileutil.write(fn2, DATA2) # Bug #534 # Assure that uploading a file whose name contains unicode character doesn't @@ -1171,13 +1171,14 @@ class Cp(GridTestMixin, CLITestMixin, unittest.TestCase): self.set_up_grid() outdir = os.path.join(self.basedir, "outdir") os.mkdir(outdir) - self.do_cli("create-alias", "tahoe") fn1 = os.path.join(self.basedir, "Metallica") fn2 = os.path.join(outdir, "Not Metallica") fn3 = os.path.join(outdir, "test2") DATA1 = "puppies" * 10000 - open(fn1, "wb").write(DATA1) - d = self.do_cli("put", fn1) + fileutil.write(fn1, DATA1) + + d = self.do_cli("create-alias", "tahoe") + d.addCallback(lambda ign: self.do_cli("put", fn1)) def _put_file((rc, out, err)): self.failUnlessEqual(rc, 0) # keep track of the filecap @@ -1190,6 +1191,8 @@ class Cp(GridTestMixin, CLITestMixin, unittest.TestCase): self.failUnlessEqual(rc, 0) results = open(fn2, "r").read() self.failUnlessEqual(results, DATA1) + d.addCallback(_copy_file) + # Test with ./ (see #761) # cp FILECAP localdir d.addCallback(lambda res: self.do_cli("cp", self.filecap, outdir)) diff --git a/src/allmydata/util/fileutil.py b/src/allmydata/util/fileutil.py index bc500cbd..740e5093 100644 --- a/src/allmydata/util/fileutil.py +++ b/src/allmydata/util/fileutil.py @@ -194,3 +194,17 @@ def move_into_place(source, dest): if "win32" in sys.platform.lower(): remove_if_possible(dest) os.rename(source, dest) + +def write(path, data): + wf = open(path, "wb") + try: + wf.write(data) + finally: + wf.close() + +def read(path): + rf = open(path, "rb") + try: + return rf.read() + finally: + rf.close() -- 2.45.2