github.com/anchore/syft@v1.38.2/test/install/1_download_snapshot_asset_test.sh (about)

     1  . test_harness.sh
     2  
     3  DOWNLOAD_SNAPSHOT_POSITIVE_CASES=0
     4  
     5  # helper for asserting test_positive_snapshot_download_asset positive cases
     6  test_positive_snapshot_download_asset() {
     7    os="$1"
     8    arch="$2"
     9    format="$3"
    10  
    11    # for troubleshooting
    12    # log_set_priority 10
    13  
    14    name=${PROJECT_NAME}
    15    github_download=$(snapshot_download_url)
    16    version=$(snapshot_version)
    17  
    18    tmpdir=$(mktemp -d)
    19  
    20    actual_filepath=$(download_asset "${github_download}" "${tmpdir}" "${name}" "${os}" "${arch}" "${version}" "${format}" )
    21  
    22    assertFileExists "${actual_filepath}" "download_asset os=${os} arch=${arch} format=${format}"
    23  
    24    assertFilesEqual \
    25      "$(snapshot_dir)/${name}_${version}_${os}_${arch}.${format}" \
    26      "${actual_filepath}" \
    27      "unable to download os=${os} arch=${arch} format=${format}"
    28  
    29    ((DOWNLOAD_SNAPSHOT_POSITIVE_CASES++))
    30  
    31    rm -rf -- "$tmpdir"
    32  }
    33  
    34  
    35  test_download_snapshot_asset_exercised_all_assets() {
    36    expected=$(snapshot_assets_count)
    37  
    38    assertEquals "${expected}" "${DOWNLOAD_SNAPSHOT_POSITIVE_CASES}" "did not download all possible assets (missing an os/arch/format variant?)"
    39  }
    40  
    41  # helper for asserting download_asset negative cases
    42  test_negative_snapshot_download_asset() {
    43    os="$1"
    44    arch="$2"
    45    format="$3"
    46  
    47    # for troubleshooting
    48    # log_set_priority 10
    49  
    50    name=${PROJECT_NAME}
    51    github_download=$(snapshot_download_url)
    52    version=$(snapshot_version)
    53  
    54    tmpdir=$(mktemp -d)
    55  
    56    actual_filepath=$(download_asset "${github_download}" "${tmpdir}" "${name}" "${os}" "${arch}" "${version}" "${format}")
    57  
    58    assertEquals ""  "${actual_filepath}" "unable to download os=${os} arch=${arch} format=${format}"
    59  
    60    rm -rf -- "$tmpdir"
    61  }
    62  
    63  test_sboms_have_packages() {
    64    if ! command -v jq &> /dev/null; then
    65      echo "jq command not found. Please install jq or ensure it is in your PATH."
    66      exit 1
    67    fi
    68  
    69    find "$(snapshot_dir)/" -name "*.sbom" -print0 | while IFS= read -r -d '' file; do
    70        count=$(cat "$file" | jq ".artifacts | length")
    71        if [ "$count" -lt 80 ]; then
    72            echo "not enough packages found for file: $file"
    73            exit 1
    74        fi
    75    done
    76  }
    77  
    78  
    79  worker_pid=$(setup_snapshot_server)
    80  trap 'teardown_snapshot_server ${worker_pid}' EXIT
    81  
    82  # exercise all possible assets
    83  run_test_case test_positive_snapshot_download_asset "linux" "amd64" "sbom"
    84  run_test_case test_positive_snapshot_download_asset "linux" "amd64" "tar.gz"
    85  run_test_case test_positive_snapshot_download_asset "linux" "amd64" "rpm"
    86  run_test_case test_positive_snapshot_download_asset "linux" "amd64" "deb"
    87  run_test_case test_positive_snapshot_download_asset "linux" "arm64" "sbom"
    88  run_test_case test_positive_snapshot_download_asset "linux" "arm64" "tar.gz"
    89  run_test_case test_positive_snapshot_download_asset "linux" "arm64" "rpm"
    90  run_test_case test_positive_snapshot_download_asset "linux" "arm64" "deb"
    91  run_test_case test_positive_snapshot_download_asset "linux" "ppc64le" "sbom"
    92  run_test_case test_positive_snapshot_download_asset "linux" "ppc64le" "tar.gz"
    93  run_test_case test_positive_snapshot_download_asset "linux" "ppc64le" "rpm"
    94  run_test_case test_positive_snapshot_download_asset "linux" "ppc64le" "deb"
    95  run_test_case test_positive_snapshot_download_asset "linux" "s390x" "sbom"
    96  run_test_case test_positive_snapshot_download_asset "linux" "s390x" "tar.gz"
    97  run_test_case test_positive_snapshot_download_asset "linux" "s390x" "rpm"
    98  run_test_case test_positive_snapshot_download_asset "linux" "s390x" "deb"
    99  
   100  run_test_case test_positive_snapshot_download_asset "darwin" "amd64" "sbom"
   101  run_test_case test_positive_snapshot_download_asset "darwin" "amd64" "tar.gz"
   102  run_test_case test_positive_snapshot_download_asset "darwin" "arm64" "sbom"
   103  run_test_case test_positive_snapshot_download_asset "darwin" "arm64" "tar.gz"
   104  
   105  run_test_case test_positive_snapshot_download_asset "windows" "amd64" "sbom"
   106  run_test_case test_positive_snapshot_download_asset "windows" "amd64" "zip"
   107  run_test_case test_positive_snapshot_download_asset "windows" "arm64" "sbom"
   108  run_test_case test_positive_snapshot_download_asset "windows" "arm64" "zip"
   109  # note: the mac signing process produces a dmg which is not part of the snapshot process (thus is not exercised here)
   110  
   111  # let's make certain we covered all assets that were expected
   112  run_test_case test_download_snapshot_asset_exercised_all_assets
   113  
   114  # make certain we handle missing assets alright
   115  run_test_case test_negative_snapshot_download_asset "bogus" "amd64" "zip"
   116  
   117  # given we've downloaded the SBOMs, sanity check that they have a reasonable number of packages
   118  run_test_case test_sboms_have_packages
   119  
   120  trap - EXIT
   121  teardown_snapshot_server "${worker_pid}"