zotregistry.io/zot@v1.4.4-0.20231124084042-02a8ed785457/test/blackbox/referrers.bats (about)

     1  # Note: Intended to be run as "make run-blackbox-ci"
     2  #       Makefile target installs & checks all necessary tooling
     3  #       Extra tools that are not covered in Makefile target needs to be added in verify_prerequisites()
     4  
     5  load helpers_zot
     6  
     7  function verify_prerequisites() {
     8      if [ ! $(command -v curl) ]; then
     9          echo "you need to install curl as a prerequisite to running the tests" >&3
    10          return 1
    11      fi
    12  
    13      if [ ! $(command -v jq) ] &>/dev/null; then
    14          echo "you need to install jq as a prerequisite to running the tests" >&3
    15          return 1
    16      fi
    17  
    18      return 0
    19  }
    20  
    21  function setup() {
    22      # Verify prerequisites are available
    23      if ! $(verify_prerequisites); then
    24          exit 1
    25      fi
    26  
    27      # Download test data to folder common for the entire suite, not just this file
    28      skopeo --insecure-policy copy --format=oci docker://ghcr.io/project-zot/golang:1.20 oci:${TEST_DATA_DIR}/golang:1.20
    29  
    30      # Setup zot server
    31      ZOT_ROOT_DIR=${BATS_FILE_TMPDIR}/zot
    32      echo ${ZOT_ROOT_DIR}
    33      ZOT_LOG_FILE=${ZOT_ROOT_DIR}/zot-log.json
    34      ZOT_CONFIG_FILE=${BATS_FILE_TMPDIR}/zot_config.json
    35      mkdir -p ${ZOT_ROOT_DIR}
    36      touch ${ZOT_LOG_FILE}
    37      zot_port=$(get_free_port)
    38      echo ${zot_port} > ${BATS_FILE_TMPDIR}/zot.port
    39      cat >${ZOT_CONFIG_FILE} <<EOF
    40  {
    41      "distSpecVersion": "1.1.0-dev",
    42      "storage": {
    43          "rootDirectory": "${ZOT_ROOT_DIR}"
    44      },
    45      "http": {
    46          "address": "0.0.0.0",
    47          "port": "${zot_port}"
    48      },
    49      "log": {
    50          "level": "debug",
    51          "output": "${ZOT_LOG_FILE}"
    52      },
    53      "extensions": {
    54          "search": {
    55              "enable": true
    56          }
    57      }
    58  }
    59  EOF
    60  
    61      # Add artifact contents to files
    62      ARTIFACT_BLOBS_DIR=${BATS_FILE_TMPDIR}/artifact-blobs
    63      mkdir -p ${ARTIFACT_BLOBS_DIR}
    64  
    65      IMAGE_MANIFEST_REFERRER=${ARTIFACT_BLOBS_DIR}/image-manifest-ref-blob
    66      echo IMAGE_MANIFEST_REFERRER=${IMAGE_MANIFEST_REFERRER}
    67      touch ${IMAGE_MANIFEST_REFERRER}
    68      cat >${IMAGE_MANIFEST_REFERRER} <<EOF
    69          This artifact is represented as an ispec image manifest, this is the layer inside the manifest.
    70  EOF
    71  
    72      zot_serve ${ZOT_PATH} ${ZOT_CONFIG_FILE}
    73      wait_zot_reachable ${zot_port}
    74  
    75      run skopeo --insecure-policy copy --dest-tls-verify=false \
    76          oci:${TEST_DATA_DIR}/golang:1.20 \
    77          docker://127.0.0.1:${zot_port}/golang:1.20
    78      [ "$status" -eq 0 ]
    79      run curl http://127.0.0.1:${zot_port}/v2/_catalog
    80      [ "$status" -eq 0 ]
    81      [ $(echo "${lines[-1]}" | jq '.repositories[]') = '"golang"' ]
    82  
    83      run oras attach --plain-http --image-spec v1.1-image --artifact-type image.artifact/type 127.0.0.1:${zot_port}/golang:1.20 ${IMAGE_MANIFEST_REFERRER}
    84      [ "$status" -eq 0 ]
    85  
    86      MANIFEST_DIGEST=$(skopeo inspect --tls-verify=false docker://localhost:${zot_port}/golang:1.20 | jq -r '.Digest')
    87      echo ${MANIFEST_DIGEST}
    88  
    89      curl -X GET http://127.0.0.1:${zot_port}/v2/golang/referrers/${MANIFEST_DIGEST}?artifactType=image.artifact/type
    90  }
    91  
    92  function teardown() {
    93      # conditionally printing on failure is possible from teardown but not from from teardown_file
    94      cat ${BATS_FILE_TMPDIR}/zot/zot-log.json
    95  }
    96  
    97  function teardown_file() {
    98      zot_stop_all
    99  }
   100  
   101  @test "add referrers, one artifact and one image" {
   102      zot_port=`cat ${BATS_FILE_TMPDIR}/zot.port`
   103      # Check referrers API using the normal REST endpoint
   104      run curl -X GET http://127.0.0.1:${zot_port}/v2/golang/referrers/${MANIFEST_DIGEST}?artifactType=image.artifact/type
   105      [ "$status" -eq 0 ]
   106      [ $(echo "${lines[-1]}" | jq '.manifests[].artifactType') = '"image.artifact/type"' ]
   107  
   108      # Check referrers API using the GQL endpoint
   109      REFERRER_QUERY_DATA="{ \"query\": \"{ Referrers(repo:\\\"golang\\\", digest:\\\"${MANIFEST_DIGEST}\\\", type:[\\\"image.artifact/type\\\"]) { MediaType ArtifactType Digest Size} }\"}"
   110      run curl -X POST -H "Content-Type: application/json" --data "${REFERRER_QUERY_DATA}" http://localhost:${zot_port}/v2/_zot/ext/search
   111      [ "$status" -eq 0 ]
   112      [ $(echo "${lines[-1]}" | jq '.data.Referrers[].ArtifactType') = '"image.artifact/type"' ]
   113  }