# -*- sh -*-
#
# test container-related endpoints
#

podman pull $IMAGE &>/dev/null

# Unimplemented
#t POST libpod/containers/create '' 201 'sdf'

# Ensure clean slate
podman rm -a -f &>/dev/null

t GET "libpod/containers/json (at start: clean slate)" 200 length=0

podman run $IMAGE true

t GET libpod/containers/json 200 length=0

t GET libpod/containers/json?all=true 200 \
  length=1 \
  .[0].Id~[0-9a-f]\\{64\\} \
  .[0].Image=$IMAGE \
  .[0].Command[0]="true" \
  .[0].State~\\\(exited\\\|stopped\\\) \
  .[0].ExitCode=0 \
  .[0].IsInfra=false

# Make sure `limit` works.
t GET libpod/containers/json?limit=1 200 \
  length=1 \
  .[0].Id~[0-9a-f]\\{64\\} \
  .[0].Image=$IMAGE \
  .[0].Command[0]="true" \
  .[0].State~\\\(exited\\\|stopped\\\) \
  .[0].ExitCode=0 \
  .[0].IsInfra=false

# Make sure `last` works, which is an alias for `limit`.
# See https://github.com/containers/podman/issues/6413.
t GET libpod/containers/json?last=1 200 \
  length=1 \
  .[0].Id~[0-9a-f]\\{64\\} \
  .[0].Image=$IMAGE \
  .[0].Command[0]="true" \
  .[0].State~\\\(exited\\\|stopped\\\) \
  .[0].ExitCode=0 \
  .[0].IsInfra=false

cid=$(jq -r '.[0].Id' <<<"$output")

t DELETE libpod/containers/$cid 204

# Issue #6799: it should be possible to start a container, even w/o args.
t POST libpod/containers/create?name=test_noargs Image=${IMAGE} 201 \
  .Id~[0-9a-f]\\{64\\}
cid=$(jq -r '.Id' <<<"$output")
# Prior to the fix in #6835, this would fail 500 "args must not be empty"
t POST   libpod/containers/${cid}/start '' 204
# Container should exit almost immediately. Wait for it, confirm successful run
t POST   libpod/containers/${cid}/wait  '' 200 '0'
t GET    libpod/containers/${cid}/json 200 \
  .Id=$cid \
  .State.Status~\\\(exited\\\|stopped\\\) \
  .State.Running=false \
  .State.ExitCode=0
t DELETE libpod/containers/$cid 204

CNAME=myfoo
podman run --name $CNAME $IMAGE -td top
t GET libpod/containers/json?all=true 200 \
  .[0].Id~[0-9a-f]\\{64\\}
cid=$(jq -r '.[0].Id' <<<"$output")

# No such container
t POST "libpod/commit?container=nonesuch" '' 404

# Comment can only be used with docker format, not OCI
cparam="repo=newrepo&comment=foo&author=bob"
t POST "libpod/commit?container=$CNAME&$cparam"  '' 500 \
  .cause="messages are only compatible with the docker image format (-f docker)"

# Commit a new image from the container
t POST "libpod/commit?container=$CNAME" '' 200 \
  .Id~[0-9a-f]\\{64\\}
iid=$(jq -r '.Id' <<<"$output")
t GET libpod/images/$iid/json 200 \
  .RepoTags[0]=null \
  .Author="" \
  .Comment=""

# Commit a new image w/o tag
cparam="repo=newrepo&comment=foo&author=bob&format=docker"
t POST "libpod/commit?container=$CNAME&$cparam" '' 200
t GET libpod/images/newrepo:latest/json 200 \
  .RepoTags[0]=localhost/newrepo:latest	\
  .Author=bob \
  .Comment=foo

# Commit a new image w/ specified tag and author
cparam="repo=newrepo&tag=v1&author=alice"
t POST "libpod/commit?container=$cid&$cparam&pause=false" '' 200
t GET libpod/images/newrepo:v1/json 200 \
  .RepoTags[0]=localhost/newrepo:v1     \
  .Author=alice

# Commit a new image w/ full parameters
cparam="repo=newrepo&tag=v2&comment=bar&author=eric"
cparam="$cparam&format=docker&changes=CMD=/bin/foo"
t POST "libpod/commit?container=${cid:0:12}&$cparam&pause=true" '' 200
t GET libpod/images/newrepo:v2/json 200 \
  .RepoTags[0]=localhost/newrepo:v2	\
  .Author=eric \
  .Comment=bar \
  .Config.Cmd[-1]="/bin/foo"

# Create a container for testing the container initializing later
podman create -t -i --name myctr $IMAGE ls

# Check configuration before initializing
t GET libpod/containers/myctr/json 200 \
  .Id~[0-9a-f]\\{64\\} \
  .State.Status="configured" \
  .State.Pid=0 \
  .ResolvConfPath="" \
  .HostnamePath="" \
  .HostsPath="" \
  .NetworkSettings.SandboxKey=""

cpid_file=$(jq -r '.ConmonPidFile' <<<"$output")
userdata_path=$(dirname $cpid_file)

# Initializing the container
t POST libpod/containers/myctr/init '' 204

# Check configuration after initializing
t GET libpod/containers/myctr/json 200 \
  .Id~[0-9a-f]\\{64\\} \
  .State.Status="created" \
  .State.Pid~[0-9]\\{1\,8\\} \
  .ResolvConfPath=$userdata_path/resolv.conf \
  .HostnamePath=$userdata_path/hostname \
  .HostsPath=$userdata_path/hosts \
  .NetworkSettings.SandboxKey~.*/netns/cni- \
  .OCIConfigPath~.*config\.json \
  .GraphDriver.Data.MergedDir~.*merged

t DELETE images/localhost/newrepo:latest?force=true 200
t DELETE images/localhost/newrepo:v1?force=true 200
t DELETE images/localhost/newrepo:v2?force=true 200
t DELETE libpod/containers/$cid 204
t DELETE libpod/containers/myctr 204


# test apiv2 create container with correct entrypoint and cmd
# --data '{"Image":"quay.io/libpod/alpine_labels:latest","Entrypoint":["echo"],"Cmd":["param1","param2"]}'
t POST containers/create '"Image":"'$IMAGE'","Entrypoint":["echo"],"Cmd":["param1","param2"]' 201 \
  .Id~[0-9a-f]\\{64\\}
cid=$(jq -r '.Id' <<<"$output")
t GET containers/$cid/json 200 \
  .Config.Entrypoint[0]="echo" \
  .Config.Cmd[0]="param1" \
  .Config.Cmd[1]="param2" \
  .Path="echo" \
  .Args[0]="param1" \
  .Args[1]="param2"
t DELETE containers/$cid 204

# test only set the entrpoint, Cmd should be []
t POST containers/create '"Image":"'$IMAGE'","Entrypoint":["echo","param1"]' 201 \
  .Id~[0-9a-f]\\{64\\}
cid=$(jq -r '.Id' <<<"$output")
t GET containers/$cid/json 200 \
  .Config.Entrypoint[0]="echo" \
  .Config.Entrypoint[1]="param1" \
  .Config.Cmd='[]' \
  .Path="echo" \
  .Args[0]="param1"
t DELETE containers/$cid 204

# vim: filetype=sh
