简体   繁体   中英

Running testcontainers-python in GitHub Actions CI

In order to ensure functionality, we want to execute all tests within out CI on GitHub Actions . Here we configured a section called tests that executes pytests in a defined folder. This is already working for "unit tests" - meaning all those tests that do not require interactions with other containers.

Additionally, we want to add tests that interact with containers (ie a database). However, out-of-the-box this is failing, as launching a testcontainer fails. An exception is raised, because within the start script of testcontainers a POST is executed to test the readiness of the container.

    .venv/lib/python3.8/site-packages/urllib3/connectionpool.py:392:

    def request(self, method, url, body=None, headers={}, *,
            encode_chunked=False):
    """Send a complete request to the server."""
>   self._send_request(method, url, body, headers, encode_chunked)

Our Action looks like this:

name: Test CI

on: [push]

env:
  PROJECT: test
  ACTION_URL: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
  PYTHON_VERSION: 3.8

jobs:
  tests:
    name: Tests
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: pytest
        run: |
          IMAGE_NAME="${{ env.PROJECT }}:test"
          docker build . --file test.dockerfile -t $IMAGE_NAME
          docker run $IMAGE_NAME

Turns out, that the default container launched by GitHub does not feature a docker client. Part of the solution therefore was to manually install docker by adding the following to our dockerfile:

RUN apt -y install docker.io

Additionally, the container still will not be able to run a docker container as it has no own build daemon. The trick here is to use the one provided by the GitHub Actions owned container by passing the docker.sock as a volume. We thus exchanged the last line in the CI yml with this:

docker run -v "/var/run/docker.sock":"/var/run/docker.sock" $IMAGE_NAME

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM