github.com/greenboxal/deis@v1.12.1/tests/bin/test-setup.sh (about)

     1  #!/usr/bin/env bash
     2  #
     3  # Prepares the process environment to run a test
     4  
     5  function log_phase {
     6      echo
     7      echo ">>> $1 <<<"
     8      echo
     9  }
    10  
    11  log_phase "Preparing test environment"
    12  
    13  # use GOPATH to determine project root
    14  export DEIS_ROOT=${GOPATH?}/src/github.com/deis/deis
    15  echo "DEIS_ROOT=$DEIS_ROOT"
    16  
    17  # the "deis" binary CLI to use in testing
    18  export DEIS_BINARY=${DEIS_BINARY:-$DEIS_ROOT/client/deis}
    19  echo "DEIS_BINARY=$DEIS_BINARY"
    20  
    21  # prepend GOPATH/bin to PATH
    22  export PATH=${GOPATH}/bin:$PATH
    23  
    24  # the application under test
    25  export DEIS_TEST_APP=${DEIS_TEST_APP:-example-dockerfile-http}
    26  echo "DEIS_TEST_APP=$DEIS_TEST_APP"
    27  
    28  # SSH key name used for testing
    29  export DEIS_TEST_AUTH_KEY=${DEIS_TEST_AUTH_KEY:-deis-test}
    30  echo "DEIS_TEST_AUTH_KEY=$DEIS_TEST_AUTH_KEY"
    31  
    32  # SSH key used for deisctl tunneling
    33  export DEIS_TEST_SSH_KEY=${DEIS_TEST_SSH_KEY:-~/.vagrant.d/insecure_private_key}
    34  echo "DEIS_TEST_SSH_KEY=$DEIS_TEST_SSH_KEY"
    35  
    36  # domain used for wildcard DNS
    37  export DEIS_TEST_DOMAIN=${DEIS_TEST_DOMAIN:-local3.deisapp.com}
    38  echo "DEIS_TEST_DOMAIN=$DEIS_TEST_DOMAIN"
    39  
    40  # SSH tunnel used by deisctl
    41  export DEISCTL_TUNNEL=${DEISCTL_TUNNEL:-127.0.0.1:2222}
    42  echo "DEISCTL_TUNNEL=$DEISCTL_TUNNEL"
    43  
    44  # set units used by deisctl
    45  export DEISCTL_UNITS=${DEISCTL_UNITS:-$DEIS_ROOT/deisctl/units}
    46  echo "DEISCTL_UNITS=$DEISCTL_UNITS"
    47  
    48  # ip address for docker containers to communicate in functional tests
    49  export HOST_IPADDR=${HOST_IPADDR?}
    50  echo "HOST_IPADDR=$HOST_IPADDR"
    51  
    52  # the registry used to host dev-release images
    53  # must be accessible to local Docker engine and Deis cluster
    54  export DEV_REGISTRY=${DEV_REGISTRY?}
    55  echo "DEV_REGISTRY=$DEV_REGISTRY"
    56  
    57  # random 10-char (5-byte) hex string to identify a test run
    58  export DEIS_TEST_ID=${DEIS_TEST_ID:-$(openssl rand -hex 5)}
    59  echo "DEIS_TEST_ID=$DEIS_TEST_ID"
    60  
    61  # give this session a unique ~/.deis/<client>.json file
    62  export DEIS_PROFILE=test-$DEIS_TEST_ID
    63  rm -f $HOME/.deis/test-$DEIS_TEST_ID.json
    64  
    65  # bail if registry is not accessible
    66  if ! curl -s $DEV_REGISTRY && ! curl -s https://$DEV_REGISTRY; then
    67    echo "DEV_REGISTRY is not accessible, exiting..."
    68    exit 1
    69  fi
    70  echo ; echo
    71  
    72  # disable git+ssh host key checking
    73  export GIT_SSH=$DEIS_ROOT/tests/bin/git-ssh-nokeycheck.sh
    74  
    75  # install required go dependencies
    76  go get -v github.com/golang/lint/golint
    77  go get -v github.com/tools/godep
    78  
    79  # cleanup any stale example applications
    80  rm -rf $DEIS_ROOT/tests/example-*
    81  
    82  # generate ssh keys if they don't already exist
    83  test -e ~/.ssh/$DEIS_TEST_AUTH_KEY || ssh-keygen -t rsa -f ~/.ssh/$DEIS_TEST_AUTH_KEY -N ''
    84  # TODO: parameterize this key required for keys_test.go?
    85  test -e ~/.ssh/deiskey || ssh-keygen -q -t rsa -f ~/.ssh/deiskey -N '' -C deiskey
    86  
    87  # prepare the SSH agent
    88  ssh-add -D || eval $(ssh-agent) && ssh-add -D
    89  ssh-add ~/.ssh/$DEIS_TEST_AUTH_KEY
    90  ssh-add $DEIS_TEST_SSH_KEY
    91  
    92  # clear the drink of choice in case the user has set it
    93  unset DEIS_DRINK_OF_CHOICE
    94  
    95  # wipe out all vagrants & deis virtualboxen
    96  function cleanup {
    97      if [ "$SKIP_CLEANUP" != true ]; then
    98          log_phase "Cleaning up"
    99          set +e
   100          ${GOPATH}/src/github.com/deis/deis/tests/bin/destroy-all-vagrants.sh
   101          VBoxManage list vms | grep deis | sed -n -e 's/^.* {\(.*\)}/\1/p' | xargs -L1 -I {} VBoxManage unregistervm {} --delete
   102          vagrant global-status --prune
   103          docker rm -f -v `docker ps | grep deis- | awk '{print $1}'` 2>/dev/null
   104          log_phase "Test run complete"
   105      fi
   106  }
   107  
   108  function dump_logs {
   109    log_phase "Error detected, dumping logs"
   110    TIMESTAMP=`date +%Y-%m-%d-%H%M%S`
   111    FAILED_LOGS_DIR=$HOME/deis-test-failure-$TIMESTAMP
   112    mkdir -p $FAILED_LOGS_DIR
   113    set +e
   114    export FLEETCTL_TUNNEL=$DEISCTL_TUNNEL
   115    fleetctl -strict-host-key-checking=false list-units
   116    # application unit logs
   117    for APP in `fleetctl -strict-host-key-checking=false list-units --no-legend --fields=unit | grep -v "deis-"`;do
   118      CURRENT_APP=$(echo $APP | sed "s/\.service//")
   119      #echo "$CURRENT_APP"
   120      get_journal_logs $CURRENT_APP
   121    done
   122    # component logs
   123    get_logs deis-builder
   124    get_logs deis-controller
   125    get_logs deis-database
   126    get_logs deis-logger
   127    get_logs deis-registry@1 deis-registry deis-registry-1
   128    get_logs deis-router@1 deis-router deis-router-1
   129    get_logs deis-router@2 deis-router deis-router-2
   130    get_logs deis-router@3 deis-router deis-router-3
   131    # deis-store logs
   132    get_logs deis-router@1 deis-store-monitor deis-store-monitor-1
   133    get_logs deis-router@1 deis-store-daemon deis-store-daemon-1
   134    get_logs deis-router@1 deis-store-metadata deis-store-metadata-1
   135    get_logs deis-router@1 deis-store-volume deis-store-volume-1
   136    get_logs deis-router@2 deis-store-monitor deis-store-monitor-2
   137    get_logs deis-router@2 deis-store-daemon deis-store-daemon-2
   138    get_logs deis-router@2 deis-store-metadata deis-store-metadata-2
   139    get_logs deis-router@2 deis-store-volume deis-store-volume-2
   140    get_logs deis-router@3 deis-store-monitor deis-store-monitor-3
   141    get_logs deis-router@3 deis-store-daemon deis-store-daemon-3
   142    get_logs deis-router@3 deis-store-metadata deis-store-metadata-3
   143    get_logs deis-router@3 deis-store-volume deis-store-volume-3
   144    get_logs deis-store-gateway
   145  
   146    # docker logs
   147    fleetctl -strict-host-key-checking=false ssh deis-router@1 journalctl --no-pager -u docker \
   148      > $FAILED_LOGS_DIR/docker-1.log
   149    fleetctl -strict-host-key-checking=false ssh deis-router@2 journalctl --no-pager -u docker \
   150      > $FAILED_LOGS_DIR/docker-2.log
   151    fleetctl -strict-host-key-checking=false ssh deis-router@3 journalctl --no-pager -u docker \
   152      > $FAILED_LOGS_DIR/docker-3.log
   153  
   154    # etcd logs
   155    fleetctl -strict-host-key-checking=false ssh deis-router@1 journalctl --no-pager -u etcd \
   156      > $FAILED_LOGS_DIR/debug-etcd-1.log
   157    fleetctl -strict-host-key-checking=false ssh deis-router@2 journalctl --no-pager -u etcd \
   158      > $FAILED_LOGS_DIR/debug-etcd-2.log
   159    fleetctl -strict-host-key-checking=false ssh deis-router@3 journalctl --no-pager -u etcd \
   160      > $FAILED_LOGS_DIR/debug-etcd-3.log
   161  
   162    # etcdctl dump
   163    fleetctl -strict-host-key-checking=false ssh deis-router@1 etcdctl ls / --recursive \
   164      > $FAILED_LOGS_DIR/etcdctl-dump.log
   165  
   166    # tarball logs
   167    BUCKET=jenkins-failure-logs
   168    FILENAME=deis-test-failure-$TIMESTAMP.tar.gz
   169    cd $FAILED_LOGS_DIR && tar -czf $FILENAME *.log && mv $FILENAME .. && cd ..
   170    rm -rf $FAILED_LOGS_DIR
   171    if [ `which s3cmd` ] && [ -f $HOME/.s3cfg ]; then
   172      echo "configured s3cmd found in path. Attempting to upload logs to S3"
   173      s3cmd put $HOME/$FILENAME s3://$BUCKET
   174      rm $HOME/$FILENAME
   175      echo "Logs are accessible at https://s3.amazonaws.com/$BUCKET/$FILENAME"
   176    else
   177      echo "Logs are accessible at $HOME/$FILENAME"
   178    fi
   179    exit 1
   180  }
   181  
   182  function get_logs {
   183    TARGET="$1"
   184    CONTAINER="$2"
   185    FILENAME="$3"
   186    if [ -z "$CONTAINER" ]; then
   187      CONTAINER=$TARGET
   188    fi
   189    if [ -z "$FILENAME" ]; then
   190      FILENAME=$TARGET
   191    fi
   192    fleetctl -strict-host-key-checking=false ssh "$TARGET" docker logs "$CONTAINER" > $FAILED_LOGS_DIR/$FILENAME.log
   193  }
   194  
   195  function get_journal_logs {
   196    TARGET="$1"
   197    fleetctl -strict-host-key-checking=false journal --lines=1000 "$TARGET" > $FAILED_LOGS_DIR/$TARGET.log
   198  }