diff --git a/pytest/tests/mocknet/helpers/neard_runner.py b/pytest/tests/mocknet/helpers/neard_runner.py index fb10a6dc0d2..c754bc715aa 100644 --- a/pytest/tests/mocknet/helpers/neard_runner.py +++ b/pytest/tests/mocknet/helpers/neard_runner.py @@ -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() diff --git a/pytest/tests/mocknet/mirror.py b/pytest/tests/mocknet/mirror.py index c3b7ad5ffda..d96455ed339 100755 --- a/pytest/tests/mocknet/mirror.py +++ b/pytest/tests/mocknet/mirror.py @@ -4,6 +4,7 @@ """ from argparse import ArgumentParser, BooleanOptionalAction import pathlib +import json import random from rc import pmap, run import requests @@ -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):