【问题标题】:Microservices FastAPI test in docker, return 404docker 中的微服务 FastAPI 测试,返回 404
【发布时间】:2020-10-25 21:19:01
【问题描述】:

我在微服务中创建了一个简单的库项目来研究和实现 FastAPI。 Docker 启动 5 个主要服务:

  • 书籍
  • 数据库书
  • 作者
  • 数据库作者
  • nginx

一切正常,向邮递员提出请求我没有问题。

结构

问题描述

我添加了一个测试目录来测试端点。

(不完整的)作者测试示例

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 . 返回这个

============================================================================================================= 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 ==============================================================================================================

我尝试直接从容器 shell 开始测试,但结果完全不同。 此问题仅发生在按照文档(使用 starlette / fastapi)和请求完成的测试中

你可以在这里找到完整的项目 Library Microsrevices example

环境

  • 操作系统:[Linux Fedora 32]
  • FastAPI 版本 [0.55.1]:
  • Python:[Python 3.8.3]

码头工人撰写文件

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:

【问题讨论】:

  • 请在此处添加您的 docker-compose 文件?您的网络是如何声明的?
  • @abestrad 添加了 docker-compose
  • 嗨@Sanjiv 我不明白本指南如何帮助我。它绝对没有谈论如何设置 docker 来运行测试

标签: python docker microservices pytest fastapi


【解决方案1】:

这里的主要问题是您在测试文件上的端点

测试示例已修复:

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

【讨论】:

    【解决方案2】:

    使用 docker0 网络 IP 地址和请求修复。现在可以在端口 8080 上使用 172.13.0.1 开始测试

    【讨论】:

      猜你喜欢
      • 2022-07-16
      • 1970-01-01
      • 1970-01-01
      • 2020-10-14
      • 2017-10-04
      • 2019-08-25
      • 1970-01-01
      • 2016-10-07
      • 1970-01-01
      相关资源
      最近更新 更多