I want to test from one container via pytest (test
below), whether another (web
below) interacts nicely with our Postgresql DB.
I am having the following docker-compose.yaml file
version: '3.8'
services:
web:
build:
context: ./src/
dockerfile: Dockerfile
command: |
bash -c 'while !</dev/tcp/db/5432; do sleep 1; done; uvicorn app.main:app --reload --workers 1 --host 0.0.0.0 --port 8000'
volumes:
- ./src/:/usr/src/app/
ports:
- 8002:8000
environment:
- DATABASE_URL=postgresql://postgres:postgres@db:5432/postgres_dev
depends_on:
- db
test:
build:
context: ./src/
dockerfile: Dockerfile-test
volumes:
- ./src/:/usr/src/app/
stdin_open: true
environment:
- DATABASE_URL=postgresql://postgres:postgres@db:5432/postgres_dev
- TEST_PORT=8002
depends_on:
- web
db:
image: postgres:13-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
ports:
- 5432:5432
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=postgres_dev
volumes:
postgres_data:
Furthermore, Dockerfile-test
looks like
# pull official base image
FROM python:3.9.4-alpine
# set work directory
WORKDIR /usr/src/
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# copy requirements file
COPY ./tests/requirements.txt /usr/src/app/requirements.txt
# install dependencies
RUN set -eux \
&& apk add --no-cache --virtual .build-deps build-base \
libressl-dev libffi-dev gcc musl-dev python3-dev \
postgresql-dev bash \
&& pip install --upgrade pip setuptools wheel \
&& pip install -r /usr/src/app/requirements.txt \
&& rm -rf /root/.cache/pip
# copy project
COPY ./tests/ /usr/src/app/tests
and requirements.txt
looks like
pytest==6.2.5
requests==2.26.0
pytest-httpserver==1.0.3
psycopg2==2.9.2
and conftest.py looks like
import pytest
import psycopg2
@pytest.fixture(scope='function')
def session():
conn = psycopg2.connect(host="localhost", database="hello_fastapi_dev", user="hello_fastapi", password="hello_fastapi")
# Create a cursor object
session = conn.cursor()
yield session
# Close the cursor and connection to so the server can allocate
# bandwidth to other requests
session.close()
conn.close()
However, when I run
docker-compose up -d --build
docker-compose run test pytest
I get
../local/lib/python3.9/site-packages/requests/adapters.py:516: ConnectionError
=========================================================================================================================== short test summary info ============================================================================================================================
FAILED app/tests/test_projects.py::test_ping - requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=8002): Max retries exceeded with url: /ping (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7faf175a86a0>: Failed...
FAILED app/tests/test_projects.py::test_auth - requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=8002): Max retries exceeded with url: /auth/register (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7faf174d0070...
ERROR app/tests/test_projects.py::test_project_create - psycopg2.OperationalError: could not connect to server: Connection refused
========================================================================================================================== 2 failed, 1 error in 0.49s ==========================================================================================================================
ERROR: 1
Hence, it seems like I cannot get a connection. Could you help me here?