]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/commitdiff
fix unit tests WIP
authorDaira Hopwood <daira@jacaranda.org>
Tue, 28 Apr 2015 18:43:29 +0000 (19:43 +0100)
committerDaira Hopwood <daira@jacaranda.org>
Tue, 28 Apr 2015 18:43:29 +0000 (19:43 +0100)
Signed-off-by: Daira Hopwood <daira@jacaranda.org>
src/allmydata/frontends/drop_upload.py
src/allmydata/test/test_drop_upload.py

index ca91814bf5945ff477604bc49cff6c817b646f6e..90607863f97be6dacab099cb6f59c45a3c9035f8 100644 (file)
@@ -132,6 +132,12 @@ class DropUploader(service.MultiService):
         #print "adding file to upload queue %s" % (path,)
         pass
 
+    def Pause(self):
+        self.is_upload_ready = False
+
+    def Resume(self):
+        self.is_upload_ready = True
+
     def upload_ready(self):
         """upload_ready is used to signal us to start
         processing the upload items...
@@ -175,7 +181,17 @@ class DropUploader(service.MultiService):
             return self._parent.add_file(name, u)
 
         def _add_dir(ignore):
-             return self._parent.create_subdirectory(name)
+            print "_add_dir %s" % (path.path,)
+            self._pending.remove(path)
+            name = path.basename()
+            dirname = path.path
+            # on Windows the name is already Unicode
+            if sys.platform != "win32":
+                name = name.decode(get_filesystem_encoding())
+                dirname = path.path.decode(get_filesystem_encoding())
+
+            self._scan(dirname)
+            return self._parent.create_subdirectory(name)
 
         def _maybe_upload(val):
             if not os.path.exists(path.path):
@@ -206,7 +222,7 @@ class DropUploader(service.MultiService):
 
         def _failed(f):
             self._stats_provider.count('drop_upload.objects_queued', -1)
-            if path.exists():
+            if os.path.exists(path.path):
                 self._log("drop-upload: %r failed to upload due to %r" % (path.path, f))
                 self._stats_provider.count('drop_upload.objects_failed', 1)
                 return f
index 7f3a770d99353dc8ca01e32fb3d9b29ffcf4f8c5..aebcd9f9e3af0e3e2568ebc323e8600ee593e52f 100644 (file)
@@ -34,7 +34,7 @@ class DropUploadTestMixin(GridTestMixin, ShouldFailMixin, ReallyEqualMixin, NonA
         self.failUnlessEqual(bdb.VERSION, 2)
         return bdb
 
-    def _made_upload_dir(n):
+    def _made_upload_dir(self, n):
         self.failUnless(IDirectoryNode.providedBy(n))
         self.upload_dirnode = n
         self.upload_dircap = n.get_uri()
@@ -80,17 +80,19 @@ class DropUploadTestMixin(GridTestMixin, ShouldFailMixin, ReallyEqualMixin, NonA
         self.mkdir_nonascii(self.local_dir)
 
         self.client = self.g.clients[0]
+        self.stats_provider = self.client.stats_provider
+
         d = self.client.create_dirnode()
         d.addCallback(self._made_upload_dir)
 
         def testMoveEmptyTree(res):
+            print "moving tree into %s" % self.local_dir
             tree_dir = os.path.join(self.basedir, 'apple_tree')
             os.mkdir(tree_dir)
-            shutil.move(tree_dir, self.local_dir)
+            os.rename(tree_dir, os.path.join(self.local_dir, 'apple_tree'))
             d = defer.Deferred()
             self.uploader.set_uploaded_callback(d.callback)
             return d
-
         d.addCallback(testMoveEmptyTree)
         d.addCallback(lambda ign: self.failUnlessReallyEqual(self._get_count('drop_upload.objects_uploaded'), 1))
         d.addCallback(lambda ign: self.failUnlessReallyEqual(self._get_count('drop_upload.files_uploaded'), 0))
@@ -101,18 +103,52 @@ class DropUploadTestMixin(GridTestMixin, ShouldFailMixin, ReallyEqualMixin, NonA
             tree_dir = os.path.join(self.basedir, 'creepy_tree')
             os.mkdir(tree_dir)
             os.path.join(tree_dir, u"tree_frog")
-            f = open(path.path, "wb")
+            f = open(os.path.join(tree_dir, 'what'), "wb")
             f.write("meow")
             f.close()
-            shutil.move(tree_dir, self.local_dir)
+            os.rename(tree_dir, os.path.join(self.local_dir,'creepy_tree'))
             d = defer.Deferred()
             self.uploader.set_uploaded_callback(d.callback)
             return d
         d.addCallback(testMoveSmallTree)
         d.addCallback(lambda ign: self.failUnlessReallyEqual(self._get_count('drop_upload.objects_uploaded'), 2))
+        d.addCallback(lambda ign: self.failUnlessReallyEqual(self._get_count('drop_upload.directories_created'), 2))
         d.addCallback(lambda ign: self.failUnlessReallyEqual(self._get_count('drop_upload.files_uploaded'), 1))
         d.addCallback(lambda ign: self.failUnlessReallyEqual(self._get_count('drop_upload.objects_queued'), 0))
-        d.addCallback(lambda ign: self.failUnlessReallyEqual(self._get_count('drop_upload.directories_created'), 1))
+
+        d.addBoth(self._cleanup)
+        return d
+
+    def _test_persistence(self):
+        self.uploader = None
+        self.set_up_grid()
+        self.local_dir = os.path.join(self.basedir, u"test_persistence")
+        self.mkdir_nonascii(self.local_dir)
+
+        self.client = self.g.clients[0]
+        self.stats_provider = self.client.stats_provider
+
+        d = self.client.create_dirnode()
+        d.addCallback(self._made_upload_dir)
+        d.addCallback(lambda ign: self.uploader.Pause())
+        def create_file(val):
+            print "creating file..."
+            myFile = os.path.join(self.local_dir, "what")
+            f = open(myFile, "wb")
+            f.write("meow")
+            f.close()
+            return None
+        d.addCallback(create_file)
+        d.addCallback(lambda ign: self.failUnlessReallyEqual(self._get_count('drop_upload.objects_queued'), 0))
+
+        def resume_uploader(val):
+            self.uploader.Resume()
+            d = defer.Deferred()
+            self.uploader.set_uploaded_callback(d.callback)
+            return d
+        d.addCallback(resume_uploader)
+        d.addCallback(lambda ign: self.failUnlessReallyEqual(self._get_count('drop_upload.objects_queued'), 0))
+        d.addCallback(lambda ign: self.failUnlessReallyEqual(self._get_count('drop_upload.objects_uploaded'), 1))
 
         d.addBoth(self._cleanup)
         return d
@@ -248,14 +284,20 @@ class MockTest(DropUploadTestMixin, unittest.TestCase):
 
     def test_uploader_start_service(self):
         self.inotify = fake_inotify
-        self.basedir = "drop_upload.MockTest._test_uploader_start_service"
+        self.basedir = "drop_upload.MockTest.test_uploader_start_service"
         return self._test_uploader_start_service()
 
     def test_move_tree(self):
         self.inotify = fake_inotify
-        self.basedir = "drop_upload.MockTest._test_move_tree"
+        self.basedir = "drop_upload.MockTest.test_move_tree"
         return self._test_move_tree()
 
+    def test_persistence(self):
+        self.inotify = fake_inotify
+        self.basedir = "drop_upload.MockTest.test_persistence"
+        return self._test_persistence()
+
+
 class RealTest(DropUploadTestMixin, unittest.TestCase):
     """This is skipped unless both Twisted and the platform support inotify."""
 
@@ -287,3 +329,8 @@ class RealTest(DropUploadTestMixin, unittest.TestCase):
         self.inotify = None
         self.basedir = "drop_upload.RealTest._test_move_tree"
         return self._test_move_tree()
+
+    def test_persistence(self):
+        self.inotify = None
+        self.basedir = "drop_upload.RealTest.test_persistence"
+        return self._test_persistence()