From 5e7badfc8701c170579e085206cea7c53a2f2cc3 Mon Sep 17 00:00:00 2001 From: Alfonso Acosta Date: Thu, 24 Oct 2019 14:33:42 +0200 Subject: [PATCH 1/5] Factor out polling functions in e2e test --- test/e2e/helm_chart_smoke_test.bats | 92 ++++------------------------- test/e2e/lib/poll.bash | 26 ++++++++ 2 files changed, 38 insertions(+), 80 deletions(-) create mode 100644 test/e2e/lib/poll.bash diff --git a/test/e2e/helm_chart_smoke_test.bats b/test/e2e/helm_chart_smoke_test.bats index 011b2a943..67c4fd639 100644 --- a/test/e2e/helm_chart_smoke_test.bats +++ b/test/e2e/helm_chart_smoke_test.bats @@ -1,6 +1,7 @@ #!/usr/bin/env bats load lib/install +load lib/poll function setup() { install_git_srv @@ -8,85 +9,14 @@ function setup() { install_flux_with_helm } - @test "Helm chart installation smoke test" { + # The gitconfig secret must exist and have the right value + poll_until_equals "gitconfig secret" "${GITCONFIG}" "kubectl get secrets -n "${FLUX_NAMESPACE}" gitconfig -ojsonpath={..data.gitconfig} | base64 --decode" - echo -n '>>> Waiting for gitconfig secret ' >&3 - retries=24 - count=0 - ok=false - until ${ok}; do - actual=$(kubectl get secrets -n "${FLUX_NAMESPACE}" gitconfig -ojsonpath={..data.gitconfig} | base64 --decode) - if [ "${actual}" = "${GITCONFIG}" ]; then - kubectl get secrets -n "${FLUX_NAMESPACE}" gitconfig - ok=true - else - echo -n '.' >&3 - ok=false - sleep 5 - fi - count=$(($count + 1)) - if [[ ${count} -eq ${retries} ]]; then - echo ' No more retries left' >&3 - kubectl -n "${FLUX_NAMESPACE}" get secrets - false - fi - done - echo ' done' >&3 - - echo -n ">>> Waiting for namespace ${DEMO_NAMESPACE} " >&3 - retries=24 - count=1 - ok=false - until ${ok}; do - kubectl describe "ns/${DEMO_NAMESPACE}" && ok=true || ok=false - echo -n '.' >&3 - sleep 5 - count=$(($count + 1)) - if [[ ${count} -eq ${retries} ]]; then - echo ' No more retries left' - false - fi - done - echo ' done' >&3 - - echo -n '>>> Waiting for workload podinfo ' >&3 - retries=24 - count=0 - ok=false - until ${ok}; do - kubectl -n "${DEMO_NAMESPACE}" describe deployment/podinfo && ok=true || ok=false - echo -n '.' >&3 - sleep 5 - count=$(($count + 1)) - if [[ ${count} -eq ${retries} ]]; then - echo ' No more retries left' >&3 - false - fi - done - echo ' done' >&3 - - echo -n '>>> Waiting for mongodb HelmRelease ' >&3 - retries=24 - count=0 - ok=false - until ${ok}; do - kubectl -n $DEMO_NAMESPACE describe helmrelease/mongodb && ok=true || ok=false - echo -n '.' >&3 - sleep 5 - count=$(($count + 1)) - if [[ ${count} -eq ${retries} ]]; then - echo ' No more retries left' >&3 - false - fi - done - echo ' done' >&3 - - echo '>>> List pods' >&3 - kubectl -n "${DEMO_NAMESPACE}" get pods - - echo '>>> Check workload' >&3 - kubectl -n "${DEMO_NAMESPACE}" rollout status deployment/podinfo + # Check that all the resources in the demo namespace are created by Flux + poll_until_true "namespace ${DEMO_NAMESPACE}" "kubectl describe ns/${DEMO_NAMESPACE}" + poll_until_true 'workload podinfo' "kubectl -n "${DEMO_NAMESPACE}" describe deployment/podinfo" + poll_until_true 'mongodb HelmRelease' "kubectl -n ${DEMO_NAMESPACE} describe helmrelease/mongodb" } @@ -94,9 +24,11 @@ function teardown() { # For debugging purposes (in case the test fails) echo '>>> Flux logs' kubectl -n "${FLUX_NAMESPACE}" logs deployment/flux - echo '>>> Helm Operator logs' - kubectl -n "${FLUX_NAMESPACE}" logs deployment/flux-helm-operator - + echo '>>> List pods' + kubectl -n "${DEMO_NAMESPACE}" get pods + echo '>>> Check workload' + kubectl -n "${DEMO_NAMESPACE}" rollout status deployment/podinfo + uninstall_flux_with_helm uninstall_tiller uninstall_git_srv diff --git a/test/e2e/lib/poll.bash b/test/e2e/lib/poll.bash new file mode 100644 index 000000000..6bf062652 --- /dev/null +++ b/test/e2e/lib/poll.bash @@ -0,0 +1,26 @@ +#!/usr/bin/env bash + +function poll_until_equals() { + local what="$1" + local expected="$2" + local check_cmd="$3" + poll_until_true "$what" "[ '$expected' = \"\$( $check_cmd )\" ]" +} + +function poll_until_true() { + local what="$1" + local check_cmd="$2" + echo -n ">>> Waiting for $what " >&3 + retries=24 + count=0 + until eval "$check_cmd"; do + echo -n '.' >&3 + sleep 5 + count=$(($count + 1)) + if [[ ${count} -eq ${retries} ]]; then + echo ' No more retries left!' >&3 + return 1 # fail + fi + done + echo ' done' >&3 +} From 63f966e35952ffbd89dd525b2873f80d0ad26d2f Mon Sep 17 00:00:00 2001 From: Alfonso Acosta Date: Thu, 24 Oct 2019 19:08:00 +0200 Subject: [PATCH 2/5] Add test for smoke test for 'fluxctl install' --- ...art_smoke_test.bats => 10_helm_chart.bats} | 9 +++-- test/e2e/11_fluxctl_install.bats | 29 ++++++++++++++++ test/e2e/fixtures/gitsrv.yaml | 6 ++-- test/e2e/lib/install.bash | 33 ++++++++++++++++--- test/e2e/run.bash | 5 ++- 5 files changed, 66 insertions(+), 16 deletions(-) rename test/e2e/{helm_chart_smoke_test.bats => 10_helm_chart.bats} (69%) create mode 100644 test/e2e/11_fluxctl_install.bats diff --git a/test/e2e/helm_chart_smoke_test.bats b/test/e2e/10_helm_chart.bats similarity index 69% rename from test/e2e/helm_chart_smoke_test.bats rename to test/e2e/10_helm_chart.bats index 67c4fd639..b954d1920 100644 --- a/test/e2e/helm_chart_smoke_test.bats +++ b/test/e2e/10_helm_chart.bats @@ -13,11 +13,10 @@ function setup() { # The gitconfig secret must exist and have the right value poll_until_equals "gitconfig secret" "${GITCONFIG}" "kubectl get secrets -n "${FLUX_NAMESPACE}" gitconfig -ojsonpath={..data.gitconfig} | base64 --decode" - # Check that all the resources in the demo namespace are created by Flux - poll_until_true "namespace ${DEMO_NAMESPACE}" "kubectl describe ns/${DEMO_NAMESPACE}" - poll_until_true 'workload podinfo' "kubectl -n "${DEMO_NAMESPACE}" describe deployment/podinfo" - poll_until_true 'mongodb HelmRelease' "kubectl -n ${DEMO_NAMESPACE} describe helmrelease/mongodb" - + # Test that the resources from https://github.com/fluxcd/flux-get-started are deployed + poll_until_true 'namespace demo' 'kubectl describe ns/demo' + poll_until_true 'workload podinfo' 'kubectl -n demo describe deployment/podinfo' + poll_until_true 'mongodb HelmRelease' 'kubectl -n demo describe helmrelease/mongodb' } function teardown() { diff --git a/test/e2e/11_fluxctl_install.bats b/test/e2e/11_fluxctl_install.bats new file mode 100644 index 000000000..c3e6f1401 --- /dev/null +++ b/test/e2e/11_fluxctl_install.bats @@ -0,0 +1,29 @@ +#!/usr/bin/env bats + +load lib/install +load lib/poll + +function setup() { + install_git_srv + install_flux_with_fluxctl +} + +@test "'fluxctl install' smoke test" { + # Test that the resources from https://github.com/fluxcd/flux-get-started are deployed + poll_until_true 'namespace demo' 'kubectl describe ns/demo' + poll_until_true 'workload podinfo' 'kubectl -n demo describe deployment/podinfo' +} + +function teardown() { + # For debugging purposes (in case the test fails) + echo '>>> Flux logs' + kubectl -n "${FLUX_NAMESPACE}" logs deployment/flux + echo '>>> List pods' + kubectl -n "${DEMO_NAMESPACE}" get pods + echo '>>> Check workload' + kubectl -n "${DEMO_NAMESPACE}" rollout status deployment/podinfo + + uninstall_flux_with_fluxctl + uninstall_git_srv + kubectl delete namespace demo +} diff --git a/test/e2e/fixtures/gitsrv.yaml b/test/e2e/fixtures/gitsrv.yaml index 840cb27da..d387569d1 100644 --- a/test/e2e/fixtures/gitsrv.yaml +++ b/test/e2e/fixtures/gitsrv.yaml @@ -31,11 +31,11 @@ spec: - mountPath: /git-server/repos name: git-server-data - mountPath: /git-server/keys - name: ssh-git + name: flux-git-deploy volumes: - - name: ssh-git + - name: flux-git-deploy secret: - secretName: ssh-git + secretName: flux-git-deploy - name: git-server-data emptyDir: {} --- diff --git a/test/e2e/lib/install.bash b/test/e2e/lib/install.bash index 3d48d908d..9d351870c 100755 --- a/test/e2e/lib/install.bash +++ b/test/e2e/lib/install.bash @@ -1,11 +1,11 @@ #!/usr/bin/env bash function install_tiller() { -if ! helm version > /dev/null 2>&1; then # only if helm isn't already installed + if ! helm version > /dev/null 2>&1; then # only if helm isn't already installed kubectl --namespace kube-system create sa tiller kubectl create clusterrolebinding tiller-cluster-rule --clusterrole=cluster-admin --serviceaccount=kube-system:tiller helm init --service-account tiller --upgrade --wait -fi + fi } function uninstall_tiller() { @@ -27,13 +27,13 @@ function install_flux_with_helm() { --set image.repository=docker.io/fluxcd/flux \ --set image.tag=latest \ --set git.url=ssh://git@gitsrv/git-server/repos/cluster.git \ ---set git.secretName=ssh-git \ +--set git.secretName=flux-git-deploy \ --set git.pollInterval=30s \ --set git.config.secretName=gitconfig \ --set git.config.enabled=true \ --set-string git.config.data="${GITCONFIG}" \ --set helmOperator.create=true `# just needed to add the HelmRelease CRD`\ ---set helmOperator.git.secretName=ssh-git \ +--set helmOperator.git.secretName=flux-git-deploy \ --set helmOperator.createCRD="${create_crds}" \ --set registry.excludeImage=* \ --set-string ssh.known_hosts="${KNOWN_HOSTS}" \ @@ -46,6 +46,29 @@ function uninstall_flux_with_helm() { kubectl delete crd helmreleases.flux.weave.works > /dev/null 2>&1 } +fluxctl_install_cmd="fluxctl install --namespace "${FLUX_NAMESPACE}" --git-url=ssh://git@gitsrv/git-server/repos/cluster.git --git-email=foo" + +function install_flux_with_fluxctl() { + local eol=$'\n' + # use the local Flux image instead of the latest release, use a poll interval of 30s + # and disable registry polling + $fluxctl_install_cmd | \ + sed 's%docker\.io/fluxcd/flux:.*%fluxcd/flux:latest%' | \ + sed "s%--git-email=foo%--git-email=foo\\$eol - --git-poll-interval=30s%" | \ + sed "s%--git-email=foo%--git-email=foo\\$eol - --sync-interval=30s%" | \ + sed "s%--git-email=foo%--git-email=foo\\$eol - --registry-exclude-image=\*%" | \ + kubectl apply -f - + kubectl -n "${FLUX_NAMESPACE}" rollout status deployment/flux + # Add the known hosts file manually (it's much easier than editing the manifests to add a volume) + local flux_podname=$(kubectl get pod -n flux-e2e -l name=flux -o jsonpath="{['items'][0].metadata.name}") + kubectl exec -n "${FLUX_NAMESPACE}" "${flux_podname}" -- sh -c "echo '$(cat ${FIXTURES_DIR}/known_hosts)' > /root/.ssh/known_hosts" +} + +function uninstall_flux_with_fluxctl() { + $fluxctl_install_cmd | kubectl delete -f - + +} + function install_git_srv() { kubectl apply -n "${FLUX_NAMESPACE}" -f "${E2E_DIR}/fixtures/gitsrv.yaml" kubectl -n "${FLUX_NAMESPACE}" rollout status deployment/gitsrv @@ -53,4 +76,4 @@ function install_git_srv() { function uninstall_git_srv() { kubectl delete -n "${FLUX_NAMESPACE}" -f "${E2E_DIR}/fixtures/gitsrv.yaml" -} \ No newline at end of file +} diff --git a/test/e2e/run.bash b/test/e2e/run.bash index 5eee4f189..fd0a3806e 100755 --- a/test/e2e/run.bash +++ b/test/e2e/run.bash @@ -12,7 +12,6 @@ export E2E_DIR="${FLUX_ROOT_DIR}/test/e2e" export FIXTURES_DIR="${E2E_DIR}/fixtures" export KNOWN_HOSTS=$(cat "${FIXTURES_DIR}/known_hosts") export GITCONFIG=$(cat "${FIXTURES_DIR}/gitconfig") -export DEMO_NAMESPACE=demo KIND_VERSION="v0.5.1" CACHE_DIR="${FLUX_ROOT_DIR}/cache/$CURRENT_OS_ARCH" @@ -46,7 +45,7 @@ defer kubectl delete namespace "$FLUX_NAMESPACE" echo '>>> Creating ssh key and Git access secret' ssh-keygen -t rsa -N "" -f "${FIXTURES_DIR}/id_rsa" defer rm -f "${FIXTURES_DIR}/id_rsa" "${FIXTURES_DIR}/id_rsa.pub" -kubectl create secret generic ssh-git --namespace="${FLUX_NAMESPACE}" --from-file="${FIXTURES_DIR}/known_hosts" --from-file="${FIXTURES_DIR}/id_rsa" --from-file=identity="${FIXTURES_DIR}/id_rsa" --from-file="${FIXTURES_DIR}/id_rsa.pub" +kubectl create secret generic flux-git-deploy --namespace="${FLUX_NAMESPACE}" --from-file="${FIXTURES_DIR}/known_hosts" --from-file="${FIXTURES_DIR}/id_rsa" --from-file=identity="${FIXTURES_DIR}/id_rsa" --from-file="${FIXTURES_DIR}/id_rsa.pub" if [ "${USING_KIND}" = 'true' ]; then echo '>>> Loading images into the Kind cluster' @@ -55,4 +54,4 @@ fi # Run the tests echo '>>> Running the tests' -(cd "${E2E_DIR}"; "${E2E_DIR}/bats/bin/bats" -t .) +(cd "${E2E_DIR}"; "${E2E_DIR}/bats/bin/bats" -t .) From 97a0d068792b5f2d3d3426d96e8cfba381170473 Mon Sep 17 00:00:00 2001 From: Alfonso Acosta Date: Thu, 24 Oct 2019 19:09:51 +0200 Subject: [PATCH 3/5] Reducing e2e polling interval to 10s --- test/e2e/lib/install.bash | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/e2e/lib/install.bash b/test/e2e/lib/install.bash index 9d351870c..e7f1586d7 100755 --- a/test/e2e/lib/install.bash +++ b/test/e2e/lib/install.bash @@ -28,7 +28,7 @@ function install_flux_with_helm() { --set image.tag=latest \ --set git.url=ssh://git@gitsrv/git-server/repos/cluster.git \ --set git.secretName=flux-git-deploy \ ---set git.pollInterval=30s \ +--set git.pollInterval=10s \ --set git.config.secretName=gitconfig \ --set git.config.enabled=true \ --set-string git.config.data="${GITCONFIG}" \ @@ -50,12 +50,12 @@ fluxctl_install_cmd="fluxctl install --namespace "${FLUX_NAMESPACE}" --git-url=s function install_flux_with_fluxctl() { local eol=$'\n' - # use the local Flux image instead of the latest release, use a poll interval of 30s - # and disable registry polling + # Use the local Flux image instead of the latest release, use a poll interval of 10s + # (to make tests quicker) and disable registry polling (to avoid overloading kind) $fluxctl_install_cmd | \ sed 's%docker\.io/fluxcd/flux:.*%fluxcd/flux:latest%' | \ - sed "s%--git-email=foo%--git-email=foo\\$eol - --git-poll-interval=30s%" | \ - sed "s%--git-email=foo%--git-email=foo\\$eol - --sync-interval=30s%" | \ + sed "s%--git-email=foo%--git-email=foo\\$eol - --git-poll-interval=10s%" | \ + sed "s%--git-email=foo%--git-email=foo\\$eol - --sync-interval=10s%" | \ sed "s%--git-email=foo%--git-email=foo\\$eol - --registry-exclude-image=\*%" | \ kubectl apply -f - kubectl -n "${FLUX_NAMESPACE}" rollout status deployment/flux From c14cb77f11551551a11a5d3c67dd20b7566a91fa Mon Sep 17 00:00:00 2001 From: Alfonso Acosta Date: Fri, 25 Oct 2019 11:05:31 +0200 Subject: [PATCH 4/5] Restore demo namespace env varible --- test/e2e/run.bash | 1 + 1 file changed, 1 insertion(+) diff --git a/test/e2e/run.bash b/test/e2e/run.bash index fd0a3806e..cd42ff0e4 100755 --- a/test/e2e/run.bash +++ b/test/e2e/run.bash @@ -12,6 +12,7 @@ export E2E_DIR="${FLUX_ROOT_DIR}/test/e2e" export FIXTURES_DIR="${E2E_DIR}/fixtures" export KNOWN_HOSTS=$(cat "${FIXTURES_DIR}/known_hosts") export GITCONFIG=$(cat "${FIXTURES_DIR}/gitconfig") +export DEMO_NAMESPACE=demo KIND_VERSION="v0.5.1" CACHE_DIR="${FLUX_ROOT_DIR}/cache/$CURRENT_OS_ARCH" From 005ba5c803d0122863cfbc359bd15f034bca4ed4 Mon Sep 17 00:00:00 2001 From: Alfonso Acosta Date: Fri, 25 Oct 2019 13:10:55 +0200 Subject: [PATCH 5/5] Parameterize polling retry count and wait period --- test/e2e/lib/poll.bash | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/test/e2e/lib/poll.bash b/test/e2e/lib/poll.bash index 6bf062652..fe21a110c 100644 --- a/test/e2e/lib/poll.bash +++ b/test/e2e/lib/poll.bash @@ -4,18 +4,22 @@ function poll_until_equals() { local what="$1" local expected="$2" local check_cmd="$3" - poll_until_true "$what" "[ '$expected' = \"\$( $check_cmd )\" ]" + local retries="$4" + local wait_period="$5" + poll_until_true "$what" "[ '$expected' = \"\$( $check_cmd )\" ]" "$retries" "$wait_period" } function poll_until_true() { local what="$1" local check_cmd="$2" + # timeout after $retries * $wait_period seconds + local retries=${3:-24} + local wait_period=${4:-5} echo -n ">>> Waiting for $what " >&3 - retries=24 count=0 until eval "$check_cmd"; do echo -n '.' >&3 - sleep 5 + sleep $wait_period count=$(($count + 1)) if [[ ${count} -eq ${retries} ]]; then echo ' No more retries left!' >&3