From f606beb0651e64169092ae631bf34508eb7b630d Mon Sep 17 00:00:00 2001 From: Jean-Paul Calderone Date: Fri, 14 Jun 2019 18:06:14 -0400 Subject: [PATCH] Test and support plugins without any configuration --- src/allmydata/client.py | 16 ++++- src/allmydata/test/storage_plugin.py | 2 +- src/allmydata/test/test_client.py | 102 ++++++++++++++++++++------- 3 files changed, 89 insertions(+), 31 deletions(-) diff --git a/src/allmydata/client.py b/src/allmydata/client.py index 8eed471bd..68bb6c0fb 100644 --- a/src/allmydata/client.py +++ b/src/allmydata/client.py @@ -2,6 +2,7 @@ import os, stat, time, weakref from base64 import urlsafe_b64encode from functools import partial from errno import ENOENT, EPERM +from ConfigParser import NoSectionError import attr from zope.interface import implementer @@ -691,11 +692,20 @@ class _Client(node.Node, pollmixin.PollMixin): def _get_storage_plugin_configuration(self, storage_plugin_name): - return dict( + """ + Load the configuration for a storage server plugin with the given name. + + :return dict: The matching configuration. + """ + try: # Need to reach past the Tahoe-LAFS-supplied wrapper around the # underlying ConfigParser... - self.config.config.items("storageserver.plugins." + storage_plugin_name) - ) + config = self.config.config.items( + "storageserver.plugins." + storage_plugin_name, + ) + except NoSectionError: + config = [] + return dict(config) def _enable_storage_servers(self, announceable_storage_servers): diff --git a/src/allmydata/test/storage_plugin.py b/src/allmydata/test/storage_plugin.py index c82eda8f7..a11116705 100644 --- a/src/allmydata/test/storage_plugin.py +++ b/src/allmydata/test/storage_plugin.py @@ -39,7 +39,7 @@ class DummyStorage(object): def get_storage_server(self, configuration, get_anonymous_storage_server): return AnnounceableStorageServer( - announcement={u"value": configuration[u"some"]}, + announcement={u"value": configuration.get(u"some", u"default-value")}, storage_server=DummyStorageServer(get_anonymous_storage_server), ) diff --git a/src/allmydata/test/test_client.py b/src/allmydata/test/test_client.py index e26cd2950..d347e7e21 100644 --- a/src/allmydata/test/test_client.py +++ b/src/allmydata/test/test_client.py @@ -1068,6 +1068,34 @@ def get_published_announcements(client): +def matches_dummy_announcement(basedir, name, value): + """ + Matches the announcement for the ``DummyStorage`` storage server plugin. + + :param str basedir: The path to the node the storage server plugin is + loaded into. + + :param unicode name: The name of the dummy plugin. + + :param unicode value: The arbitrary value in the dummy plugin + announcement. + + :return: a testtools-style matcher + """ + return MatchesStructure( + service_name=Equals("storage"), + ann=MatchesDict({ + # Everyone gets a name and a fURL added to their announcement. + u"name": Equals(name), + u"storage-server-FURL": matches_furl(), + # The plugin can contribute things, too. + u"value": Equals(value), + }), + signing_key=MatchesNodePublicKey(basedir), + ) + + + class StorageAnnouncementTests(SyncTestCase): """ Tests for the storage announcement published by the client. @@ -1152,6 +1180,7 @@ introducer.furl = pb://abcde@nowhere/fake """ self.useFixture(UseTestPlugins()) + value = u"thing" config = config_from_string( self.basedir, u"tub.port", @@ -1160,28 +1189,21 @@ introducer.furl = pb://abcde@nowhere/fake more_storage=b"plugins=tahoe-lafs-dummy-v1", more_sections=( b"[storageserver.plugins.tahoe-lafs-dummy-v1]\n" - b"some = thing\n" + b"some = {}\n".format(value) ), ), ) - matches_dummy_announcement = MatchesStructure( - service_name=Equals("storage"), - ann=MatchesDict({ - # Everyone gets a name and a fURL added to their announcement. - u"name": Equals(u"tahoe-lafs-dummy-v1"), - u"storage-server-FURL": matches_furl(), - # The plugin can contribute things, too. - u"value": Equals(u"thing"), - }), - signing_key=MatchesNodePublicKey(self.basedir), - ) self.assertThat( client.create_client_from_config(config, introducer_factory=MemoryIntroducerClient), succeeded(AfterPreprocessing( get_published_announcements, MatchesListwise([ matches_anonymous_storage_announcement(self.basedir), - matches_dummy_announcement, + matches_dummy_announcement( + self.basedir, + u"tahoe-lafs-dummy-v1", + value, + ), ]), )), ) @@ -1208,26 +1230,22 @@ introducer.furl = pb://abcde@nowhere/fake ), ), ) - def matches_dummy_announcement(v): - return MatchesStructure( - service_name=Equals("storage"), - ann=MatchesDict({ - # Everyone gets a name and a fURL added to their announcement. - u"name": Equals(u"tahoe-lafs-dummy-v{}".format(v)), - u"storage-server-FURL": matches_furl(), - # The plugin can contribute things, too. - u"value": Equals(u"thing-{}".format(v)), - }), - signing_key=MatchesNodePublicKey(self.basedir), - ) self.assertThat( client.create_client_from_config(config, introducer_factory=MemoryIntroducerClient), succeeded(AfterPreprocessing( get_published_announcements, MatchesListwise([ matches_anonymous_storage_announcement(self.basedir), - matches_dummy_announcement(b"1"), - matches_dummy_announcement(b"2"), + matches_dummy_announcement( + self.basedir, + u"tahoe-lafs-dummy-v1", + u"thing-1", + ), + matches_dummy_announcement( + self.basedir, + u"tahoe-lafs-dummy-v2", + u"thing-2", + ), ]), )), ) @@ -1272,3 +1290,33 @@ introducer.furl = pb://abcde@nowhere/fake MatchesSameElements(), )), ) + + + def test_storage_plugin_without_configuration(self): + """ + A storage plugin with no configuration is loaded and announced. + """ + self.useFixture(UseTestPlugins()) + + config = config_from_string( + self.basedir, + u"tub.port", + self.get_config( + storage_enabled=True, + more_storage=b"plugins=tahoe-lafs-dummy-v1", + ), + ) + self.assertThat( + client.create_client_from_config(config, introducer_factory=MemoryIntroducerClient), + succeeded(AfterPreprocessing( + get_published_announcements, + MatchesListwise([ + matches_anonymous_storage_announcement(self.basedir), + matches_dummy_announcement( + self.basedir, + u"tahoe-lafs-dummy-v1", + u"default-value", + ), + ]), + )), + )