Skip to content

Commit

Permalink
test: fix unreliability, add checking if resource is available
Browse files Browse the repository at this point in the history
kubectl wait is able to wait only for resource available on API
  • Loading branch information
kasia-kujawa committed Apr 2, 2021
1 parent df550bb commit 4b0c230
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 21 deletions.
6 changes: 1 addition & 5 deletions operator/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ deploy: manifests kustomize
sed -i.backup "s#sumologic/tailing-sidecar:latest#${TAILING_SIDECAR_IMG}#g" config/default/manager_patch.yaml
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
$(KUSTOMIZE) build config/default | kubectl apply -f -
kubectl wait --for=condition=available --timeout 60s deployment tailing-sidecar-operator -n tailing-sidecar-system
kubectl wait --for=condition=ready --timeout 60s pod -l control-plane=tailing-sidecar-operator -n tailing-sidecar-system

# Remove resources created with make deploy
Expand All @@ -78,10 +79,6 @@ deploy-examples:
kubectl apply -f examples/deployment_with_annotations.yaml
kubectl apply -f examples/daemonset_with_annotations.yaml
kubectl apply -f examples/pod_without_annotations.yaml
kubectl wait --for=condition=ready --timeout 60s pod -l app=pod-with-annotations -n tailing-sidecar-system
kubectl wait --for=condition=ready --timeout 60s pod -l app=deployment-with-annotations -n tailing-sidecar-system
kubectl wait --for=condition=ready --timeout 60s pod -l app=statefulset-with-annotations -n tailing-sidecar-system
kubectl wait --for=condition=ready --timeout 60s pod -l app=daemonset-with-annotations -n tailing-sidecar-system

# Deploy examples update
deploy-examples-update:
Expand All @@ -91,7 +88,6 @@ deploy-examples-update:
deploy-example-with-crd:
kubectl apply -f config/samples/tailing-sidecar_v1_tailingsidecar.yaml -n tailing-sidecar-system
kubectl apply -f tests/pod_with_annotations_crd.yaml
kubectl wait --for=condition=ready --timeout 60s pod -l app=pod-with-annotations-crd -n tailing-sidecar-system

# Remove resources created with make deploy-examples
teardown-examples:
Expand Down
20 changes: 20 additions & 0 deletions operator/tests/functions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash

function wait_for_pod() {
local namespace="$1"
local pod="$2"
local time="$3"
local state="Running"

for i in $(seq 0 $time)
do
get="$(kubectl get pods -n $namespace $pod)"
if [[ ! $get =~ $state ]]; then
echo "Waiting for pod $pod in $i interation"
sleep 1
else
echo "Found pod $pod"
break
fi
done
}
19 changes: 14 additions & 5 deletions operator/tests/test-update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@

set -e

readonly ROOT_DIR="$(dirname "$(dirname "${0}")")"
source "${ROOT_DIR}"/tests/functions.sh

readonly NAMESPACE="tailing-sidecar-system"
readonly TIME=300

readonly POD="pod-with-annotations"
wait_for_pod ${NAMESPACE} ${POD} ${TIME}
# Check Pod logs
[[ $(kubectl logs pod-with-annotations tailing-sidecar-0 -n tailing-sidecar-system --tail 5 | grep example | wc -l) -ne 5 ]] && exit 1
[[ $(kubectl logs pod-with-annotations named-sidecar -n tailing-sidecar-system --tail 5 | grep example | wc -l) -ne 5 ]] && exit 1
[[ $(kubectl logs pod-with-annotations tailing-sidecar-1 -n tailing-sidecar-system --tail 5 | grep example | wc -l) -ne 5 ]] && exit 1
[[ $(kubectl logs ${POD} tailing-sidecar-0 -n ${NAMESPACE} --tail 5 | grep example | wc -l) -ne 5 ]] && exit 1
[[ $(kubectl logs ${POD} named-sidecar -n ${NAMESPACE} --tail 5 | grep example | wc -l) -ne 5 ]] && exit 1
[[ $(kubectl logs ${POD} tailing-sidecar-1 -n ${NAMESPACE} --tail 5 | grep example | wc -l) -ne 5 ]] && exit 1

# Check Deployment logs
readonly DEPLOYMENT_POD_NAME="$(kubectl get pod -l app=deployment-with-annotations -n tailing-sidecar-system -o jsonpath="{.items[0].metadata.name}")"
[[ $(kubectl logs ${DEPLOYMENT_POD_NAME} tailing-sidecar-0 -n tailing-sidecar-system --tail 5 | grep example | wc -l) -ne 5 ]] && exit 1
readonly DEPLOYMENT_POD_NAME="$(kubectl get pod -l app=deployment-with-annotations -n ${NAMESPACE} -o jsonpath="{.items[0].metadata.name}")"
wait_for_pod ${NAMESPACE} ${DEPLOYMENT_POD_NAME} ${TIME}
[[ $(kubectl logs ${DEPLOYMENT_POD_NAME} tailing-sidecar-0 -n ${NAMESPACE} --tail 5 | grep example | wc -l) -ne 5 ]] && exit 1

echo "ok"
exit 0
36 changes: 25 additions & 11 deletions operator/tests/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,40 @@

set -e

readonly ROOT_DIR="$(dirname "$(dirname "${0}")")"
source "${ROOT_DIR}"/tests/functions.sh

readonly NAMESPACE="tailing-sidecar-system"
readonly TIME=60

# Check Pod logs
[[ $(kubectl logs pod-with-annotations tailing-sidecar-0 -n tailing-sidecar-system --tail 5 | grep example | wc -l) -ne 5 ]] && exit 1
[[ $(kubectl logs pod-with-annotations named-container -n tailing-sidecar-system --tail 5 | grep example | wc -l) -ne 5 ]] && exit 1
readonly POD="pod-with-annotations"
wait_for_pod ${NAMESPACE} ${POD} ${TIME}
[[ $(kubectl logs ${POD} tailing-sidecar-0 -n ${NAMESPACE} --tail 5 | grep example | wc -l) -ne 5 ]] && exit 1
[[ $(kubectl logs ${POD} named-container -n ${NAMESPACE} --tail 5 | grep example | wc -l) -ne 5 ]] && exit 1

# Check StatefulSet logs
[[ $(kubectl logs statefulset-with-annotations-0 my-named-sidecar -n tailing-sidecar-system --tail 5 | grep example | wc -l) -ne 5 ]] && exit 1
[[ $(kubectl logs statefulset-with-annotations-0 tailing-sidecar-1 -n tailing-sidecar-system --tail 5 | grep example | wc -l) -ne 5 ]] && exit 1
readonly STATEFULSET_POD_NAME="statefulset-with-annotations-0"
wait_for_pod ${NAMESPACE} ${STATEFULSET_POD_NAME} ${TIME}
[[ $(kubectl logs ${STATEFULSET_POD_NAME} my-named-sidecar -n ${NAMESPACE} --tail 5 | grep example | wc -l) -ne 5 ]] && exit 1
[[ $(kubectl logs ${STATEFULSET_POD_NAME} tailing-sidecar-1 -n ${NAMESPACE} --tail 5 | grep example | wc -l) -ne 5 ]] && exit 1

# Check Deployment logs
readonly DEPLOYMENT_POD_NAME="$(kubectl get pod -l app=deployment-with-annotations -n tailing-sidecar-system -o jsonpath="{.items[0].metadata.name}")"
[[ $(kubectl logs ${DEPLOYMENT_POD_NAME} tailing-sidecar-0 -n tailing-sidecar-system --tail 5 | grep example | wc -l) -ne 5 ]] && exit 1
[[ $(kubectl logs ${DEPLOYMENT_POD_NAME} tailing-sidecar-1 -n tailing-sidecar-system --tail 5 | grep example | wc -l) -ne 5 ]] && exit 1
readonly DEPLOYMENT_POD_NAME="$(kubectl get pod -l app=deployment-with-annotations -n ${NAMESPACE} -o jsonpath="{.items[0].metadata.name}")"
wait_for_pod ${NAMESPACE} ${DEPLOYMENT_POD_NAME} ${TIME}
[[ $(kubectl logs ${DEPLOYMENT_POD_NAME} tailing-sidecar-0 -n ${NAMESPACE} --tail 5 | grep example | wc -l) -ne 5 ]] && exit 1
[[ $(kubectl logs ${DEPLOYMENT_POD_NAME} tailing-sidecar-1 -n ${NAMESPACE} --tail 5 | grep example | wc -l) -ne 5 ]] && exit 1

# Check Daemonset logs
readonly DAEMONSET_POD_NAME="$(kubectl get pod -l app=daemonset-with-annotations -n tailing-sidecar-system -o jsonpath="{.items[0].metadata.name}")"
[[ $(kubectl logs ${DAEMONSET_POD_NAME} tailing-sidecar-0 -n tailing-sidecar-system --tail 5 | grep example | wc -l) -ne 5 ]] && exit 1
[[ $(kubectl logs ${DAEMONSET_POD_NAME} tailing-sidecar-1 -n tailing-sidecar-system --tail 5 | grep example | wc -l) -ne 5 ]] && exit 1
readonly DAEMONSET_POD_NAME="$(kubectl get pod -l app=daemonset-with-annotations -n ${NAMESPACE} -o jsonpath="{.items[0].metadata.name}")"
wait_for_pod ${NAMESPACE} ${DAEMONSET_POD_NAME} ${TIME}
[[ $(kubectl logs ${DAEMONSET_POD_NAME} tailing-sidecar-0 -n ${NAMESPACE} --tail 5 | grep example | wc -l) -ne 5 ]] && exit 1
[[ $(kubectl logs ${DAEMONSET_POD_NAME} tailing-sidecar-1 -n ${NAMESPACE} --tail 5 | grep example | wc -l) -ne 5 ]] && exit 1

# Test Pod with configuration in CRD
[[ $(kubectl logs pod-with-annotations-crd tailing-sidecar-0 -n tailing-sidecar-system --tail 5 | grep example | wc -l) -ne 5 ]] && exit 1
readonly POD_WITH_CRD="pod-with-annotations-crd"
wait_for_pod ${NAMESPACE} ${POD_WITH_CRD} ${TIME}
[[ $(kubectl logs ${POD_WITH_CRD} tailing-sidecar-0 -n ${NAMESPACE} --tail 5 | grep example | wc -l) -ne 5 ]] && exit 1

echo "ok"
exit 0

0 comments on commit 4b0c230

Please sign in to comment.