From 7f58dc19d45a258d023bff262509d4f748f12c5a Mon Sep 17 00:00:00 2001 From: Phil Leggetter Date: Wed, 26 Feb 2025 15:08:37 +0000 Subject: [PATCH 1/2] chore: automate checking that the quickstart works as expected --- .gitignore | 3 ++ test/clean.sh | 1 + test/quickstart.sh | 108 +++++++++++++++++++++++++++++++++++++++++++++ test/server.py | 38 ++++++++++++++++ 4 files changed, 150 insertions(+) create mode 100755 test/clean.sh create mode 100755 test/quickstart.sh create mode 100644 test/server.py diff --git a/.gitignore b/.gitignore index 5b6679f5..677215f4 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,6 @@ # Misc .DS_Store + +# Scripted test runs +test/runs diff --git a/test/clean.sh b/test/clean.sh new file mode 100755 index 00000000..9ba65b05 --- /dev/null +++ b/test/clean.sh @@ -0,0 +1 @@ +rm -rf runs/* \ No newline at end of file diff --git a/test/quickstart.sh b/test/quickstart.sh new file mode 100755 index 00000000..dd02e97d --- /dev/null +++ b/test/quickstart.sh @@ -0,0 +1,108 @@ +#!/bin/bash + +TEST_SCRIPT_DIR=$(dirname "$0") +TEST_RUN_ID=$(date +%Y-%m-%d_%H%M%S) +TEST_RUN_DIR="$TEST_SCRIPT_DIR/runs/$TEST_RUN_ID" +DOCKER_COMPOSE_DIR="$TEST_RUN_DIR/examples/docker-compose" +TEST_API_KEY="this_is_a_test_api_key" + +echo "Creating a new test run $TEST_RUN_DIR" + +mkdir "$TEST_RUN_DIR" + +git clone https://github.com/hookdeck/outpost.git "$TEST_RUN_DIR" +cp .env.example "$TEST_RUN_DIR/examples/docker-compose/.env" + +cd "$DOCKER_COMPOSE_DIR" + +wait_for_service() { + local url=$1 + local timeout=6 + while ! curl --fail --silent "$url"; do + printf '.' + sleep 5 + ((timeout--)) + if [ $timeout -le 0 ]; then + echo "Timeout reached. Exiting." + exit 1 + fi + done +} + +# Update the value within the new .env file +sed -i '' "s/apikey/$TEST_API_KEY/" .env + +docker-compose -f compose.yml -f compose-rabbitmq.yml up -d + +# Wait until the services are running +echo "Waiting for the services to start" +wait_for_service "localhost:3333/api/v1/healthz" + +echo "" +echo "āœ… Services are up and running" + +echo "" +echo "Creating a tenant" + +# Create a tenant +curl --location --request PUT "localhost:3333/api/v1/$TEST_RUN_ID" \ +--header 'Content-Type: application/json' \ +--header "Authorization: Bearer $TEST_API_KEY" + +# Create a webhook destination +echo "" +echo "Creating a webhook destination" + +curl --location "localhost:3333/api/v1/$TEST_RUN_ID/destinations" \ +--header 'Content-Type: application/json' \ +--header "Authorization: Bearer $TEST_API_KEY" \ +--data "{ + \"type\": \"webhook\", + \"topics\": [\"*\"], + \"config\": { + \"url\": \"http://host.docker.internal:4444/webhook/$TEST_RUN_ID\" + } +}" + +# Start a simple HTTP server in the background +printf "\n\nStarting a simple HTTP server in the background" +touch "server.log" +python3 "../../../../server.py" 4444 > "server.log" 2>&1 & +printf "\nWaiting for the test server to start..." +wait_for_service "localhost:4444" +printf "\nāœ… Test server started" + +# Capture the server's PID to kill it later +SERVER_PID=$! +# Capture the killing of the script and kill the server +trap "kill $SERVER_PID; echo 'Script interrupted. Killing the server...'; exit 1" INT TERM + +# Publish an event +printf "\n\nPublishing an event" + +EVENT_BODY='{"user_id":"userid"}' + +curl --location 'localhost:3333/api/v1/publish' \ +--header 'Content-Type: application/json' \ +--header "Authorization: Bearer $TEST_API_KEY" \ +--data "{ + \"tenant_id\": \"$TEST_RUN_ID\", + \"topic\": \"user.created\", + \"eligible_for_retry\": true, + \"metadata\": { + \"meta\": \"data\" + }, + \"data\": $EVENT_BODY +}" + +# Example condition: wait for a specific log entry +printf "\nWaiting for the expected event to reach test server for test run $TEST_RUN_ID..." +while ! grep -q "$EVENT_BODY" "server.log"; do + printf '.' + sleep 5 +done +echo "" +echo "āœ… Received the expected request. Success! Exiting." + +# Now kill the server +kill $SERVER_PID diff --git a/test/server.py b/test/server.py new file mode 100644 index 00000000..50e6597b --- /dev/null +++ b/test/server.py @@ -0,0 +1,38 @@ +import http.server +import logging +import sys + + +class SimpleHTTPRequestHandler(http.server.BaseHTTPRequestHandler): + def do_POST(self): + logging.basicConfig(stream=sys.stdout, level=logging.INFO) + post_data = self.rfile.read() + logging.info( + "POST request,\nPath: %s\nHeaders:\n%s\n\nBody:\n%s\n", + str(self.path), + str(self.headers), + post_data.decode("utf-8"), + ) + self.send_response(200) + self.end_headers() + self.wfile.write(b"POST request received") + + def do_GET(self): + logging.basicConfig(stream=sys.stdout, level=logging.INFO) + logging.info( + "GET request,\nPath: %s\nHeaders:\n%s\n", + str(self.path), + str(self.headers), + ) + self.send_response(200) + self.end_headers() + self.wfile.write(b"GET request received") + + +if len(sys.argv) != 2: + print("Usage: python server.py ") + sys.exit(1) + +port = int(sys.argv[1]) +httpd = http.server.HTTPServer(("127.0.0.1", port), SimpleHTTPRequestHandler) +httpd.serve_forever() From b41be77103fac87e03dbe65a74cf8c608d3d3d9b Mon Sep 17 00:00:00 2001 From: Alex Luong Date: Fri, 28 Feb 2025 16:32:46 +0700 Subject: [PATCH 2/2] chore: Update quickstart script and .env.example to support PG instead of CH --- .env.example | 16 ++++++++++------ test/quickstart.sh | 2 +- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/.env.example b/.env.example index 47fd6999..c8e38448 100644 --- a/.env.example +++ b/.env.example @@ -22,12 +22,6 @@ REDIS_PORT="6379" REDIS_PASSWORD="password" REDIS_DATABASE="0" -# Clickhouse -CLICKHOUSE_ADDR="clickhouse:9000" -CLICKHOUSE_USERNAME="default" -CLICKHOUSE_DATABASE="outpost" -CLICKHOUSE_PASSWORD="" - # Misc Configs PUBLISHMQ_MAX_CONCURRENCY=1 DELIVERYMQ_MAX_CONCURRENCY=1 @@ -46,6 +40,16 @@ TOPICS="user.created,user.updated,user.deleted" # comma separated list of topics # DISABLE_TELEMETRY=true +# ============================== Log Storage ============================== + +# Clickhouse +# CLICKHOUSE_ADDR="clickhouse:9000" +# CLICKHOUSE_USERNAME="default" +# CLICKHOUSE_DATABASE="outpost" +# CLICKHOUSE_PASSWORD="" + +# POSTGRES_URL="postgres://outpost:outpost@postgres:5432/outpost?sslmode=disable" + # ============================== Infra MQ ============================== # MQs, Uncomment the one you want to use diff --git a/test/quickstart.sh b/test/quickstart.sh index dd02e97d..780b3195 100755 --- a/test/quickstart.sh +++ b/test/quickstart.sh @@ -32,7 +32,7 @@ wait_for_service() { # Update the value within the new .env file sed -i '' "s/apikey/$TEST_API_KEY/" .env -docker-compose -f compose.yml -f compose-rabbitmq.yml up -d +docker-compose -f compose.yml -f compose-rabbitmq.yml -f compose-postgres.yml up -d # Wait until the services are running echo "Waiting for the services to start"