Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
WyriHaximus committed Jan 16, 2025
1 parent 703e00a commit 5870634
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 28 deletions.
52 changes: 26 additions & 26 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"
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:
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ LABEL org.label-schema.title="Testinfra Docker container" \
org.opencontainers.image.vendor="WyriHaximus.net" \
org.opencontainers.image.authors="Cees-Jan Kiewiet <[email protected]>"

WORKDIR /project

# hadolint ignore=DL3018
RUN apk add --no-cache docker python3 py-pip
# hadolint ignore=DL3013
RUN 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"]
35 changes: 35 additions & 0 deletions tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/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"

printf "Starting a container for '%s'\\n" "$DOCKER_TAG"

DOCKER_CONTAINER=$(docker run --rm -v "$(pwd)/test:/tests" -v "/var/run/docker.sock:/var/run/docker.sock:ro" -t -d "$DOCKER_TAG")
readonly DOCKER_CONTAINER

# 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 rm -f "$DOCKER_CONTAINER" &>/dev/null &
}
trap tearDown EXIT TERM ERR

# Finally, run the tests!
echo "Running test suite"
docker run --rm -t \
-v "$(pwd)/test:/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"
9 changes: 9 additions & 0 deletions tests/test_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import pytest

def test_pip_packages(host):
packages = host.pip_package.get_packages()
assert "docker" in packages
assert "pytest-testinfra" in packages

def test_docker_executable(host):
assert host.exists("docker")

0 comments on commit 5870634

Please sign in to comment.