File: common.containers

package info (click to toggle)
pcp 7.0.5-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 252,916 kB
  • sloc: ansic: 1,478,844; sh: 177,285; xml: 160,462; cpp: 83,809; python: 24,349; perl: 18,303; yacc: 6,877; lex: 2,864; makefile: 2,694; awk: 165; fortran: 60; java: 52
file content (122 lines) | stat: -rw-r--r-- 3,184 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#
# 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 2>&1
	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
}