# -*- sh -*-
#
# Tests for image-related endpoints
#

# FIXME: API doesn't support pull yet, so use podman
podman pull -q $IMAGE

t GET libpod/images/json 200 \
  .[0].Id~[0-9a-f]\\{64\\}
iid=$(jq -r '.[0].Id' <<<"$output")

t GET libpod/images/$iid/exists                     204
t GET libpod/images/$PODMAN_TEST_IMAGE_NAME/exists  204
t GET libpod/images/${iid}abcdef/exists  404 \
  .cause="failed to find image ${iid}abcdef"

# FIXME: compare to actual podman info
t GET libpod/images/json 200  \
  .[0].Id=${iid}

t GET libpod/images/$iid/json 200 \
  .Id=$iid \
  .RepoTags[0]=$IMAGE

# Same thing, but with abbreviated image id
t GET libpod/images/${iid:0:12}/json 200 \
  .Id=$iid \
  .RepoTags[0]=$IMAGE

# Docker API V1.24 filter parameter compatibility
t GET images/json?filter=$IMAGE 200 \
  length=1 \
  .[0].Names[0]=$IMAGE

# Negative test case
t GET images/json?filter=nonesuch 200 length=0

# FIXME: docker API incompatibility: libpod returns 'id', docker 'sha256:id'
t GET images/$iid/json 200 \
  .Id=sha256:$iid \
  .RepoTags[0]=$IMAGE

t POST "images/create?fromImage=alpine" 200 .error~null .status~".*Download complete.*"

t POST "images/create?fromImage=alpine&tag=latest" 200

# 10977 - handle platform parameter correctly
t POST "images/create?fromImage=alpine&platform=linux/arm64" 200
t GET  "images/alpine/json" 200 \
  .Architecture=arm64

# Make sure that new images are pulled
old_iid=$(podman image inspect --format "{{.ID}}" docker.io/library/alpine:latest)
podman rmi -f docker.io/library/alpine:latest
podman tag $IMAGE docker.io/library/alpine:latest
t POST "images/create?fromImage=alpine" 200 .error~null .status~".*$old_iid.*"
podman untag $IMAGE docker.io/library/alpine:latest

t POST "images/create?fromImage=quay.io/libpod/alpine&tag=sha256:fa93b01658e3a5a1686dc3ae55f170d8de487006fb53a28efcd12ab0710a2e5f" 200

# Display the image history
t GET libpod/images/nonesuch/history 404

for i in $iid ${iid:0:12} $PODMAN_TEST_IMAGE_NAME; do
  t GET libpod/images/$i/history 200 \
    .[0].Id=$iid \
    .[0].Created~[0-9]\\{10\\} \
    .[0].Tags=null \
    .[0].Size=0 \
    .[0].Comment=
done

# Export an image on the local
t GET libpod/images/nonesuch/get 404
t GET libpod/images/$iid/get?format=foo 500
t GET libpod/images/$PODMAN_TEST_IMAGE_NAME/get?compress=bar 400

for i in $iid ${iid:0:12} $PODMAN_TEST_IMAGE_NAME; do
  t GET "libpod/images/$i/get"                200 '[POSIX tar archive]'
  t GET "libpod/images/$i/get?compress=true"  200 '[POSIX tar archive]'
  t GET "libpod/images/$i/get?compress=false" 200 '[POSIX tar archive]'
done

#compat api list images sanity checks
t GET images/json?filters='garb1age}' 500 \
    .cause="invalid character 'g' looking for beginning of value"
t GET images/json?filters='{"label":["testl' 500 \
    .cause="unexpected end of JSON input"

#libpod api list images sanity checks
t GET libpod/images/json?filters='garb1age}' 500 \
    .cause="invalid character 'g' looking for beginning of value"
t GET libpod/images/json?filters='{"label":["testl' 500 \
    .cause="unexpected end of JSON input"

# Prune images - bad filter input
t POST images/prune?filters='garb1age}' 500 \
    .cause="invalid character 'g' looking for beginning of value"
t POST libpod/images/prune?filters='garb1age}' 500 \
    .cause="invalid character 'g' looking for beginning of value"

## Prune images with illformed label
t POST images/prune?filters='{"label":["tes' 500 \
    .cause="unexpected end of JSON input"
t POST libpod/images/prune?filters='{"label":["tes' 500 \
    .cause="unexpected end of JSON input"


#create, list and remove dangling image
podman image build -t test:test -<<EOF
from alpine
RUN >file1
EOF

podman image build -t test:test --label xyz -<<EOF
from alpine
RUN >file2
EOF

t GET images/json?filters='{"dangling":["true"]}' 200 length=1
t POST images/prune?filters='{"dangling":["true"]}' 200
t GET images/json?filters='{"dangling":["true"]}' 200 length=0

#label filter check in libpod and compat
t GET images/json?filters='{"label":["xyz"]}' 200 length=1
t GET libpod/images/json?filters='{"label":["xyz"]}' 200 length=1

t DELETE libpod/images/test:test 200

t GET images/json?filters='{"label":["xyz"]}' 200 length=0
t GET libpod/images/json?filters='{"label":["xyz"]}' 200 length=0


# to be used in prune until filter tests
podman image build -t test1:latest -<<EOF
from alpine
RUN >file3
EOF

# image should not be deleted
t GET images/json?filters='{"reference":["test1"]}' 200 length=1
t POST images/prune?filters='{"until":["500000"]}' 200
t GET images/json?filters='{"reference":["test1"]}' 200 length=1

t DELETE libpod/images/test1:latest 200

t GET "images/get?names=alpine" 200 '[POSIX tar archive]'

podman pull busybox
t GET "images/get?names=alpine&names=busybox" 200 '[POSIX tar archive]'
img_cnt=$(tar xf "$WORKDIR/curl.result.out" manifest.json -O | jq "length")
is "$img_cnt" 2 "number of images in tar archive"

# check build works when uploading container file as a tar, see issue #10660
TMPD=$(mktemp -d podman-apiv2-test.build.XXXXXXXX)
function cleanBuildTest() {
    podman rmi -a -f
    rm -rf "${TMPD}" &> /dev/null
}
CONTAINERFILE_TAR="${TMPD}/containerfile.tar"
cat > $TMPD/containerfile << EOF
FROM quay.io/libpod/alpine_labels:latest
EOF
tar --format=posix -C $TMPD -cvf ${CONTAINERFILE_TAR} containerfile &> /dev/null

curl -XPOST --data-binary @<(cat $CONTAINERFILE_TAR) \
    -H "content-type: application/x-tar" \
    --dump-header "${TMPD}/headers.txt" \
    -o "${TMPD}/response.txt" \
    "http://$HOST:$PORT/v1.40/libpod/build?dockerfile=containerfile" &> /dev/null

BUILD_TEST_ERROR=""

if ! grep -q '200 OK' "${TMPD}/headers.txt"; then
    echo -e "${red}NOK: Image build from tar failed response was not 200 OK (application/x-tar)"
    BUILD_TEST_ERROR="1"
fi

if ! grep -q 'quay.io/libpod/alpine_labels' "${TMPD}/response.txt"; then
    echo -e "${red}NOK: Image build from tar failed image name not in response"
    BUILD_TEST_ERROR="1"
fi

curl -XPOST --data-binary @<(cat $CONTAINERFILE_TAR) \
    -H "content-type: application/tar" \
    --dump-header "${TMPD}/headers.txt" \
    -o /dev/null \
    "http://$HOST:$PORT/v1.40/libpod/build?dockerfile=containerfile" &> /dev/null
if ! grep -q '200 OK' "${TMPD}/headers.txt"; then
    echo -e "${red}NOK: Image build from tar failed response was not 200 OK (application/tar)"
    BUILD_TEST_ERROR="1"
fi

# Yes, this is very un-RESTful re: Content-Type header ignored when compatibility endpoint used
# See https://github.com/containers/podman/issues/11012
curl -XPOST --data-binary @<(cat $CONTAINERFILE_TAR) \
    -H "content-type: application/json" \
    --dump-header "${TMPD}/headers.txt" \
    -o /dev/null \
    "http://$HOST:$PORT/v1.40/build?dockerfile=containerfile" &> /dev/null
if ! grep -q '200 OK' "${TMPD}/headers.txt"; then
    echo -e "${red}NOK: Image build from tar failed response was not 200 OK (application/tar)"
    BUILD_TEST_ERROR="1"
fi

curl -XPOST --data-binary @<(cat $CONTAINERFILE_TAR) \
    -H "content-type: application/json" \
    --dump-header "${TMPD}/headers.txt" \
    -o /dev/null \
    "http://$HOST:$PORT/v1.40/libpod/build?dockerfile=containerfile" &> /dev/null
if ! grep -q '400 Bad Request' "${TMPD}/headers.txt"; then
    echo -e "${red}NOK: Image build should have failed with 400 (wrong Content-Type)"
    BUILD_TEST_ERROR="1"
fi

cleanBuildTest
if [[ "${BUILD_TEST_ERROR}" ]]; then
    exit 1
fi

# vim: filetype=sh