Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
soedirgo committed Sep 17, 2023
1 parent 86122e0 commit 52c9f2c
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 6 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ jobs:
retries: 20
container: supabase_postgres

- name: Run tests
- name: Run aio integration tests
run: |
pip3 install docker pytest pytest-testinfra
pytest -vv testinfra/test_all_in_one.py
- name: Run db tests
run: pg_prove migrations/tests/test.sql
env:
PGHOST: localhost
Expand Down
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ venv/
docker/cache/

ansible/image-manifest*.json

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
9 changes: 6 additions & 3 deletions docker/all-in-one/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,24 @@ All Supabase backend services bundled in a single Docker image for quick local t
## Build

```bash
docker build . -t supabase/all-in-one
# cwd: repo root
docker build -f docker/all-in-one/Dockerfile -t supabase/all-in-one .
```

## Run

```bash
docker run --rm \
docker run --rm -it \
-e POSTGRES_PASSWORD=postgres \
-e JWT_SECRET=super-secret-jwt-token-with-at-least-32-characters-long \
-e ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyAgCiAgICAicm9sZSI6ICJhbm9uIiwKICAgICJpc3MiOiAic3VwYWJhc2UtZGVtbyIsCiAgICAiaWF0IjogMTY0MTc2OTIwMCwKICAgICJleHAiOiAxNzk5NTM1NjAwCn0.dc_X5iR_VP_qT0zsiyj_I_OZ2T9FtRU2BBNWN8Bu4GE \
-e SERVICE_ROLE_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyAgCiAgICAicm9sZSI6ICJzZXJ2aWNlX3JvbGUiLAogICAgImlzcyI6ICJzdXBhYmFzZS1kZW1vIiwKICAgICJpYXQiOiAxNjQxNzY5MjAwLAogICAgImV4cCI6IDE3OTk1MzU2MDAKfQ.DaYlNEoUrrEn2Ig7tqibS-PHK5vgusbcbo7X36XVt4Q \
-e ADMIN_API_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoic3VwYWJhc2VfYWRtaW4iLCJpc3MiOiJzdXBhYmFzZS1kZW1vIiwiaWF0IjoxNjQxNzY5MjAwLCJleHAiOjE3OTk1MzU2MDB9.Y9mSNVuTw2TdfryoaqM5wySvwQemGGWfSe9ixcklVfM \
-e DATA_VOLUME_MOUNTPOINT=/data \
-e MACHINE_TYPE=shared_cpu_1x_512m \
-p 5432:5432 \
-p 8000:8000 \
-it supabase/all-in-one
supabase/all-in-one
```

Use bind mount to start from an existing physical backup: `-v $(pwd)/data:/var/lib/postgresql/data`
Expand Down
1 change: 1 addition & 0 deletions docker/all-in-one/etc/gotrue.env
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
API_EXTERNAL_HOST=api_external_host
GOTRUE_API_HOST=gotrue_api_host
GOTRUE_SITE_URL=gotrue_site_url
GOTRUE_DB_DRIVER=postgres
Expand Down
6 changes: 4 additions & 2 deletions docker/all-in-one/healthcheck.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,7 @@ fail2ban-client status
# prometheus exporter up
curl -sSfI "http://localhost:$PGEXPORTER_PORT/metrics"

# vector is up
curl -sSfI "http://localhost:$VECTOR_API_PORT/health"
# vector is up (if starting logflare)
if [ -n "${LOGFLARE_API_KEY:-}" ]; then
curl -sSfI "http://localhost:$VECTOR_API_PORT/health"
fi
1 change: 1 addition & 0 deletions docker/all-in-one/init/configure-gotrue.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ touch /var/log/services/gotrue.log

/usr/local/bin/configure-shim.sh /dist/gotrue /opt/gotrue/gotrue

sed -i "s|api_external_host|${API_EXTERNAL_HOST:-}|g" /etc/gotrue.env
sed -i "s|gotrue_api_host|${GOTRUE_API_HOST:-0.0.0.0}|g" /etc/gotrue.env
sed -i "s|gotrue_site_url|$GOTRUE_SITE_URL|g" /etc/gotrue.env
sed -i "s|gotrue_jwt_secret|$JWT_SECRET|g" /etc/gotrue.env
Expand Down
79 changes: 79 additions & 0 deletions testinfra/test_all_in_one.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
from docker.models.containers import Container
from time import sleep
from typing import cast
import docker
import pytest
import subprocess
import testinfra

all_in_one_image_tag = "supabase/all-in-one:testinfra"
all_in_one_envs = {
"POSTGRES_PASSWORD": "postgres",
"JWT_SECRET": "super-secret-jwt-token-with-at-least-32-characters-long",
"ANON_KEY": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyAgCiAgICAicm9sZSI6ICJhbm9uIiwKICAgICJpc3MiOiAic3VwYWJhc2UtZGVtbyIsCiAgICAiaWF0IjogMTY0MTc2OTIwMCwKICAgICJleHAiOiAxNzk5NTM1NjAwCn0.dc_X5iR_VP_qT0zsiyj_I_OZ2T9FtRU2BBNWN8Bu4GE",
"SERVICE_ROLE_KEY": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyAgCiAgICAicm9sZSI6ICJzZXJ2aWNlX3JvbGUiLAogICAgImlzcyI6ICJzdXBhYmFzZS1kZW1vIiwKICAgICJpYXQiOiAxNjQxNzY5MjAwLAogICAgImV4cCI6IDE3OTk1MzU2MDAKfQ.DaYlNEoUrrEn2Ig7tqibS-PHK5vgusbcbo7X36XVt4Q",
"ADMIN_API_KEY": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoic3VwYWJhc2VfYWRtaW4iLCJpc3MiOiJzdXBhYmFzZS1kZW1vIiwiaWF0IjoxNjQxNzY5MjAwLCJleHAiOjE3OTk1MzU2MDB9.Y9mSNVuTw2TdfryoaqM5wySvwQemGGWfSe9ixcklVfM",
"DATA_VOLUME_MOUNTPOINT": "/data",
"MACHINE_TYPE": "shared_cpu_1x_512m",
}


# scope='session' uses the same container for all the tests;
# scope='function' uses a new container per test function.
@pytest.fixture(scope="session")
def host(request):
# We build the image with the Docker CLI in path instead of using docker-py
# (official Docker SDK for Python) because the latter doesn't use BuildKit,
# so things like `ARG TARGETARCH` don't work:
# - https://github.com/docker/docker-py/issues/2230
# - https://docs.docker.com/engine/reference/builder/#automatic-platform-args-in-the-global-scope
subprocess.check_call(
[
"docker",
"buildx",
"build",
"--file",
"docker/all-in-one/Dockerfile",
"--output",
"type=image",
"--tag",
all_in_one_image_tag,
".",
]
)

docker_client = docker.from_env()
container = cast(
Container,
docker_client.containers.run(
all_in_one_image_tag,
detach=True,
environment=all_in_one_envs,
ports={
"5432/tcp": 5432,
"8000/tcp": 8000,
},
),
)

def get_health(container: Container) -> str:
docker_api_client = docker.APIClient()
inspect_results = docker_api_client.inspect_container(container.name)
return inspect_results["State"]["Health"]["Status"]

while True:
health = get_health(container)
if health == "healthy":
break
sleep(1)

# return a testinfra connection to the container
yield testinfra.get_host("docker://" + cast(str, container.name))

# at the end of the test suite, destroy the container
container.remove(v=True, force=True)


def test_postgrest_service(host):
postgrest = host.supervisor("services:postgrest")
assert postgrest.is_running

0 comments on commit 52c9f2c

Please sign in to comment.