Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Websocket client python #85

Merged
merged 6 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions integration/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@ These integration tests currently available for Goerli testnet must run as non-r
% pip3 install -r requirements.txt
```

Currently, `websocat`, `json-diff` and `json-patch-jsondiff` are also required:
Currently, `json-diff` and `json-patch-jsondiff` are also required:

```
% sudo apt update
% sudo apt install npm
% 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
Expand Down
84 changes: 64 additions & 20 deletions integration/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import time
import pytz
import jwt
from websocket import create_connection

SILK = "silk"
RPCDAEMON = "rpcdaemon"
Expand Down Expand Up @@ -267,17 +268,18 @@ def is_message_to_be_converted(test_name, net: str):
return 0


def run_shell_command(net: str, command: str, command1: str, expected_response: str, verbose_level: int, exit_on_fail: bool,
def run_shell_command(net: str, result, command, result1, command1: str, expected_response: str, verbose_level: int, exit_on_fail: bool,
output_dir: str, silk_file: str,
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. """

result = os.popen(command).read()
if verbose_level > 1:
print("First request/response:")
print(command)
print(len(result))
print(result)
if len(result) == 0: # if result not provided execute command and retrive it
result = os.popen(command).read()
if verbose_level > 1:
print("First request/response:")
print(command)
print(len(result))
print(result)

if len(result) == 0:
if verbose_level:
Expand All @@ -304,15 +306,16 @@ def run_shell_command(net: str, command: str, command1: str, expected_response:
sys.exit(1)
return 1

if command1 != "":
result1 = os.popen(command1).read()
if command1 != "" or result1 != "":
if result1 == "": # if result is not provided retrive it
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 len(result1) == 0: # is error
if verbose_level:
print("Failed (json1 response zero length)")
return 1
Expand Down Expand Up @@ -463,7 +466,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, websocket_as_signalling: bool):
jwt_secret: str, websocket_as_transport: bool):
""" Run integration tests. """
json_filename = test_dir + json_file
ext = os.path.splitext(json_file)[1]
Expand Down Expand Up @@ -498,16 +501,30 @@ def run_tests(net: str, test_dir: str, output_dir: str, json_file: str, verbose_
target = get_target(daemon_under_test, method, infura_url, daemon_on_host, daemon_on_port)
if jwt_secret == "":
jwt_auth = ""
encoded = ""
else:
byte_array_secret = bytes.fromhex(jwt_secret)
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:
if websocket_as_signalling == 0:
if websocket_as_transport == 0:
result = ""
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
ws_target = "ws://" + target
if encoded != "":
http_header=["Authorization: Bearer " + str(encoded)]
try:
web_service = create_connection(ws_target, header=http_header)
except:
print("\nConnection to server failed")
print("TEST ABORTED!")
sys.exit(1)
web_service.send(request_dumps)
result = web_service.recv()
cmd = ""
cmd1 = ""
result1=""
output_api_filename = output_dir + json_file[:-4]
output_dir_name = output_api_filename[:output_api_filename.rfind("/")]
response = json_rpc["response"]
Expand All @@ -517,14 +534,39 @@ 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)
if websocket_as_signalling == 0:
if websocket_as_transport == 0:
result = ""
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:
cmd=""
http_header=""
ws_target = "ws://" + target
if encoded != "":
http_header=["Authorization: Bearer " + str(encoded)]
try:
web_service = create_connection(ws_target, header=http_header)
except:
print("\nConnection to server failed")
print("TEST ABORTED!")
sys.exit(1)
web_service.send(request_dumps)
result = web_service.recv()
if websocket_as_transport == 0:
result1 = ""
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
cmd1=""
ws_target = "ws://" + target1
if encoded != "":
http_header=["Authorization: Bearer " + str(encoded)]
try:
web_service = create_connection(ws_target, header=http_header)
except:
print("\nConnection to server failed")
print("TEST ABORTED!")
sys.exit(1)
web_service.send(request_dumps)
result1 = web_service.recv()
output_api_filename = output_dir + json_file[:-4]
output_dir_name = output_api_filename[:output_api_filename.rfind("/")]
response = ""
Expand All @@ -534,7 +576,9 @@ def run_tests(net: str, test_dir: str, output_dir: str, json_file: str, verbose_

return run_shell_command(
net,
result,
cmd,
result1,
cmd1,
response,
verbose_level,
Expand Down Expand Up @@ -606,7 +650,7 @@ def main(argv):
start_test = ""
jwt_secret = ""
display_only_fail = 0
websocket_as_signalling = 0
websocket_as_transport = 0

try:
opts, _ = getopt.getopt(argv[1:], "whfrcv:t:l:a:di:b:ox:X:H:k:s:p:")
Expand Down Expand Up @@ -642,7 +686,7 @@ def main(argv):
elif option == "-o":
dump_output = 1
elif option == "-w":
websocket_as_signalling = 1
websocket_as_transport = 1
elif option == "-b":
net = optarg
json_dir = "./" + net + "/"
Expand Down Expand Up @@ -712,7 +756,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, websocket_as_signalling)
daemon_on_port, jwt_secret, websocket_as_transport)
if ret == 0:
success_tests = success_tests + 1
else:
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ psutil
pytz
pyjwt
web3
websocket-client
pylint==2.11.*