mirror of
https://github.com/tahoe-lafs/tahoe-lafs.git
synced 2025-02-07 20:00:20 +00:00
test_deepcheck: replace t.w.client.getPage with treq
This commit is contained in:
parent
dc6398d265
commit
2ec3791411
@ -1,7 +1,9 @@
|
|||||||
|
|
||||||
import re
|
import re
|
||||||
|
import treq
|
||||||
from twisted.internet import defer
|
from twisted.internet import defer
|
||||||
from twisted.web import client
|
from twisted.web import client
|
||||||
|
from twisted.web.error import Error
|
||||||
from nevow.testutil import FakeRequest
|
from nevow.testutil import FakeRequest
|
||||||
from nevow import inevow, context
|
from nevow import inevow, context
|
||||||
|
|
||||||
@ -81,3 +83,13 @@ class HTTPClientHEADFactory(client.HTTPClientFactory):
|
|||||||
|
|
||||||
class HTTPClientGETFactory(client.HTTPClientFactory):
|
class HTTPClientGETFactory(client.HTTPClientFactory):
|
||||||
protocol = MyGetter
|
protocol = MyGetter
|
||||||
|
|
||||||
|
@defer.inlineCallbacks
|
||||||
|
def do_http(method, url, **kwargs):
|
||||||
|
response = yield treq.request(method, url, persistent=False, **kwargs)
|
||||||
|
body = yield treq.content(response)
|
||||||
|
# TODO: replace this with response.fail_for_status when
|
||||||
|
# https://github.com/twisted/treq/pull/159 has landed
|
||||||
|
if 400 <= response.code < 600:
|
||||||
|
raise Error(response.code, response=body)
|
||||||
|
defer.returnValue(body)
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
import os, json, urllib
|
import os, json, urllib
|
||||||
from twisted.trial import unittest
|
from twisted.trial import unittest
|
||||||
from twisted.internet import defer
|
from twisted.internet import defer
|
||||||
|
from twisted.internet.defer import inlineCallbacks, returnValue
|
||||||
from allmydata.immutable import upload
|
from allmydata.immutable import upload
|
||||||
from allmydata.mutable.common import UnrecoverableFileError
|
from allmydata.mutable.common import UnrecoverableFileError
|
||||||
from allmydata.mutable.publish import MutableData
|
from allmydata.mutable.publish import MutableData
|
||||||
@ -11,11 +12,11 @@ from allmydata.interfaces import ICheckResults, ICheckAndRepairResults, \
|
|||||||
IDeepCheckResults, IDeepCheckAndRepairResults
|
IDeepCheckResults, IDeepCheckAndRepairResults
|
||||||
from allmydata.monitor import Monitor, OperationCancelledError
|
from allmydata.monitor import Monitor, OperationCancelledError
|
||||||
from allmydata.uri import LiteralFileURI
|
from allmydata.uri import LiteralFileURI
|
||||||
from twisted.web.client import getPage
|
|
||||||
|
|
||||||
from allmydata.test.common import ErrorMixin, _corrupt_mutable_share_data, \
|
from allmydata.test.common import ErrorMixin, _corrupt_mutable_share_data, \
|
||||||
ShouldFailMixin
|
ShouldFailMixin
|
||||||
from .common_util import StallMixin, run_cli
|
from .common_util import StallMixin, run_cli
|
||||||
|
from .common_web import do_http
|
||||||
from allmydata.test.no_network import GridTestMixin
|
from allmydata.test.no_network import GridTestMixin
|
||||||
from .cli.common import CLITestMixin
|
from .cli.common import CLITestMixin
|
||||||
|
|
||||||
@ -148,54 +149,47 @@ class DeepCheckBase(GridTestMixin, ErrorMixin, StallMixin, ShouldFailMixin,
|
|||||||
le.args = tuple(le.args + (unit,))
|
le.args = tuple(le.args + (unit,))
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
@inlineCallbacks
|
||||||
def web(self, n, method="GET", **kwargs):
|
def web(self, n, method="GET", **kwargs):
|
||||||
# returns (data, url)
|
# returns (data, url)
|
||||||
url = (self.client_baseurls[0] + "uri/%s" % urllib.quote(n.get_uri())
|
url = (self.client_baseurls[0] + "uri/%s" % urllib.quote(n.get_uri())
|
||||||
+ "?" + "&".join(["%s=%s" % (k,v) for (k,v) in kwargs.items()]))
|
+ "?" + "&".join(["%s=%s" % (k,v) for (k,v) in kwargs.items()]))
|
||||||
d = getPage(url, method=method)
|
data = yield do_http(method, url, browser_like_redirects=True)
|
||||||
d.addCallback(lambda data: (data,url))
|
returnValue((data,url))
|
||||||
return d
|
|
||||||
|
|
||||||
def wait_for_operation(self, ignored, ophandle):
|
@inlineCallbacks
|
||||||
|
def wait_for_operation(self, ophandle):
|
||||||
url = self.client_baseurls[0] + "operations/" + ophandle
|
url = self.client_baseurls[0] + "operations/" + ophandle
|
||||||
url += "?t=status&output=JSON"
|
url += "?t=status&output=JSON"
|
||||||
d = getPage(url)
|
while True:
|
||||||
def _got(res):
|
body = yield do_http("get", url)
|
||||||
try:
|
data = json.loads(body)
|
||||||
data = json.loads(res)
|
if data["finished"]:
|
||||||
except ValueError:
|
break
|
||||||
self.fail("%s: not JSON: '%s'" % (url, res))
|
yield self.stall(delay=0.1)
|
||||||
if not data["finished"]:
|
returnValue(data)
|
||||||
d = self.stall(delay=1.0)
|
|
||||||
d.addCallback(self.wait_for_operation, ophandle)
|
|
||||||
return d
|
|
||||||
return data
|
|
||||||
d.addCallback(_got)
|
|
||||||
return d
|
|
||||||
|
|
||||||
def get_operation_results(self, ignored, ophandle, output=None):
|
@inlineCallbacks
|
||||||
|
def get_operation_results(self, ophandle, output=None):
|
||||||
url = self.client_baseurls[0] + "operations/" + ophandle
|
url = self.client_baseurls[0] + "operations/" + ophandle
|
||||||
url += "?t=status"
|
url += "?t=status"
|
||||||
if output:
|
if output:
|
||||||
url += "&output=" + output
|
url += "&output=" + output
|
||||||
d = getPage(url)
|
body = yield do_http("get", url)
|
||||||
def _got(res):
|
|
||||||
if output and output.lower() == "json":
|
if output and output.lower() == "json":
|
||||||
try:
|
data = json.loads(body)
|
||||||
return json.loads(res)
|
else:
|
||||||
except ValueError:
|
data = body
|
||||||
self.fail("%s: not JSON: '%s'" % (url, res))
|
returnValue(data)
|
||||||
return res
|
|
||||||
d.addCallback(_got)
|
|
||||||
return d
|
|
||||||
|
|
||||||
|
@inlineCallbacks
|
||||||
def slow_web(self, n, output=None, **kwargs):
|
def slow_web(self, n, output=None, **kwargs):
|
||||||
# use ophandle=
|
# use ophandle=
|
||||||
handle = base32.b2a(os.urandom(4))
|
handle = base32.b2a(os.urandom(4))
|
||||||
d = self.web(n, "POST", ophandle=handle, **kwargs)
|
yield self.web(n, "POST", ophandle=handle, **kwargs)
|
||||||
d.addCallback(self.wait_for_operation, handle)
|
yield self.wait_for_operation(handle)
|
||||||
d.addCallback(self.get_operation_results, handle, output=output)
|
data = yield self.get_operation_results(handle, output=output)
|
||||||
return d
|
returnValue(data)
|
||||||
|
|
||||||
|
|
||||||
class DeepCheckWebGood(DeepCheckBase, unittest.TestCase):
|
class DeepCheckWebGood(DeepCheckBase, unittest.TestCase):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user