github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/test/install/test_harness.sh (about)

     1  # disable using the install.sh entrypoint such that we can unit test
     2  # script functions without invoking main()
     3  TEST_INSTALL_SH=true
     4  
     5  . ../../install.sh
     6  set -u
     7  
     8  echoerr() {
     9    echo "$@" 1>&2
    10  }
    11  
    12  printferr() {
    13    printf "%s" "$*" >&2
    14  }
    15  
    16  
    17  assertTrue() {
    18    if eval "$1"; then
    19      echo "assertTrue failed: $2"
    20      exit 2
    21    fi
    22  }
    23  
    24  assertFalse() {
    25    if eval "$1"; then
    26      echo "assertFalse failed: $2"
    27      exit 2
    28    fi
    29  }
    30  
    31  assertEquals() {
    32    want=$1
    33    got=$2
    34    msg=$3
    35    if [ "$want" != "$got" ]; then
    36      echo "assertEquals failed: want='$want' got='$got' $msg"
    37      exit 2
    38    fi
    39  }
    40  
    41  assertFilesDoesNotExist() {
    42    path="$1"
    43    msg=$2
    44    if [ -f "${path}" ]; then
    45      echo "assertFilesDoesNotExist failed: path exists '$path': $msg"
    46      exit 2
    47    fi
    48  }
    49  
    50  assertFileExists() {
    51    path="$1"
    52    msg=$2
    53    if [ ! -f "${path}" ]; then
    54      echo "assertFileExists failed: path does not exist '$path': $msg"
    55      exit 2
    56    fi
    57  }
    58  
    59  assertFilesEqual() {
    60    want=$1
    61    got=$2
    62    msg=$3
    63  
    64    diff "$1" "$2"
    65    if [ $? -ne 0 ]; then
    66      echo "assertFilesEqual failed: $msg"
    67      exit 2
    68    fi
    69  }
    70  
    71  assertNotEquals() {
    72    want=$1
    73    got=$2
    74    msg=$3
    75    if [ "$want" = "$got" ]; then
    76      echo "assertNotEquals failed: want='$want' got='$got' $msg"
    77      exit 2
    78    fi
    79  }
    80  
    81  log_test_case() {
    82    echo "  running $@"
    83  }
    84  
    85  run_test_case_with_snapshot_release() {
    86    log_test_case ${@:1}
    87  
    88    worker_pid=$(setup_snapshot_server)
    89    trap "teardown_snapshot_server $worker_pid" EXIT
    90  
    91    # run test function with all arguments
    92    ${@:1}
    93  
    94    trap - EXIT
    95    teardown_snapshot_server "${worker_pid}"
    96  }
    97  
    98  serve_port=8000
    99  
   100  setup_snapshot_server() {
   101    # if you want to see proof in the logs, feel free to adjust the redirection
   102    python3 -m http.server --directory "$(snapshot_dir)" $serve_port &> /dev/null &
   103    worker_pid=$!
   104  
   105    echoerr "serving up $(snapshot_dir) on port $serve_port"
   106  
   107    echoerr "$(ls -1 $(snapshot_dir) | sed 's/^/  ▕―― /')"
   108  
   109    check_snapshots_server_ready
   110  
   111    echoerr "snapshot server ready! (worker=${worker_pid})"
   112  
   113    echo "$worker_pid"
   114  }
   115  
   116  check_snapshots_server_ready() {
   117    i=0
   118    until $(curl -m 3 --output /dev/null --silent --head --fail localhost:$serve_port/); do
   119      sleep 1
   120      ((i=i+1))
   121      if [ "$i" -gt "30" ]; then
   122        echoerr "could not connect to local snapshot server! bailing..."
   123        exit 1
   124      fi
   125      printferr '.'
   126    done
   127  }
   128  
   129  teardown_snapshot_server() {
   130    worker_pid="$1"
   131    echoerr "stopping worker=${worker_pid}"
   132    kill "$worker_pid"
   133  }
   134  
   135  snapshot_version() {
   136    partial=$(ls ../../snapshot/*_checksums.txt | grep -o "_.*_checksums.txt")
   137    partial="${partial%_checksums.txt}"
   138    echo "${partial#_}"
   139  }
   140  
   141  snapshot_download_url() {
   142    echo "localhost:${serve_port}"
   143  }
   144  
   145  snapshot_dir() {
   146    echo "../../snapshot"
   147  }
   148  
   149  snapshot_checksums_path() {
   150    echo "$(ls $(snapshot_dir)/*_checksums.txt)"
   151  }
   152  
   153  snapshot_assets_count() {
   154    # example output before wc -l:
   155  
   156    #  ../../snapshot/syft_0.36.0-SNAPSHOT-e5e847a_linux_arm64.deb
   157    #  ../../snapshot/syft_0.36.0-SNAPSHOT-e5e847a_linux_arm64.tar.gz
   158    #  ../../snapshot/syft_0.36.0-SNAPSHOT-e5e847a_linux_amd64.rpm
   159    #  ../../snapshot/syft_0.36.0-SNAPSHOT-e5e847a_darwin_arm64.tar.gz
   160    #  ../../snapshot/syft_0.36.0-SNAPSHOT-e5e847a_linux_amd64.deb
   161    #  ../../snapshot/syft_0.36.0-SNAPSHOT-e5e847a_linux_arm64.rpm
   162    #  ../../snapshot/syft_0.36.0-SNAPSHOT-e5e847a_darwin_amd64.zip
   163    #  ../../snapshot/syft_0.36.0-SNAPSHOT-e5e847a_windows_amd64.zip
   164    #  ../../snapshot/syft_0.36.0-SNAPSHOT-e5e847a_darwin_arm64.zip
   165    #  ../../snapshot/syft_0.36.0-SNAPSHOT-e5e847a_linux_amd64.tar.gz
   166    #  ../../snapshot/syft_0.36.0-SNAPSHOT-e5e847a_darwin_amd64.tar.gz
   167  
   168    echo "$(find ../../snapshot -maxdepth 1 -type f | grep 'syft_' | grep -v checksums | wc -l | tr -d '[:space:]')"
   169  }
   170  
   171  
   172  snapshot_assets_archive_count() {
   173    # example output before wc -l:
   174  
   175    #  ../../snapshot/syft_0.36.0-SNAPSHOT-e5e847a_linux_arm64.tar.gz
   176    #  ../../snapshot/syft_0.36.0-SNAPSHOT-e5e847a_darwin_arm64.tar.gz
   177    #  ../../snapshot/syft_0.36.0-SNAPSHOT-e5e847a_darwin_amd64.zip
   178    #  ../../snapshot/syft_0.36.0-SNAPSHOT-e5e847a_windows_amd64.zip
   179    #  ../../snapshot/syft_0.36.0-SNAPSHOT-e5e847a_darwin_arm64.zip
   180    #  ../../snapshot/syft_0.36.0-SNAPSHOT-e5e847a_linux_amd64.tar.gz
   181    #  ../../snapshot/syft_0.36.0-SNAPSHOT-e5e847a_darwin_amd64.tar.gz
   182  
   183    echo "$(find ../../snapshot -maxdepth 1  -type f | grep 'syft_' | grep 'tar\|zip' | wc -l | tr -d '[:space:]')"
   184  }
   185  
   186  
   187  run_test_case() {
   188    log_test_case ${@:1}
   189    ${@:1}
   190  }