From ea912e9a33e90e53b4af4033986450d234d82789 Mon Sep 17 00:00:00 2001 From: Michael Bridgen Date: Tue, 5 Nov 2019 10:40:20 +0000 Subject: [PATCH] Test gc by kustomizing flux deployment This adds the ability to give an alternate kustomization when installing flux, and uses that to test flux with garbage collection. --- test/e2e/12_sync.bats | 2 +- test/e2e/13_sync_gc.bats | 60 +++++++++++++++++++ .../fixtures/kustom/13_sync_gc/gc_patch.json | 3 + .../kustom/13_sync_gc/kustomization.yaml | 12 ++++ .../base/e2e_patch.json} | 0 .../{ => kustom/base}/kustomization.yaml | 2 +- test/e2e/fixtures/test_patch.yaml | 1 + test/e2e/lib/install.bash | 19 ++++-- 8 files changed, 93 insertions(+), 6 deletions(-) create mode 100644 test/e2e/13_sync_gc.bats create mode 100644 test/e2e/fixtures/kustom/13_sync_gc/gc_patch.json create mode 100644 test/e2e/fixtures/kustom/13_sync_gc/kustomization.yaml rename test/e2e/fixtures/{e2e_patch.yaml => kustom/base/e2e_patch.json} (100%) rename test/e2e/fixtures/{ => kustom/base}/kustomization.yaml (93%) create mode 100644 test/e2e/fixtures/test_patch.yaml diff --git a/test/e2e/12_sync.bats b/test/e2e/12_sync.bats index f276f3348..313c4ca85 100644 --- a/test/e2e/12_sync.bats +++ b/test/e2e/12_sync.bats @@ -38,7 +38,7 @@ function setup() { sed -i'.bak' 's%stefanprodan/podinfo:2.1.0%stefanprodan/podinfo:3.1.5%' "${clone_dir}/workloads/podinfo-dep.yaml" git -c 'user.email=foo@bar.com' -c 'user.name=Foo' commit -am "Bump podinfo" head_hash=$(git rev-list -n 1 HEAD) - git push + git push >&3 poll_until_equals "podinfo image" "stefanprodan/podinfo:3.1.5" "kubectl get pod -n demo -l app=podinfo -o\"jsonpath={['items'][0]['spec']['containers'][0]['image']}\"" git pull -f --tags sync_tag_hash=$(git rev-list -n 1 flux) diff --git a/test/e2e/13_sync_gc.bats b/test/e2e/13_sync_gc.bats new file mode 100644 index 000000000..86b5e1bce --- /dev/null +++ b/test/e2e/13_sync_gc.bats @@ -0,0 +1,60 @@ +#!/usr/bin/env bats + +load lib/env +load lib/install +load lib/poll +load lib/defer + +git_port_forward_pid="" + +function setup() { + kubectl create namespace "$FLUX_NAMESPACE" + # Install flux and the git server, allowing external access + install_git_srv flux-git-deploy git_srv_result + # shellcheck disable=SC2154 + git_ssh_cmd="${git_srv_result[0]}" + export GIT_SSH_COMMAND="$git_ssh_cmd" + # shellcheck disable=SC2154 + git_port_forward_pid="${git_srv_result[1]}" + install_flux_with_fluxctl "13_sync_gc" +} + +@test "Sync with garbage collection test" { + # Wait until flux deploys the workloads, which indicates it has at least started a sync + poll_until_true 'workload podinfo' 'kubectl -n demo describe deployment/podinfo' + + # make sure we have _finished_ a sync run + fluxctl --k8s-fwd-ns "${FLUX_NAMESPACE}" sync + + # Clone the repo and check the sync tag + local clone_dir + clone_dir="$(mktemp -d)" + defer rm -rf "$clone_dir" + git clone -b master ssh://git@localhost/git-server/repos/cluster.git "$clone_dir" + cd "$clone_dir" + local sync_tag_hash + sync_tag_hash=$(git rev-list -n 1 flux) + head_hash=$(git rev-list -n 1 HEAD) + [ "$sync_tag_hash" = "$head_hash" ] + + # Remove a manifest and commit that + git rm workloads/podinfo-dep.yaml + git -c 'user.email=foo@bar.com' -c 'user.name=Foo' commit -m "Remove podinfo deployment" + head_hash=$(git rev-list -n 1 HEAD) + git push >&3 + + fluxctl --k8s-fwd-ns "${FLUX_NAMESPACE}" sync + + poll_until_equals "podinfo deployment removed" "[]" "kubectl get deploy -n demo -o\"jsonpath={['items']}\"" + git pull -f --tags >&3 + sync_tag_hash=$(git rev-list -n 1 flux) + [ "$sync_tag_hash" = "$head_hash" ] +} + +function teardown() { + kill "$git_port_forward_pid" + # Removing the namespace also takes care of removing Flux and gitsrv. + kubectl delete namespace "$FLUX_NAMESPACE" + # Only remove the demo workloads after Flux, so that they cannot be recreated. + kubectl delete namespace "$DEMO_NAMESPACE" +} diff --git a/test/e2e/fixtures/kustom/13_sync_gc/gc_patch.json b/test/e2e/fixtures/kustom/13_sync_gc/gc_patch.json new file mode 100644 index 000000000..8a5b923a4 --- /dev/null +++ b/test/e2e/fixtures/kustom/13_sync_gc/gc_patch.json @@ -0,0 +1,3 @@ +[ + { "op": "add", "path": "/spec/template/spec/containers/0/args/-", "value": "--sync-garbage-collection" } +] diff --git a/test/e2e/fixtures/kustom/13_sync_gc/kustomization.yaml b/test/e2e/fixtures/kustom/13_sync_gc/kustomization.yaml new file mode 100644 index 000000000..fc89e8c81 --- /dev/null +++ b/test/e2e/fixtures/kustom/13_sync_gc/kustomization.yaml @@ -0,0 +1,12 @@ +bases: +- "../base" +patchesJson6902: +## this patch is for test-specific patches; supply a filename to +## install_flux_with_fluxctl and it will use that rather than the +## (empty) default. +- target: + group: apps + version: v1 + kind: Deployment + name: flux + path: gc_patch.json diff --git a/test/e2e/fixtures/e2e_patch.yaml b/test/e2e/fixtures/kustom/base/e2e_patch.json similarity index 100% rename from test/e2e/fixtures/e2e_patch.yaml rename to test/e2e/fixtures/kustom/base/e2e_patch.json diff --git a/test/e2e/fixtures/kustomization.yaml b/test/e2e/fixtures/kustom/base/kustomization.yaml similarity index 93% rename from test/e2e/fixtures/kustomization.yaml rename to test/e2e/fixtures/kustom/base/kustomization.yaml index fc1e1c4b3..129155952 100644 --- a/test/e2e/fixtures/kustomization.yaml +++ b/test/e2e/fixtures/kustom/base/kustomization.yaml @@ -12,4 +12,4 @@ patchesJson6902: version: v1 kind: Deployment name: flux - path: e2e_patch.yaml + path: e2e_patch.json diff --git a/test/e2e/fixtures/test_patch.yaml b/test/e2e/fixtures/test_patch.yaml new file mode 100644 index 000000000..fe51488c7 --- /dev/null +++ b/test/e2e/fixtures/test_patch.yaml @@ -0,0 +1 @@ +[] diff --git a/test/e2e/lib/install.bash b/test/e2e/lib/install.bash index 66341b139..85ed06de8 100755 --- a/test/e2e/lib/install.bash +++ b/test/e2e/lib/install.bash @@ -53,10 +53,21 @@ fluxctl_install_cmd="fluxctl install --git-url=ssh://git@gitsrv/git-server/repos function install_flux_with_fluxctl() { local kustomtmp kustomtmp="$(mktemp -d)" - # This generates the base descriptions, which we'll then patch with a kustomization - $fluxctl_install_cmd --namespace "${FLUX_NAMESPACE}" -o "${kustomtmp}" 2>&3 - cp ${E2E_DIR}/fixtures/{kustomization,e2e_patch}.yaml "${kustomtmp}/" - kubectl apply -k "${kustomtmp}" >&3 + mkdir "${kustomtmp}/base" + # This generates the base manifests, which we'll then patch with a kustomization + echo ">>> writing base configuration to ${kustomtmp}" >&3 + $fluxctl_install_cmd --namespace "${FLUX_NAMESPACE}" -o "${kustomtmp}/base/" + # Everything goes into one directory, but not everything is + # necessarily used by the kustomization + cp -R "${E2E_DIR}"/fixtures/kustom/* "${kustomtmp}/" + local kustomization + kustomization="base" + if [ -n "$1" ]; then + # use the kustomization given instead; ../base will still be + # there to be used as a base + kustomization="$1" + fi + kubectl apply -k "${kustomtmp}/${kustomization}" >&3 kubectl -n "${FLUX_NAMESPACE}" rollout status -w --timeout=30s deployment/flux # Add the known hosts file manually (it's much easier than editing the manifests to add a volume) local flux_podname