consistent naming for client fixtures / helpers

This commit is contained in:
meejah 2019-08-14 14:43:17 -06:00
parent 5e15470f55
commit 5ca3a3a1ab
2 changed files with 31 additions and 21 deletions

View File

@ -13,15 +13,16 @@ import util
import requests import requests
import pytest_twisted import pytest_twisted
import html5lib import html5lib
from bs4 import BeautifulSoup
def test_index(alice): def test_index(alice):
""" """
we can download the index file we can download the index file
""" """
util.web_get(alice.node_dir, u"") util.web_get(alice, u"")
# ...and json mode is valid json # ...and json mode is valid json
json.loads(util.web_get(alice.node_dir, u"?t=json")) json.loads(util.web_get(alice, u"", params={u"t": u"json"}))
def test_upload_download(alice): def test_upload_download(alice):
@ -32,8 +33,7 @@ def test_upload_download(alice):
FILE_CONTENTS = "some contents" FILE_CONTENTS = "some contents"
readcap = util.web_post( readcap = util.web_post(
alice.node_dir, alice, u"uri",
u"uri",
data={ data={
u"t": u"upload", u"t": u"upload",
u"format": u"mdmf", u"format": u"mdmf",
@ -45,7 +45,7 @@ def test_upload_download(alice):
readcap = readcap.strip() readcap = readcap.strip()
data = util.web_get( data = util.web_get(
alice.node_dir, u"uri", alice, u"uri",
params={ params={
u"uri": readcap, u"uri": readcap,
u"filename": u"boom", u"filename": u"boom",
@ -82,6 +82,9 @@ def test_helper_status(storage_nodes):
url = util.node_url(storage_nodes[0].node_dir, "helper_status") url = util.node_url(storage_nodes[0].node_dir, "helper_status")
resp = requests.get(url) resp = requests.get(url)
assert resp.status_code >= 200 and resp.status_code < 300 assert resp.status_code >= 200 and resp.status_code < 300
# XXX put in util.parse_html(resp) ...?
dom = BeautifulSoup(resp.content, "html5lib")
assert unicode(dom.h1.string) == u"Helper Status"
def test_deep_stats(alice): def test_deep_stats(alice):
@ -447,7 +450,7 @@ def test_mkdir_with_children(alice):
# create a new directory with one file and one sub-dir (all-at-once) # create a new directory with one file and one sub-dir (all-at-once)
resp = util.web_post( resp = util.web_post(
alice.node_dir, u"uri", alice, u"uri",
params={u"t": "mkdir-with-children"}, params={u"t": "mkdir-with-children"},
data=json.dumps(meta), data=json.dumps(meta),
) )

View File

@ -164,6 +164,9 @@ class TahoeProcess(object):
def node_dir(self): def node_dir(self):
return self._node_dir return self._node_dir
def __str__(self):
return "<TahoeProcess in '{}'>".format(self._node_dir)
def _run_node(reactor, node_dir, request, magic_text): def _run_node(reactor, node_dir, request, magic_text):
""" """
@ -407,35 +410,39 @@ def _check_status(response):
) )
def web_get(node_dir, uri_fragment, **kwargs): def web_get(tahoe, uri_fragment, **kwargs):
""" """
Make a GET request to the webport of `node_dir`. This will look Make a GET request to the webport of `tahoe` (a `TahoeProcess`,
like: `http://localhost:<webport>/<uri_fragment>`. All `kwargs` usually from a fixture (e.g. `alice`). This will look like:
are passed on to `requests.get` `http://localhost:<webport>/<uri_fragment>`. All `kwargs` are
passed on to `requests.get`
""" """
url = node_url(node_dir, uri_fragment) url = node_url(tahoe.node_dir, uri_fragment)
resp = requests.get(url, **kwargs) resp = requests.get(url, **kwargs)
_check_status(resp) _check_status(resp)
return resp.content return resp.content
def web_post(node_dir, uri_fragment, **kwargs): def web_post(tahoe, uri_fragment, **kwargs):
""" """
Make a POST request to the webport of `node_dir`. This will look Make a POST request to the webport of `node` (a `TahoeProcess,
like: `http://localhost:<webport>/<uri_fragment>`. All `kwargs` usually from a fixture e.g. `alice`). This will look like:
are passed on to `requests.post` `http://localhost:<webport>/<uri_fragment>`. All `kwargs` are
passed on to `requests.post`
""" """
url = node_url(node_dir, uri_fragment) url = node_url(tahoe.node_dir, uri_fragment)
resp = requests.post(url, **kwargs) resp = requests.post(url, **kwargs)
_check_status(resp) _check_status(resp)
return resp.content return resp.content
def await_client_ready(process, timeout=10, liveness=60*2): def await_client_ready(tahoe, timeout=10, liveness=60*2):
""" """
Uses the status API to wait for a client-type node to be Uses the status API to wait for a client-type node (in `tahoe`, a
`TahoeProcess` instance usually from a fixture e.g. `alice`) to be
'ready'. A client is deemed ready if: 'ready'. A client is deemed ready if:
- it answers http://<node_url>/statistics/?t=json/
- it answers `http://<node_url>/statistics/?t=json/`
- there is at least one storage-server connected - there is at least one storage-server connected
- every storage-server has a "last_received_data" and it is - every storage-server has a "last_received_data" and it is
within the last `liveness` seconds within the last `liveness` seconds
@ -446,7 +453,7 @@ def await_client_ready(process, timeout=10, liveness=60*2):
start = time.time() start = time.time()
while (time.time() - start) < float(timeout): while (time.time() - start) < float(timeout):
try: try:
data = web_get(process.node_dir, u"", params={u"t": u"json"}) data = web_get(tahoe, u"", params={u"t": u"json"})
js = json.loads(data) js = json.loads(data)
except Exception as e: except Exception as e:
print("waiting because '{}'".format(e)) print("waiting because '{}'".format(e))
@ -481,7 +488,7 @@ def await_client_ready(process, timeout=10, liveness=60*2):
raise RuntimeError( raise RuntimeError(
"Waited {} seconds for {} to be 'ready' but it never was".format( "Waited {} seconds for {} to be 'ready' but it never was".format(
timeout, timeout,
process.node_dir, tahoe,
) )
) )