From ea48edf682da1dbd5989fa8ce43143e6a38bb945 Mon Sep 17 00:00:00 2001 From: David Stainton Date: Thu, 25 Jun 2015 14:27:14 -0700 Subject: [PATCH] Remove/clean code duplication for CLI unit tests --- src/allmydata/test/test_cli_magic_folder.py | 106 ++++++++++++-------- src/allmydata/test/test_magic_folder.py | 56 +---------- 2 files changed, 67 insertions(+), 95 deletions(-) diff --git a/src/allmydata/test/test_cli_magic_folder.py b/src/allmydata/test/test_cli_magic_folder.py index 89664472..ea4ed113 100644 --- a/src/allmydata/test/test_cli_magic_folder.py +++ b/src/allmydata/test/test_cli_magic_folder.py @@ -13,29 +13,9 @@ from allmydata.util.encodingutil import quote_output, get_io_encoding from .test_cli import CLITestMixin from allmydata.scripts import magic_folder_cli -class CreateMagicFolder(GridTestMixin, CLITestMixin, unittest.TestCase): - def _create_magic_folder(self): - d = self.do_cli("magic-folder", "create", "magic") - def _done((rc,stdout,stderr)): - self.failUnless(rc == 0) - self.failUnless("Alias 'magic' created" in stdout) - self.failIf(stderr) - aliases = get_aliases(self.get_clientdir()) - self.failUnless("magic" in aliases) - self.failUnless(aliases["magic"].startswith("URI:DIR2:")) - d.addCallback(_done) - return d - - def _invite(self, ignore): - d = self.do_cli("magic-folder", "invite", u"magic", u"Alice") - def _done((rc,stdout,stderr)): - self.failUnless(rc == 0) - return (rc,stdout,stderr) - d.addCallback(_done) - return d - - def _diminish(self, write_cap): +class MagicFolderCLITestMixin(CLITestMixin, GridTestMixin): + def diminish_readonly(self, write_cap): d = self.do_cli("ls", "--json", write_cap) def get_readonly_cap((rc,stdout,stderr)): self.failUnless(rc == 0) @@ -44,7 +24,10 @@ class CreateMagicFolder(GridTestMixin, CLITestMixin, unittest.TestCase): d.addCallback(get_readonly_cap) return d - def _try_joined_config(self, result): + def check_joined_config(self, result): + """Tests that wour collective directory has the readonly cap of + our upload directory. + """ collective_readonly_cap = fileutil.read(os.path.join(self.get_clientdir(), "private/collective_dircap")) d = self.do_cli("ls", "--json", collective_readonly_cap) def _done((rc,stdout,stderr)): @@ -52,7 +35,7 @@ class CreateMagicFolder(GridTestMixin, CLITestMixin, unittest.TestCase): return (rc,stdout,stderr) d.addCallback(_done) def test_joined_magic_folder((rc,stdout,stderr)): - d2 = self._diminish(self.dmd_write_cap) + d2 = self.diminish_readonly(self.upload_dircap) def fail_unless_dmd_readonly_exists(readonly_cap): s = re.search(readonly_cap, stdout) self.failUnless(s is not None) @@ -61,49 +44,86 @@ class CreateMagicFolder(GridTestMixin, CLITestMixin, unittest.TestCase): d.addCallback(test_joined_magic_folder) return d - def _get_caps_from_files(self, result): - self.magic_readonly_cap = fileutil.read(os.path.join(self.get_clientdir(), "private/collective_dircap")) - self.dmd_write_cap = fileutil.read(os.path.join(self.get_clientdir(), "private/magic_folder_dircap")) - self.failIf(self.magic_readonly_cap is None or self.dmd_write_cap is None) + def get_caps_from_files(self, result): + self.collective_dircap = fileutil.read(os.path.join(self.get_clientdir(), "private/collective_dircap")) + self.upload_dircap = fileutil.read(os.path.join(self.get_clientdir(), "private/magic_folder_dircap")) + self.failIf(self.collective_dircap is None or self.upload_dircap is None) + return None + + def check_config(self, result): + client_config = fileutil.read(os.path.join(self.get_clientdir(), "tahoe.cfg")) + # XXX utf-8? + ret = re.search("\[magic_folder\]\nenabled = True\nlocal.directory = %s" % (self.local_dir.encode('utf-8'),), client_config) + self.failIf(ret is None) + return result + + def create_invite_join_magic_folder(self, nickname, local_dir): + d = self.do_cli("magic-folder", "create", u"magic", nickname, local_dir) + def _done((rc,stdout,stderr)): + self.failUnless(rc == 0) + return (rc,stdout,stderr) + d.addCallback(_done) + d.addCallback(self.get_caps_from_files) + d.addCallback(self.check_joined_config) + d.addCallback(self.check_config) + return d + + +class CreateMagicFolder(MagicFolderCLITestMixin, unittest.TestCase): + + def _create_magic_folder(self): + d = self.do_cli("magic-folder", "create", "magic") + def _done((rc,stdout,stderr)): + self.failUnless(rc == 0) + self.failUnless("Alias 'magic' created" in stdout) + self.failIf(stderr) + aliases = get_aliases(self.get_clientdir()) + self.failUnless("magic" in aliases) + self.failUnless(aliases["magic"].startswith("URI:DIR2:")) + d.addCallback(_done) + return d + + def _invite(self, ignore): + d = self.do_cli("magic-folder", "invite", u"magic", u"Alice") + def _done((rc,stdout,stderr)): + self.failUnless(rc == 0) + return (rc,stdout,stderr) + d.addCallback(_done) + return d def _join(self, result): invite_code = result[1].strip() self.magic_readonly_cap, self.dmd_write_cap = invite_code.split(magic_folder_cli.INVITE_SEPERATOR) - d = self.do_cli("magic-folder", "join", invite_code, self.magic_local_dir) + d = self.do_cli("magic-folder", "join", invite_code, self.local_dir) def _done((rc,stdout,stderr)): self.failUnless(rc == 0) return (rc,stdout,stderr) d.addCallback(_done) return d - def _check_config(self, result): - client_config = fileutil.read(os.path.join(self.get_clientdir(), "tahoe.cfg")) - ret = re.search("\[magic_folder\]\nenabled = True\nlocal.directory = %s" % (self.magic_local_dir,), client_config) - self.failIf(ret is None) - return result - def test_create_and_then_invite_join(self): self.basedir = "cli/MagicFolder/create-and-then-invite-join" self.set_up_grid() - self.magic_local_dir = os.path.join(self.basedir, "magic") + self.local_dir = os.path.join(self.basedir, "magic") d = self._create_magic_folder() d.addCallback(self._invite) d.addCallback(self._join) - d.addCallback(self._try_joined_config) - d.addCallback(self._check_config) + d.addCallback(self.get_caps_from_files) + d.addCallback(self.check_joined_config) + d.addCallback(self.check_config) return d def test_create_invite_join(self): self.basedir = "cli/MagicFolder/create-invite-join" self.set_up_grid() - self.magic_local_dir = os.path.join(self.basedir, "magic") - d = self.do_cli("magic-folder", "create", u"magic", u"Alice", self.magic_local_dir) + self.local_dir = os.path.join(self.basedir, "magic") + d = self.do_cli("magic-folder", "create", u"magic", u"Alice", self.local_dir) def _done((rc,stdout,stderr)): self.failUnless(rc == 0) return (rc,stdout,stderr) d.addCallback(_done) - d.addCallback(self._get_caps_from_files) - d.addCallback(self._try_joined_config) - d.addCallback(self._check_config) + d.addCallback(self.get_caps_from_files) + d.addCallback(self.check_joined_config) + d.addCallback(self.check_config) return d diff --git a/src/allmydata/test/test_magic_folder.py b/src/allmydata/test/test_magic_folder.py index 406c865b..c746b230 100644 --- a/src/allmydata/test/test_magic_folder.py +++ b/src/allmydata/test/test_magic_folder.py @@ -13,6 +13,7 @@ from allmydata.test.no_network import GridTestMixin from allmydata.test.common_util import ReallyEqualMixin, NonASCIIPathMixin from allmydata.test.common import ShouldFailMixin from allmydata.test.test_cli import CLITestMixin +from allmydata.test.test_cli_magic_folder import MagicFolderCLITestMixin from allmydata.frontends import magic_folder from allmydata.frontends.magic_folder import MagicFolder @@ -20,7 +21,7 @@ from allmydata import backupdb from allmydata.util.fileutil import abspath_expanduser_unicode -class MagicFolderTestMixin(CLITestMixin, GridTestMixin, ShouldFailMixin, ReallyEqualMixin, NonASCIIPathMixin): +class MagicFolderTestMixin(MagicFolderCLITestMixin, ShouldFailMixin, ReallyEqualMixin, NonASCIIPathMixin): """ These tests will be run both with a mock notifier, and (on platforms that support it) with the real INotify. @@ -43,55 +44,6 @@ class MagicFolderTestMixin(CLITestMixin, GridTestMixin, ShouldFailMixin, ReallyE self.failUnlessEqual(bdb.VERSION, 2) return bdb - def _diminish(self, write_cap): - d = self.do_cli("ls", "--json", write_cap) - def get_readonly_cap((rc,stdout,stderr)): - self.failUnless(rc == 0) - readonly_cap = json.loads(stdout)[1][u"ro_uri"] - return readonly_cap - d.addCallback(get_readonly_cap) - return d - - def _try_joined_config(self, result): - collective_readonly_cap = fileutil.read(os.path.join(self.get_clientdir(), "private/collective_dircap")) - d = self.do_cli("ls", "--json", collective_readonly_cap) - def _done((rc,stdout,stderr)): - self.failUnless(rc == 0) - return (rc,stdout,stderr) - d.addCallback(_done) - def test_joined_magic_folder((rc,stdout,stderr)): - d2 = self._diminish(self.upload_dircap) - def fail_unless_dmd_readonly_exists(readonly_cap): - s = re.search(readonly_cap, stdout) - self.failUnless(s is not None) - d2.addCallback(fail_unless_dmd_readonly_exists) - return d2 - d.addCallback(test_joined_magic_folder) - return d - - def _get_caps_from_files(self, result): - self.collective_dircap = fileutil.read(os.path.join(self.get_clientdir(), "private/collective_dircap")) - self.upload_dircap = fileutil.read(os.path.join(self.get_clientdir(), "private/magic_folder_dircap")) - self.failIf(self.collective_dircap is None or self.upload_dircap is None) - - def _check_config(self, result): - client_config = fileutil.read(os.path.join(self.get_clientdir(), "tahoe.cfg")) - # XXX utf-8? - ret = re.search("\[magic_folder\]\nenabled = True\nlocal.directory = %s" % (self.local_dir.encode('utf-8'),), client_config) - self.failIf(ret is None) - return result - - def _create_invite_join_magic_folder(self): - d = self.do_cli("magic-folder", "create", u"magic", u"Alice", self.local_dir) - def _done((rc,stdout,stderr)): - self.failUnless(rc == 0) - return (rc,stdout,stderr) - d.addCallback(_done) - d.addCallback(self._get_caps_from_files) - d.addCallback(self._try_joined_config) - d.addCallback(self._check_config) - return d - def _made_upload_dir(self, n): if self.dir_node == None: self.dir_node = n @@ -186,7 +138,7 @@ class MagicFolderTestMixin(CLITestMixin, GridTestMixin, ShouldFailMixin, ReallyE small_tree_dir = abspath_expanduser_unicode(small_tree_name, base=self.basedir) new_small_tree_dir = abspath_expanduser_unicode(small_tree_name, base=self.local_dir) - d = self._create_invite_join_magic_folder() + d = self.create_invite_join_magic_folder(u"Alice", self.local_dir) d.addCallback(self._create_magicfolder) def _check_move_empty_tree(res): @@ -261,7 +213,7 @@ class MagicFolderTestMixin(CLITestMixin, GridTestMixin, ShouldFailMixin, ReallyE self.client = self.g.clients[0] self.stats_provider = self.client.stats_provider - d = self._create_invite_join_magic_folder() + d = self.create_invite_join_magic_folder(u"Alice", self.local_dir) d.addCallback(self._create_magicfolder) def create_test_file(result): -- 2.45.2