Run in-cluster example in integration tests
This commit is contained in:
23
.travis.yml
23
.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
|
||||
|
||||
5
examples/in-cluster/Dockerfile
Normal file
5
examples/in-cluster/Dockerfile
Normal file
@@ -0,0 +1,5 @@
|
||||
FROM ubuntu:xenial
|
||||
|
||||
RUN apt-get update && apt-get install -y libgmp3-dev
|
||||
|
||||
COPY in-cluster-example /usr/local/bin
|
||||
68
examples/in-cluster/run-test.sh
Executable file
68
examples/in-cluster/run-test.sh
Executable file
@@ -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
|
||||
34
examples/in-cluster/test-pod.yaml
Normal file
34
examples/in-cluster/test-pod.yaml
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user