Skip to content

Commit

Permalink
add tests to OIDC authservice rock (#14)
Browse files Browse the repository at this point in the history
* add oidc-authservice rock tests

* edit dummy rock tests to implement sanity checks

edits the original copy/pasted tox.ini and test_rock.py files to suit this repo.

tox.ini was refactored because it either did not work before, or I didn't fully understand what it should be doing.
tox.ini's integration environment has been replaced with a passing stub so it does not cause CI failure.

* update per review suggestion

Co-authored-by: Orfeas Kourkakis <orfeas.kourkakis@canonical.com>

* update per review suggestion

Co-authored-by: Orfeas Kourkakis <orfeas.kourkakis@canonical.com>

* Apply suggestions from code review

* fix: rename var to be less verbose

* fix: pin chisme to main

* feat: add gitignore

* fix: gitignore

* fix: microk8s and juju channel compatible with ckf 1.7

* fix: remove pycache file

* ci: modify on pull request and add on push workflow

* fix: update build and publish workflow

---------

Co-authored-by: Orfeas Kourkakis <orfeas.kourkakis@canonical.com>
Co-authored-by: NohaIhab <noha.ihab@canonical.com>
  • Loading branch information
3 people committed Dec 15, 2023
1 parent 21476c9 commit fc0b0b8
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 8 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/build_and_publish_rock.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ name: Build and publish rock
on:
workflow_call:
inputs:
rockcraft-dir:
rock-dir:
description: "Path to rock directory, i.e. directory containing rockcraft.yaml"
required: true
default: '.'
Expand All @@ -16,7 +16,7 @@ on:
type: string
workflow_dispatch:
inputs:
rockcraft-dir:
rock-dir:
description: "Path to rock directory, i.e. directory containing rockcraft.yaml"
required: true
default: '.'
Expand All @@ -32,8 +32,8 @@ jobs:
name: Build and publish ROCK
uses: canonical/charmed-kubeflow-workflows/.github/workflows/build_and_publish_rock.yaml@main
with:
rockcraft-dir: ${{ inputs.rockcraft-dir }}
rock-dir: ${{ inputs.rock-dir }}
source-branch: ${{ inputs.source-branch }}
secrets:
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
11 changes: 7 additions & 4 deletions .github/workflows/on_pull_request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ on:

jobs:

get-rocks-modified-and-build-scan-test:
on-pull-request:
name: Get ROCKs modified and build-scan-test them
uses: canonical/charmed-kubeflow-workflows/.github/workflows/get_rocks_modified_and_build_scan_test.yaml@main
uses: canonical/charmed-kubeflow-workflows/.github/workflows/get-rocks-modified-and-build-scan-test-publish.yaml@main
permissions:
pull-requests: read
secrets: inherit
with:
microk8s-channel: 1.25/stable
juju-channel: 3.1/stable
microk8s-channel: 1.24/stable
juju-channel: 2.9/stable
python-version: "3.8"
20 changes: 20 additions & 0 deletions .github/workflows/on_push.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: On Push

on:
push:
branches:
- main
- track/**

jobs:

on-push:
name: Get ROCKs modified and build-scan-test-publish them
uses: canonical/charmed-kubeflow-workflows/.github/workflows/get-rocks-modified-and-build-scan-test-publish.yaml@main
permissions:
pull-requests: read
secrets: inherit
with:
microk8s-channel: 1.24/stable
juju-channel: 2.9/stable
python-version: "3.8"
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__pycache__
.tox
*.rock
40 changes: 40 additions & 0 deletions oidc-authservice/tests/test_rock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright 2022 Canonical Ltd.
# See LICENSE file for licensing details.
#
#

from pathlib import Path

import os
import logging
import random
import pytest
import string
import subprocess
import yaml

from charmed_kubeflow_chisme.rock import CheckRock

@pytest.fixture()
def rock_test_env(tmpdir):
"""Yields a temporary directory and random docker container name, then cleans them up after."""
container_name = "".join([str(i) for i in random.choices(string.ascii_lowercase, k=8)])
yield tmpdir, container_name

try:
subprocess.run(["docker", "rm", container_name])
except Exception:
pass
# tmpdir fixture we use here should clean up the other files for us

@pytest.mark.abort_on_fail
def test_rock(rock_test_env):
"""Test rock."""
temp_dir, container_name = rock_test_env
check_rock = CheckRock("rockcraft.yaml")
rock_image = check_rock.get_name()
rock_version = check_rock.get_version()
LOCAL_ROCK_IMAGE = f"{rock_image}:{rock_version}"

# create ROCK filesystem
subprocess.run(["docker", "run", LOCAL_ROCK_IMAGE, "exec", "ls", "-la", "/bin/oidc-authservice"], check=True)
57 changes: 57 additions & 0 deletions oidc-authservice/tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Copyright 2022 Canonical Ltd.
# See LICENSE file for licensing details.
[tox]
skipsdist = True
skip_missing_interpreters = True
envlist = unit, sanity, integration

[testenv]
setenv =
PYTHONPATH={toxinidir}
PYTHONBREAKPOINT=ipdb.set_trace
CHARM_REPO=https://github.com/canonical/oidc-gatekeeper-operator.git
CHARM_BRANCH=main
LOCAL_CHARM_DIR=charm_repo

[testenv:pack]
passenv = *
allowlist_externals =
rockcraft
commands =
rockcraft pack

[testenv:export-to-docker]
passenv = *
allowlist_externals =
bash
skopeo
yq
commands =
# pack rock and export to docker
bash -c 'NAME=$(yq eval .name rockcraft.yaml) && \
VERSION=$(yq eval .version rockcraft.yaml) && \
ARCH=$(yq eval ".platforms | keys | .[0]" rockcraft.yaml) && \
ROCK="$\{NAME\}_$\{VERSION\}_$\{ARCH\}.rock" && \
DOCKER_IMAGE=$NAME:$VERSION && \\
echo "Exporting $ROCK to docker as $DOCKER_IMAGE" && \\
skopeo --insecure-policy copy oci-archive:$ROCK docker-daemon:$DOCKER_IMAGE'

[testenv:sanity]
passenv = *
deps =
pytest
# TODO: unpin this after this chisme PR is released:
# https://github.com/canonical/charmed-kubeflow-chisme/pull/81
# charmed-kubeflow-chisme
git+https://github.com/canonical/charmed-kubeflow-chisme.git@main
commands =
# run rock tests
pytest -v --tb native --show-capture=all --log-cli-level=INFO {posargs} {toxinidir}/tests

[testenv:integration]
passenv = *
allowlist_externals =
echo
commands =
# TODO: Implement integration tests here
echo "WARNING: This is a placeholder test - no test is implemented here."

0 comments on commit fc0b0b8

Please sign in to comment.