Skip to content

Commit fa15b98

Browse files
committed
Run in-cluster example in integration tests
1 parent a7a2688 commit fa15b98

File tree

4 files changed

+128
-2
lines changed

4 files changed

+128
-2
lines changed

.travis.yml

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
- 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/
2222

2323
# Download and install kind
24-
- 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
24+
- 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
2525
before_script:
2626
# Create a new Kubernetes cluster using KinD
2727
- kind create cluster
@@ -32,9 +32,28 @@ jobs:
3232
# Verify if kubernetes installation is alright
3333
- kubectl get pods -A
3434

35+
# Compile the examples
36+
- |
37+
pushd examples \
38+
&& stack build --no-terminal --fast \
39+
&& popd
40+
3541
# Run simple test
3642
- |
3743
pushd examples \
38-
&& stack build --no-terminal \
3944
&& stack exec simple \
4045
&& popd
46+
47+
# Build and load the in-cluster-example image
48+
- |
49+
pushd examples \
50+
&& cp "$(stack exec which in-cluster)" in-cluster-example \
51+
&& docker build . -f in-cluster/Dockerfile -t in-cluster-example:latest \
52+
&& kind load docker-image in-cluster-example:latest \
53+
&& popd
54+
55+
# Wait for kind node to be ready
56+
- kubectl wait --for=condition=Ready node --all
57+
58+
# Run the test pod
59+
- ./examples/in-cluster/run-test.sh

examples/in-cluster/Dockerfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM ubuntu:xenial
2+
3+
RUN apt-get update && apt-get install -y libgmp3-dev
4+
5+
COPY in-cluster-example /usr/local/bin

examples/in-cluster/run-test.sh

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
SCRIPT_DIR="$( cd "$(dirname "$0")" ; pwd -P )"
6+
MAX_SECONDS=20
7+
8+
main(){
9+
kubectl apply -f "$SCRIPT_DIR/test-pod.yaml"
10+
start_time="$(date +%s)"
11+
while true; do
12+
phase="$(get-pod-phase in-cluster-example)"
13+
consumed_seconds="$(seconds-since $start_time)"
14+
15+
if [[ "$phase" == "Succeeded" ]]; then
16+
echo "------------------------------"
17+
echo "Test passed!"
18+
echo "------------------------------"
19+
echo
20+
echo "------------------------------"
21+
echo "Logs from test:"
22+
echo "------------------------------"
23+
kubectl logs in-cluster-example
24+
exit 0
25+
elif [[ "$phase" == "Failed" ]]; then
26+
echo "------------------------------"
27+
echo "Test failed!"
28+
echo "------------------------------"
29+
print-failure in-cluster-example
30+
exit 1
31+
elif (( consumed_seconds > MAX_SECONDS )); then
32+
echo "------------------------------"
33+
echo "Test timed out after $MAX_SECONDS seconds!"
34+
echo "------------------------------"
35+
print-failure in-cluster-example
36+
exit 2
37+
else
38+
echo "Test still running, pod phase = $phase"
39+
sleep 0.5
40+
fi
41+
done
42+
}
43+
44+
get-pod-phase() {
45+
kubectl get pod $1 -o 'jsonpath={.status.phase}'
46+
}
47+
48+
print-failure() {
49+
echo
50+
echo "------------------------------"
51+
echo "Pod Description:"
52+
echo "------------------------------"
53+
kubectl describe pod $1
54+
echo
55+
echo "------------------------------"
56+
echo "Logs from test:"
57+
echo "------------------------------"
58+
kubectl logs $1
59+
}
60+
61+
# Takes epoch time
62+
seconds-since() {
63+
local start=$1
64+
local end=$(date +%s)
65+
echo $(( end - start))
66+
}
67+
68+
main

examples/in-cluster/test-pod.yaml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
apiVersion: v1
3+
kind: ServiceAccount
4+
metadata:
5+
name: in-cluster-example
6+
namespace: default
7+
---
8+
apiVersion: rbac.authorization.k8s.io/v1
9+
kind: ClusterRoleBinding
10+
metadata:
11+
name: in-cluster-example
12+
roleRef:
13+
apiGroup: rbac.authorization.k8s.io
14+
kind: ClusterRole
15+
name: cluster-admin
16+
subjects:
17+
- kind: ServiceAccount
18+
name: in-cluster-example
19+
namespace: default
20+
---
21+
apiVersion: v1
22+
kind: Pod
23+
metadata:
24+
name: in-cluster-example
25+
namespace: default
26+
spec:
27+
restartPolicy: Never
28+
serviceAccountName: in-cluster-example
29+
containers:
30+
- name: in-cluster-example
31+
image: in-cluster-example:latest
32+
imagePullPolicy: Never
33+
command:
34+
- in-cluster-example

0 commit comments

Comments
 (0)