-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #721 from MrCreosote/develop
Fix workspace container tests
- Loading branch information
Showing
7 changed files
with
150 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
This directory contains tests not part of the standard java suite. Currently it has a very small | ||
test suite that runs against the workspace in a docker container. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/usr/bin/env bash | ||
# | ||
# Script to run the python workspace_container_test.py locally or on GitHub Actions. | ||
# Builds and mounts auth2, workspace, and mongo docker containers, and then calls | ||
# the python script. | ||
# | ||
# See .github/workflows/test.yml for GitHub Actions implementation. | ||
|
||
# build and start the containers | ||
docker compose up -d --build | ||
compose_up_exit_code=$? | ||
if [ $compose_up_exit_code -ne 0 ]; then | ||
echo "Error: docker-compose up -d --build command failed with exit code $compose_up_exit_code." | ||
exit $compose_up_exit_code | ||
fi | ||
|
||
# get the path to the current directory and add it to the python execution path | ||
current_dir="$( dirname -- "$( readlink -f -- "$0"; )"; )" | ||
PYTHONPATH="$current_dir":$PYTHONPATH python -m pytest test/workspace_container_test.py | ||
exit_code=$? | ||
|
||
docker compose down | ||
exit $exit_code |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
from lib.biokbase.workspace.client import Workspace | ||
import pytest | ||
import requests | ||
import json | ||
import time | ||
|
||
""" workspace_container_test.py | ||
Very simple tests to ensure that local workspace and auth2 servers are functioning correctly. | ||
Requires the python libraries `pytest` and `requests` to be installed. | ||
Assumes that the workspace and auth2 are running locally on ports 8080 and 7058 respectively. | ||
Use the wrapper shell script, `run_tests.sh`, to create the necessary set up and run the tests: | ||
sh scripts/run_tests.sh | ||
""" | ||
|
||
WORKSPACE_VERSION = "0.14.3" | ||
|
||
AUTH_URL = "http://localhost:8080" | ||
WS_URL = "http://localhost:7058" | ||
USER_NAME = "some_dull_user" | ||
WS_NAME = "my_cool_new_workspace" | ||
WAIT_TIMES = [1, 2, 5, 10, 30] | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def ready(): | ||
wait_for_auth() | ||
wait_for_ws() | ||
|
||
yield | ||
|
||
def wait_for_auth(): | ||
print("waiting for auth service...") | ||
for t in WAIT_TIMES: | ||
try: | ||
res = requests.get(AUTH_URL) | ||
res.raise_for_status() | ||
return | ||
except Exception as e: | ||
print(f"Failed to connect to auth, waiting {t} sec and trying again:\n\t{e}") | ||
time.sleep(t) | ||
raise Exception(f"Couldn't connect to the auth after {len(WAIT_TIMES)} attempts") | ||
|
||
|
||
def wait_for_ws(): | ||
print("waiting for workspace service...") | ||
ws = Workspace(WS_URL) | ||
for t in WAIT_TIMES: | ||
try: | ||
ws.ver() | ||
return | ||
except Exception as e: | ||
print(f"Failed to connect to workspace, waiting {t} sec and trying again:\n\t{e}") | ||
time.sleep(t) | ||
raise Exception(f"Couldn't connect to the workspace after {len(WAIT_TIMES)} attempts") | ||
|
||
|
||
def test_create_user_create_workspace(ready): | ||
"""create a user and then create a workspace for that user""" | ||
token = create_auth2_user_token() | ||
get_ws_version(token) | ||
create_read_workspace(token) | ||
|
||
|
||
def create_auth2_user_token() -> str: | ||
"""create a user and generate a token for that user""" | ||
# create a new user | ||
user_json = json.dumps({"user": USER_NAME, "display": "Blah blah"}) | ||
response = requests.post( | ||
AUTH_URL + "/testmode/api/V2/testmodeonly/user", | ||
data=user_json, | ||
headers={"content-type":"application/json"} | ||
) | ||
assert response.status_code == 200 | ||
user_data = response.json() | ||
assert 'created' in user_data | ||
|
||
# create the toke | ||
token_json = json.dumps({ "user": USER_NAME, "type": "Login" }) | ||
response = requests.post( | ||
AUTH_URL + "/testmode/api/V2/testmodeonly/token", | ||
data=token_json, | ||
headers={"content-type":"application/json"} | ||
) | ||
assert response.status_code == 200 | ||
token_data = response.json() | ||
assert 'created' in token_data | ||
assert 'token' in token_data | ||
return token_data["token"] | ||
|
||
|
||
def get_ws_version(token: str) -> None: | ||
"""get the current workspace version""" | ||
ws = Workspace(WS_URL, token=token) | ||
assert ws.ver() == WORKSPACE_VERSION | ||
|
||
|
||
def create_read_workspace(token: str) -> None: | ||
"""create a new workspace and then get the workspace information""" | ||
ws = Workspace(WS_URL, token=token) | ||
new_ws = ws.create_workspace({'workspace': WS_NAME}) | ||
assert new_ws[1] == WS_NAME | ||
assert new_ws[2] == USER_NAME | ||
assert ws.get_workspace_info({ "id": new_ws[0] }) == new_ws | ||
|
||
|
||
def test_get_docs(ready) -> None: | ||
"""check that the workspace documentation can be accessed""" | ||
response = requests.get(WS_URL + "/docs/") | ||
assert response.status_code == 200 | ||
assert response.text.find("KBase Workspace Service Manual") != -1 | ||
assert response.text.find("KBase Workspace " + WORKSPACE_VERSION + " documentation") != -1 |