Skip to content

Commit

Permalink
use deployed MR for python tests
Browse files Browse the repository at this point in the history
Signed-off-by: Isabella do Amaral <idoamara@redhat.com>
  • Loading branch information
isinyaaa committed Oct 2, 2024
1 parent 0c369a0 commit 5ecf0da
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 68 deletions.
21 changes: 9 additions & 12 deletions .github/workflows/build-image-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
- ".github/ISSUE_TEMPLATE/**"
- ".github/dependabot.yml"
- "docs/**"
- "clients/python/docs/**"
- "clients/python/**"
env:
IMG_ORG: kubeflow
IMG_REPO: model-registry
Expand All @@ -31,7 +31,7 @@ jobs:
shell: bash
env:
VERSION: ${{ steps.tags.outputs.tag }}
run: ./scripts/build_deploy.sh
run: make image/build
- name: Start Kind Cluster
uses: helm/kind-action@v1.10.0
with:
Expand All @@ -41,7 +41,7 @@ jobs:
IMG: "${{ env.IMG_ORG }}/${{ env.IMG_REPO }}:${{ steps.tags.outputs.tag }}"
run: |
kind load docker-image -n chart-testing ${IMG}
- name: Create Test Registry
- name: Setup kustomize
env:
IMG: "${{ env.IMG_ORG }}/${{ env.IMG_REPO }}:${{ steps.tags.outputs.tag }}"
run: |
Expand All @@ -50,16 +50,13 @@ jobs:
curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" | bash -s "5.2.1" "$GITHUB_WORKSPACE/kustomize"
PATH=$GITHUB_WORKSPACE/kustomize:$PATH
echo "Display Kustomize version"
kustomize version
echo "Deploying Model Registry using Manifests; branch ${BRANCH}"
kubectl create namespace kubeflow
cd manifests/kustomize/overlays/db
kustomize edit set image kubeflow/model-registry:latest $IMG
kustomize build | kubectl apply -f -
- name: Wait for Test Registry Deployment
kustomize version
- name: Deploy Model Registry using manifests
env:
IMG: "${{ env.IMG_ORG }}/${{ env.IMG_REPO }}:${{ steps.tags.outputs.tag }}"
run: ./scripts/deploy_on_kind.sh
- name: Deployment logs
run: |
kubectl wait --for=condition=available -n kubeflow deployment/model-registry-db --timeout=5m
kubectl wait --for=condition=available -n kubeflow deployment/model-registry-deployment --timeout=5m
kubectl logs -n kubeflow deployment/model-registry-deployment
- name: Set up Python
uses: actions/setup-python@v5
Expand Down
43 changes: 42 additions & 1 deletion .github/workflows/python-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,52 @@ jobs:
pipx install --pip-args=--constraint=${{ github.workspace }}/.github/workflows/constraints.txt nox
pipx inject --pip-args=--constraint=${{ github.workspace }}/.github/workflows/constraints.txt nox nox-poetry
nox --version
- name: Generate Tag
if: matrix.session == 'tests'
shell: bash
id: tags
run: |
commit_sha=${{ github.event.after }}
tag=main-${commit_sha:0:7}
echo "tag=${tag}" >> $GITHUB_OUTPUT
- name: Build Image
if: matrix.session == 'tests'
shell: bash
env:
VERSION: ${{ steps.tags.outputs.tag }}
run: make image/build
- name: Start Kind Cluster
if: matrix.session == 'tests'
uses: helm/kind-action@v1.10.0
with:
node_image: "kindest/node:v1.27.11"
- name: Load Local Registry Test Image
if: matrix.session == 'tests'
env:
IMG: "${{ env.IMG_ORG }}/${{ env.IMG_REPO }}:${{ steps.tags.outputs.tag }}"
run: |
kind load docker-image -n chart-testing ${IMG}
- name: Setup kustomize
if: matrix.session == 'tests'
env:
IMG: "${{ env.IMG_ORG }}/${{ env.IMG_REPO }}:${{ steps.tags.outputs.tag }}"
run: |
echo "Download kustomize 5.2.1"
mkdir $GITHUB_WORKSPACE/kustomize
curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" | bash -s "5.2.1" "$GITHUB_WORKSPACE/kustomize"
PATH=$GITHUB_WORKSPACE/kustomize:$PATH
echo "Display Kustomize version"
kustomize version
- name: Deploy Model Registry using manifests
if: matrix.session == 'tests'
env:
IMG: "${{ env.IMG_ORG }}/${{ env.IMG_REPO }}:${{ steps.tags.outputs.tag }}"
run: ./scripts/deploy_on_kind.sh
- name: Run Nox
working-directory: clients/python
run: |
if [[ ${{ matrix.session }} == "tests" ]]; then
make build-mr
make deploy-mr
nox --python=${{ matrix.python }} -- --cov-report=xml
poetry build
elif [[ ${{ matrix.session }} == "mypy" ]]; then
Expand Down
9 changes: 4 additions & 5 deletions clients/python/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
all: install tidy

IMG_REGISTRY ?= docker.io
IMG_VERSION ?= latest

.PHONY: install
Expand All @@ -14,12 +13,12 @@ install:
clean:
rm -rf src/mr_openapi

.PHONY: build-mr
build-mr:
cd ../../ && IMG_REGISTRY=${IMG_REGISTRY} IMG_VERSION=${IMG_VERSION} make image/build
.PHONY: deploy-latest-mr
deploy-latest-mr:
cd ../../ && IMG_VERSION=${IMG_VERSION} make image/build && ./scripts/deploy_kind.sh

.PHONY: test-e2e
test-e2e: build-mr
test-e2e: deploy-latest-mr
poetry run pytest --e2e -s

.PHONY: test
Expand Down
61 changes: 12 additions & 49 deletions clients/python/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import time
from contextlib import asynccontextmanager
from pathlib import Path
from time import sleep
from urllib.parse import urlparse

import pytest
import requests
Expand All @@ -29,13 +29,14 @@ def pytest_collection_modifyitems(config, items):
continue


REGISTRY_HOST = "http://localhost"
REGISTRY_PORT = 8080
REGISTRY_URL = f"{REGISTRY_HOST}:{REGISTRY_PORT}"
COMPOSE_FILE = "docker-compose.yaml"
MAX_POLL_TIME = 1200 # the first build is extremely slow if using docker-compose-*local*.yaml for bootstrap of builder image
REGISTRY_URL = os.environ.get("MR_URL", "http://localhost:8080")
parsed = urlparse(REGISTRY_URL)
host, port = parsed.netloc.split(":")
REGISTRY_HOST = f"{parsed.scheme}://{host}"
REGISTRY_PORT = int(port)

MAX_POLL_TIME = 10
POLL_INTERVAL = 1
DOCKER = os.getenv("DOCKER", "docker")
start_time = time.time()


Expand Down Expand Up @@ -64,57 +65,18 @@ def poll_for_ready():
time.sleep(POLL_INTERVAL)


@pytest.fixture(scope="session")
def _compose_mr(root):
print("Assuming this is the Model Registry root directory:", root)
shared_volume = root / "test/config/ml-metadata"
sqlite_db_file = shared_volume / "metadata.sqlite.db"
if sqlite_db_file.exists():
msg = f"The file {sqlite_db_file} already exists; make sure to cancel it before running these tests."
raise FileExistsError(msg)
print(f" Starting Docker Compose in folder {root}")
p = subprocess.Popen( # noqa: S602
f"{DOCKER} compose -f {COMPOSE_FILE} up",
shell=True,
cwd=root,
)
yield

p.kill()
print(f" Closing Docker Compose in folder {root}")
subprocess.call( # noqa: S602
f"{DOCKER} compose -f {COMPOSE_FILE} down",
shell=True,
cwd=root,
)
try:
os.remove(sqlite_db_file)
print(f"Removed {sqlite_db_file} successfully.")
except Exception as e:
print(f"An error occurred while removing {sqlite_db_file}: {e}")


def cleanup(client):
async def yield_and_restart(_compose_mr, root):
async def yield_and_restart(root):
poll_for_ready()
if inspect.iscoroutinefunction(client) or inspect.isasyncgenfunction(client):
async with asynccontextmanager(client)() as async_client:
yield async_client
else:
yield client()

sqlite_db_file = root / "test/config/ml-metadata/metadata.sqlite.db"
try:
os.remove(sqlite_db_file)
print(f"Removed {sqlite_db_file} successfully.")
except Exception as e:
print(f"An error occurred while removing {sqlite_db_file}: {e}")
# we have to wait to make sure the server restarts after the file is gone
sleep(1)

print("Restarting model-registry...")
print("Cleaning DB...")
subprocess.call( # noqa: S602
f"{DOCKER} compose -f {COMPOSE_FILE} restart model-registry",
"./scripts/cleanup.sh",
shell=True,
cwd=root,
)
Expand All @@ -135,6 +97,7 @@ def event_loop():
def client() -> ModelRegistry:
return ModelRegistry(REGISTRY_HOST, REGISTRY_PORT, author="author", is_secure=False)


@pytest.fixture(scope="module")
def setup_env_user_token():
with tempfile.NamedTemporaryFile(delete=False) as token_file:
Expand Down
2 changes: 1 addition & 1 deletion clients/python/tests/regression_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_create_tagged_version(client: ModelRegistry):


@pytest.mark.e2e
def test_get_model_without_user_token(setup_env_user_token, client):
def test_get_model_without_user_token(setup_env_user_token, client: ModelRegistry):
"""Test regression for using client methods without an user_token in the init arguments.
Reported on: https://github.com/kubeflow/model-registry/issues/340
Expand Down
34 changes: 34 additions & 0 deletions scripts/cleanup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env bash

set -e

MR_NAMESPACE="${MR_NAMESPACE:-kubeflow}"
TEST_DB_NAME="${TEST_DB_NAME:-metadb}"

SQL_CMD=$(
cat <<EOF
DELETE FROM Artifact;
DELETE FROM ArtifactProperty;
DELETE FROM Association;
DELETE FROM Attribution;
DELETE FROM Context;
DELETE FROM ContextProperty;
DELETE FROM Event;
DELETE FROM EventPath;
DELETE FROM Execution;
DELETE FROM ExecutionProperty;
DELETE FROM ParentContext;
COMMIT;
EOF
)

if [[ -n "$LOCAL" ]]; then
echo 'Cleaning up local sqlite DB'

sqlite3 test/config/ml-metadata/metadata.sqlite.db <<<"BEGIN TRANSACTION; $SQL_CMD"
else
echo 'Cleaning up kubernetes MySQL DB'

kubectl exec -n "$MR_NAMESPACE" -it "$(kubectl get pods -l component=db -o jsonpath="{.items[0].metadata.name}" -n "$MR_NAMESPACE")" \
-- mysql -u root -ptest -D "$TEST_DB_NAME" -e "START TRANSACTION; $SQL_CMD"
fi
25 changes: 25 additions & 0 deletions scripts/deploy_on_kind.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash

set -e

MR_NAMESPACE="${MR_NAMESPACE:-kubeflow}"

if [[ -n "$LOCAL" ]]; then
CLUSTER_NAME="${CLUSTER_NAME:-kind}"
IMG="${IMG:-kubeflow/model-registry:latest}"

echo 'Creating local Kind cluster and loading image'
kind create cluster -n "$CLUSTER_NAME"
kind load docker-image -n "$CLUSTER_NAME" "$IMG"
echo 'Image loaded into kind cluster - use this command to port forward the mr service:'
echo "kubectl port-forward -n $MR_NAMESPACE service/model-registry-service 8080:8080 &"
fi

echo 'Deploying model registry to Kind cluster'

kubectl create namespace "$MR_NAMESPACE"
cd manifests/kustomize/overlays/db
kustomize edit set image kubeflow/model-registry:latest "$IMG"
kustomize build | kubectl apply -f -
kubectl wait --for=condition=available -n "$MR_NAMESPACE" deployment/model-registry-db --timeout=5m
kubectl wait --for=condition=available -n "$MR_NAMESPACE" deployment/model-registry-deployment --timeout=5m

0 comments on commit 5ecf0da

Please sign in to comment.