Skip to content

Commit

Permalink
Fix tests and add github CI
Browse files Browse the repository at this point in the history
  • Loading branch information
cjrh committed Mar 29, 2024
1 parent ddfe9e2 commit 0571836
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/pythonapp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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:
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
11 changes: 9 additions & 2 deletions dockerctx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 4 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import logging


logging.basicConfig(level=logging.DEBUG)
8 changes: 5 additions & 3 deletions tests/test_pg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 0571836

Please sign in to comment.