From: Daira Hopwood Date: Tue, 28 Apr 2015 18:43:29 +0000 (+0100) Subject: fix unit tests WIP X-Git-Url: https://git.rkrishnan.org/simplejson/components/frontends/%22news.html/?a=commitdiff_plain;h=47bf82789bbf20bddfd9fbc31faa33f5178f05ad;p=tahoe-lafs%2Ftahoe-lafs.git fix unit tests WIP Signed-off-by: Daira Hopwood --- diff --git a/src/allmydata/frontends/drop_upload.py b/src/allmydata/frontends/drop_upload.py index ca91814b..90607863 100644 --- a/src/allmydata/frontends/drop_upload.py +++ b/src/allmydata/frontends/drop_upload.py @@ -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 diff --git a/src/allmydata/test/test_drop_upload.py b/src/allmydata/test/test_drop_upload.py index 7f3a770d..aebcd9f9 100644 --- a/src/allmydata/test/test_drop_upload.py +++ b/src/allmydata/test/test_drop_upload.py @@ -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()