From: Daira Hopwood Date: Fri, 17 Apr 2015 17:28:56 +0000 (+0100) Subject: Add very basic unit tests for dir scan and db X-Git-Url: https://git.rkrishnan.org/zeppelin?a=commitdiff_plain;h=c682eb98434cc4cfccbf0280f012736655fdbcc8;p=tahoe-lafs%2Ftahoe-lafs.git Add very basic unit tests for dir scan and db Signed-off-by: Daira Hopwood --- diff --git a/src/allmydata/frontends/drop_upload.py b/src/allmydata/frontends/drop_upload.py index 521b8148..7d321130 100644 --- a/src/allmydata/frontends/drop_upload.py +++ b/src/allmydata/frontends/drop_upload.py @@ -1,5 +1,5 @@ -import sys +import sys, os from collections import deque from twisted.internet import defer @@ -14,6 +14,10 @@ from allmydata.util.fileutil import abspath_expanduser_unicode from allmydata.immutable.upload import FileName from allmydata.scripts import backupdb, tahoe_backup +from allmydata.util.encodingutil import listdir_unicode, quote_output, \ + quote_local_unicode_path, to_str, FilenameEncodingError, unicode_to_url + + class DropUploader(service.MultiService): name = 'drop-upload' @@ -37,6 +41,7 @@ class DropUploader(service.MultiService): self._stats_provider = client.stats_provider self._convergence = client.convergence self._local_path = FilePath(local_dir) + self._local_dir = unicode(local_dir, 'UTF-8') self._dbfile = dbfile self._upload_deque = deque() @@ -79,11 +84,10 @@ class DropUploader(service.MultiService): assert self._db != None use_timestamps = True r = self._db.check_file(childpath, use_timestamps) - return !r.was_uploaded() + return not r.was_uploaded() def _scan(self, localpath): quoted_path = quote_local_unicode_path(localpath) - try: children = listdir_unicode(localpath) except EnvironmentError: @@ -99,21 +103,25 @@ class DropUploader(service.MultiService): # recurse on the child directory self._scan(childpath) elif os.path.isfile(childpath) and not os.path.islink(childpath): - try: - must_upload = self._check_db_file(childpath) - if must_upload: - self._add_to_dequeue(childpath) + must_upload = self._check_db_file(childpath) + if must_upload: + self._add_to_dequeue(childpath) + else: + if os.path.islink(childpath): + self.warn("WARNING: cannot backup symlink %s" % quote_local_unicode_path(childpath)) + else: + self.warn("WARNING: cannot backup special file %s" % quote_local_unicode_path(childpath)) def startService(self): - self._db = backupdb.get_backupdb(self._dbfile, stderr) - if not self.backupdb: - # XXX or raise an exception? + self._db = backupdb.get_backupdb(self._dbfile) + if self._db is None: return Failure(Exception('ERROR: Unable to load magic folder db.')) - self._scan(self._local_path) - service.MultiService.startService(self) d = self._notifier.startReading() + + self._scan(self._local_dir) + self._stats_provider.count('drop_upload.dirs_monitored', 1) return d diff --git a/src/allmydata/test/test_drop_upload.py b/src/allmydata/test/test_drop_upload.py index 724faf27..100445fe 100644 --- a/src/allmydata/test/test_drop_upload.py +++ b/src/allmydata/test/test_drop_upload.py @@ -15,6 +15,7 @@ from allmydata.test.common_util import ReallyEqualMixin, NonASCIIPathMixin from allmydata.test.common import ShouldFailMixin from allmydata.frontends.drop_upload import DropUploader +from allmydata.scripts import backupdb class DropUploadTestMixin(GridTestMixin, ShouldFailMixin, ReallyEqualMixin, NonASCIIPathMixin): @@ -41,7 +42,7 @@ class DropUploadTestMixin(GridTestMixin, ShouldFailMixin, ReallyEqualMixin, NonA self.upload_dirnode = n self.upload_dircap = n.get_uri() self.uploader = DropUploader(self.client, self.upload_dircap, self.local_dir.encode('utf-8'), - inotify=self.inotify, pending_delay=0.2) + "magicfolderdb.sqlite", inotify=self.inotify, pending_delay=0.2) self.uploader.setServiceParent(self.client) d = self.uploader.startService() self.uploader.upload_ready() @@ -172,6 +173,45 @@ class MockTest(DropUploadTestMixin, unittest.TestCase): class RealTest(DropUploadTestMixin, unittest.TestCase): """This is skipped unless both Twisted and the platform support inotify.""" + def create(self, dbfile): + bdb = backupdb.get_backupdb(dbfile) + self.failUnless(bdb, "unable to create backupdb from %r" % (dbfile,)) + return bdb + + def test_db_basic(self): + self.basedir = basedir = os.path.join("dropupload", "basic") + fileutil.make_dirs(basedir) + dbfile = os.path.join(basedir, "dbfile") + bdb = self.create(dbfile) + + def test_uploader_startService(self): + self.uploader = None + self.inotify = None # use the appropriate inotify for the platform + self.basedir = "drop_upload.RealTest.test_uploader_startService" + self.set_up_grid() + self.client = self.g.clients[0] + + d = self.client.create_dirnode() + def _made_upload_dir(n): + self.failUnless(IDirectoryNode.providedBy(n)) + self.upload_dirnode = n + self.upload_dircap = n.get_uri() + self.uploader = DropUploader(self.client, self.upload_dircap, self.basedir.encode('utf-8'), + "magicfolderdb.sqlite", inotify=self.inotify) + self.uploader.startService() + self.failUnlessEqual(self.uploader._db.VERSION, 2) + d.addCallback(_made_upload_dir) + + # Prevent unclean reactor errors. + def _cleanup(res): + d = defer.succeed(None) + if self.uploader is not None: + d.addCallback(lambda ign: self.uploader.finish(for_tests=True)) + d.addCallback(lambda ign: res) + return d + d.addBoth(_cleanup) + return d + def test_drop_upload(self): # We should always have runtime.platform.supportsINotify, because we're using # Twisted >= 10.1.