From 54ed71f969dc3a492086a36c431ece69e78b4790 Mon Sep 17 00:00:00 2001 From: Marco Franssen Date: Tue, 30 May 2023 20:03:07 +0200 Subject: [PATCH] Add back tests for examples Signed-off-by: Marco Franssen --- .github/scripts/parse-versions.sh | 9 ++++ .github/tests/post-install.sh | 1 - .github/tests/pre-install.sh | 10 ++-- .github/workflows/helm-chart-ci.yaml | 64 ++++++++++++++++++++++- Makefile | 8 +++ examples/external-mysql/run-tests.sh | 42 +++++++++++++++ examples/external-mysql/values.yaml | 5 +- examples/external-postgresql/run-tests.sh | 42 +++++++++++++++ examples/external-postgresql/values.yaml | 5 +- examples/production/run-tests.sh | 36 +++++++++++++ examples/tornjak/run-tests.sh | 30 +++++++++++ 11 files changed, 238 insertions(+), 14 deletions(-) create mode 100644 .github/scripts/parse-versions.sh create mode 100755 examples/external-mysql/run-tests.sh create mode 100755 examples/external-postgresql/run-tests.sh create mode 100755 examples/production/run-tests.sh create mode 100755 examples/tornjak/run-tests.sh diff --git a/.github/scripts/parse-versions.sh b/.github/scripts/parse-versions.sh new file mode 100644 index 0000000..eca9881 --- /dev/null +++ b/.github/scripts/parse-versions.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +TESTS_PATH="$(dirname "${BASH_SOURCE[0]}")/../tests" + +# Set repo and version env variables +REPOS=$(jq -r '.[] | "export " + ("HELM_REPO_" + .name | ascii_upcase | gsub("-";"_")) + "=" + .repo' "${TESTS_PATH}/charts.json") +VERSIONS=$(jq -r '.[] | "export " + ("VERSION_" + .name | ascii_upcase | gsub("-";"_")) + "=" + .version' "${TESTS_PATH}/charts.json") +eval "$REPOS" +eval "$VERSIONS" diff --git a/.github/tests/post-install.sh b/.github/tests/post-install.sh index cfa1516..1981868 100755 --- a/.github/tests/post-install.sh +++ b/.github/tests/post-install.sh @@ -4,7 +4,6 @@ set -x SCRIPT="$(readlink -f "$0")" SCRIPTPATH="$(dirname "${SCRIPT}")" -scenario="${scenario:-$(basename "${SCRIPTPATH}")}" # shellcheck source=/dev/null source "${SCRIPTPATH}/common.sh" diff --git a/.github/tests/pre-install.sh b/.github/tests/pre-install.sh index 34d0ae5..a82e767 100755 --- a/.github/tests/pre-install.sh +++ b/.github/tests/pre-install.sh @@ -6,11 +6,8 @@ SCRIPT="$(readlink -f "$0")" SCRIPTPATH="$(dirname "${SCRIPT}")" DEPS="${SCRIPTPATH}/dependencies" -# Set repo and version env variables -REPOS=$(jq -r '.[] | "export " + ("HELM_REPO_" + .name | ascii_upcase | gsub("-";"_")) + "=" + .repo' "${SCRIPTPATH}/charts.json") -VERSIONS=$(jq -r '.[] | "export " + ("VERSION_" + .name | ascii_upcase | gsub("-";"_")) + "=" + .version' "${SCRIPTPATH}/charts.json") -eval "$REPOS" -eval "$VERSIONS" +# shellcheck source=/dev/null +source "${SCRIPTPATH}/../scripts/parse-versions.sh" helm_install=(helm upgrade --install --create-namespace) @@ -21,8 +18,7 @@ kubectl create namespace spire-server || true # nginx ingress "${helm_install[@]}" ingress-nginx ingress-nginx --version "${VERSION_INGRESS_NGINX}" --repo "${HELM_REPO_INGRESS_NGINX}" \ --namespace ingress-nginx \ - --set controller.extraArgs.enable-ssl-passthrough='' \ - --wait + --set controller.extraArgs.enable-ssl-passthrough= kubectl wait --namespace ingress-nginx --for=condition=ready pod --selector=app.kubernetes.io/component=controller # prometheus diff --git a/.github/workflows/helm-chart-ci.yaml b/.github/workflows/helm-chart-ci.yaml index 45dc683..e7352ff 100644 --- a/.github/workflows/helm-chart-ci.yaml +++ b/.github/workflows/helm-chart-ci.yaml @@ -12,6 +12,7 @@ on: - '.github/tests/**/*.sh' - '.github/tests/**/*.json' - 'examples/**/*.yaml' + - 'examples/**/*.sh' - 'tests/**/*' - 'helm-docs.sh' @@ -22,6 +23,7 @@ concurrency: env: HELM_VERSION: v3.12.0 PYTHON_VERSION: 3.11.3 + KIND_VERSION: v0.19.0 CHART_TESTING_VERSION: v3.8.0 jobs: @@ -160,7 +162,7 @@ jobs: uses: helm/kind-action@v1.8.0 # Only build a kind cluster if there are chart changes to test. with: - version: v0.19.0 + version: ${{ env.KIND_VERSION }} node_image: kindest/node:${{ matrix.k8s }} config: .github/kind/conf/kind-config.yaml verbosity: 1 @@ -172,9 +174,67 @@ jobs: - name: Run chart-testing (install) run: | ct install --config ct.yaml \ - --target-branch ${{ github.base_ref }} \ + --target-branch ${{ github.base_ref }} - name: Test summary if: always() run: ./post-install.sh working-directory: .github/tests + + build-matrix: + name: Build matrix + runs-on: ubuntu-22.04 + + steps: + - name: Checkout + uses: actions/checkout@v3.5.2 + + - id: set-matrix + name: Collect all examples + run: | + examples="$(find examples -maxdepth 2 -type f -name run-tests.sh | xargs -I % dirname %)" + examples_json="$(echo "$examples" | jq -c --slurp --raw-input 'split("\n") | map(select(. != ""))')" + echo "${examples_json}" + echo "examples=$examples_json" >>"$GITHUB_OUTPUT" + + outputs: + examples: ${{ steps.set-matrix.outputs.examples }} + + example-test: + runs-on: ubuntu-22.04 + + needs: + - lint-chart + - build-matrix + + strategy: + fail-fast: false + matrix: + example: + - ${{ fromJson(needs.build-matrix.outputs.examples) }} + + steps: + - name: Checkout + uses: actions/checkout@v3.5.2 + + - name: Set up Helm + uses: azure/setup-helm@v3.5 + with: + version: ${{ env.HELM_VERSION }} + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: ${{ env.PYTHON_VERSION }} + + - name: Create kind cluster + uses: helm/kind-action@v1.7.0 + # Only build a kind cluster if there are chart changes to test. + with: + version: ${{ env.KIND_VERSION }} + node_image: kindest/node:v1.26.4 + config: .github/kind/conf/kind-config.yaml + verbosity: 1 + + - name: Install and test example + run: ${{ matrix.example }}/run-tests.sh diff --git a/Makefile b/Makefile index c6e17ed..7b4705e 100644 --- a/Makefile +++ b/Makefile @@ -52,3 +52,11 @@ cleanup-test-dependencies: ## Cleans up all test dependencies resources @kubectl delete ns postgresql 2>/dev/null || true @helm uninstall -n ingress-nginx ingress-nginx 2>/dev/null || true @kubectl delete ns ingress-nginx 2>/dev/null || true + +test-example-%: + @echo Running tests for $* example… + @examples/$*/run-tests.sh + @echo + +.PHONY: test-examples +test-examples: $(patsubst examples/%/values.yaml,test-example-%,$(wildcard examples/*/values.yaml)) ## Run helm install for and helm test for all the examples diff --git a/examples/external-mysql/run-tests.sh b/examples/external-mysql/run-tests.sh new file mode 100755 index 0000000..b6b5ee5 --- /dev/null +++ b/examples/external-mysql/run-tests.sh @@ -0,0 +1,42 @@ +#!/usr/bin/env bash + +set -xe + +SCRIPT="$(readlink -f "$0")" +SCRIPTPATH="$(dirname "${SCRIPT}")" +TESTDIR="${SCRIPTPATH}/../../.github/tests" +DEPS="${TESTDIR}/dependencies" + +# shellcheck source=/dev/null +source "${SCRIPTPATH}/../../.github/scripts/parse-versions.sh" +# shellcheck source=/dev/null +source "${TESTDIR}/common.sh" + +helm_install=(helm upgrade --install --create-namespace) +ns=spire-system + +teardown() { + helm uninstall --namespace "${ns}" spire 2>/dev/null || true + kubectl delete ns "${ns}" 2>/dev/null || true + + helm uninstall --namespace mysql mysql 2>/dev/null || true + kubectl delete ns mysql 2>/dev/null || true +} + +trap 'trap - SIGTERM && teardown' SIGINT SIGTERM EXIT + +"${helm_install[@]}" mysql mysql --version "$VERSION_MYSQL" --repo "$HELM_REPO_MYSQL" \ + --namespace mysql \ + --values "${DEPS}/mysql.yaml" \ + --wait + +"${helm_install[@]}" --namespace "${ns}" --values "${SCRIPTPATH}/values.yaml" \ + --set 'spire-server.dataStore.sql.password=sp1ff3Test' --wait spire charts/spire +helm test --namespace "${ns}" spire + +print_helm_releases +print_spire_workload_status "${ns}" + +if [[ "$1" -ne 0 ]]; then + get_namespace_details "${ns}" +fi diff --git a/examples/external-mysql/values.yaml b/examples/external-mysql/values.yaml index c0a8fff..129ee27 100644 --- a/examples/external-mysql/values.yaml +++ b/examples/external-mysql/values.yaml @@ -2,7 +2,8 @@ spire-server: dataStore: sql: databaseType: mysql - databaseName: spire - host: mysql + databaseName: spire-server + host: mysql.mysql port: 3306 username: spire + # password: CHANGE_ME diff --git a/examples/external-postgresql/run-tests.sh b/examples/external-postgresql/run-tests.sh new file mode 100755 index 0000000..1ffe1f6 --- /dev/null +++ b/examples/external-postgresql/run-tests.sh @@ -0,0 +1,42 @@ +#!/usr/bin/env bash + +set -xe + +SCRIPT="$(readlink -f "$0")" +SCRIPTPATH="$(dirname "${SCRIPT}")" +TESTDIR="${SCRIPTPATH}/../../.github/tests" +DEPS="${TESTDIR}/dependencies" + +# shellcheck source=/dev/null +source "${SCRIPTPATH}/../../.github/scripts/parse-versions.sh" +# shellcheck source=/dev/null +source "${TESTDIR}/common.sh" + +helm_install=(helm upgrade --install --create-namespace) +ns=spire-system + +teardown() { + helm uninstall --namespace "${ns}" spire 2>/dev/null || true + kubectl delete ns "${ns}" 2>/dev/null || true + + helm uninstall --namespace postgresql postgresql 2>/dev/null || true + kubectl delete ns postgresql 2>/dev/null || true +} + +trap 'trap - SIGTERM && teardown' SIGINT SIGTERM EXIT + +"${helm_install[@]}" postgresql postgresql --version "$VERSION_POSTGRESQL" --repo "$HELM_REPO_POSTGRESQL" \ + --namespace postgresql \ + --values "${DEPS}/postgresql.yaml" \ + --wait + +"${helm_install[@]}" --namespace "${ns}" --values "${SCRIPTPATH}/values.yaml" \ + --set 'spire-server.dataStore.sql.password=sp1ff3Test' --wait spire charts/spire +helm test --namespace "${ns}" spire + +print_helm_releases +print_spire_workload_status "${ns}" + +if [[ "$1" -ne 0 ]]; then + get_namespace_details "${ns}" +fi diff --git a/examples/external-postgresql/values.yaml b/examples/external-postgresql/values.yaml index 5f1fcfa..8972e87 100644 --- a/examples/external-postgresql/values.yaml +++ b/examples/external-postgresql/values.yaml @@ -2,9 +2,10 @@ spire-server: dataStore: sql: databaseType: postgres - databaseName: spire - host: postgresql + databaseName: spire-server + host: postgresql.postgresql port: 5432 username: spire + # password: CHANGE_ME options: - sslmode: disable diff --git a/examples/production/run-tests.sh b/examples/production/run-tests.sh new file mode 100755 index 0000000..f64f6c2 --- /dev/null +++ b/examples/production/run-tests.sh @@ -0,0 +1,36 @@ +#!/usr/bin/env bash + +set -xe + +SCRIPT="$(readlink -f "$0")" +SCRIPTPATH="$(dirname "${SCRIPT}")" +TESTDIR="${SCRIPTPATH}/../../.github/tests" + +# shellcheck source=/dev/null +source "${TESTDIR}/common.sh" + +helm_install=(helm upgrade --install --create-namespace) +ns=spire-server + +teardown() { + helm uninstall --namespace "${ns}" spire 2>/dev/null || true + kubectl delete ns "${ns}" 2>/dev/null || true + kubectl delete ns spire-system 2>/dev/null || true +} + +trap 'trap - SIGTERM && teardown' SIGINT SIGTERM EXIT + +kubectl create namespace spire-system 2>/dev/null || true +kubectl label namespace spire-system pod-security.kubernetes.io/enforce=privileged || true +kubectl create namespace "${ns}" 2>/dev/null || true +kubectl label namespace "${ns}" pod-security.kubernetes.io/enforce=restricted || true + +"${helm_install[@]}" --namespace "${ns}" --values "${SCRIPTPATH}/values.yaml" --wait spire charts/spire +helm test --namespace "${ns}" spire + +print_helm_releases +print_spire_workload_status "${ns}" + +if [[ "$1" -ne 0 ]]; then + get_namespace_details "${ns}" +fi diff --git a/examples/tornjak/run-tests.sh b/examples/tornjak/run-tests.sh new file mode 100755 index 0000000..e4449cd --- /dev/null +++ b/examples/tornjak/run-tests.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash + +set -xe + +SCRIPT="$(readlink -f "$0")" +SCRIPTPATH="$(dirname "${SCRIPT}")" +TESTDIR="${SCRIPTPATH}/../../.github/tests" + +# shellcheck source=/dev/null +source "${TESTDIR}/common.sh" + +helm_install=(helm upgrade --install --create-namespace) +ns=spire-system + +teardown() { + helm uninstall --namespace "${ns}" spire 2>/dev/null || true + kubectl delete ns "${ns}" 2>/dev/null || true +} + +trap 'trap - SIGTERM && teardown' SIGINT SIGTERM EXIT + +"${helm_install[@]}" --namespace "${ns}" --values "${SCRIPTPATH}/values.yaml" --wait spire charts/spire +helm test --namespace "${ns}" spire + +print_helm_releases +print_spire_workload_status "${ns}" + +if [[ "$1" -ne 0 ]]; then + get_namespace_details "${ns}" +fi