Skip to content

Commit

Permalink
feat(mocknet): change HTTP request logic (#10023)
Browse files Browse the repository at this point in the history
We'll change things to bind to "localhost" instead of "0.0.0.0", and
make HTTP requests by SSH + curl on the remote server. It's also
possible to set up an explicit SSH/SOCKS proxy, but that would require
one for each server and would be long-lived, which might be a bit
annoying for people running the test scripts
  • Loading branch information
marcelo-gonzalez authored Oct 30, 2023
2 parents 3043048 + 868f8db commit dfc1d80
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
5 changes: 4 additions & 1 deletion pytest/tests/mocknet/helpers/neard_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,10 @@ def serve(self, port):
# something so lightweight
main_loop = threading.Thread(target=self.main_loop)
main_loop.start()
s = RpcServer(('0.0.0.0', port), self)
# this will listen only on the loopback interface and won't be accessible
# over the internet. If connecting to another machine, we can SSH and then make
# the request locally
s = RpcServer(('localhost', port), self)
s.serve_forever()


Expand Down
26 changes: 19 additions & 7 deletions pytest/tests/mocknet/mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""
from argparse import ArgumentParser, BooleanOptionalAction
import pathlib
import json
import random
from rc import pmap, run
import requests
Expand Down Expand Up @@ -328,14 +329,25 @@ def stop_traffic_cmd(args, traffic_generator, nodes):


def neard_runner_jsonrpc(node, method, params=[]):
j = {'method': method, 'params': params, 'id': 'dontcare', 'jsonrpc': '2.0'}
r = requests.post(f'http://{node.machine.ip}:3000', json=j, timeout=30)
if r.status_code != 200:
logger.warning(
f'bad response {r.status_code} trying to send {method} JSON RPC to neard runner on {node.instance_name}:\n{r.content}'
body = {
'method': method,
'params': params,
'id': 'dontcare',
'jsonrpc': '2.0'
}
body = json.dumps(body)
# '"'"' will be interpreted as ending the first quote and then concatenating it with "'",
# followed by a new quote started with ' and the rest of the string, to get any single quotes
# in method or params into the command correctly
body = body.replace("'", "'\"'\"'")
r = run_cmd(node, f'curl localhost:3000 -d \'{body}\'')
response = json.loads(r.stdout)
if 'error' in response:
# TODO: errors should be handled better here in general but just exit for now
sys.exit(
f'bad response trying to send {method} JSON RPC to neard runner on {node.instance_name}:\n{response}'
)
r.raise_for_status()
return r.json()['result']
return response['result']


def neard_runner_start(node):
Expand Down

0 comments on commit dfc1d80

Please sign in to comment.