|
1 | 1 | #!/usr/bin/env bash
|
2 | 2 |
|
| 3 | +set -eux |
| 4 | + |
3 | 5 | source hack/lib/test_lib.sh
|
4 | 6 |
|
5 | 7 | DEST_IMAGE="quay.io/example/memcached-operator:v0.0.2"
|
6 |
| - |
7 |
| -set -ex |
| 8 | +ROOTDIR="$(pwd)" |
| 9 | +GOTMP="$(mktemp -d -p $GOPATH/src)" |
| 10 | +trap_add 'rm -rf $GOTMP' EXIT |
| 11 | +BASEIMAGEDIR="$GOTMP/ansible-operator" |
| 12 | +mkdir -p "$BASEIMAGEDIR" |
| 13 | + |
| 14 | +deploy_operator() { |
| 15 | + kubectl create -f "$OPERATORDIR/deploy/service_account.yaml" |
| 16 | + kubectl create -f "$OPERATORDIR/deploy/role.yaml" |
| 17 | + kubectl create -f "$OPERATORDIR/deploy/role_binding.yaml" |
| 18 | + kubectl create -f "$OPERATORDIR/deploy/crds/ansible_v1alpha1_memcached_crd.yaml" |
| 19 | + kubectl create -f "$OPERATORDIR/deploy/operator.yaml" |
| 20 | +} |
| 21 | + |
| 22 | +remove_operator() { |
| 23 | + kubectl delete --ignore-not-found=true -f "$OPERATORDIR/deploy/service_account.yaml" |
| 24 | + kubectl delete --ignore-not-found=true -f "$OPERATORDIR/deploy/role.yaml" |
| 25 | + kubectl delete --ignore-not-found=true -f "$OPERATORDIR/deploy/role_binding.yaml" |
| 26 | + kubectl delete --ignore-not-found=true -f "$OPERATORDIR/deploy/crds/ansible_v1alpha1_memcached_crd.yaml" |
| 27 | + kubectl delete --ignore-not-found=true -f "$OPERATORDIR/deploy/operator.yaml" |
| 28 | +} |
| 29 | + |
| 30 | +test_operator() { |
| 31 | + # wait for operator pod to run |
| 32 | + if ! timeout 1m kubectl rollout status deployment/memcached-operator; |
| 33 | + then |
| 34 | + echo FAIL: operator failed to run |
| 35 | + kubectl logs deployment/memcached-operator |
| 36 | + exit 1 |
| 37 | + fi |
| 38 | + |
| 39 | + # create CR |
| 40 | + kubectl create -f deploy/crds/ansible_v1alpha1_memcached_cr.yaml |
| 41 | + if ! timeout 20s bash -c -- 'until kubectl get deployment -l app=memcached | grep memcached; do sleep 1; done'; |
| 42 | + then |
| 43 | + echo FAIL: operator failed to create memcached Deployment |
| 44 | + kubectl logs deployment/memcached-operator |
| 45 | + exit 1 |
| 46 | + fi |
| 47 | + memcached_deployment=$(kubectl get deployment -l app=memcached -o jsonpath="{..metadata.name}") |
| 48 | + if ! timeout 1m kubectl rollout status deployment/${memcached_deployment}; |
| 49 | + then |
| 50 | + echo FAIL: memcached Deployment failed rollout |
| 51 | + kubectl logs deployment/${memcached_deployment} |
| 52 | + exit 1 |
| 53 | + fi |
| 54 | + |
| 55 | + |
| 56 | + # make a configmap that the finalizer should remove |
| 57 | + kubectl create configmap deleteme |
| 58 | + trap_add 'kubectl delete --ignore-not-found configmap deleteme' EXIT |
| 59 | + |
| 60 | + kubectl delete -f ${OPERATORDIR}/deploy/crds/ansible_v1alpha1_memcached_cr.yaml --wait=true |
| 61 | + # if the finalizer did not delete the configmap... |
| 62 | + if kubectl get configmap deleteme; |
| 63 | + then |
| 64 | + echo FAIL: the finalizer did not delete the configmap |
| 65 | + kubectl logs deployment/memcached-operator |
| 66 | + exit 1 |
| 67 | + fi |
| 68 | + |
| 69 | + # The deployment should get garbage collected, so we expect to fail getting the deployment. |
| 70 | + if ! timeout 20s bash -c -- "while kubectl get deployment ${memcached_deployment}; do sleep 1; done"; |
| 71 | + then |
| 72 | + echo FAIL: memcached Deployment did not get garbage collected |
| 73 | + kubectl logs deployment/memcached-operator |
| 74 | + exit 1 |
| 75 | + fi |
| 76 | + |
| 77 | + ## TODO enable when this is fixed: https://github.com/operator-framework/operator-sdk/issues/818 |
| 78 | + # if kubectl logs deployment/memcached-operator | grep -i error; |
| 79 | + # then |
| 80 | + # echo FAIL: the operator log includes errors |
| 81 | + # kubectl logs deployment/memcached-operator |
| 82 | + # exit 1 |
| 83 | + # fi |
| 84 | +} |
8 | 85 |
|
9 | 86 | # switch to the "default" namespace if on openshift, to match the minikube test
|
10 | 87 | if which oc 2>/dev/null; then oc project default; fi
|
11 | 88 |
|
12 | 89 | # build operator binary and base image
|
13 |
| -go build -o test/ansible-operator/ansible-operator test/ansible-operator/cmd/ansible-operator/main.go |
14 |
| -pushd test/ansible-operator |
15 |
| -docker build -t quay.io/water-hole/ansible-operator . |
| 90 | +pushd "$BASEIMAGEDIR" |
| 91 | +go run "$ROOTDIR/commands/ansible-operator-base/main.go" |
| 92 | +dep ensure |
| 93 | + |
| 94 | +# overwrite operator-sdk source with the latest source from the local checkout |
| 95 | +pushd vendor/github.com/operator-framework/ |
| 96 | +rm -Rf operator-sdk/* |
| 97 | +cp -a "$ROOTDIR"/{pkg,version,LICENSE} operator-sdk/ |
16 | 98 | popd
|
17 | 99 |
|
18 |
| -# Make a test directory for Ansible tests so we avoid using default GOPATH. |
19 |
| -# Save test directory so we can delete it on exit. |
20 |
| -ANSIBLE_TEST_DIR="$(mktemp -d)" |
21 |
| -trap_add 'rm -rf $ANSIBLE_TEST_DIR' EXIT |
22 |
| -cp -a test/ansible-* "$ANSIBLE_TEST_DIR" |
23 |
| -pushd "$ANSIBLE_TEST_DIR" |
24 |
| - |
25 |
| -# Ansible tests should not run in a Golang environment. |
26 |
| -unset GOPATH GOROOT |
| 100 | +operator-sdk build quay.io/water-hole/ansible-operator |
| 101 | +popd |
27 | 102 |
|
28 | 103 | # create and build the operator
|
| 104 | +pushd "$GOTMP" |
29 | 105 | operator-sdk new memcached-operator --api-version=ansible.example.com/v1alpha1 --kind=Memcached --type=ansible
|
30 |
| -cp ansible-memcached/tasks.yml memcached-operator/roles/Memcached/tasks/main.yml |
31 |
| -cp ansible-memcached/defaults.yml memcached-operator/roles/Memcached/defaults/main.yml |
32 |
| -cp -a ansible-memcached/memfin memcached-operator/roles/ |
33 |
| -cat ansible-memcached/watches-finalizer.yaml >> memcached-operator/watches.yaml |
| 106 | +cp "$ROOTDIR/test/ansible-memcached/tasks.yml" memcached-operator/roles/Memcached/tasks/main.yml |
| 107 | +cp "$ROOTDIR/test/ansible-memcached/defaults.yml" memcached-operator/roles/Memcached/defaults/main.yml |
| 108 | +cp -a "$ROOTDIR/test/ansible-memcached/memfin" memcached-operator/roles/ |
| 109 | +cat "$ROOTDIR/test/ansible-memcached/watches-finalizer.yaml" >> memcached-operator/watches.yaml |
34 | 110 |
|
35 | 111 | pushd memcached-operator
|
36 | 112 | operator-sdk build "$DEST_IMAGE"
|
37 | 113 | sed -i "s|REPLACE_IMAGE|$DEST_IMAGE|g" deploy/operator.yaml
|
38 | 114 | sed -i 's|Always|Never|g' deploy/operator.yaml
|
39 | 115 |
|
40 |
| -DIR2="$(pwd)" |
41 |
| -# deploy the operator |
42 |
| -kubectl create -f deploy/service_account.yaml |
43 |
| -trap_add 'kubectl delete -f ${DIR2}/deploy/service_account.yaml' EXIT |
44 |
| -kubectl create -f deploy/role.yaml |
45 |
| -trap_add 'kubectl delete -f ${DIR2}/deploy/role.yaml' EXIT |
46 |
| -kubectl create -f deploy/role_binding.yaml |
47 |
| -trap_add 'kubectl delete -f ${DIR2}/deploy/role_binding.yaml' EXIT |
48 |
| -kubectl create -f deploy/crds/ansible_v1alpha1_memcached_crd.yaml |
49 |
| -trap_add 'kubectl delete -f ${DIR2}/deploy/crds/ansible_v1alpha1_memcached_crd.yaml' EXIT |
50 |
| -kubectl create -f deploy/operator.yaml |
51 |
| -trap_add 'kubectl delete -f ${DIR2}/deploy/operator.yaml' EXIT |
52 |
| - |
53 |
| -# wait for operator pod to run |
54 |
| -if ! timeout 1m kubectl rollout status deployment/memcached-operator; |
55 |
| -then |
56 |
| - kubectl logs deployment/memcached-operator |
57 |
| - exit 1 |
58 |
| -fi |
| 116 | +OPERATORDIR="$(pwd)" |
59 | 117 |
|
60 |
| -# create CR |
61 |
| -kubectl create -f deploy/crds/ansible_v1alpha1_memcached_cr.yaml |
62 |
| -if ! timeout 20s bash -c -- 'until kubectl get deployment -l app=memcached | grep memcached; do sleep 1; done'; |
63 |
| -then |
64 |
| - kubectl logs deployment/memcached-operator |
65 |
| - exit 1 |
66 |
| -fi |
67 |
| -memcached_deployment=$(kubectl get deployment -l app=memcached -o jsonpath="{..metadata.name}") |
68 |
| -if ! timeout 1m kubectl rollout status deployment/${memcached_deployment}; |
69 |
| -then |
70 |
| - kubectl logs deployment/${memcached_deployment} |
71 |
| - exit 1 |
72 |
| -fi |
| 118 | +deploy_operator |
| 119 | +trap_add 'remove_operator' EXIT |
| 120 | +test_operator |
| 121 | +remove_operator |
73 | 122 |
|
74 |
| -# make a configmap that the finalizer should remove |
75 |
| -kubectl create configmap deleteme |
76 |
| -trap_add 'kubectl delete --ignore-not-found configmap deleteme' EXIT |
| 123 | +echo "###" |
| 124 | +echo "### Base image testing passed" |
| 125 | +echo "### Now testing migrate to hybrid operator" |
| 126 | +echo "###" |
77 | 127 |
|
78 |
| -kubectl delete -f ${DIR2}/deploy/crds/ansible_v1alpha1_memcached_cr.yaml --wait=true |
79 |
| -# if the finalizer did not delete the configmap... |
80 |
| -if kubectl get configmap deleteme; |
81 |
| -then |
82 |
| - echo FAIL: the finalizer did not delete the configmap |
83 |
| - kubectl logs deployment/memcached-operator |
84 |
| - exit 1 |
85 |
| -fi |
| 128 | +operator-sdk migrate |
86 | 129 |
|
87 |
| -# The deployment should get garbage collected, so we expect to fail getting the deployment. |
88 |
| -if ! timeout 20s bash -c -- "while kubectl get deployment ${memcached_deployment}; do sleep 1; done"; |
| 130 | +if [[ ! -e build/Dockerfile.sdkold ]]; |
89 | 131 | then
|
90 |
| - kubectl logs deployment/memcached-operator |
| 132 | + echo FAIL the old Dockerful should have been renamed to Dockerfile.sdkold |
91 | 133 | exit 1
|
92 | 134 | fi
|
93 | 135 |
|
| 136 | +dep ensure |
| 137 | +# overwrite operator-sdk source with the latest source from the local checkout |
| 138 | +pushd vendor/github.com/operator-framework/ |
| 139 | +rm -Rf operator-sdk/* |
| 140 | +cp -a "$ROOTDIR"/{pkg,version,LICENSE} operator-sdk/ |
| 141 | +popd |
| 142 | + |
| 143 | +operator-sdk build "$DEST_IMAGE" |
94 | 144 |
|
95 |
| -## TODO enable when this is fixed: https://github.com/operator-framework/operator-sdk/issues/818 |
96 |
| -# if kubectl logs deployment/memcached-operator | grep -i error; |
97 |
| -# then |
98 |
| - # echo FAIL: the operator log includes errors |
99 |
| - # kubectl logs deployment/memcached-operator |
100 |
| - # exit 1 |
101 |
| -# fi |
| 145 | +deploy_operator |
| 146 | +test_operator |
102 | 147 |
|
103 | 148 | popd
|
104 | 149 | popd
|
0 commit comments