summaryrefslogtreecommitdiff
path: root/contrib/python/podman/test/test_runner.sh
blob: 081b907791658c9214db0cd36f70f6f17b9b8046 (plain)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/bin/bash

# podman needs to play some games with resources
if [[ $(id -u) != 0 ]]; then
  echo >&2 $0 must be run as root.
  exit 2
fi

# setup path to find new binaries _NOT_ system binaries
if [[ ! -x ../../../bin/podman ]]; then
  echo 1>&2 Cannot find podman binary from libpod root directory. Run \"make binaries\"
  exit 1
fi
export PATH=../../../bin:$PATH

function usage {
  echo 1>&2 $0 '[-v] [-h] [test.<TestCase>|test.<TestCase>.<step>]'
}

while getopts "vh" arg; do
  case $arg in
    v ) VERBOSE='-v'; export LOG_LEVEL=debug ;;
    h ) usage ; exit 0 ;;
    \? ) usage ; exit 2 ;;
  esac
done
shift $((OPTIND -1))

function cleanup {
  set +xeuo pipefail
  # aggressive cleanup as tests may crash leaving crap around
  umount '^(shm|nsfs)'
  umount '\/run\/netns'
  rm -r "$1"
}

# Create temporary directory for storage
export TMPDIR=`mktemp -d /tmp/podman.XXXXXXXXXX`
trap "cleanup $TMPDIR" EXIT

function umount {
  # xargs -r always ran once, so write any mount points to file first
  mount |awk "/$1/"' { print $3 }' >${TMPDIR}/mounts
  if [[ -s ${TMPDIR}/mounts ]]; then
    xargs <${TMPDIR}/mounts -t umount
  fi
}

function showlog {
  [[ -s $1 ]] && cat <<-EOT
$1 =====
$(cat "$1")

EOT
}

# Need locations to store stuff
mkdir -p ${TMPDIR}/{podman,crio,crio-run,cni/net.d,ctnr,tunnel}

# Cannot be done in python unittest fixtures.  EnvVar not picked up.
export REGISTRIES_CONFIG_PATH=${TMPDIR}/registry.conf
cat >$REGISTRIES_CONFIG_PATH <<-EOT
  [registries.search]
    registries = ['docker.io']
  [registries.insecure]
    registries = []
  [registries.block]
    registries = []
EOT

export CNI_CONFIG_PATH=${TMPDIR}/cni/net.d
cat >$CNI_CONFIG_PATH/87-podman-bridge.conflist <<-EOT
{
  "cniVersion": "0.3.0",
  "name": "podman",
  "plugins": [{
      "type": "bridge",
      "bridge": "cni0",
      "isGateway": true,
      "ipMasq": true,
      "ipam": {
        "type": "host-local",
        "subnet": "10.88.0.0/16",
        "routes": [{
          "dst": "0.0.0.0/0"
        }]
      }
    },
    {
      "type": "portmap",
      "capabilities": {
        "portMappings": true
      }
    }
  ]
}
EOT

cat >$TMPDIR/ctnr/hello.sh <<-EOT
echo 'Hello, World'
exit 0
EOT

cat >$TMPDIR/ctnr/Dockerfile <<-EOT
FROM alpine:latest
COPY ./hello.sh /tmp/
RUN chmod 755 /tmp/hello.sh
ENTRYPOINT ["/tmp/hello.sh"]
EOT

export PODMAN_HOST="unix:${TMPDIR}/podman/io.podman"
PODMAN_ARGS="--storage-driver=vfs \
  --root=${TMPDIR}/crio \
  --runroot=${TMPDIR}/crio-run \
  --cni-config-dir=$CNI_CONFIG_PATH \
  --cgroup-manager=cgroupfs \
  "
if [[ -n $VERBOSE ]]; then
  PODMAN_ARGS="$PODMAN_ARGS --log-level=$LOG_LEVEL"
fi
PODMAN="podman $PODMAN_ARGS"

cat <<-EOT |tee /tmp/test_podman.output
$($PODMAN --version)
$PODMAN varlink --timeout=0 ${PODMAN_HOST}
==========================================
EOT

# Run podman in background without systemd for test purposes
$PODMAN varlink --timeout=0 ${PODMAN_HOST} >>/tmp/test_podman.output 2>&1 &
if [[ $? != 0 ]]; then
  echo 1>&2 Failed to start podman
  showlog /tmp/test_podman.output
fi

if [[ -z $1 ]]; then
  export PYTHONPATH=.
  python3 -m unittest discover -s . $VERBOSE
  RETURNCODE=$?
else
  export PYTHONPATH=.:./test
  python3 -m unittest $1 $VERBOSE
  RETURNCODE=$?
fi

if [[ "$RETURNCODE" -ne 0 ]] && [[ -n "$FLAKE_DEBUG_DELAY" ]]
then
    cat << EOF > /dev/stderr
*****
***** WARNING: \$FLAKE_DEBUG_DELAY IS SET AND PYTHON-PODMAN TESTS EXITED: $RETURNCODE
***** WARNING: Sleeping for 30 minutes for test-VM preservation oportunity.
*****
EOF
    sleep 30m
fi


pkill -9 podman
pkill -9 conmon

showlog /tmp/test_podman.output
showlog /tmp/alpine.log
showlog /tmp/busybox.log

exit $RETURNCODE