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