From a76cef6bdefbe7a01c95056157de87b5a846c4ca Mon Sep 17 00:00:00 2001 From: Paulo Gomes Date: Thu, 16 Dec 2021 15:40:17 +0000 Subject: [PATCH] Extract e2e tests from github actions Enable users to run the exact same tests locally as they would be executed as part of their PRs. Signed-off-by: Paulo Gomes --- .github/workflows/e2e.yaml | 74 +---------------------- .gitignore | 3 + Makefile | 5 +- hack/ci/e2e.sh | 119 +++++++++++++++++++++++++++++++++++++ 4 files changed, 129 insertions(+), 72 deletions(-) create mode 100755 hack/ci/e2e.sh diff --git a/.github/workflows/e2e.yaml b/.github/workflows/e2e.yaml index 12472092..c6245a06 100644 --- a/.github/workflows/e2e.yaml +++ b/.github/workflows/e2e.yaml @@ -43,76 +43,8 @@ jobs: echo 'run make test and commit changes' exit 1 fi - - name: Build container image - run: make docker-build IMG=test/source-controller TAG=latest BUILD_PLATFORMS=linux/amd64 BUILD_ARGS=--load - - name: Load test image - run: kind load docker-image test/source-controller:latest - - name: Deploy controller - run: make dev-deploy IMG=test/source-controller TAG=latest - - name: Run smoke tests - run: | - kubectl -n source-system apply -f ./config/samples - kubectl -n source-system rollout status deploy/source-controller --timeout=1m - kubectl -n source-system wait gitrepository/gitrepository-sample --for=condition=ready --timeout=1m - kubectl -n source-system wait helmrepository/helmrepository-sample --for=condition=ready --timeout=1m - kubectl -n source-system wait helmchart/helmchart-sample --for=condition=ready --timeout=1m - kubectl -n source-system delete -f ./config/samples - - name: Run HelmChart values file tests - run: | - kubectl -n source-system apply -f ./config/testdata/helmchart-valuesfile - kubectl -n source-system wait helmchart/podinfo --for=condition=ready --timeout=5m - kubectl -n source-system wait helmchart/podinfo-git --for=condition=ready --timeout=5m - kubectl -n source-system delete -f ./config/testdata/helmchart-valuesfile - - name: Setup Minio + - name: Run E2E tests env: MINIO_VER: ${{ 'v6.3.1' }} - run: | - kubectl create ns minio - helm repo add minio https://helm.min.io/ - helm upgrade --wait -i minio minio/minio \ - --version $MINIO_VER \ - --namespace minio \ - --set accessKey=myaccesskey \ - --set secretKey=mysecretkey \ - --set resources.requests.memory=128Mi \ - --set persistence.enable=false - kubectl -n minio port-forward svc/minio 9000:9000 &>/dev/null & - sleep 2 - wget -q https://dl.min.io/client/mc/release/linux-amd64/mc - chmod +x mc - ./mc alias set minio http://localhost:9000 myaccesskey mysecretkey --api S3v4 - kubectl -n source-system apply -f ./config/testdata/minio/secret.yaml - - name: Run Bucket tests - run: | - ./mc mb minio/podinfo - ./mc mirror ./config/testdata/minio/manifests/ minio/podinfo - - kubectl -n source-system apply -f ./config/testdata/bucket/source.yaml - kubectl -n source-system wait bucket/podinfo --for=condition=ready --timeout=1m - - name: Run HelmChart from Bucket tests - run: | - ./mc mb minio/charts - ./mc mirror ./controllers/testdata/charts/helmchart/ minio/charts/helmchart - - kubectl -n source-system apply -f ./config/testdata/helmchart-from-bucket/source.yaml - kubectl -n source-system wait bucket/charts --for=condition=ready --timeout=1m - kubectl -n source-system wait helmchart/helmchart-bucket --for=condition=ready --timeout=1m - - name: Run large Git repo tests - run: | - kubectl -n source-system apply -f ./config/testdata/git/large-repo.yaml - kubectl -n source-system wait gitrepository/large-repo-go-git --for=condition=ready --timeout=2m - kubectl -n source-system wait gitrepository/large-repo-libgit2 --for=condition=ready --timeout=2m - - name: Logs - run: | - kubectl -n source-system logs deploy/source-controller - - name: Debug failure - if: failure() - run: | - kubectl -n kube-system describe pods - kubectl -n source-system describe pods - kubectl -n source-system get gitrepositories -oyaml - kubectl -n source-system get helmrepositories -oyaml - kubectl -n source-system get helmcharts -oyaml - kubectl -n source-system get all - kubectl -n source-system logs deploy/source-controller - kubectl -n minio get all + CREATE_CLUSTER: false + run: make e2e diff --git a/.gitignore b/.gitignore index b9375325..b76d6a22 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,6 @@ config/release/ # Exclude all libgit2 related files hack/libgit2/ + +# Exclude temporary build files +build/ diff --git a/Makefile b/Makefile index 4f5567a0..992221c2 100644 --- a/Makefile +++ b/Makefile @@ -224,4 +224,7 @@ help: ## Display this help menu @awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-20s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST) update-attributions: - ./hack/update-attributions.sh \ No newline at end of file + ./hack/update-attributions.sh + +e2e: + ./hack/ci/e2e.sh diff --git a/hack/ci/e2e.sh b/hack/ci/e2e.sh new file mode 100755 index 00000000..296b53b7 --- /dev/null +++ b/hack/ci/e2e.sh @@ -0,0 +1,119 @@ +#!/usr/bin/env bash + +set -eoux pipefail + +MINIO_VER="${MINIO_VER:-v6.3.1}" +CREATE_CLUSTER="${CREATE_CLUSTER:-true}" +KIND_CLUSTER_NAME="${KIND_CLUSTER_NAME:-kind}" + +IMG=test/source-controller +TAG=latest +MC_RELEASE=mc.RELEASE.2021-12-10T00-14-28Z +MC_SHA256=01ec33b51ad208634deb8d701d52ac8f6be088e72563a92475ba6e6470653b73 + +ROOT_DIR="$(git rev-parse --show-toplevel)" +BUILD_DIR="${ROOT_DIR}/build" + +if "${CREATE_CLUSTER}"; then + KIND_CLUSTER_NAME="flux-${RANDOM}" + export KUBECONFIG="${ROOT_DIR}/build/kindconfig" + + echo "Spinning up flux kind cluster" + kind create cluster --name "${KIND_CLUSTER_NAME}" --kubeconfig "${KUBECONFIG}" +fi + +function cleanup(){ + EXIT_CODE="$?" + + # only dump all logs if an error has occurred + if [ ${EXIT_CODE} -ne 0 ]; then + kubectl -n kube-system describe pods + kubectl -n source-system describe pods + kubectl -n source-system get gitrepositories -oyaml + kubectl -n source-system get helmrepositories -oyaml + kubectl -n source-system get helmcharts -oyaml + kubectl -n source-system get all + kubectl -n source-system logs deploy/source-controller + kubectl -n minio get all + else + echo "All E2E tests passed!" + fi + + if "${CREATE_CLUSTER}"; then + echo "Delete cluster" + kind delete cluster --name "${KIND_CLUSTER_NAME}" + fi + exit ${EXIT_CODE} +} +trap cleanup EXIT + +kubectl wait node "${KIND_CLUSTER_NAME}-control-plane" --for=condition=ready --timeout=2m + +echo "Build, load image into kind and deploy controller" +make docker-build IMG="${IMG}" TAG="${TAG}" BUILD_PLATFORMS=linux/amd64 BUILD_ARGS=--load +kind load docker-image --name "${KIND_CLUSTER_NAME}" "${IMG}":"${TAG}" +make dev-deploy IMG="${IMG}" TAG="${TAG}" + +echo "Run smoke tests" +kubectl -n source-system apply -f "${ROOT_DIR}/config/samples" +kubectl -n source-system rollout status deploy/source-controller --timeout=1m +kubectl -n source-system wait gitrepository/gitrepository-sample --for=condition=ready --timeout=1m +kubectl -n source-system wait helmrepository/helmrepository-sample --for=condition=ready --timeout=1m +kubectl -n source-system wait helmchart/helmchart-sample --for=condition=ready --timeout=1m +kubectl -n source-system delete -f "${ROOT_DIR}/config/samples" + +echo "Run HelmChart values file tests" +kubectl -n source-system apply -f "${ROOT_DIR}/config/testdata/helmchart-valuesfile" +kubectl -n source-system wait helmchart/podinfo --for=condition=ready --timeout=5m +kubectl -n source-system wait helmchart/podinfo-git --for=condition=ready --timeout=5m +kubectl -n source-system delete -f "${ROOT_DIR}/config/testdata/helmchart-valuesfile" + +echo "Setup Minio" +kubectl create ns minio +helm repo add minio https://helm.min.io/ +helm upgrade --wait -i minio minio/minio \ + --version "${MINIO_VER}" \ + --namespace minio \ + --set accessKey=myaccesskey \ + --set secretKey=mysecretkey \ + --set resources.requests.memory=128Mi \ + --set persistence.enable=false +kubectl -n minio port-forward svc/minio 9000:9000 &>/dev/null & + +sleep 2 + +if [ ! -f "${BUILD_DIR}/mc" ]; then + mkdir -p "${BUILD_DIR}" + curl -o "${BUILD_DIR}/mc" -LO "https://dl.min.io/client/mc/release/linux-amd64/archive/${MC_RELEASE}" + if ! echo "${MC_SHA256} ${BUILD_DIR}/mc" | sha256sum --check; then + echo "Checksum failed for mc." + rm "${BUILD_DIR}/mc" + exit 1 + fi + + chmod +x "${BUILD_DIR}/mc" +fi + +"${BUILD_DIR}/mc" alias set minio http://localhost:9000 myaccesskey mysecretkey --api S3v4 +kubectl -n source-system apply -f "${ROOT_DIR}/config/testdata/minio/secret.yaml" + +echo "Run Bucket tests" +"${BUILD_DIR}/mc" mb minio/podinfo +"${BUILD_DIR}/mc" mirror "${ROOT_DIR}/config/testdata/minio/manifests/" minio/podinfo + +kubectl -n source-system apply -f "${ROOT_DIR}/config/testdata/bucket/source.yaml" +kubectl -n source-system wait bucket/podinfo --for=condition=ready --timeout=1m + + +echo "Run HelmChart from Bucket tests" +"${BUILD_DIR}/mc" mb minio/charts +"${BUILD_DIR}/mc" mirror "${ROOT_DIR}/controllers/testdata/charts/helmchart/" minio/charts/helmchart + +kubectl -n source-system apply -f "${ROOT_DIR}/config/testdata/helmchart-from-bucket/source.yaml" +kubectl -n source-system wait bucket/charts --for=condition=ready --timeout=1m +kubectl -n source-system wait helmchart/helmchart-bucket --for=condition=ready --timeout=1m + +echo "Run large Git repo tests" +kubectl -n source-system apply -f "${ROOT_DIR}/config/testdata/git/large-repo.yaml" +kubectl -n source-system wait gitrepository/large-repo-go-git --for=condition=ready --timeout=2m +kubectl -n source-system wait gitrepository/large-repo-libgit2 --for=condition=ready --timeout=2m