zotregistry.dev/zot@v1.4.4-0.20240314164342-eec277e14d20/test/blackbox/metadata.bats (about)

     1  # Note: Intended to be run as "make run-blackbox-tests" or "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) ]; then
    14          echo "you need to install jq as a prerequisite to running the tests" >&3
    15          return 1
    16      fi
    17  
    18      if [ ! $(command -v htpasswd) ]; then
    19          echo "you need to install htpasswd as a prerequisite to running the tests" >&3
    20          return 1
    21      fi
    22  
    23      return 0
    24  }
    25  
    26  function setup_file() {
    27      # Verify prerequisites are available
    28      if ! $(verify_prerequisites); then
    29          exit 1
    30      fi
    31  
    32      # Download test data to folder common for the entire suite, not just this file
    33      skopeo --insecure-policy copy --format=oci docker://ghcr.io/project-zot/test-images/busybox:1.36 oci:${TEST_DATA_DIR}/busybox:1.36
    34  
    35      # Setup zot server
    36      local zot_root_dir=${BATS_FILE_TMPDIR}/zot
    37      local zot_config_file=${BATS_FILE_TMPDIR}/zot_config.json
    38      local oci_data_dir=${BATS_FILE_TMPDIR}/oci
    39      local zot_htpasswd_file=${BATS_FILE_TMPDIR}/htpasswd
    40      mkdir -p ${zot_root_dir}
    41      mkdir -p ${oci_data_dir}
    42      zot_port=$(get_free_port)
    43      echo ${zot_port} > ${BATS_FILE_TMPDIR}/zot.port
    44      htpasswd -Bbn ${AUTH_USER} ${AUTH_PASS} >> ${zot_htpasswd_file}
    45      cat > ${zot_config_file}<<EOF
    46  {
    47      "distSpecVersion": "1.1.0",
    48      "storage": {
    49          "rootDirectory": "${zot_root_dir}"
    50      },
    51      "extensions": {
    52          "search": {
    53              "enable": true
    54          },
    55          "ui": {
    56              "enable": true
    57          }
    58      },
    59      "http": {
    60          "address": "0.0.0.0",
    61          "port": "${zot_port}",
    62          "auth": {
    63              "htpasswd": {
    64                  "path": "${zot_htpasswd_file}"
    65              }
    66          },
    67          "accessControl": {
    68              "repositories": {
    69                  "**": {
    70                      "anonymousPolicy": ["read"],
    71                      "policies": [
    72                          {
    73                              "users": [
    74                                  "${AUTH_USER}"
    75                              ],
    76                              "actions": [
    77                                  "read",
    78                                  "create",
    79                                  "update"
    80                              ]
    81                          }
    82                      ]
    83                  }
    84              }
    85          }
    86      },
    87      "log": {
    88          "level": "debug",
    89          "output": "${BATS_FILE_TMPDIR}/zot.log"
    90      }
    91  }
    92  EOF
    93      git -C ${BATS_FILE_TMPDIR} clone https://github.com/project-zot/helm-charts.git
    94      zot_serve ${ZOT_PATH} ${zot_config_file}
    95      wait_zot_reachable ${zot_port}
    96  }
    97  
    98  function teardown() {
    99      # conditionally printing on failure is possible from teardown but not from from teardown_file
   100      cat ${BATS_FILE_TMPDIR}/zot.log
   101  }
   102  
   103  function teardown_file() {
   104      zot_stop_all
   105  }
   106  
   107  @test "push image user policy" {
   108      zot_port=`cat ${BATS_FILE_TMPDIR}/zot.port`
   109      run skopeo --insecure-policy copy --dest-creds ${AUTH_USER}:${AUTH_PASS} --dest-tls-verify=false \
   110          oci:${TEST_DATA_DIR}/busybox:1.36 \
   111          docker://127.0.0.1:${zot_port}/busybox:1.36
   112      [ "$status" -eq 0 ]
   113  }
   114  
   115  @test "User metadata starredRepos" {
   116      zot_port=`cat ${BATS_FILE_TMPDIR}/zot.port`
   117      run skopeo --insecure-policy copy --dest-creds ${AUTH_USER}:${AUTH_PASS} --dest-tls-verify=false \
   118          oci:${TEST_DATA_DIR}/busybox:1.36 \
   119          docker://127.0.0.1:${zot_port}/busybox:1.36
   120      [ "$status" -eq 0 ]
   121  
   122      USER_STAR_REPOS_QUERY='{ "query": "{ StarredRepos { Results { Name } } }"}'
   123  
   124      run curl --user ${AUTH_USER}:${AUTH_PASS} -X POST -H "Content-Type: application/json" --data "${USER_STAR_REPOS_QUERY}" http://localhost:${zot_port}/v2/_zot/ext/search
   125      [ "$status" -eq 0 ]
   126      [ $(echo "${lines[-1]}" | jq '.data.StarredRepos.Results') = '[]' ]
   127  
   128      run curl --user ${AUTH_USER}:${AUTH_PASS} -X PUT "http://127.0.0.1:${zot_port}/v2/_zot/ext/userprefs?repo=busybox&action=toggleStar"
   129      [ "$status" -eq 0 ]
   130  
   131      run curl --user ${AUTH_USER}:${AUTH_PASS} -X POST -H "Content-Type: application/json" --data "${USER_STAR_REPOS_QUERY}" http://localhost:${zot_port}/v2/_zot/ext/search
   132      [ "$status" -eq 0 ]
   133      echo  $(echo "${lines[-1]}" | jq '.data.StarredRepos.Results[0].Name')
   134      [ $(echo "${lines[-1]}" | jq -r '.data.StarredRepos.Results[0].Name') = 'busybox' ]
   135  
   136      run curl --user ${AUTH_USER}:${AUTH_PASS} -X PUT "http://127.0.0.1:${zot_port}/v2/_zot/ext/userprefs?repo=busybox&action=toggleStar"
   137      [ "$status" -eq 0 ]
   138  
   139          run curl --user ${AUTH_USER}:${AUTH_PASS} -X POST -H "Content-Type: application/json" --data "${USER_STAR_REPOS_QUERY}" http://localhost:${zot_port}/v2/_zot/ext/search
   140      [ "$status" -eq 0 ]
   141      echo  $(echo "${lines[-1]}" | jq '.data.StarredRepos.Results')
   142      [ $(echo "${lines[-1]}" | jq -r '.data.StarredRepos.Results') = '[]' ]
   143  }
   144  
   145  @test "User metadata bookmarkedRepos" {
   146      zot_port=`cat ${BATS_FILE_TMPDIR}/zot.port`
   147      run skopeo --insecure-policy copy --dest-creds ${AUTH_USER}:${AUTH_PASS} --dest-tls-verify=false \
   148          oci:${TEST_DATA_DIR}/busybox:1.36 \
   149          docker://127.0.0.1:${zot_port}/busybox:1.36
   150      [ "$status" -eq 0 ]
   151  
   152      USER_BOOKMARK_REPOS_QUERY='{ "query": "{ BookmarkedRepos { Results { Name } } }"}'
   153  
   154       run curl --user ${AUTH_USER}:${AUTH_PASS} -X POST -H "Content-Type: application/json" --data "${USER_BOOKMARK_REPOS_QUERY}" http://localhost:${zot_port}/v2/_zot/ext/search
   155      [ "$status" -eq 0 ]
   156      [ $(echo "${lines[-1]}" | jq '.data.BookmarkedRepos.Results') = '[]' ]
   157  
   158      run curl --user ${AUTH_USER}:${AUTH_PASS} -X PUT "http://127.0.0.1:${zot_port}/v2/_zot/ext/userprefs?repo=busybox&action=toggleBookmark"
   159      [ "$status" -eq 0 ]
   160  
   161      run curl --user ${AUTH_USER}:${AUTH_PASS} -X POST -H "Content-Type: application/json" --data "${USER_BOOKMARK_REPOS_QUERY}" http://localhost:${zot_port}/v2/_zot/ext/search
   162      [ "$status" -eq 0 ]
   163      [ $(echo "${lines[-1]}" | jq -r '.data.BookmarkedRepos.Results[0].Name') = 'busybox' ]
   164  
   165      run curl --user ${AUTH_USER}:${AUTH_PASS} -X PUT "http://127.0.0.1:${zot_port}/v2/_zot/ext/userprefs?repo=busybox&action=toggleBookmark"
   166      [ "$status" -eq 0 ]
   167  
   168          run curl --user ${AUTH_USER}:${AUTH_PASS} -X POST -H "Content-Type: application/json" --data "${USER_BOOKMARK_REPOS_QUERY}" http://localhost:${zot_port}/v2/_zot/ext/search
   169      [ "$status" -eq 0 ]
   170      [ $(echo "${lines[-1]}" | jq -r '.data.BookmarkedRepos.Results') = '[]' ]
   171  }