Skip to content

Instantly share code, notes, and snippets.

@renatomefi
Last active February 27, 2019 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renatomefi/acfd2f733ad1ca20bc233a4552aec79d to your computer and use it in GitHub Desktop.
Save renatomefi/acfd2f733ad1ca20bc233a4552aec79d to your computer and use it in GitHub Desktop.

Let's first build and run our newly image with a sleep time, thus pytest will have time to execute the test in it, no worries, this won't be the final solution

$ docker build -t alpine-app .
$ docker run --rm -d alpine-app sleep 60
7ecbe3205a3e9e871d4bc73237ee374d75e1e8508b008d578f2538ce937b2aa5

We want now to copy the running container ID, and let's put it in the Testinfra command so it knows what container we want to test

docker run --rm -t \ # Remove after test, give it tty
-v "$(pwd)/test:/tests:ro" \ # Mount the test directory, which we have put our test_user.py in
-v /var/run/docker.sock:/var/run/docker.sock:ro \ # Give pytest access to our running Docker container, yours might differ
renatomefi/docker-testinfra:1 \ # The Docker image with pytest, Testinfra and more
-v --hosts="docker://7ecbe3205a3e9e871d4bc73237ee374d75e1e8508b008d578f2538ce937b2aa5" # Make it verbose, and set the running container id

The output of it will look like:

============== test session starts =======================================================
platform linux2 -- Python 2.7.15, pytest-4.3.0, py-1.8.0, pluggy-0.9.0 -- /usr/bin/python2
rootdir: /tests, inifile:
plugins: testinfra-1.19.0
collected 1 item                                                                                                                                                            

test_user.py::test_userPresent[docker://7ecbe3205a3e] PASSED                                                                                                          [100%]

============== 1 passed in 0.72 seconds ===================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment