diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 4980a80..99abec6 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -80,37 +80,37 @@ jobs: - run: docker load --input /tmp/docker-image/docker_image-${{ env.PLATFORM_PAIR }}.tar - run: rm -Rf /tmp/docker-image/ - run: echo -e "${{ env.DOCKER_IMAGE }}:${{ env.PLATFORM_PAIR }}" | xargs -I % sh -c 'docker run -v /tmp/trivy:/var/lib/trivy -v /var/run/docker.sock:/var/run/docker.sock -t aquasec/trivy:latest --cache-dir /var/lib/trivy image --exit-code 1 --no-progress --format table % || true' -# tests: -# name: Test ${{ matrix.platform }} -# needs: -# - supported-platform-matrix -# - scan-vulnerability -# strategy: -# fail-fast: false -# matrix: -# platform: ${{ fromJson(needs.supported-platform-matrix.outputs.platform) }} -# runs-on: ubuntu-latest -# steps: -# - name: Prepare -# run: | -# platform=${{ matrix.platform }} -# echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV -# - name: Set up QEMU -# uses: docker/setup-qemu-action@v3 -# - uses: actions/checkout@v4 -# - uses: actions/download-artifact@v4 -# with: -# name: docker-image-${{ env.PLATFORM_PAIR }} -# path: /tmp/docker-image -# - run: docker load --input /tmp/docker-image/docker_image-${{ env.PLATFORM_PAIR }}.tar -# - run: docker image ls -a -# - run: ./tests.sh "${DOCKER_IMAGE}:${{ env.PLATFORM_PAIR }}" + tests: + name: Test ${{ matrix.platform }} + needs: + - supported-platform-matrix + - scan-vulnerability + strategy: + fail-fast: false + matrix: + platform: ${{ fromJson(needs.supported-platform-matrix.outputs.platform) }} + runs-on: ubuntu-latest + steps: + - name: Prepare + run: | + platform=${{ matrix.platform }} + echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + - uses: actions/checkout@v4 + - uses: actions/download-artifact@v4 + with: + name: docker-image-${{ env.PLATFORM_PAIR }} + path: /tmp/docker-image + - run: docker load --input /tmp/docker-image/docker_image-${{ env.PLATFORM_PAIR }}.tar + - run: docker image ls -a + - run: ./tests.sh "${DOCKER_IMAGE}:${{ env.PLATFORM_PAIR }}" "${DOCKER_IMAGE}:${{ env.PLATFORM_PAIR }}" "${{ matrix.platform }}" push-image: if: (github.event_name == 'push' || github.event_name == 'schedule') && github.ref == 'refs/heads/main' name: Push needs: - supported-platform-matrix -# - tests + - tests runs-on: ubuntu-latest services: registry: diff --git a/Dockerfile b/Dockerfile index bcb7024..0ad512e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,12 +17,13 @@ LABEL org.label-schema.title="Testinfra Docker container" \ org.opencontainers.image.vendor="WyriHaximus.net" \ org.opencontainers.image.authors="Cees-Jan Kiewiet " -WORKDIR /project - # hadolint ignore=DL3018 -RUN apk add --no-cache docker python3 py-pip +RUN apk add --no-cache docker python3 py-pip && \ + pip3 install --no-cache-dir --upgrade pip --break-system-packages && \ # hadolint ignore=DL3013 -RUN pip install --no-cache-dir docker --break-system-packages && \ + pip install --no-cache-dir docker --break-system-packages && \ pip install --no-cache-dir pytest-testinfra --break-system-packages +WORKDIR /tests + ENTRYPOINT ["py.test", "-p", "no:cacheprovider"] diff --git a/tests.sh b/tests.sh new file mode 100755 index 0000000..af561e7 --- /dev/null +++ b/tests.sh @@ -0,0 +1,42 @@ +#!/bin/bash +# +# A simple script to start a Docker container +# and run Testinfra in it +# Original script: https://gist.github.com/renatomefi/bbf44d4e8a2614b1390416c6189fbb8e +# Author: @renatomefi https://github.com/renatomefi +# + +set -eEuo pipefail + +# The first parameter is a Docker tag or image id +declare -r DOCKER_TAG="$1" +declare -r TESTINFRA_IMAGE="$2" +declare -r PLATFORM="$3" + +printf "Starting a container for '%s'\\n" "$DOCKER_TAG" + +DOCKER_CONTAINER=$(docker run --rm -v "$(pwd)/tests:/tests" -v "/var/run/docker.sock:/var/run/docker.sock:ro" -t -d "$DOCKER_TAG") +readonly DOCKER_CONTAINER + +docker ps + +# Let's register a trap function, if our tests fail, finish or the script gets +# interrupted, we'll still be able to remove the running container +function tearDown { + docker logs "$DOCKER_CONTAINER" + docker rm -f "$DOCKER_CONTAINER" &>/dev/null & +} +trap tearDown EXIT TERM ERR + +# Finally, run the tests! +echo "Running test suite" +docker run --rm -t \ + --platform="$PLATFORM" \ + -v "$(pwd)/tests:/tests" \ + -v "$(pwd)/tmp/test-results:/results" \ + -v /var/run/docker.sock:/var/run/docker.sock:ro \ + "$TESTINFRA_IMAGE" \ + --disable-pytest-warnings \ + --verbose --hosts="docker://$DOCKER_CONTAINER" + +docker ps diff --git a/tests/test_image.py b/tests/test_image.py new file mode 100644 index 0000000..27e688e --- /dev/null +++ b/tests/test_image.py @@ -0,0 +1,20 @@ +import pytest + +def test_docker(host): + output = host.run('docker -v') + assert output.rc == 0 + assert u'Docker Version ' in output.stderr + assert output.stderr == '' + +def test_pip(host): + assert host.exists("pip") + +def test_pytest(host): + assert host.exists("py.test") + +# def test_pip_packages(host): +# packages = host.pip_package.get_packages() +# assert "pip" in packages +# assert "docker" in packages +# assert "testinfra" in packages +# assert "paramiko" in packages