github.com/RedHatInsights/insights-content-service@v1.0.0/test.sh (about)

     1  #!/usr/bin/env bash
     2  # Copyright 2020 Red Hat, Inc
     3  #
     4  # Licensed under the Apache License, Version 2.0 (the "License");
     5  # you may not use this file except in compliance with the License.
     6  # You may obtain a copy of the License at
     7  #
     8  #      http://www.apache.org/licenses/LICENSE-2.0
     9  #
    10  # Unless required by applicable law or agreed to in writing, software
    11  # distributed under the License is distributed on an "AS IS" BASIS,
    12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  # See the License for the specific language governing permissions and
    14  # limitations under the License.
    15  
    16  COLORS_RED=$(tput setab 1)
    17  COLORS_RESET=$(tput sgr0) # No Color
    18  LOG_LEVEL="fatal"
    19  VERBOSE=false
    20  
    21  function cleanup() {
    22      print_descendent_pids() {
    23          pids=$(pgrep -P "$1")
    24          echo "$pids"
    25          for pid in $pids; do
    26              print_descendent_pids "$pid"
    27          done
    28      }
    29  
    30      echo Exiting and killing all children...
    31      for pid in $(print_descendent_pids $$); do
    32          if ! kill "$pid" &>/dev/null; then
    33              # wait for it to stop correctly
    34              sleep 1
    35              kill -9 "$pid" &>/dev/null
    36          fi
    37      done
    38      sleep 1
    39  }
    40  trap cleanup EXIT
    41  
    42  go clean -testcache
    43  
    44  if go build -race; then
    45      echo "Service build ok"
    46  else
    47      echo "Build failed"
    48      exit 1
    49  fi
    50  
    51  function start_service() {
    52      echo "Starting a service"
    53      INSIGHTS_CONTENT_SERVICE__LOGGING__LOG_LEVEL=$LOG_LEVEL \
    54      INSIGHTS_CONTENT_SERVICE_CONFIG_FILE=./tests/tests \
    55        ./insights-content-service ||
    56        echo -e "${COLORS_RED}service exited with error${COLORS_RESET}" &
    57      # shellcheck disable=2181
    58      if [ $? -ne 0 ]; then
    59          echo "Could not start the service"
    60          exit 1
    61      fi
    62  }
    63  
    64  function test_rest_api() {
    65      start_service
    66  
    67      echo "Building REST API tests utility"
    68      if go build -o rest-api-tests tests/rest_api_tests.go; then
    69          echo "REST API tests build ok"
    70      else
    71          echo "Build failed"
    72          return 1
    73      fi
    74      sleep 1
    75      curl http://localhost:8080/api/v1/ || {
    76          echo -e "${COLORS_RED}server is not running(for some reason)${COLORS_RESET}"
    77          exit 1
    78      }
    79  
    80      if [ "$VERBOSE" = true ]; then
    81          ./rest-api-tests 2>&1
    82      else
    83          ./rest-api-tests 2>&1 | grep -v -E "^Pass "
    84      fi
    85  
    86      return $?
    87  }
    88  
    89  test_rest_api
    90  EXIT_VALUE=$?
    91  
    92  echo -e "------------------------------------------------------------------------------------------------"
    93  
    94  exit $EXIT_VALUE