diff --git a/.github/workflows/pythonapp.yml b/.github/workflows/pythonapp.yml new file mode 100644 index 0000000..f3a8b27 --- /dev/null +++ b/.github/workflows/pythonapp.yml @@ -0,0 +1,46 @@ +name: Python application + +on: [push] + +jobs: + build: + name: Test on Python ${{ matrix.python-version }} and ${{ matrix.os }} + runs-on: ${{ matrix.os }} + strategy: + matrix: + python-version: ['3.7', '3.8', '3.9', '3.10', '3.11', '3.12'] + os: [ubuntu-latest] + fail-fast: false + timeout-minutes: 10 + steps: + - uses: actions/checkout@v2 + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + architecture: x64 + - name: Install dependencies + run: | + python -m pip install --upgrade pip wheel + pip install -r requirements-test.txt + - name: Test with pytest + env: + PYTHONPATH: . + run: | + pytest --durations=10 --cov dockerctx tests + - name: Extract branch name + shell: bash + run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})" + id: extract_branch + - name: Upload coverage + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }} + # This will only work if we also send a "done" event after the + # whole job is finished. + # See: https://docs.coveralls.io/parallel-build-webhook + #COVERALLS_PARALLEL: "true" + CI_BRANCH: ${{ steps.extract_branch.outputs.branch }} + if: matrix.os == 'ubuntu-latest' + run: | + coveralls diff --git a/dockerctx.py b/dockerctx.py index c030c42..1bb3aaf 100644 --- a/dockerctx.py +++ b/dockerctx.py @@ -83,9 +83,16 @@ def new_container( else: logger.info('Stopping container %s', name) # TODO: container.stop() does not seem to work here (e.g. for postgres) - container.kill() + try: + container.stop(timeout=1) + except docker.errors.APIError as e: + logger.error('Error stopping container: %s', e) + logger.info('Removing container %s', name) - container.remove() + try: + container.remove(force=True) + except docker.errors.APIError as e: + logger.error('Error removing container: %s', e) def accepting_connections(host, port, timeout=20): diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..8ff7c02 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,4 @@ +import logging + + +logging.basicConfig(level=logging.DEBUG) diff --git a/tests/test_pg.py b/tests/test_pg.py index 29a9077..e4f880f 100644 --- a/tests/test_pg.py +++ b/tests/test_pg.py @@ -72,12 +72,14 @@ def test_pg(): with new_container( image_name='postgres:alpine', ports={'5432/tcp': port}, - ready_test=lambda: pg_ready('localhost', port), + ready_test=lambda: pg_ready('127.0.0.1', port, timeout=20000), # Travis CI fails otherwise :`( - docker_api_version='1.24') as container: + docker_api_version='1.24', + environment={'POSTGRES_PASSWORD': 'password'} + ) as container: logger.debug(container.name) - url = "postgres://postgres@localhost:%d/mydb" % port + url = "postgresql://postgres:password@127.0.0.1:%d/mydb" % port logger.info('create engine') if not database_exists(url): logger.info('create database')