github.com/jmccormick2001/sdk-scorecard@v0.8.2/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  # ansible proxy test require a running cluster; run during e2e instead
     8  go test -count=1 ./pkg/ansible/proxy/...
     9  
    10  DEST_IMAGE="quay.io/example/memcached-operator:v0.0.2"
    11  ROOTDIR="$(pwd)"
    12  GOTMP="$(mktemp -d -p $GOPATH/src)"
    13  trap_add 'rm -rf $GOTMP' EXIT
    14  
    15  deploy_operator() {
    16      kubectl create -f "$OPERATORDIR/deploy/service_account.yaml"
    17      kubectl create -f "$OPERATORDIR/deploy/role.yaml"
    18      kubectl create -f "$OPERATORDIR/deploy/role_binding.yaml"
    19      kubectl create -f "$OPERATORDIR/deploy/crds/ansible_v1alpha1_memcached_crd.yaml"
    20      kubectl create -f "$OPERATORDIR/deploy/crds/ansible_v1alpha1_foo_crd.yaml"
    21      kubectl create -f "$OPERATORDIR/deploy/operator.yaml"
    22  }
    23  
    24  remove_operator() {
    25      kubectl delete --ignore-not-found=true -f "$OPERATORDIR/deploy/service_account.yaml"
    26      kubectl delete --ignore-not-found=true -f "$OPERATORDIR/deploy/role.yaml"
    27      kubectl delete --ignore-not-found=true -f "$OPERATORDIR/deploy/role_binding.yaml"
    28      kubectl delete --ignore-not-found=true -f "$OPERATORDIR/deploy/crds/ansible_v1alpha1_memcached_crd.yaml"
    29      kubectl delete --ignore-not-found=true -f "$OPERATORDIR/deploy/crds/ansible_v1alpha1_foo_crd.yaml"
    30      kubectl delete --ignore-not-found=true -f "$OPERATORDIR/deploy/operator.yaml"
    31  }
    32  
    33  test_operator() {
    34      # wait for operator pod to run
    35      if ! timeout 1m kubectl rollout status deployment/memcached-operator;
    36      then
    37          echo FAIL: operator failed to run
    38          kubectl logs deployment/memcached-operator -c operator
    39          kubectl logs deployment/memcached-operator -c ansible
    40          exit 1
    41      fi
    42  
    43      # create CR
    44      kubectl create -f deploy/crds/ansible_v1alpha1_memcached_cr.yaml
    45      if ! timeout 20s bash -c -- 'until kubectl get deployment -l app=memcached | grep memcached; do sleep 1; done';
    46      then
    47          echo FAIL: operator failed to create memcached Deployment
    48          kubectl logs deployment/memcached-operator -c operator
    49          kubectl logs deployment/memcached-operator -c ansible
    50          exit 1
    51      fi
    52      memcached_deployment=$(kubectl get deployment -l app=memcached -o jsonpath="{..metadata.name}")
    53      if ! timeout 1m kubectl rollout status deployment/${memcached_deployment};
    54      then
    55          echo FAIL: memcached Deployment failed rollout
    56          kubectl logs deployment/${memcached_deployment}
    57          exit 1
    58      fi
    59  
    60  
    61      # make a configmap that the finalizer should remove
    62      kubectl create configmap deleteme
    63      trap_add 'kubectl delete --ignore-not-found configmap deleteme' EXIT
    64  
    65      kubectl delete -f ${OPERATORDIR}/deploy/crds/ansible_v1alpha1_memcached_cr.yaml --wait=true
    66      # if the finalizer did not delete the configmap...
    67      if kubectl get configmap deleteme 2> /dev/null;
    68      then
    69          echo FAIL: the finalizer did not delete the configmap
    70          kubectl logs deployment/memcached-operator -c operator
    71          kubectl logs deployment/memcached-operator -c ansible
    72          exit 1
    73      fi
    74  
    75      # The deployment should get garbage collected, so we expect to fail getting the deployment.
    76      if ! timeout 20s bash -c -- "while kubectl get deployment ${memcached_deployment} 2> /dev/null; do sleep 1; done";
    77      then
    78          echo FAIL: memcached Deployment did not get garbage collected
    79          kubectl logs deployment/memcached-operator -c operator
    80          kubectl logs deployment/memcached-operator -c ansible
    81          exit 1
    82      fi
    83  
    84      # Ensure that no errors appear in the log
    85      if kubectl logs deployment/memcached-operator -c operator | grep -i error;
    86      then
    87          echo FAIL: the operator log includes errors
    88          kubectl logs deployment/memcached-operator -c operator
    89          kubectl logs deployment/memcached-operator -c ansible
    90          exit 1
    91      fi
    92  }
    93  
    94  # switch to the "default" namespace if on openshift, to match the minikube test
    95  if which oc 2>/dev/null; then oc project default; fi
    96  
    97  # create and build the operator
    98  pushd "$GOTMP"
    99  operator-sdk new memcached-operator --api-version=ansible.example.com/v1alpha1 --kind=Memcached --type=ansible
   100  cp "$ROOTDIR/test/ansible-memcached/tasks.yml" memcached-operator/roles/memcached/tasks/main.yml
   101  cp "$ROOTDIR/test/ansible-memcached/defaults.yml" memcached-operator/roles/memcached/defaults/main.yml
   102  cp -a "$ROOTDIR/test/ansible-memcached/memfin" memcached-operator/roles/
   103  cat "$ROOTDIR/test/ansible-memcached/watches-finalizer.yaml" >> memcached-operator/watches.yaml
   104  # Append Foo kind to watches to test watching multiple Kinds
   105  cat "$ROOTDIR/test/ansible-memcached/watches-foo-kind.yaml" >> memcached-operator/watches.yaml
   106  
   107  pushd memcached-operator
   108  # Add a second Kind to test watching multiple GVKs
   109  operator-sdk add crd --kind=Foo --api-version=ansible.example.com/v1alpha1
   110  sed -i 's|\(FROM quay.io/operator-framework/ansible-operator\)\(:.*\)\?|\1:dev|g' build/Dockerfile
   111  operator-sdk build "$DEST_IMAGE"
   112  sed -i "s|{{ REPLACE_IMAGE }}|$DEST_IMAGE|g" deploy/operator.yaml
   113  sed -i 's|{{ pull_policy.default..Always.. }}|Never|g' deploy/operator.yaml
   114  
   115  OPERATORDIR="$(pwd)"
   116  
   117  deploy_operator
   118  trap_add 'remove_operator' EXIT
   119  test_operator
   120  remove_operator
   121  
   122  echo "###"
   123  echo "### Base image testing passed"
   124  echo "### Now testing migrate to hybrid operator"
   125  echo "###"
   126  
   127  export GO111MODULE=on
   128  operator-sdk migrate
   129  
   130  if [[ ! -e build/Dockerfile.sdkold ]];
   131  then
   132      echo FAIL the old Dockerfile should have been renamed to Dockerfile.sdkold
   133      exit 1
   134  fi
   135  
   136  add_go_mod_replace "github.com/operator-framework/operator-sdk" "$ROOTDIR"
   137  go mod vendor
   138  
   139  operator-sdk build "$DEST_IMAGE"
   140  
   141  deploy_operator
   142  test_operator
   143  
   144  popd
   145  popd