|
| 1 | +from docker.models.containers import Container |
| 2 | +from time import sleep |
| 3 | +from typing import cast |
| 4 | +import docker |
| 5 | +import pytest |
| 6 | +import subprocess |
| 7 | +import testinfra |
| 8 | + |
| 9 | +all_in_one_image_tag = "supabase/all-in-one:testinfra" |
| 10 | +all_in_one_envs = { |
| 11 | + "POSTGRES_PASSWORD": "postgres", |
| 12 | + "JWT_SECRET": "super-secret-jwt-token-with-at-least-32-characters-long", |
| 13 | + "ANON_KEY": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyAgCiAgICAicm9sZSI6ICJhbm9uIiwKICAgICJpc3MiOiAic3VwYWJhc2UtZGVtbyIsCiAgICAiaWF0IjogMTY0MTc2OTIwMCwKICAgICJleHAiOiAxNzk5NTM1NjAwCn0.dc_X5iR_VP_qT0zsiyj_I_OZ2T9FtRU2BBNWN8Bu4GE", |
| 14 | + "SERVICE_ROLE_KEY": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyAgCiAgICAicm9sZSI6ICJzZXJ2aWNlX3JvbGUiLAogICAgImlzcyI6ICJzdXBhYmFzZS1kZW1vIiwKICAgICJpYXQiOiAxNjQxNzY5MjAwLAogICAgImV4cCI6IDE3OTk1MzU2MDAKfQ.DaYlNEoUrrEn2Ig7tqibS-PHK5vgusbcbo7X36XVt4Q", |
| 15 | + "ADMIN_API_KEY": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoic3VwYWJhc2VfYWRtaW4iLCJpc3MiOiJzdXBhYmFzZS1kZW1vIiwiaWF0IjoxNjQxNzY5MjAwLCJleHAiOjE3OTk1MzU2MDB9.Y9mSNVuTw2TdfryoaqM5wySvwQemGGWfSe9ixcklVfM", |
| 16 | + "DATA_VOLUME_MOUNTPOINT": "/data", |
| 17 | + "MACHINE_TYPE": "shared_cpu_1x_512m", |
| 18 | +} |
| 19 | + |
| 20 | +# TODO: spin up local Logflare for Vector tests. |
| 21 | + |
| 22 | + |
| 23 | +# scope='session' uses the same container for all the tests; |
| 24 | +# scope='function' uses a new container per test function. |
| 25 | +@pytest.fixture(scope="session") |
| 26 | +def host(request): |
| 27 | + # We build the image with the Docker CLI in path instead of using docker-py |
| 28 | + # (official Docker SDK for Python) because the latter doesn't use BuildKit, |
| 29 | + # so things like `ARG TARGETARCH` don't work: |
| 30 | + # - https://github.com/docker/docker-py/issues/2230 |
| 31 | + # - https://docs.docker.com/engine/reference/builder/#automatic-platform-args-in-the-global-scope |
| 32 | + subprocess.check_call( |
| 33 | + [ |
| 34 | + "docker", |
| 35 | + "buildx", |
| 36 | + "build", |
| 37 | + "--file", |
| 38 | + "docker/all-in-one/Dockerfile", |
| 39 | + "--load", |
| 40 | + "--tag", |
| 41 | + all_in_one_image_tag, |
| 42 | + ".", |
| 43 | + ] |
| 44 | + ) |
| 45 | + |
| 46 | + docker_client = docker.from_env() |
| 47 | + container = cast( |
| 48 | + Container, |
| 49 | + docker_client.containers.run( |
| 50 | + all_in_one_image_tag, |
| 51 | + detach=True, |
| 52 | + environment=all_in_one_envs, |
| 53 | + ports={ |
| 54 | + "5432/tcp": 5432, |
| 55 | + "8000/tcp": 8000, |
| 56 | + }, |
| 57 | + ), |
| 58 | + ) |
| 59 | + |
| 60 | + def get_health(container: Container) -> str: |
| 61 | + inspect_results = docker_client.api.inspect_container(container.name) |
| 62 | + return inspect_results["State"]["Health"]["Status"] |
| 63 | + |
| 64 | + while True: |
| 65 | + health = get_health(container) |
| 66 | + if health == "healthy": |
| 67 | + break |
| 68 | + sleep(1) |
| 69 | + |
| 70 | + # return a testinfra connection to the container |
| 71 | + yield testinfra.get_host("docker://" + cast(str, container.name)) |
| 72 | + |
| 73 | + # at the end of the test suite, destroy the container |
| 74 | + container.remove(v=True, force=True) |
| 75 | + |
| 76 | + |
| 77 | +def test_postgrest_service(host): |
| 78 | + postgrest = host.supervisor("services:postgrest") |
| 79 | + assert postgrest.is_running |
0 commit comments