Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add container testing #13

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ jobs:
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 }}"
push-image:
if: (github.event_name == 'push' || github.event_name == 'schedule') && github.ref == 'refs/heads/main'
name: Push
Expand Down
9 changes: 9 additions & 0 deletions test/test_bash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import pytest

def test_bash_true_results_in_0(host):
output = host.run('bash -c "true"')
assert output.rc == 0

def test_bash_true_results_in_0(host):
output = host.run('bash -c "false"')
assert output.rc > 0
11 changes: 11 additions & 0 deletions test/test_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import pytest

def test_user_app(host):
userName = 'runner'
groupName = 'runner'
homeDir = '/home/runner'

usr = host.user(userName)
assert userName in usr.name
assert groupName in usr.group
assert homeDir in usr.home
34 changes: 34 additions & 0 deletions tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/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"

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

DOCKER_CONTAINER=$(docker run --rm -v "$(pwd)/test:/tests" -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 \
renatomefi/docker-testinfra:5 \
--disable-pytest-warnings \
--verbose --hosts="docker://$DOCKER_CONTAINER"