tahoe-lafs/src/allmydata/test/storage_plugin.py

123 lines
3.1 KiB
Python
Raw Normal View History

"""
A storage server plugin the test suite can use to validate the
functionality.
"""
2020-11-03 15:40:53 +00:00
from future.utils import native_str, native_str_to_bytes
from six import ensure_str
import attr
from zope.interface import (
implementer,
)
from twisted.internet.defer import (
succeed,
)
from twisted.web.resource import (
Resource,
)
from twisted.web.static import (
Data,
)
from foolscap.api import (
RemoteInterface,
)
from allmydata.interfaces import (
IFoolscapStoragePlugin,
2019-06-28 12:52:17 +00:00
IStorageServer,
)
from allmydata.client import (
AnnounceableStorageServer,
)
2020-11-03 15:40:53 +00:00
from allmydata.util.jsonbytes import (
dumps,
)
class RIDummy(RemoteInterface):
2020-09-28 14:20:00 +00:00
__remote_name__ = native_str("RIDummy.tahoe.allmydata.com")
def just_some_method():
"""
Just some method so there is something callable on this object. We won't
pretend to actually offer any storage capabilities.
"""
2020-11-29 21:04:56 +00:00
@implementer(IFoolscapStoragePlugin) # type: ignore # todo: make stubs for twisted
2019-06-14 21:54:35 +00:00
@attr.s
class DummyStorage(object):
2019-06-14 21:54:35 +00:00
name = attr.ib()
@property
def _client_section_name(self):
return u"storageclient.plugins.{}".format(self.name)
def get_storage_server(self, configuration, get_anonymous_storage_server):
if u"invalid" in configuration:
raise Exception("The plugin is unhappy.")
announcement = {u"value": configuration.get(u"some", u"default-value")}
storage_server = DummyStorageServer(get_anonymous_storage_server)
return succeed(
AnnounceableStorageServer(
announcement,
storage_server,
),
)
def get_storage_client(self, configuration, announcement, get_rref):
return DummyStorageClient(
get_rref,
dict(configuration.items(self._client_section_name, [])),
announcement,
)
def get_client_resource(self, configuration):
"""
:return: A static data resource that produces the given configuration when
rendered, as an aid to testing.
"""
items = configuration.items(self._client_section_name, [])
resource = Data(
2020-11-03 15:40:53 +00:00
native_str_to_bytes(dumps(dict(items))),
ensure_str("text/json"),
)
# Give it some dynamic stuff too.
resource.putChild(b"counter", GetCounter())
return resource
2019-11-26 13:43:05 +00:00
class GetCounter(Resource, object):
2019-11-26 12:49:25 +00:00
"""
``GetCounter`` is a resource that returns a count of the number of times
it has rendered a response to a GET request.
:ivar int value: The number of ``GET`` requests rendered so far.
"""
value = 0
def render_GET(self, request):
self.value += 1
2020-11-03 15:40:53 +00:00
return native_str_to_bytes(dumps({"value": self.value}))
@implementer(RIDummy)
@attr.s(frozen=True)
class DummyStorageServer(object): # type: ignore # warner/foolscap#78
get_anonymous_storage_server = attr.ib()
def remote_just_some_method(self):
pass
2019-06-28 12:52:17 +00:00
@implementer(IStorageServer)
@attr.s
class DummyStorageClient(object):
get_rref = attr.ib()
configuration = attr.ib()
announcement = attr.ib()