github.com/jmrodri/operator-sdk@v0.5.0/hack/tests/e2e-ansible.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  source hack/lib/test_lib.sh
     4  
     5  set -eux
     6  
     7  DEST_IMAGE="quay.io/example/memcached-operator:v0.0.2"
     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/crds/ansible_v1alpha1_foo_crd.yaml"
    18      kubectl create -f "$OPERATORDIR/deploy/operator.yaml"
    19  }
    20  
    21  remove_operator() {
    22      kubectl delete --ignore-not-found=true -f "$OPERATORDIR/deploy/service_account.yaml"
    23      kubectl delete --ignore-not-found=true -f "$OPERATORDIR/deploy/role.yaml"
    24      kubectl delete --ignore-not-found=true -f "$OPERATORDIR/deploy/role_binding.yaml"
    25      kubectl delete --ignore-not-found=true -f "$OPERATORDIR/deploy/crds/ansible_v1alpha1_memcached_crd.yaml"
    26      kubectl delete --ignore-not-found=true -f "$OPERATORDIR/deploy/crds/ansible_v1alpha1_foo_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 2> /dev/null;
    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} 2> /dev/null; 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      # Ensure that no errors appear in the log
    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  }
    85  
    86  # switch to the "default" namespace if on openshift, to match the minikube test
    87  if which oc 2>/dev/null; then oc project default; fi
    88  
    89  # create and build the operator
    90  pushd "$GOTMP"
    91  operator-sdk new memcached-operator --api-version=ansible.example.com/v1alpha1 --kind=Memcached --type=ansible
    92  cp "$ROOTDIR/test/ansible-memcached/tasks.yml" memcached-operator/roles/memcached/tasks/main.yml
    93  cp "$ROOTDIR/test/ansible-memcached/defaults.yml" memcached-operator/roles/memcached/defaults/main.yml
    94  cp -a "$ROOTDIR/test/ansible-memcached/memfin" memcached-operator/roles/
    95  cat "$ROOTDIR/test/ansible-memcached/watches-finalizer.yaml" >> memcached-operator/watches.yaml
    96  # Append Foo kind to watches to test watching multiple Kinds
    97  cat "$ROOTDIR/test/ansible-memcached/watches-foo-kind.yaml" >> memcached-operator/watches.yaml
    98  
    99  pushd memcached-operator
   100  # Add a second Kind to test watching multiple GVKs
   101  operator-sdk add crd --kind=Foo --api-version=ansible.example.com/v1alpha1
   102  sed -i 's|\(FROM quay.io/operator-framework/ansible-operator\)\(:.*\)\?|\1:dev|g' build/Dockerfile
   103  operator-sdk build "$DEST_IMAGE"
   104  sed -i "s|{{ REPLACE_IMAGE }}|$DEST_IMAGE|g" deploy/operator.yaml
   105  sed -i 's|{{ pull_policy.default..Always.. }}|Never|g' deploy/operator.yaml
   106  
   107  OPERATORDIR="$(pwd)"
   108  
   109  deploy_operator
   110  trap_add 'remove_operator' EXIT
   111  test_operator
   112  remove_operator
   113  
   114  echo "###"
   115  echo "### Base image testing passed"
   116  echo "### Now testing migrate to hybrid operator"
   117  echo "###"
   118  
   119  operator-sdk migrate
   120  
   121  if [[ ! -e build/Dockerfile.sdkold ]];
   122  then
   123      echo FAIL the old Dockerfile should have been renamed to Dockerfile.sdkold
   124      exit 1
   125  fi
   126  
   127  # We can't reliably run `dep ensure` because when there are changes to
   128  # operator-sdk itself, and those changes are not merged upstream, we hit this
   129  # bug: https://github.com/golang/dep/issues/1747
   130  # Instead, this re-uses operator-sdk's own vendor directory.
   131  cp -a "$ROOTDIR"/vendor ./
   132  mkdir -p vendor/github.com/operator-framework/operator-sdk/
   133  # We cannot just use operator-sdk from $GOPATH because compilation tries to use
   134  # its vendor directory, which can conflict with the local one.
   135  cp -a "$ROOTDIR"/{internal,pkg,version,LICENSE} vendor/github.com/operator-framework/operator-sdk/
   136  
   137  operator-sdk build "$DEST_IMAGE"
   138  
   139  deploy_operator
   140  test_operator
   141  
   142  popd
   143  popd