]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/commitdiff
unit tests for vdrive
authorBrian Warner <warner@lothar.com>
Mon, 4 Dec 2006 04:11:26 +0000 (21:11 -0700)
committerBrian Warner <warner@lothar.com>
Mon, 4 Dec 2006 04:11:26 +0000 (21:11 -0700)
allmydata/filetable.py
allmydata/test/test_vdrive.py [new file with mode: 0644]
allmydata/vdrive.py

index 0f0992097f8155099e3bd502cb05cf6e944c43e0..bacae6803b969157236d50a862966d920da390f0 100644 (file)
@@ -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 (file)
index 0000000..20ae210
--- /dev/null
@@ -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
+
index 0d1df3f023fa6eefa23537947d59ac79c52e6241..06670a83e0598bbe6e98c459d8a18d3b72a6de97 100644 (file)
@@ -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