Skip to content

Commit

Permalink
Fix workspace container tests attempt 1
Browse files Browse the repository at this point in the history
Tests pass locally but consistently fail on Github Actions. Try making
the tests themselves ensure that the services are available rather than
checking a possibly fragile log line.

Also move them to a test directory rather than scripts.

Also also slightly modify the Docker file so slow steps are cached
before copying in code, which busts the cache.
  • Loading branch information
MrCreosote committed Mar 21, 2024
1 parent 182b08e commit 8c1a304
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 136 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
- name: Run tests
shell: bash
run: |
sh scripts/run_tests.sh
sh test/run_tests.sh
workspace_deluxe_tests:
runs-on: ubuntu-latest
Expand Down
12 changes: 7 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
FROM eclipse-temurin:11-jdk as build

COPY . /tmp/workspace_deluxe
WORKDIR /tmp
RUN apt-get update -y && \
apt-get install -y ant git ca-certificates python3-sphinx && \
git clone https://github.com/kbase/jars && \
cd workspace_deluxe && \
RUN apt update -y && \
apt install -y ant git ca-certificates python3-sphinx && \
git clone https://github.com/kbase/jars

COPY . /tmp/workspace_deluxe

RUN cd workspace_deluxe && \
make docker_deps

# updated/slimmed down version of what's in kbase/kb_jre
Expand Down
54 changes: 0 additions & 54 deletions scripts/run_tests.sh

This file was deleted.

76 changes: 0 additions & 76 deletions scripts/workspace_container_test.py

This file was deleted.

2 changes: 2 additions & 0 deletions test/README.md
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.
23 changes: 23 additions & 0 deletions test/run_tests.sh
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
116 changes: 116 additions & 0 deletions test/workspace_container_test.py
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.fail"

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

0 comments on commit 8c1a304

Please sign in to comment.