From 886915f03b182a80adedd02b2b86b3efd1ef444d Mon Sep 17 00:00:00 2001 From: Vladimir Samsonov Date: Wed, 13 Dec 2023 00:35:36 +0500 Subject: [PATCH] add cleanup for tests and remove deprecating `--record` option for kubectl --- .github/workflows/ci.yml | 2 +- kubedeployer/kubectl.py | 1 - scripts/run_tests.sh | 63 ++++++++++++++++++++++++++++++++++++---- 3 files changed, 59 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e33ba7a..7717004 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -56,7 +56,7 @@ jobs: - name: Run integration tests run: | chmod +x scripts/run_tests.sh - ./scripts/run_tests.sh + ./scripts/run_tests.sh --rm create-release: name: Create release diff --git a/kubedeployer/kubectl.py b/kubedeployer/kubectl.py index ab3f8e4..cb5e447 100644 --- a/kubedeployer/kubectl.py +++ b/kubedeployer/kubectl.py @@ -58,7 +58,6 @@ def apply_manifests(*paths: PathLike, dry_run: bool = False) -> str: cmd = ( f"kubectl apply" f" --v={settings.kube_verbosity.value}" - f" --record" # TODO: replace this flag, because it's deprecated f" --dry-run={dry_run and 'client' or 'none'}" f" {' '.join(f'-f {str(p)}' for p in paths)}" ) diff --git a/scripts/run_tests.sh b/scripts/run_tests.sh index cae9c78..7c13ef7 100755 --- a/scripts/run_tests.sh +++ b/scripts/run_tests.sh @@ -1,8 +1,61 @@ #!/bin/sh -docker stop deployer_runner -docker rm deployer_runner +need_cleanup=false -docker build --progress=plain -t deployer -f tests/containers/Dockerfile . -chmod +x tests/containers/integration_tests/integration_entrypoint.sh -docker run --rm -v /var/run/docker.sock:/var/run/docker.sock --add-host=host.docker.internal:host-gateway --entrypoint=tests/containers/integration_tests/integration_entrypoint.sh --name=deployer_runner deployer:latest \ No newline at end of file +cleanup() { + if [ "$need_cleanup" = false ]; then + return + fi + + container_output_fmt="{{.ID}} ({{.Image}})" + + docker ps --format "$container_output_fmt" | while IFS= read -r container_info; do + container_id=$(echo "$container_info" | cut -d ' ' -f 1) + docker stop "$container_id" > /dev/null \ + && echo "Stopping container $container_info" + done + + docker ps -a --format "$container_output_fmt" | while IFS= read -r container_info; do + container_id=$(echo "$container_info" | cut -d ' ' -f 1) + docker rm "$container_id" > /dev/null \ + && echo "Removing container $container_info" + done +} + +startup() { + while [ "$#" -gt 0 ] + do + case "$1" in + --rm) + need_cleanup=true + shift + ;; + --help|*) + echo "Run integration tests locally. Usage:" + echo " --rm" + echo " Automatically stop and remove all containers when tests done" + echo " --help" + echo " Print usage" + exit 1 + ;; + esac + done + + # Stop and remove running containers for escape conflicts + cleanup +} + +run() { + docker build --progress=plain -t deployer -f tests/containers/Dockerfile . + chmod +x tests/containers/integration_tests/integration_entrypoint.sh + docker run --rm \ + -v /var/run/docker.sock:/var/run/docker.sock \ + --add-host=host.docker.internal:host-gateway \ + --entrypoint=tests/containers/integration_tests/integration_entrypoint.sh \ + --name=deployer_runner deployer:latest +} + + +startup "$@" +run +cleanup