From e82213415029e9ddf6703fd1ee12c1ff67809c92 Mon Sep 17 00:00:00 2001 From: lupin012 <58134934+lupin012@users.noreply.github.com> Date: Thu, 25 Jan 2024 18:18:01 +0100 Subject: [PATCH] add websocket support (#84) --- integration/README.md | 5 ++- integration/run_tests.py | 97 ++++++++++++++++++++++++++++++---------- 2 files changed, 77 insertions(+), 25 deletions(-) diff --git a/integration/README.md b/integration/README.md index e4566785..4bd367e2 100644 --- a/integration/README.md +++ b/integration/README.md @@ -8,7 +8,7 @@ These integration tests currently available for Goerli testnet must run as non-r % pip3 install -r requirements.txt ``` -Currently, `json-diff` and `json-patch-jsondiff` are also required: +Currently, `websocat`, `json-diff` and `json-patch-jsondiff` are also required: ``` % sudo apt update @@ -16,6 +16,9 @@ Currently, `json-diff` and `json-patch-jsondiff` are also required: % npm install -g json-diff % sudo apt install python3-jsonpatch + +% sudo wget -qO /usr/local/bin/websocat https://github.com/vi/websocat/releases/latest/download/websocat.x86_64-unknown-linux-musl +% sudo chmod a+x /usr/local/bin/websocat ``` # Run tests diff --git a/integration/run_tests.py b/integration/run_tests.py index 9992142e..3d8c127d 100755 --- a/integration/run_tests.py +++ b/integration/run_tests.py @@ -6,9 +6,7 @@ import gzip import json import os -import shlex import shutil -import subprocess import sys import tarfile import time @@ -274,29 +272,67 @@ def run_shell_command(net: str, command: str, command1: str, expected_response: exp_rsp_file: str, diff_file: str, dump_output, json_file: str, test_number): """ Run the specified command as shell. If exact result or error don't care, they are null but present in expected_response. """ - command_and_args = shlex.split(command) - process = subprocess.run(command_and_args, stdout=subprocess.PIPE, universal_newlines=True, check=True) - if process.returncode != 0: - sys.exit(process.returncode) - process.stdout = process.stdout.strip('\n') + result = os.popen(command).read() if verbose_level > 1: - print(process.stdout) - response = json.loads(process.stdout) + print("First request/response:") + print(command) + print(len(result)) + print(result) + + if len(result) == 0: + if verbose_level: + print("Failed (json response zero length)") + return 1 + file = json_file.ljust(60) + print(f"{test_number:03d}. {file} Failed (json response is zero length)") + if exit_on_fail: + print("TEST ABORTED!") + sys.exit(1) + return 1 + result= result.strip('\n') + + try: + response = json.loads(result) + except json.decoder.JSONDecodeError: + if verbose_level: + print("Failed (bad json format on expected rsp)") + return 1 + file = json_file.ljust(60) + print(f"{test_number:03d}. {file} Failed (bad json format on expected rsp)") + if exit_on_fail: + print("TEST ABORTED!") + sys.exit(1) + return 1 + if command1 != "": - command_and_args = shlex.split(command1) - process = subprocess.run(command_and_args, stdout=subprocess.PIPE, universal_newlines=True, check=True) - if process.returncode != 0: - sys.exit(process.returncode) - process.stdout = process.stdout.strip('\n') + result1 = os.popen(command1).read() + if verbose_level > 1: + print("Second request/response:") + print(command1) + print(len(result1)) + print(result1) + + if len(result1) == 0: + if verbose_level: + print("Failed (json1 response zero length)") + return 1 + file = json_file.ljust(60) + print(f"{test_number:03d}. {file} Failed (json1 response is zero length)") + if exit_on_fail: + print("TEST ABORTED!") + sys.exit(1) + return 1 + + result1 = result1.strip('\n') try: - expected_response = json.loads(process.stdout) + expected_response = json.loads(result1) except json.decoder.JSONDecodeError: if verbose_level: - print("Failed (bad json format on expected rsp)") - print(process.stdout) + print("Failed (bad json1 format on expected rsp)") + print(result1) return 1 file = json_file.ljust(60) - print(f"{test_number:03d}. {file} Failed (bad json format on expected rsp)") + print(f"{test_number:03d}. {file} Failed (bad json1 format on expected rsp)") if exit_on_fail: print("TEST ABORTED!") sys.exit(1) @@ -427,7 +463,7 @@ def run_shell_command(net: str, command: str, command1: str, expected_response: def run_tests(net: str, test_dir: str, output_dir: str, json_file: str, verbose_level: int, daemon_under_test: str, exit_on_fail: bool, verify_with_daemon: bool, daemon_as_reference: str, dump_output: bool, test_number, infura_url: str, daemon_on_host: str, daemon_on_port: int, - jwt_secret: str): + jwt_secret: str, websocket_as_signalling: bool): """ Run integration tests. """ json_filename = test_dir + json_file ext = os.path.splitext(json_file)[1] @@ -467,7 +503,10 @@ def run_tests(net: str, test_dir: str, output_dir: str, json_file: str, verbose_ encoded = jwt.encode({"iat": datetime.now(pytz.utc)}, byte_array_secret, algorithm="HS256") jwt_auth = "-H \"Authorization: Bearer " + str(encoded) + "\" " if verify_with_daemon == 0: - cmd = '''curl --silent -X POST -H "Content-Type: application/json" ''' + jwt_auth + ''' --data \'''' + request_dumps + '''\' ''' + target + if websocket_as_signalling == 0: + cmd = '''curl --silent -X POST -H "Content-Type: application/json" ''' + jwt_auth + ''' --data \'''' + request_dumps + '''\' ''' + target + else: + cmd = "echo '" + request_dumps + "' | websocat -B 1000000000 ws://" + target cmd1 = "" output_api_filename = output_dir + json_file[:-4] output_dir_name = output_api_filename[:output_api_filename.rfind("/")] @@ -478,8 +517,14 @@ def run_tests(net: str, test_dir: str, output_dir: str, json_file: str, verbose_ else: target = get_target(SILK, method, infura_url, daemon_on_host, daemon_on_port) target1 = get_target(daemon_as_reference, method, infura_url, daemon_on_host, daemon_on_port) - cmd = '''curl --silent -X POST -H "Content-Type: application/json" ''' + jwt_auth + ''' --data \'''' + request_dumps + '''\' ''' + target - cmd1 = '''curl --silent -X POST -H "Content-Type: application/json" ''' + jwt_auth + ''' --data \'''' + request_dumps + '''\' ''' + target1 + if websocket_as_signalling == 0: + cmd = '''curl --silent -X POST -H "Content-Type: application/json" ''' + jwt_auth + ''' --data \'''' + request_dumps + '''\' ''' + target + else: + cmd = "echo '" + request_dumps + "' | websocat -B 1000000000 ws://" + target + if websocket_as_signalling == 0: + cmd1 = '''curl --silent -X POST -H "Content-Type: application/json" ''' + jwt_auth + ''' --data \'''' + request_dumps + '''\' ''' + target1 + else: + cmd1 = "echo '" + request_dumps + "' | websocat -B 1000000000 ws://" + target1 output_api_filename = output_dir + json_file[:-4] output_dir_name = output_api_filename[:output_api_filename.rfind("/")] response = "" @@ -528,6 +573,7 @@ def usage(argv): print("-o dump response") print("-k authentication token file") print("-x exclude API list (e.g.: txpool_content,txpool_status,engine_)") + print("-w use web-socket") print("-X exclude test list (e.g.: 18,22)") print("-H host where the RpcDaemon is located (e.g.: 10.10.2.3)") print("-p port where the RpcDaemon is located (e.g.: 8545)") @@ -560,9 +606,10 @@ def main(argv): start_test = "" jwt_secret = "" display_only_fail = 0 + websocket_as_signalling = 0 try: - opts, _ = getopt.getopt(argv[1:], "hfrcv:t:l:a:di:b:ox:X:H:k:s:p:") + opts, _ = getopt.getopt(argv[1:], "whfrcv:t:l:a:di:b:ox:X:H:k:s:p:") for option, optarg in opts: if option in ("-h", "--help"): usage(argv) @@ -594,6 +641,8 @@ def main(argv): verify_with_daemon = 1 elif option == "-o": dump_output = 1 + elif option == "-w": + websocket_as_signalling = 1 elif option == "-b": net = optarg json_dir = "./" + net + "/" @@ -663,7 +712,7 @@ def main(argv): ret = run_tests(net, json_dir, output_dir, test_file, verbose_level, daemon_under_test, exit_on_fail, verify_with_daemon, daemon_as_reference, dump_output, global_test_number, infura_url, daemon_on_host, - daemon_on_port, jwt_secret) + daemon_on_port, jwt_secret, websocket_as_signalling) if ret == 0: success_tests = success_tests + 1 else: