From 9c8b6879cb563bcbc0cf3dad570a2a293d58dcd6 Mon Sep 17 00:00:00 2001 From: Artem Yevsiukov Date: Wed, 31 Jul 2024 14:15:06 +0300 Subject: [PATCH] Updated `admission-webhook` (#124) * Updated `admission-webhook` * updated version * refactor * fixed tests * refactored --- admission-webhook/rockcraft.yaml | 25 +++++-------- admission-webhook/tests/test_rock.py | 55 ++++++++++++++++++++++++++++ admission-webhook/tox.ini | 51 ++++++++++++++++++++++++++ 3 files changed, 115 insertions(+), 16 deletions(-) create mode 100644 admission-webhook/tests/test_rock.py create mode 100644 admission-webhook/tox.ini diff --git a/admission-webhook/rockcraft.yaml b/admission-webhook/rockcraft.yaml index 89a396a..3907754 100644 --- a/admission-webhook/rockcraft.yaml +++ b/admission-webhook/rockcraft.yaml @@ -1,6 +1,6 @@ name: admission-webhook -base: ubuntu:20.04 -version: v1.7.0_1 # version format: _ +base: ubuntu@20.04 +version: v1.9.0 summary: An image for Kubeflow's admission-webhook description: | Admission webhook controller in general, intercepts requests to the Kubernetes API server, @@ -11,12 +11,12 @@ description: | license: Apache-2.0 platforms: - amd64: + amd64: services: base-admission-webhook: override: merge - command: webhook + command: "admission-webhook" startup: enabled user: ubuntu @@ -30,22 +30,15 @@ parts: admission-webhook: plugin: go build-snaps: - - go/1.17/stable + - go/1.21/stable source: https://github.com/kubeflow/kubeflow.git - source-tag: v1.7-branch - build-environment: - - CGO_ENABLED: 0 - - GOOS: linux - override-build: | - cd components/admission-webhook - go build -o webhook -a . - cp webhook $CRAFT_PART_INSTALL/webhook - organize: - webhook: "bin/webhook" + source-tag: v1.9.0 + source-depth: 1 + source-subdir: components/admission-webhook non-root-user: plugin: nil - after: [admission-webhook] + after: [ admission-webhook ] overlay-script: | # Create a user in the $CRAFT_OVERLAY chroot groupadd -R $CRAFT_OVERLAY -g 1001 ubuntu diff --git a/admission-webhook/tests/test_rock.py b/admission-webhook/tests/test_rock.py new file mode 100644 index 0000000..97efcef --- /dev/null +++ b/admission-webhook/tests/test_rock.py @@ -0,0 +1,55 @@ +# Copyright 2024 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}" + + subprocess.run( + [ + "docker", + "run", + "--rm", + LOCAL_ROCK_IMAGE, + "exec", + "ls", + "-la", + "/bin/admission-webhook", + ], + check=True, + ) diff --git a/admission-webhook/tox.ini b/admission-webhook/tox.ini new file mode 100644 index 0000000..1055ca6 --- /dev/null +++ b/admission-webhook/tox.ini @@ -0,0 +1,51 @@ +# Copyright 2024 Canonical Ltd. +# See LICENSE file for licensing details. +[tox] +skipsdist = True +skip_missing_interpreters = True +envlist = pack, export-to-docker, sanity, integration + +[testenv] +setenv = + PYTHONPATH={toxinidir} + PYTHONBREAKPOINT=ipdb.set_trace + +[testenv:pack] +passenv = * +allowlist_externals = + rockcraft +commands = + rockcraft pack + +[testenv:export-to-docker] +passenv = * +allowlist_externals = + bash + skopeo + yq +commands = + # export rock 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 + charmed-kubeflow-chisme +commands = + # run rock tests + pytest -s -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."