|
| 1 | +import os |
| 2 | + |
| 3 | +import pytest |
| 4 | +from fastapi.testclient import TestClient |
| 5 | +from testcontainers.minio import MinioContainer |
| 6 | +from testcontainers.postgres import PostgresContainer |
| 7 | +from testcontainers.rabbitmq import RabbitMqContainer |
| 8 | + |
| 9 | +from cogstack_model_gateway.gateway.main import app |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture(scope="module", autouse=True) |
| 13 | +def setup(request): |
| 14 | + print("Setting up test containers") |
| 15 | + postgres = PostgresContainer("postgres:17.2") |
| 16 | + rabbitmq = RabbitMqContainer("rabbitmq:4.0.4-management-alpine") |
| 17 | + minio = MinioContainer("minio/minio:RELEASE.2024-11-07T00-52-20Z") |
| 18 | + |
| 19 | + def remove_containers(): |
| 20 | + for testcontainer in [postgres, rabbitmq, minio]: |
| 21 | + testcontainer.stop() |
| 22 | + |
| 23 | + request.addfinalizer(remove_containers) |
| 24 | + |
| 25 | + for testcontainer in [postgres, rabbitmq, minio]: |
| 26 | + testcontainer.start() |
| 27 | + |
| 28 | + queue_connection_params = rabbitmq.get_connection_params() |
| 29 | + minio_host, minio_port = minio.get_config()["endpoint"].split(":") |
| 30 | + env = { |
| 31 | + "CMG_DB_USER": postgres.username, |
| 32 | + "CMG_DB_PASSWORD": postgres.password, |
| 33 | + "CMG_DB_HOST": postgres.get_container_host_ip(), |
| 34 | + "CMG_DB_PORT": postgres.get_exposed_port(postgres.port), |
| 35 | + "CMG_DB_NAME": "test", |
| 36 | + "CMG_QUEUE_USER": rabbitmq.username, |
| 37 | + "CMG_QUEUE_PASSWORD": rabbitmq.password, |
| 38 | + "CMG_QUEUE_HOST": queue_connection_params.host, |
| 39 | + "CMG_QUEUE_PORT": str(queue_connection_params.port), |
| 40 | + "CMG_QUEUE_NAME": "test", |
| 41 | + "CMG_OBJECT_STORE_HOST": minio_host, |
| 42 | + "CMG_OBJECT_STORE_PORT": minio_port, |
| 43 | + "CMG_OBJECT_STORE_ACCESS_KEY": minio.access_key, |
| 44 | + "CMG_OBJECT_STORE_SECRET_KEY": minio.secret_key, |
| 45 | + "CMG_OBJECT_STORE_BUCKET_TASKS": "test-tasks", |
| 46 | + "CMG_OBJECT_STORE_BUCKET_RESULTS": "test-results", |
| 47 | + "CMG_SCHEDULER_MAX_CONCURRENT_TASKS": "1", |
| 48 | + } |
| 49 | + |
| 50 | + os.environ.update(env) |
| 51 | + |
| 52 | + |
| 53 | +@pytest.fixture(scope="module") |
| 54 | +def client(): |
| 55 | + with TestClient(app) as client: |
| 56 | + yield client |
| 57 | + |
| 58 | + |
| 59 | +def test_root(client: TestClient): |
| 60 | + response = client.get("/") |
| 61 | + assert response.status_code == 200 |
| 62 | + assert response.json() == {"message": "Enter the cult... I mean, the API."} |
0 commit comments