Avoid using nevow FakeRequest in storage test.

Use twisted.web.server.Request instead, with a DummyChannel.

There's still one line of inevitable nevow now, because of code in
web/common.py; but that should be easily replaceable once we switch
that over.
This commit is contained in:
Sajith Sasidharan 2020-04-06 19:00:55 -04:00 committed by Sajith Sasidharan
parent de5d04ab79
commit 0af744610f

View File

@ -13,6 +13,16 @@ from twisted.trial import unittest
from twisted.internet import defer from twisted.internet import defer
from twisted.application import service from twisted.application import service
from twisted.web.template import flattenString from twisted.web.template import flattenString
# We need to use `nevow.inevow.IRequest` for now for compatibility
# with the code in web/common.py. Once nevow bits are gone from
# web/common.py, we can use `twisted.web.iweb.IRequest` here.
from nevow.inevow import IRequest
from twisted.web.server import Request
from twisted.web.test.test_web import DummyChannel
from zope.interface import implements
from foolscap.api import fireEventually from foolscap.api import fireEventually
import itertools import itertools
from allmydata import interfaces from allmydata import interfaces
@ -37,7 +47,6 @@ from allmydata.mutable.layout import MDMFSlotWriteProxy, MDMFSlotReadProxy, \
SHARE_HASH_CHAIN_SIZE SHARE_HASH_CHAIN_SIZE
from allmydata.interfaces import BadWriteEnablerError from allmydata.interfaces import BadWriteEnablerError
from allmydata.test.common import LoggingServiceParent, ShouldFailMixin from allmydata.test.common import LoggingServiceParent, ShouldFailMixin
from nevow.testutil import FakeRequest
from allmydata.test.no_network import NoNetworkServer from allmydata.test.no_network import NoNetworkServer
from allmydata.web.storage import ( from allmydata.web.storage import (
StorageStatus, StorageStatus,
@ -2985,28 +2994,20 @@ def renderSynchronously(ss):
deferred = flattenString(None, elem) deferred = flattenString(None, elem)
return unittest.TestCase().successResultOf(deferred) return unittest.TestCase().successResultOf(deferred)
def renderDeferred(resource, **kwargs): def renderDeferred(ss):
""" elem = StorageStatusElement(ss._storage, ss._nickname)
Use this to exercise an overridden MultiFormatResource.render(), return flattenString(None, elem)
usually for output=json or render_GET. It returns a Deferred.
:param _MultiFormatResource resource: an HTTP resource to be rendered. class JSONRequest(Request):
implements(IRequest)
""" def __init__(self, **kwargs):
# We should be using twisted.web's DummyRequest here instead of Request.__init__(self, DummyChannel(), **kwargs)
# nevow's FakeRequest, but right now it is a bit of a problem: see self.args = {"t": ["json"]}
# web/common.py. MultiFormatResource.render() makes a get_arg() self.fields = {}
# call, which does a IRequest(ctx_or_req). IRequest can handle
# FakeRequest, but it can't handle DummyRequest. def renderJSON(resource):
req = FakeRequest(**kwargs) return resource.render(JSONRequest())
req.fields = None
d = defer.maybeDeferred(resource.render, req)
def _done(res):
if isinstance(res, str):
return res + req.v
return req.v
d.addCallback(_done)
return d
class MyBucketCountingCrawler(BucketCountingCrawler): class MyBucketCountingCrawler(BucketCountingCrawler):
def finished_prefix(self, cycle, prefix): def finished_prefix(self, cycle, prefix):
@ -4096,7 +4097,7 @@ class LeaseCrawler(unittest.TestCase, pollmixin.PollMixin):
return d return d
def render_json(self, page): def render_json(self, page):
d = renderDeferred(page, args={"t": ["json"]}) d = renderJSON(page)
return d return d
class WebStatus(unittest.TestCase, pollmixin.PollMixin): class WebStatus(unittest.TestCase, pollmixin.PollMixin):
@ -4140,7 +4141,7 @@ class WebStatus(unittest.TestCase, pollmixin.PollMixin):
return d return d
def render_json(self, page): def render_json(self, page):
d = renderDeferred(page, args={"t": ["json"]}) d = renderJSON(page)
return d return d
def test_status_no_disk_stats(self): def test_status_no_disk_stats(self):