From: Brian Warner Date: Mon, 4 Dec 2006 04:11:26 +0000 (-0700) Subject: unit tests for vdrive X-Git-Tag: tahoe_v0.1.0-0-UNSTABLE~474 X-Git-Url: https://git.rkrishnan.org/pf/content/en/htmlfontify-example.html?a=commitdiff_plain;h=b2bfd2cf09b3e4bae0dba2ca5b70ed4ab3777a7d;p=tahoe-lafs%2Ftahoe-lafs.git unit tests for vdrive --- diff --git a/allmydata/filetable.py b/allmydata/filetable.py index 0f099209..bacae680 100644 --- a/allmydata/filetable.py +++ b/allmydata/filetable.py @@ -20,6 +20,9 @@ class MutableDirectoryNode(Referenceable): def __init__(self, basedir): self._basedir = basedir + def make_subnode(self, basedir): + return self.__class__(basedir) + def validate_name(self, name): if name == "." or name == ".." or "/" in name: raise DeadDirectoryNodeError("bad filename component") @@ -29,7 +32,7 @@ class MutableDirectoryNode(Referenceable): self.validate_name(name) absname = os.path.join(self._basedir, name) if os.path.isdir(absname): - return MutableDirectoryNode(absname) + return self.make_subnode(absname) raise DeadDirectoryNodeError("no such directory") # these are the public methods, available to anyone who holds a reference @@ -42,7 +45,7 @@ class MutableDirectoryNode(Referenceable): for name in os.listdir(self._basedir): absname = os.path.join(self._basedir, name) if os.path.isdir(absname): - results.append( (name, MutableDirectoryNode(absname)) ) + results.append( (name, self.make_subnode(absname)) ) elif os.path.isfile(absname): f = open(absname, "rb") data = f.read() @@ -61,7 +64,7 @@ class MutableDirectoryNode(Referenceable): raise BadDirectoryError("the directory '%s' already exists " "(but isn't a directory)" % name) os.mkdir(absname) - return MutableDirectoryNode(absname) + return self.make_subnode(absname) remote_add_directory = add_directory def add_file(self, name, data): diff --git a/allmydata/test/test_vdrive.py b/allmydata/test/test_vdrive.py new file mode 100644 index 00000000..20ae2102 --- /dev/null +++ b/allmydata/test/test_vdrive.py @@ -0,0 +1,69 @@ + +import os +from twisted.trial import unittest +from twisted.internet import defer +from allmydata import vdrive, filetable + +class LocalDirNode(filetable.MutableDirectoryNode): + def callRemote(self, methname, *args, **kwargs): + def _call(): + meth = getattr(self, methname) + return meth(*args, **kwargs) + return defer.maybeDeferred(_call) + + +class Traverse(unittest.TestCase): + def make_tree(self, basedir): + os.makedirs(basedir) + root = LocalDirNode(basedir) + self.d1 = d1 = root.add_directory("d1") + self.d2 = d2 = root.add_directory("d2") + root.add_file("a", "a") + root.add_file("b", "b") + d1.add_file("1.a", "1.a") + d1.add_file("1.b", "1.b") + d2.add_file("2.a", "2.a") + d2.add_file("2.b", "2.b") + return root + + def test_one(self): + basedir = "test_vdrive/one" + root = self.make_tree(basedir) + v = vdrive.VDrive() + v.set_root(root) + + d = v.get_dir("") + d.addCallback(lambda dir: self.failUnlessEqual(dir, root)) + d.addCallback(lambda res: v.get_dir("/d1")) + def _check(dir): + self.failUnless(isinstance(dir, LocalDirNode)) + self.failUnlessEqual(dir._basedir, self.d1._basedir) + d.addCallback(_check) + + + d.addCallback(lambda res: v.listdir("")) + d.addCallback(lambda files: + self.failUnlessEqual(sorted(files), + ["a", "b", "d1", "d2"])) + d.addCallback(lambda res: v.listdir("/")) + d.addCallback(lambda files: + self.failUnlessEqual(sorted(files), + ["a", "b", "d1", "d2"])) + d.addCallback(lambda res: v.listdir("d1")) + d.addCallback(lambda files: + self.failUnlessEqual(sorted(files), + ["1.a", "1.b"])) + + d.addCallback(lambda res: v.make_directory("", "d3")) + d.addCallback(lambda res: v.listdir("")) + d.addCallback(lambda files: + self.failUnlessEqual(sorted(files), + ["a", "b", "d1", "d2", "d3"])) + + d.addCallback(lambda res: v.make_directory("d2", "d2.1")) + d.addCallback(lambda res: v.listdir("/d2")) + d.addCallback(lambda files: + self.failUnlessEqual(sorted(files), + ["2.a", "2.b", "d2.1"])) + return d + diff --git a/allmydata/vdrive.py b/allmydata/vdrive.py index 0d1df3f0..06670a83 100644 --- a/allmydata/vdrive.py +++ b/allmydata/vdrive.py @@ -29,13 +29,13 @@ class VDrive(service.MultiService): def _find(table, subdir): for name,target in table: if name == subdir: - return subdir + return target else: raise KeyError("no such directory '%s' in '%s'" % (subdir, [t[0] for t in table])) d.addCallback(_find, piece) def _check(subdir): - assert not isinstance(subdir, str) + assert not isinstance(subdir, str), "Hey, %s shouldn't be a string" % subdir return subdir d.addCallback(_check) return d