zotregistry.dev/zot@v1.4.4-0.20240314164342-eec277e14d20/test/blackbox/garbage_collect.bats (about) 1 load helpers_zot 2 3 function verify_prerequisites { 4 if [ ! $(command -v curl) ]; then 5 echo "you need to install curl as a prerequisite to running the tests" >&3 6 return 1 7 fi 8 9 if [ ! $(command -v jq) ]; then 10 echo "you need to install jq as a prerequisite to running the tests" >&3 11 return 1 12 fi 13 14 return 0 15 } 16 17 function setup_file() { 18 # Verify prerequisites are available 19 if ! $(verify_prerequisites); then 20 exit 1 21 fi 22 23 # Download test data to folder common for the entire suite, not just this file 24 skopeo --insecure-policy copy --format=oci docker://ghcr.io/project-zot/golang:1.20 oci:${TEST_DATA_DIR}/golang:1.20 25 # Setup zot server 26 local zot_root_dir=${BATS_FILE_TMPDIR}/zot 27 local zot_config_file=${BATS_FILE_TMPDIR}/zot_config.json 28 local oci_data_dir=${BATS_FILE_TMPDIR}/oci 29 zot_port=$(get_free_port) 30 echo ${zot_port} > ${BATS_FILE_TMPDIR}/zot.port 31 mkdir -p ${zot_root_dir} 32 mkdir -p ${oci_data_dir} 33 cat > ${zot_config_file}<<EOF 34 { 35 "distSpecVersion": "1.1.0", 36 "storage": { 37 "rootDirectory": "${zot_root_dir}", 38 "gc": true, 39 "gcDelay": "30s", 40 "gcInterval": "1s", 41 "retention": { 42 "delay": "40s", 43 "policies": [ 44 { 45 "repositories": ["**"], 46 "deleteReferrers": true, 47 "deleteUntagged": true 48 } 49 ] 50 } 51 }, 52 "http": { 53 "address": "0.0.0.0", 54 "port": "${zot_port}" 55 }, 56 "log": { 57 "level": "debug", 58 "output": "${BATS_FILE_TMPDIR}/gc.log" 59 } 60 } 61 EOF 62 zot_serve ${ZOT_PATH} ${zot_config_file} 63 wait_zot_reachable ${zot_port} 64 } 65 66 function teardown() { 67 # conditionally printing on failure is possible from teardown but not from from teardown_file 68 cat ${BATS_FILE_TMPDIR}/gc.log 69 } 70 71 function teardown_file() { 72 zot_stop_all 73 } 74 75 @test "push image" { 76 zot_port=`cat ${BATS_FILE_TMPDIR}/zot.port` 77 run skopeo --insecure-policy copy --dest-tls-verify=false \ 78 oci:${TEST_DATA_DIR}/golang:1.20 \ 79 docker://127.0.0.1:${zot_port}/golang:1.20 80 [ "$status" -eq 0 ] 81 run curl http://127.0.0.1:${zot_port}/v2/_catalog 82 [ "$status" -eq 0 ] 83 [ $(echo "${lines[-1]}" | jq '.repositories[]') = '"golang"' ] 84 run curl http://127.0.0.1:${zot_port}/v2/golang/tags/list 85 [ "$status" -eq 0 ] 86 [ $(echo "${lines[-1]}" | jq '.tags[]') = '"1.20"' ] 87 } 88 89 @test "push image index" { 90 zot_port=`cat ${BATS_FILE_TMPDIR}/zot.port` 91 # --multi-arch below pushes an image index (containing many images) instead 92 # of an image manifest (single image) 93 run skopeo --insecure-policy copy --format=oci --dest-tls-verify=false --multi-arch=all \ 94 docker://public.ecr.aws/docker/library/busybox:latest \ 95 docker://127.0.0.1:${zot_port}/busybox:latest 96 [ "$status" -eq 0 ] 97 run curl http://127.0.0.1:${zot_port}/v2/_catalog 98 [ "$status" -eq 0 ] 99 [ $(echo "${lines[-1]}" | jq '.repositories[0]') = '"busybox"' ] 100 run curl http://127.0.0.1:${zot_port}/v2/busybox/tags/list 101 [ "$status" -eq 0 ] 102 [ $(echo "${lines[-1]}" | jq '.tags[]') = '"latest"' ] 103 } 104 105 @test "attach oras artifacts" { 106 zot_port=`cat ${BATS_FILE_TMPDIR}/zot.port` 107 # attach signature to image 108 echo "{\"artifact\": \"\", \"signature\": \"pat hancock\"}" > signature.json 109 run oras attach --plain-http 127.0.0.1:${zot_port}/golang:1.20 --image-spec v1.1-image --artifact-type 'signature/example' ./signature.json:application/json 110 [ "$status" -eq 0 ] 111 # attach sbom to image 112 echo "{\"version\": \"0.0.0.0\", \"artifact\": \"'127.0.0.1:${zot_port}/golang:1.20'\", \"contents\": \"good\"}" > sbom.json 113 run oras attach --plain-http 127.0.0.1:${zot_port}/golang:1.20 --image-spec v1.1-image --artifact-type 'sbom/example' ./sbom.json:application/json 114 [ "$status" -eq 0 ] 115 116 # attach signature to index image 117 run oras attach --plain-http 127.0.0.1:${zot_port}/busybox:latest --image-spec v1.1-image --artifact-type 'signature/example' ./signature.json:application/json 118 [ "$status" -eq 0 ] 119 # attach sbom to index image 120 echo "{\"version\": \"0.0.0.0\", \"artifact\": \"'127.0.0.1:${zot_port}/golang:1.20'\", \"contents\": \"good\"}" > sbom.json 121 run oras attach --plain-http 127.0.0.1:${zot_port}/busybox:latest --image-spec v1.1-image --artifact-type 'sbom/example' ./sbom.json:application/json 122 [ "$status" -eq 0 ] 123 } 124 125 @test "push OCI artifact with regclient" { 126 zot_port=`cat ${BATS_FILE_TMPDIR}/zot.port` 127 run regctl registry set 127.0.0.1:${zot_port} --tls disabled 128 [ "$status" -eq 0 ] 129 130 run regctl artifact put --artifact-type application/vnd.example.artifact --subject 127.0.0.1:${zot_port}/golang:1.20 <<EOF 131 this is an artifact 132 EOF 133 [ "$status" -eq 0 ] 134 135 run regctl artifact get --subject 127.0.0.1:${zot_port}/golang:1.20 136 [ "$status" -eq 0 ] 137 138 run regctl artifact put --artifact-type application/vnd.example.artifact --subject 127.0.0.1:${zot_port}/busybox:latest <<EOF 139 this is an artifact 140 EOF 141 [ "$status" -eq 0 ] 142 143 run regctl artifact get --subject 127.0.0.1:${zot_port}/busybox:latest 144 [ "$status" -eq 0 ] 145 } 146 147 @test "garbage collect all artifacts after image delete" { 148 zot_port=`cat ${BATS_FILE_TMPDIR}/zot.port` 149 run skopeo --insecure-policy delete --tls-verify=false \ 150 docker://127.0.0.1:${zot_port}/golang:1.20 151 [ "$status" -eq 0 ] 152 153 run skopeo --insecure-policy delete --tls-verify=false \ 154 docker://127.0.0.1:${zot_port}/busybox:latest 155 [ "$status" -eq 0 ] 156 157 # sleep past gc delay 158 sleep 100 159 160 # gc should have removed artifacts 161 run regctl artifact get --subject 127.0.0.1:${zot_port}/golang:1.20 162 [ "$status" -eq 1 ] 163 164 run regctl artifact get --subject 127.0.0.1:${zot_port}/busybox:latest 165 [ "$status" -eq 1 ] 166 167 run oras discover --plain-http -o json 127.0.0.1:${zot_port}/golang:1.20 168 [ "$status" -eq 1 ] 169 170 run oras discover --plain-http -o json 127.0.0.1:${zot_port}/busybox:latest 171 [ "$status" -eq 1 ] 172 173 # repos should also be gc'ed 174 run curl http://127.0.0.1:${zot_port}/v2/_catalog 175 [ "$status" -eq 0 ] 176 [ $(echo "${lines[-1]}" | jq -r '.repositories | length') -eq 0 ] 177 } 178 179