github.com/weaviate/weaviate@v1.24.6/test/run.sh (about)

     1  #!/bin/bash
     2  
     3  set -eou pipefail
     4  
     5  function main() {
     6    # This script runs all non-benchmark tests if no CMD switch is given and the respective tests otherwise.
     7    run_all_tests=true
     8    run_acceptance_tests=false
     9    run_acceptance_only_fast=false
    10    run_acceptance_only_python=false
    11    run_acceptance_graphql_tests=false
    12    run_acceptance_replication_tests=false
    13    run_module_tests=false
    14    only_module=false
    15    only_module_value=false
    16    run_unit_and_integration_tests=false
    17    run_unit_tests=false
    18    run_integration_tests=false
    19    run_benchmark=false
    20    run_module_only_backup_tests=false
    21    run_module_except_backup_tests=false
    22  
    23    while [[ "$#" -gt 0 ]]; do
    24        case $1 in
    25            --unit-only|-u) run_all_tests=false; run_unit_tests=true;;
    26            --unit-and-integration-only|-ui) run_all_tests=false; run_unit_and_integration_tests=true;;
    27            --integration-only|-i) run_all_tests=false; run_integration_tests=true;;
    28            --acceptance-only|--e2e-only|-a) run_all_tests=false; run_acceptance_tests=true ;;
    29            
    30            --acceptance-only-fast|-aof) run_all_tests=false; run_acceptance_only_fast=true;;
    31            --acceptance-only-python|-aop) run_all_tests=false; run_acceptance_only_python=true;;
    32            --acceptance-only-graphql|-aog) run_all_tests=false; run_acceptance_graphql_tests=true ;;
    33            --acceptance-only-replication|-aor) run_all_tests=false; run_acceptance_replication_tests=true ;;
    34            --only-module-*|-om)run_all_tests=false; only_module=true;only_module_value=$1;;
    35            --acceptance-module-tests-only|--modules-only|-m) run_all_tests=false; run_module_tests=true; run_module_only_backup_tests=true; run_module_except_backup_tests=true;;
    36            --acceptance-module-tests-only-backup|--modules-backup-only|-mob) run_all_tests=false; run_module_tests=true; run_module_only_backup_tests=true;;
    37            --acceptance-module-tests-except-backup|--modules-except-backup|-meb) run_all_tests=false; run_module_tests=true; run_module_except_backup_tests=true; echo $run_module_except_backup_tests ;;
    38            --benchmark-only|-b) run_all_tests=false; run_benchmark=true;;
    39            --help|-h) printf '%s\n' \
    40                "Options:"\
    41                "--unit-only | -u"\
    42                "--unit-and-integration-only | -ui"\
    43                "--integration-only | -i"\
    44                "--acceptance-only | -a"\
    45                "--acceptance-only-fast | -aof"\
    46                "--acceptance-only-python | -aop"\
    47                "--acceptance-only-graphql | -aog"\
    48                "--acceptance-only-replication| -aor"\
    49                "--acceptance-module-tests-only | --modules-only | -m"\
    50                "--acceptance-module-tests-only-backup | --modules-backup-only | -mob"\
    51                "--acceptance-module-tests-except-backup | --modules-except-backup | -meb"\
    52                "--only-module-{moduleName}"
    53                "--benchmark-only | -b" \
    54                "--help | -h"; exit 1;;
    55            *) echo "Unknown parameter passed: $1"; exit 1 ;;
    56        esac
    57        shift
    58    done
    59  
    60    # Jump to root directory
    61    cd "$( dirname "${BASH_SOURCE[0]}" )"/..
    62  
    63    echo "INFO: In directory $PWD"
    64  
    65    echo "INFO: This script will suppress most output, unless a command ultimately fails"
    66    echo "      Then it will print the output of the failed command."
    67  
    68    echo_green "Prepare workspace..."
    69  
    70    # Remove data directory in case of previous runs
    71    rm -rf data
    72    echo "Done!"
    73  
    74    if $run_unit_and_integration_tests || $run_unit_tests || $run_all_tests
    75    then
    76      echo_green "Run all unit tests..."
    77      run_unit_tests "$@"
    78      echo_green "Unit tests successful"
    79    fi
    80  
    81    if $run_unit_and_integration_tests || $run_integration_tests || $run_all_tests
    82    then
    83      echo_green "Run integration tests..."
    84      run_integration_tests "$@"
    85      echo_green "Integration tests successful"
    86    fi
    87  
    88    if $run_acceptance_tests  || $run_acceptance_only_fast || $run_acceptance_graphql_tests || $run_acceptance_replication_tests || $run_acceptance_only_python || $run_all_tests || $run_benchmark
    89    then
    90      echo "Start docker container needed for acceptance and/or benchmark test"
    91      echo_green "Stop any running docker-compose containers..."
    92      suppress_on_success docker compose -f docker-compose-test.yml down --remove-orphans
    93  
    94      echo_green "Start up weaviate and backing dbs in docker-compose..."
    95      echo "This could take some time..."
    96      tools/test/run_ci_server.sh
    97  
    98      # echo_green "Import required schema and test fixtures..."
    99      # # Note: It's not best practice to do this as part of the test script
   100      # # It would be better if each test independently prepared (and also 
   101      # # cleaned up) the test fixtures it needs, but one step at a time ;)
   102      # suppress_on_success import_test_fixtures
   103  
   104      if $run_benchmark
   105      then
   106        echo_green "Run performance tracker..."
   107        ./test/benchmark/run_performance_tracker.sh
   108      fi
   109  
   110      if $run_acceptance_tests  || $run_acceptance_only_fast || $run_acceptance_graphql_tests || $run_acceptance_replication_tests || $run_all_tests
   111      then
   112        echo_green "Run acceptance tests..."
   113        run_acceptance_tests "$@"
   114      fi
   115    fi
   116  
   117    if $run_acceptance_only_python || $run_all_tests
   118    then
   119      echo_green "Run python acceptance tests..."
   120      ./test/acceptance_with_python/run.sh
   121      echo_green "Python tests successful"
   122    fi
   123  
   124    if $only_module; then
   125      mod=${only_module_value//--only-module-/}
   126      echo_green "Running module acceptance tests for $mod..."
   127      for pkg in $(go list ./test/modules/... | grep '/modules/'${mod}); do
   128        build_docker_image_for_tests
   129        echo_green "Weaviate image successfully built, run module tests for $mod..."
   130        if ! go test -count 1 -race "$pkg"; then
   131          echo "Test for $pkg failed" >&2
   132          return 1
   133        fi
   134        echo_green "Module acceptance tests for $mod successful"
   135      done    
   136    fi
   137    if $run_module_tests; then
   138      echo_green "Running module acceptance tests..."
   139      build_docker_image_for_tests
   140      echo_green "Weaviate image successfully built, run module tests..."
   141      run_module_tests "$@"
   142      echo_green "Module acceptance tests successful"
   143    fi
   144    
   145    echo "Done!"
   146  }
   147  
   148  function build_docker_image_for_tests() {
   149    local module_test_image=weaviate:module-tests
   150    echo_green "Stop any running docker-compose containers..."
   151    suppress_on_success docker compose -f docker-compose-test.yml down --remove-orphans
   152    echo_green "Building weaviate image for module acceptance tests..."
   153    echo "This could take some time..."
   154    GIT_HASH=$(git rev-parse --short HEAD)
   155    docker build --build-arg GITHASH="$GIT_HASH" -t $module_test_image .
   156    export "TEST_WEAVIATE_IMAGE"=$module_test_image
   157  }
   158  
   159  function run_unit_tests() {
   160    if [[ "$*" == *--acceptance-only* ]]; then
   161      echo "Skipping unit test"
   162      return
   163    fi
   164    go test -race -coverprofile=coverage-unit.txt -covermode=atomic -count 1 $(go list ./... | grep -v 'test/acceptance' | grep -v 'test/modules') | grep -v '\[no test files\]'
   165  }
   166  
   167  function run_integration_tests() {
   168    if [[ "$*" == *--acceptance-only* ]]; then
   169      echo "Skipping integration test"
   170      return
   171    fi
   172  
   173    ./test/integration/run.sh --include-slow
   174  }
   175  
   176  function run_acceptance_tests() {
   177    if $run_acceptance_only_fast || $run_acceptance_tests || $run_all_tests; then
   178    echo "running acceptance fast only"
   179      run_acceptance_only_fast "$@"
   180    fi
   181    if $run_acceptance_graphql_tests || $run_acceptance_tests || $run_all_tests; then
   182    echo "running acceptance graphql"
   183      run_acceptance_graphql_tests "$@"
   184    fi
   185    if $run_acceptance_replication_tests || $run_acceptance_tests || $run_all_tests; then
   186    echo "running acceptance replication"
   187      run_acceptance_replication_tests "$@"
   188    fi  
   189  }
   190  
   191  function run_acceptance_only_fast() {
   192    # needed for test/docker package during replication tests
   193    export TEST_WEAVIATE_IMAGE=weaviate/test-server
   194    # for now we need to run the tests sequentially, there seems to be some sort of issues with running them in parallel
   195    for pkg in $(go list ./... | grep 'test/acceptance' | grep -v 'test/acceptance/stress_tests' | grep -v 'test/acceptance/replication' | grep -v 'test/acceptance/graphql_resolvers'); do
   196      if ! go test -count 1 -race "$pkg"; then
   197        echo "Test for $pkg failed" >&2
   198        return 1
   199      fi
   200    done
   201    for pkg in $(go list ./... | grep 'test/acceptance/stress_tests' ); do
   202      if ! go test -count 1 "$pkg"; then
   203        echo "Test for $pkg failed" >&2
   204        return 1
   205      fi
   206    done
   207    # tests with go client are in a separate package with its own dependencies to isolate them
   208    cd 'test/acceptance_with_go_client'
   209    for pkg in $(go list ./... ); do
   210      if ! go test -count 1 -race "$pkg"; then
   211        echo "Test for $pkg failed" >&2
   212        return 1
   213      fi
   214    done
   215  }
   216  function run_acceptance_graphql_tests() {
   217    for pkg in $(go list ./... | grep 'test/acceptance/graphql_resolvers'); do
   218      if ! go test -count 1 -race "$pkg"; then
   219        echo "Test for $pkg failed" >&2
   220        return 1
   221      fi
   222    done
   223  }
   224  
   225  function run_acceptance_replication_tests() {
   226    for pkg in $(go list ./.../ | grep 'test/acceptance/replication'); do
   227      if ! go test -count 1 -race "$pkg"; then
   228        echo "Test for $pkg failed" >&2
   229        return 1
   230      fi
   231    done
   232  }
   233  
   234  function run_module_only_backup_tests() {
   235    for pkg in $(go list ./... | grep 'test/modules' | grep 'test/modules/backup'); do
   236      if ! go test -count 1 -race "$pkg"; then
   237        echo "Test for $pkg failed" >&2
   238        return 1
   239      fi
   240    done
   241  }
   242  
   243  function run_module_except_backup_tests() {
   244    for pkg in $(go list ./... | grep 'test/modules' | grep -v 'test/modules/backup'); do
   245      if ! go test -count 1 -race "$pkg"; then
   246        echo "Test for $pkg failed" >&2
   247        return 1
   248      fi
   249    done
   250  }
   251  
   252  function run_module_tests() {
   253    if $run_module_only_backup_tests; then
   254      run_module_only_backup_tests "$@"
   255    fi
   256    if $run_module_except_backup_tests; then
   257      run_module_except_backup_tests "$@"
   258    fi
   259  }
   260  
   261  suppress_on_success() {
   262    out="$("${@}" 2>&1)" || { echo_red "FAILED!";  echo "$out"; return 1; }
   263    echo "Done!"
   264  }
   265  
   266  function echo_green() {
   267    green='\033[0;32m'
   268    nc='\033[0m' 
   269    echo -e "${green}${*}${nc}"
   270  }
   271  
   272  function echo_red() {
   273    red='\033[0;31m'
   274    nc='\033[0m' 
   275    echo -e "${red}${*}${nc}"
   276  }
   277  
   278  main "$@"