libeufin

Integration and sandbox testing for FinTech APIs and data formats
Log | Files | Refs | Submodules | README | LICENSE

commit ce7049b28e8809ffb2ea9fe86b05a05bcfc166c5
parent f5de2f084d17c57cb566e744d8d3b3c8bef0b5ae
Author: Florian Dold <florian.dold@gmail.com>
Date:   Mon,  8 Jun 2020 17:18:32 +0530

integration test util lib

Diffstat:
M.gitignore | 2++
Mintegration-tests/test-ebics-highlevel.py | 71++++++-----------------------------------------------------------------
Mintegration-tests/test-taler-facade.py | 7+++----
Aintegration-tests/util.py | 85+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 96 insertions(+), 69 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -10,3 +10,5 @@ presentation/*.out presentation/*.snm presentation/*.toc .idea/misc.xml +__pycache__ +*.log diff --git a/integration-tests/test-ebics-highlevel.py b/integration-tests/test-ebics-highlevel.py @@ -1,13 +1,13 @@ #!/usr/bin/env python3 from requests import post, get -from subprocess import call, Popen, PIPE from time import sleep import os -import socket import hashlib import base64 +from util import startNexus, startSandbox + # Steps implemented in this test. # # 0 Prepare sandbox. @@ -59,86 +59,29 @@ SUBSCRIBER_NAME = "Oliver Smith" BANK_ACCOUNT_LABEL = "savings" # Databases -NEXUS_DB="test-nexus.sqlite3" +NEXUS_DB = "test-nexus.sqlite3" + def fail(msg): print(msg) - nexus.terminate() - sandbox.terminate() exit(1) -def checkPorts(ports): - for i in ports: - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - try: - s.bind(("0.0.0.0", i)) - s.close() - except: - print("Port {} is not available".format(i)) - exit(77) - - def assertResponse(response): if response.status_code != 200: print("Test failed on URL: {}".format(response.url)) # stdout/stderr from both services is A LOT of text. # Confusing to dump all that to console. print("Check nexus.log and sandbox.log, probably under /tmp") - nexus.terminate() - sandbox.terminate() exit(1) # Allows for finer grained checks. return response -# -1 Clean databases and start services. os.chdir("..") -assert 0 == call(["rm", "-f", "sandbox/libeufin-sandbox.sqlite3"]) -assert 0 == call(["rm", "-f", "nexus/{}".format(NEXUS_DB)]) -DEVNULL = open(os.devnull, "w") -assert 0 == call( - ["./gradlew", "nexus:run", "--console=plain", "--args=superuser admin --password x --db-name={}".format(NEXUS_DB)] -) - -# Start nexus -checkPorts([5001]) -nexus = Popen( - ["./gradlew", "nexus:run", "--console=plain", "--args=serve --db-name={}".format(NEXUS_DB)], - stdout=PIPE, - stderr=PIPE, -) -for i in range(10): - try: - get("http://localhost:5001/") - except: - if i == 9: - nexus.terminate() - stdout, stderr = nexus.communicate() - print("Nexus timed out") - print("{}\n{}".format(stdout.decode(), stderr.decode())) - exit(77) - sleep(2) - continue - break -# Start sandbox -checkPorts([5000]) -sandbox = Popen(["./gradlew", "sandbox:run", "--args=serve"], stdout=PIPE, stderr=PIPE) -for i in range(10): - try: - get("http://localhost:5000/") - except: - if i == 9: - nexus.terminate() - sandbox.terminate() - stdout, stderr = nexus.communicate() - print("Sandbox timed out") - print("{}\n{}".format(stdout.decode(), stderr.decode())) - exit(77) - sleep(2) - continue - break +startNexus(NEXUS_DB) +startSandbox() # 0.a assertResponse( @@ -284,6 +227,4 @@ resp = assertResponse( if len(resp.json().get("transactions")) != 1: fail("Unexpected number of transactions; should be 1") -nexus.terminate() -sandbox.terminate() print("Test passed!") diff --git a/integration-tests/test-taler-facade.py b/integration-tests/test-taler-facade.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 from requests import post, get -from subprocess import call, Popen, PIPE +from subprocess import call, Popen from time import sleep import os import socket @@ -78,14 +78,13 @@ assert 0 == call( # start nexus checkPorts([5001]) -nexus = Popen(["nexus", "serve", "--db-name={}".format(NEXUS_DB)], stdout=PIPE, stderr=PIPE) +nexus = Popen(["nexus", "serve", "--db-name={}".format(NEXUS_DB)]) for i in range(10): try: get("http://localhost:5001/") except: if i == 9: nexus.terminate() - stdout, stderr = nexus.communicate() print("Nexus timed out") print("{}\n{}".format(stdout.decode(), stderr.decode())) exit(77) @@ -95,7 +94,7 @@ for i in range(10): # start sandbox checkPorts([5000]) -sandbox = Popen(["sandbox", "serve", "--db-name={}".format(SANDBOX_DB)], stdout=PIPE, stderr=PIPE) +sandbox = Popen(["sandbox", "serve", "--db-name={}".format(SANDBOX_DB)]) for i in range(10): try: get("http://localhost:5000/") diff --git a/integration-tests/util.py b/integration-tests/util.py @@ -0,0 +1,85 @@ +# Helpers for the integration tests. + +from subprocess import check_call, Popen, PIPE +import socket +from requests import post, get +from time import sleep +import atexit + + +def checkPort(port): + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + try: + s.bind(("0.0.0.0", port)) + s.close() + except: + print("Port {} is not available".format(i)) + exit(77) + + +def startSandbox(): + check_call(["rm", "-f", "sandbox/libeufin-sandbox.sqlite3"]) + check_call(["./gradlew", "sandbox:assemble"]) + checkPort(5000) + sandbox = Popen(["./gradlew", + "sandbox:run", + "--console=plain", + "--args=serve"], + stdout=open("sandbox-stdout.log", "w"), + stderr=open("sandbox-stderr.log", "w"), + ) + atexit.register(lambda: sandbox.terminate()) + for i in range(10): + try: + get("http://localhost:5000/") + except: + if i == 9: + stdout, stderr = nexus.communicate() + print("Sandbox timed out") + print("{}\n{}".format(stdout.decode(), stderr.decode())) + exit(77) + sleep(2) + continue + break + + +def startNexus(dbfile): + check_call(["rm", "-f", "nexus/{}".format(dbfile)]) + check_call( + [ + "./gradlew", + "nexus:assemble", + ] + ) + check_call( + [ + "./gradlew", + "nexus:run", + "--console=plain", + "--args=superuser admin --password x --db-name={}".format(dbfile), + ] + ) + checkPort(5001) + nexus = Popen( + [ + "./gradlew", + "nexus:run", + "--console=plain", + "--args=serve --db-name={}".format(dbfile), + ], + stdout=open("nexus-stdout.log", "w"), + stderr=open("nexus-stderr.log", "w"), + ) + atexit.register(lambda: nexus.terminate()) + for i in range(10): + try: + get("http://localhost:5001/") + except: + if i == 9: + nexus.terminate() + print("Nexus timed out") + exit(77) + sleep(1) + continue + break + return nexus