1#!/usr/bin/env bash
2
3set -x
4
5# Global variables to support finalize()
6MMD_OS=
7MMD_RELEASE=
8MMD_TARBALL_PATH=
9
10BUILDAH_PATH=$(which buildah)
11
12DOCKER_PATH=$(which docker)
13
14RETRY_CMD=$SCRIPT_DIR/retry-command.sh
15
16if [ x$BUILDAH_PATH == x ]; then
17    if [ x$DOCKER_PATH == x ]; then
18        >&2 echo "error: Neither docker nor podman available"
19        exit 1
20    else
21        MMD_BUILDAH="$RETRY_CMD sudo $DOCKER_PATH build"
22        MMD_LAYERS_TRUE=
23        MMD_LAYERS_FALSE=
24    fi
25else
26    MMD_BUILDAH="$RETRY_CMD $BUILDAH_PATH bud"
27    MMD_LAYERS_TRUE="--layers=true"
28    MMD_LAYERS_FALSE="--layers=false"
29fi
30
31PODMAN_PATH=$(which podman)
32if [  x$PODMAN_PATH == x ]; then
33    MMD_OCI="sudo $DOCKER_PATH"
34else
35    MMD_OCI="$PODMAN_PATH"
36fi
37
38function common_finalize {
39    # If any specific test launcher needs to do its own cleanup as well,
40    # it should set the EXIT trap and make sure to also call this function
41    # internally.
42    exitcode=$?
43
44    rm -f $MMD_TARBALL_PATH \
45          $SCRIPT_DIR/$MMD_OS/Dockerfile.deps.$MMD_RELEASE \
46          $SCRIPT_DIR/$MMD_OS/Dockerfile.test.$MMD_RELEASE
47
48    return $exitcode
49}
50
51trap common_finalize EXIT
52
53
54function mmd_setup_container {
55    local os release repository image
56    local deps_template deps_image
57    local "${@}"
58
59    if [ -z $SCRIPT_DIR ]; then
60        >&2 echo "Programming error: \$SCRIPT_DIR must be set"
61        exit 1
62    fi
63
64    os=${os-fedora}
65    release=${release-rawhide}
66    repository=${repository-registry.fedoraproject.org}
67
68    # Lower-case the os and release for the container registry
69    MMD_OS=${os,,}
70    MMD_RELEASE=${release,,}
71
72    deps_template=${deps_template-$MMD_OS/Dockerfile.deps.tmpl}
73    deps_image=${deps_image-libmodulemd-deps-$MMD_OS:$MMD_RELEASE}
74
75    m4  -D__IMAGE__="$repository/$image" \
76        -D__OS__=$os \
77        -D__RELEASE__=$release \
78        $SCRIPT_DIR/${deps_template} \
79        > $SCRIPT_DIR/$MMD_OS/Dockerfile.deps.$MMD_RELEASE
80
81    $MMD_BUILDAH $MMD_LAYERS_TRUE \
82        -f $SCRIPT_DIR/$MMD_OS/Dockerfile.deps.$MMD_RELEASE \
83        -t fedora-modularity/${deps_image} .
84}
85
86
87function mmd_run_docker_tests {
88    local os release repository image
89    local deps_template deps_image
90    local test_template test_image
91    local oci_extra_args
92    local "${@}"
93
94    os=${os-fedora}
95    release=${release-rawhide}
96    repository=${repository-registry.fedoraproject.org}
97
98    # Lower-case the os and release for the container registry
99    MMD_OS=${os,,}
100    MMD_RELEASE=${release,,}
101    image=${image-$MMD_OS:$MMD_RELEASE}
102
103    deps_template=${deps_template-$MMD_OS/Dockerfile.deps.tmpl}
104    test_template=${test_template-$MMD_OS/Dockerfile.tmpl}
105    deps_image=${deps_image-libmodulemd-deps-$MMD_OS:$MMD_RELEASE}
106    test_image=${test_image-libmodulemd-$MMD_OS:$MMD_RELEASE}
107
108    # Create an archive of the current checkout
109    MMD_TARBALL_PATH=`mktemp -p $SCRIPT_DIR tarball-XXXXXX.tar.bz2`
110    TARBALL=`basename $MMD_TARBALL_PATH`
111
112    pushd $SCRIPT_DIR/..
113    git ls-files |xargs tar cfj $MMD_TARBALL_PATH .git
114    popd
115
116    mmd_setup_container \
117        os=$os \
118        release=$release \
119        repository=$repository \
120        image=$image \
121        deps_template=$deps_template \
122        deps_image=$deps_image
123
124    m4  -D__DEPS_IMAGE__="fedora-modularity/${deps_image}" \
125        -D__OS__=$os \
126        -D__RELEASE__=$release \
127        $SCRIPT_DIR/${test_template} \
128        > $SCRIPT_DIR/$MMD_OS/Dockerfile.test.$MMD_RELEASE
129
130    $MMD_BUILDAH $MMD_LAYERS_FALSE \
131        -f $SCRIPT_DIR/$MMD_OS/Dockerfile.test.$MMD_RELEASE \
132        -t fedora-modularity/${test_image} \
133        --build-arg TARBALL=${TARBALL} .
134
135    eval $MMD_OCI run \
136        ${oci_extra_args} \
137        --rm fedora-modularity/${test_image}
138}
139
140set +x
141