Microservices FastAPI test in docker, return 404

Viewed 1265

I created a simple library project in microservices to study and implement FastAPI. Docker starts 5 main services:

  • books
  • db-book
  • author
  • db-author
  • nginx

Everything works as expected, making requests with postman I have no problem.

Structure

structure

Problem description

I added a test directory where I test endpoints.

Example of (incomplete) author test

from starlette.testclient import TestClient
from app.main import app
from app.api.author import authors
import logging
log = logging.getLogger('__name__')
import requests

client = TestClient(app)

def test_get_authors():
    response = client.get("/")
    assert response.status_code == 200

def test_get_author():
    response = client.get("/1")
    assert response.status_code == 200

$> docker-compose exec author_service pytest . returns this

============================================================================================================= test session starts =============================================================================================================
platform linux -- Python 3.8.3, pytest-5.3.2, py-1.9.0, pluggy-0.13.1
rootdir: /app
collected 2 items                                                                                                                                                                                                                             

tests/test_author.py FF                                                                                                                                                                                                                 [100%]

================================================================================================================== FAILURES ===================================================================================================================
______________________________________________________________________________________________________________ test_get_authors _______________________________________________________________________________________________________________

    def test_get_authors():
        response = client.get("/")
>       assert response.status_code == 200
E       assert 404 == 200
E        +  where 404 = <Response [404]>.status_code

tests/test_author.py:12: AssertionError
_______________________________________________________________________________________________________________ test_get_author _______________________________________________________________________________________________________________

    def test_get_author():
        response = client.get("/1")
>       assert response.status_code == 200
E       assert 404 == 200
E        +  where 404 = <Response [404]>.status_code

tests/test_author.py:16: AssertionError
============================================================================================================== 2 failed in 0.35s ==============================================================================================================

I tried to start the tests directly from the container shell but nothing the same. This problem occurs only with tests that are done following the documentation (using starlette / fastapi) and with requests

You can find the complete project here Library Microsrevices example

Environment

  • OS:[Linux Fedora 32]
  • FastAPI Version [0.55.1]:
  • Python: [Python 3.8.3]

docker-compose file

version: '3.7'

services:
  book_service:
    build: ./book-service
    command: uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
    volumes:
      - ./book-service/:/app/
    ports:
      - 8001:8000
    environment:
      - DATABASE_URI=postgresql://book_db_username:book_db_password@book_db/book_db_dev
      - AUTHOR_SERVICE_HOST_URL=http://author_service:8000/api/v1/authors/
    depends_on:
      - book_db

  book_db:
    image: postgres:12.1-alpine
    volumes:
      - postgres_data_book:/var/lib/postgresql/data/
    environment:
      - POSTGRES_USER=book_db_username
      - POSTGRES_PASSWORD=book_db_password
      - POSTGRES_DB=book_db_dev
  
  author_service:
    build: ./author-service
    command: uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
    volumes:
      - ./author-service/:/app/
    ports:
      - 8002:8000
    environment:
      - DATABASE_URI=postgresql://author_db_username:author_db_password@author_db/author_db_dev
    depends_on:
      - author_db

  author_db:
    image: postgres:12.1-alpine
    volumes:
      - postgres_data_author:/var/lib/postgres/data
    environment:
      - POSTGRES_USER=author_db_username
      - POSTGRES_PASSWORD=author_db_password
      - POSTGRES_DB=author_db_dev

  nginx:
    image: nginx:latest
    ports:
      - "8080:8080"
    volumes:
      - ./nginx_config.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - author_service
      - book_service

volumes:
  postgres_data_book:
  postgres_data_author:
2 Answers

Fixed using docker0 network ip address and with requests. Tests can now be started using 172.13.0.1 on port 8080

the main problem here are your endpoints on test file

Test example fixed:

from starlette.testclient import TestClient
from app.main import app
from app.api.author import authors
import logging
log = logging.getLogger('__name__')
import requests

client = TestClient(app)

def test_get_authors():
    response = client.get("/authors") # this must be your API endpoint to test
    assert response.status_code == 200

def test_get_author():
    response = client.get("/authors/1") # this must be your API endpoint to test 
    assert response.status_code == 200
Related