From fa15b988206a68cd3511579a1fd28255ff3c6910 Mon Sep 17 00:00:00 2001 From: Akshay Mankar Date: Sat, 12 Oct 2019 13:41:37 +0100 Subject: [PATCH] Run in-cluster example in integration tests --- .travis.yml | 23 ++++++++++- examples/in-cluster/Dockerfile | 5 +++ examples/in-cluster/run-test.sh | 68 +++++++++++++++++++++++++++++++ examples/in-cluster/test-pod.yaml | 34 ++++++++++++++++ 4 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 examples/in-cluster/Dockerfile create mode 100755 examples/in-cluster/run-test.sh create mode 100644 examples/in-cluster/test-pod.yaml diff --git a/.travis.yml b/.travis.yml index 89f60ab..fb35a0d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,7 +21,7 @@ jobs: - curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl && chmod +x kubectl && sudo mv kubectl /usr/local/bin/ # Download and install kind - - curl -LO https://github.com/kubernetes-sigs/kind/releases/download/v0.4.0/kind-linux-amd64 && chmod +x kind-linux-amd64 && sudo mv kind-linux-amd64 /usr/local/bin/kind + - curl -LO https://github.com/kubernetes-sigs/kind/releases/download/v0.5.1/kind-linux-amd64 && chmod +x kind-linux-amd64 && sudo mv kind-linux-amd64 /usr/local/bin/kind before_script: # Create a new Kubernetes cluster using KinD - kind create cluster @@ -32,9 +32,28 @@ jobs: # Verify if kubernetes installation is alright - kubectl get pods -A + # Compile the examples + - | + pushd examples \ + && stack build --no-terminal --fast \ + && popd + # Run simple test - | pushd examples \ - && stack build --no-terminal \ && stack exec simple \ && popd + + # Build and load the in-cluster-example image + - | + pushd examples \ + && cp "$(stack exec which in-cluster)" in-cluster-example \ + && docker build . -f in-cluster/Dockerfile -t in-cluster-example:latest \ + && kind load docker-image in-cluster-example:latest \ + && popd + + # Wait for kind node to be ready + - kubectl wait --for=condition=Ready node --all + + # Run the test pod + - ./examples/in-cluster/run-test.sh diff --git a/examples/in-cluster/Dockerfile b/examples/in-cluster/Dockerfile new file mode 100644 index 0000000..31cbe55 --- /dev/null +++ b/examples/in-cluster/Dockerfile @@ -0,0 +1,5 @@ +FROM ubuntu:xenial + +RUN apt-get update && apt-get install -y libgmp3-dev + +COPY in-cluster-example /usr/local/bin diff --git a/examples/in-cluster/run-test.sh b/examples/in-cluster/run-test.sh new file mode 100755 index 0000000..ae07e7b --- /dev/null +++ b/examples/in-cluster/run-test.sh @@ -0,0 +1,68 @@ +#!/bin/bash + +set -euo pipefail + +SCRIPT_DIR="$( cd "$(dirname "$0")" ; pwd -P )" +MAX_SECONDS=20 + +main(){ + kubectl apply -f "$SCRIPT_DIR/test-pod.yaml" + start_time="$(date +%s)" + while true; do + phase="$(get-pod-phase in-cluster-example)" + consumed_seconds="$(seconds-since $start_time)" + + if [[ "$phase" == "Succeeded" ]]; then + echo "------------------------------" + echo "Test passed!" + echo "------------------------------" + echo + echo "------------------------------" + echo "Logs from test:" + echo "------------------------------" + kubectl logs in-cluster-example + exit 0 + elif [[ "$phase" == "Failed" ]]; then + echo "------------------------------" + echo "Test failed!" + echo "------------------------------" + print-failure in-cluster-example + exit 1 + elif (( consumed_seconds > MAX_SECONDS )); then + echo "------------------------------" + echo "Test timed out after $MAX_SECONDS seconds!" + echo "------------------------------" + print-failure in-cluster-example + exit 2 + else + echo "Test still running, pod phase = $phase" + sleep 0.5 + fi + done +} + +get-pod-phase() { + kubectl get pod $1 -o 'jsonpath={.status.phase}' +} + +print-failure() { + echo + echo "------------------------------" + echo "Pod Description:" + echo "------------------------------" + kubectl describe pod $1 + echo + echo "------------------------------" + echo "Logs from test:" + echo "------------------------------" + kubectl logs $1 +} + +# Takes epoch time +seconds-since() { + local start=$1 + local end=$(date +%s) + echo $(( end - start)) +} + +main diff --git a/examples/in-cluster/test-pod.yaml b/examples/in-cluster/test-pod.yaml new file mode 100644 index 0000000..795b3c9 --- /dev/null +++ b/examples/in-cluster/test-pod.yaml @@ -0,0 +1,34 @@ +--- +apiVersion: v1 +kind: ServiceAccount +metadata: + name: in-cluster-example + namespace: default +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRoleBinding +metadata: + name: in-cluster-example +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: cluster-admin +subjects: +- kind: ServiceAccount + name: in-cluster-example + namespace: default +--- +apiVersion: v1 +kind: Pod +metadata: + name: in-cluster-example + namespace: default +spec: + restartPolicy: Never + serviceAccountName: in-cluster-example + containers: + - name: in-cluster-example + image: in-cluster-example:latest + imagePullPolicy: Never + command: + - in-cluster-example