#
# Common shell routines for testing with Docker containers
# Copyright (c) 2015,2018 Red Hat.
#

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

_check_containers()
{
    [ $PCP_PLATFORM = linux ] || _notrun "Linux-specific containers testing"
    _contain=`pmprobe -v pmcd.feature.containers | awk '{ print $3 }'`
    [ $_contain = 1 ] || _notrun "Running kernel does not support containers"
    pminfo -f pmcd.agent.type 2>/dev/null | grep linux | grep -q 'value 4'
    [ $? = 0 ] || _notrun "pmdalinux has insufficient privileges as a DSO"
}

_check_podman_binary()
{
    # allow external podman binary for testing
    podman=${PCP_PODMAN_PROG:-podman}
    which $podman >/dev/null 2>&1
    [ $? -eq 0 ] || _notrun "No podman binary found"
    podman="$sudo $podman"

    $sudo test -f "$PCP_SYSTEMDUNIT_DIR/podman.socket" || _notrun "No podman REST API service found"
}

_check_docker_binary()
{
    # allow external docker binary for testing
    docker=${PCP_DOCKER_PROG:-docker}
    which $docker >/dev/null 2>&1
    [ $? -eq 0 ] || _notrun "No docker binary found"

    # ensure we can run at least one valid docker command
    eval $docker images >/dev/null 2>&1
    if [ $? -ne 0 ]
    then
	# last ditch effort - try as root, otherwise just bail out
	docker="$sudo docker"
	eval $docker images >/dev/null 2>&1
	[ $? -eq 0 ] || _notrun "Cannot find a working 'docker images' command"
    fi
}

# check whether images are available before attempting to use it
# Pass in a list of image names to verify.  If any are not available,
# the test will notrun.
# 
_check_podman_images()
{
    __command="$podman run -it --rm"

    for __image in "$@"
    do
	# podman does not seem to terminate the line the same way (so, -c)
	__count=`eval $podman images --quiet $__image 2>/dev/null | wc -c`
	[ "$__count" -gt 0 ] || \
	    _notrun "Cannot find $image podman image.  Use: $__command $__image"
    done
}

_check_docker_images()
{
    __command="$docker run -it --rm"

    for __image in "$@"
    do
	__count=`eval $docker images --quiet $__image 2>/dev/null | wc -l`
	[ "$__count" -gt 0 ] || \
	    _notrun "Cannot find $image docker image.  Use: $__command $__image"
    done
}

# given a list of one or more (temporary) containers started during a
# QA test, ensure they are stopped and removed.
#
_remove_podman_containers()
{
    for __container in "$@"
    do
	eval $podman stop --time=2 $__container >/dev/null
	eval $podman rm --force $__container >/dev/null
    done
}

_remove_docker_containers()
{
    for __container in "$@"
    do
	eval $docker stop --time=2 $__container >/dev/null
	eval $docker rm --force $__container >/dev/null
    done
}

# given a list of one or more (temporary) pods started during a
# QA test, ensure they are stopped and removed.
#
_remove_podman_pods()
{
    for __pod in "$@"
    do
	$podman pod stop $__pod >/dev/null
	$podman pod rm $__pod >/dev/null
    done
}

# count the number of (running) containers
#
_count_podman_containers()
{
    # podman seems to need to run as root for ps
    $podman ps -q | wc -l
}

_count_docker_containers()
{
    $docker ps -q | wc -l
}
