diff --git a/.gitea/workflows/ci-server.yml b/.gitea/workflows/ci-server.yml new file mode 100644 index 0000000..8330e16 --- /dev/null +++ b/.gitea/workflows/ci-server.yml @@ -0,0 +1,16 @@ +name: Gitea Action for testing api server +run-name: Compile and test API server +on: [push] + +jobs: + run-ci-server: + runs-on: ubuntu-latest + steps: + - name: Check out repository code + uses: actions/checkout@v3 + - name: Setup go + uses: https://github.com/actions/setup-go@v4 + with: + go-version: '>=1.20.1 + - name: Run API server CI + run: bash -c ./server/run-ci.sh diff --git a/.gitignore b/.gitignore index adf8f72..ac9ce35 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ # Go workspace file go.work +*.log diff --git a/server/docker-compose.yml b/server/docker-compose.yml index 052c65f..b5e8f86 100644 --- a/server/docker-compose.yml +++ b/server/docker-compose.yml @@ -7,10 +7,18 @@ services: POSTGRES_USER: "user" POSTGRES_PASSWORD: "password" POSTGRES_DB: "grepfood" + healthcheck: + test: ["CMD-SHELL", "pg_isready", "-d", "db_prod"] + interval: 30s + timeout: 60s + retries: 5 + start_period: 80s ports: - "5432:5432" volumes: - db:/var/lib/postgresql/data + - ./mockup:/var/mockup # mount mockup SQL scripts volumes: db: + name: postgres diff --git a/server/mockup/data.sql b/server/mockup/data.sql new file mode 100644 index 0000000..3b2518d --- /dev/null +++ b/server/mockup/data.sql @@ -0,0 +1,26 @@ + +INSERT INTO ingredient (name, icon, price) +VALUES + ('potato', 'potato.png', 399), + ('tomato', 'tomato.png', 199), + ('flour', 'flour.png', 050), + ('water', 'waterglass.png', 005); + +INSERT INTO recipe (name, icon) +VALUES + ('bread', 'bread.png'), + ('pizza', 'pizza.png'), + ('cookie', 'cookie.png'), + ('pasta', 'pasta.png'); + +INSERT INTO recipe_ingredient (recipe_id, ingredient_d, amount) +VALUES + (1, 1, 340), + (1, 2, 894), + (1, 3, 123), + (1, 4, 9), + (2, 2, 1), + (2, 3, 79), + (3, 1, 64), + (3, 3, 241), + (3, 2, 99); diff --git a/server/mockup/tables.sql b/server/mockup/tables.sql new file mode 100644 index 0000000..325066c --- /dev/null +++ b/server/mockup/tables.sql @@ -0,0 +1,19 @@ +CREATE TABLE IF NOT EXISTS ingredient ( + id SERIAL, + name VARCHAR(64) UNIQUE NOT NULL, + icon VARCHAR(64), + price INT, + PRIMARY KEY(id)); + +CREATE TABLE IF NOT EXISTS recipe ( + id SERIAL, + name VARCHAR(64) UNIQUE NOT NULL, + icon VARCHAR(64), + PRIMARY KEY(id)); + +CREATE TABLE IF NOT EXISTS recipe_ingredient ( + recipe_id INT NOT NULL, + ingredient_d INT NOT NULL, + amount INT, + FOREIGN KEY (recipe_id) REFERENCES recipe(id), + FOREIGN KEY (ingredient_d) REFERENCES ingredient(id)); diff --git a/server/run-ci.sh b/server/run-ci.sh new file mode 100755 index 0000000..8600358 --- /dev/null +++ b/server/run-ci.sh @@ -0,0 +1,42 @@ +#!/bin/bash + +docker compose up -d + +RETRIES=100 + +until docker container exec postgres psql -U user -d grepfood -c "select 1" > /dev/null 2>&1 || [ "$RETRIES" -eq 0 ]; do + echo "Waiting for postgres server, $((RETRIES--)) remaining attempts..." + sleep 1 +done + +echo "Setup mockup data" +bash -c ./run-init-mockup.sh + +echo "Loading environment..." +set -a +source ./.env +set +a + +echo "Run API server" +go run . > ./server.log 2>&1 & + +echo "wait for api server startup" +until curl http://localhost:8080/version > /dev/null 2>&1 || [ "$RETRIES" -eq 0 ]; do + echo "Waiting for api server, $((RETRIES--)) remaining attempts..." + sleep 1 +done + +bash -c ./tests/run-tests.sh + +echo "Terminating server..." +pkill grepfood-server + +echo "Server output following:" +echo "-----------------------------" + +cat ./server.log + +echo "-----------------------------" +echo "End of server output" +docker compose down +docker volume rm postgres diff --git a/server/run-init-mockup.sh b/server/run-init-mockup.sh new file mode 100755 index 0000000..8db9e53 --- /dev/null +++ b/server/run-init-mockup.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +function run_query() { + docker container exec postgres psql -U user -d grepfood --command="$1" +} + +function run_file() { + docker container exec postgres psql -U user -d grepfood -f "$1" +} + +run_file "/var/mockup/tables.sql" +run_file "/var/mockup/data.sql" diff --git a/server/tests/data.sql b/server/tests/data.sql deleted file mode 100644 index 4a28c9a..0000000 --- a/server/tests/data.sql +++ /dev/null @@ -1,14 +0,0 @@ - -INSERT INTO grepfood.public.ingredient (name, icon, price) -VALUES - ('potato', 'potato.png', 399), - ('tomato', 'tomato.png', 199), - ('flour', 'flour.png', 050), - ('water', 'waterglass.png', 005); - -INSERT INTO grepfood.public.recipe (name, icon) -VALUES - ('bread', 'bread.png'), - ('pizza', 'pizza.png'), - ('cookie', 'cookie.png'), - ('pasta', 'pasta.png'); diff --git a/server/tests/run-tests.sh b/server/tests/run-tests.sh new file mode 100755 index 0000000..b0f599d --- /dev/null +++ b/server/tests/run-tests.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +function test() { + echo "-----------------------------[ TEST ]-----------------------------" + printf "running $1\n\n" + + eval "$1" + exit_status=$? + + printf "\n\n" + + if [ ! $exit_status -eq 0 ]; then + abort "command: $1 failed with: $exit_status" + fi +} + +test "curl -i http://localhost:8080/version" +test "curl -i http://localhost:8080/api/ingredients" +test "curl -i http://localhost:8080/api/recipes" +test "curl -i http://localhost:8080/api/recipe/bread/ingredients