github.com/kjdelisle/consul@v1.4.5/build-support/scripts/test-flake.sh (about)

     1  #!/bin/bash
     2  pushd $(dirname ${BASH_SOURCE[0]}) > /dev/null
     3  SCRIPT_DIR=$(pwd)
     4  pushd ../.. > /dev/null
     5  SOURCE_DIR=$(pwd)
     6  pushd build-support/docker > /dev/null
     7  IMG_DIR=$(pwd)
     8  popd > /dev/null
     9  
    10  source "${SCRIPT_DIR}/functions.sh"
    11  
    12  IMAGE="travis-img-v0.13"
    13  CONTAINER="travis-cnt"
    14  GOOS="linux"
    15  GOARCH="amd64"
    16  TEST_BINARY="flake.test"
    17  
    18  function usage {
    19  cat <<-EOF
    20  Usage: test-flake [<options ...>]
    21  
    22  Description:
    23  
    24    test-flake surfaces flakiness in tests by constraining CPU resources.
    25    
    26    Single or package-wide tests are run for multiple iterations with a configurable
    27    amount of CPU resources. 
    28  
    29    0.15 CPUs and 30 iterations are configured as sane defaults.
    30  
    31    See Docker docs for more info on tuning 'cpus' param: 
    32    https://docs.docker.com/config/containers/resource_constraints/#cpu
    33  
    34  Options:
    35  
    36    --pkg=""             Target package
    37    --test=""            Target test (requires pkg flag)
    38    --cpus=0.15          Amount of CPU resources for container
    39    --n=30               Number of times to run tests
    40  
    41  Examples:
    42  
    43    ./test-flake.sh --pkg connect/proxy
    44    ./test-flake.sh --pkg connect/proxy --cpus 0.20
    45    ./test-flake.sh --pkg connect/proxy --test Listener
    46    ./test-flake.sh --pkg connect/proxy --test TestUpstreamListener
    47    ./test-flake.sh --pkg connect/proxy --test TestUpstreamListener -n 100
    48  EOF
    49  }
    50  
    51  function build_repro_env {
    52      # Arguments:
    53      #   $1 - pkg, Target package
    54      #   $2 - test, Target tests
    55      #   $3 - cpus, Amount of CPU resources for container
    56      #   $4 - n, Number of times to run tests
    57  
    58      APP=$(pwd | awk '{n=split($0, a, "/"); print a[n]}')
    59  
    60      status_stage -e "App:\t\t$APP"
    61      status_stage -e "Package:\t$1"
    62      status_stage -e "Test:\t\t$2"
    63      status_stage -e "CPUs:\t\t$3"
    64      status_stage -e "Iterations:\t$4"
    65      echo
    66  
    67      status_stage "----> Cleaning up old containers..."
    68      if docker ps -a | grep $CONTAINER ; then
    69          docker rm $(docker ps -a | grep $CONTAINER | awk '{print $1;}')
    70      fi
    71  
    72      status_stage '----> Rebuilding image...'
    73      (cd $IMG_DIR && docker build -q -t $IMAGE --no-cache -f Test-Flake.dockerfile .)
    74  
    75      status_stage "--> Building app binary..."
    76      env GOOS=$GOOS GOARCH=$GOARCH go build -o bin/$APP
    77  
    78      status_stage "-> Building test binary..."
    79      env GOOS=$GOOS GOARCH=$GOARCH go test -c "./$1" -o $TEST_BINARY
    80  
    81      status_stage "> Running container..."
    82      status_stage
    83  
    84      docker run \
    85      --rm \
    86      --name $CONTAINER \
    87      --cpus="$3" \
    88      -v $SOURCE_DIR:/home/travis/go/$APP \
    89      -e TEST_BINARY="$TEST_BINARY" \
    90      -e TEST_PKG="$1" \
    91      -e TEST="$2" \
    92      -e ITERATIONS="$4" \
    93      -e APP="$APP" \
    94      $IMAGE
    95  }
    96  
    97  function err_usage {
    98     err "$1"
    99     err ""
   100     err "$(usage)"
   101  }
   102  
   103  function main {
   104     declare pkg=""
   105     declare test=""
   106     declare cpus=""
   107     declare n=""
   108     
   109     
   110     while test $# -gt 0
   111     do
   112        case "$1" in
   113           -h | --help )
   114              usage
   115              return 0
   116              ;;
   117  
   118           --pkg )
   119              if test -z "$2"
   120              then
   121                 err_usage "ERROR: option pkg requires an argument"
   122                 return 1
   123              fi
   124              
   125              pkg="$2"
   126              shift 2
   127              ;;
   128  
   129           --test )
   130              test="$2"
   131              shift 2
   132              ;;
   133  
   134           --cpus )
   135              if test -n "$2"
   136              then
   137                 cpus="$2"
   138              else
   139                 cpus="0.15"
   140              fi
   141              
   142              shift 2
   143              ;;
   144  
   145            --n )
   146              if test -n "$2"
   147              then
   148                 n="$2"
   149              else
   150                 n="30"
   151              fi
   152  
   153              shift 2
   154              ;;
   155  
   156           * )
   157              err_usage "ERROR: Unknown argument: '$1'"
   158              return 1
   159              ;;
   160        esac
   161     done
   162     
   163     build_repro_env "${pkg}" "${test}" "${cpus}" "${n}" || return 1
   164     
   165     return 0
   166  }
   167  
   168  main "$@"
   169  exit $?