feat: added gitea workflow

This commit is contained in:
Sven Vogel 2024-10-16 16:05:47 +02:00
parent c274500b1a
commit 26670cf190
9 changed files with 144 additions and 14 deletions

View File

@ -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

1
.gitignore vendored
View File

@ -21,3 +21,4 @@
# Go workspace file # Go workspace file
go.work go.work
*.log

View File

@ -7,10 +7,18 @@ services:
POSTGRES_USER: "user" POSTGRES_USER: "user"
POSTGRES_PASSWORD: "password" POSTGRES_PASSWORD: "password"
POSTGRES_DB: "grepfood" POSTGRES_DB: "grepfood"
healthcheck:
test: ["CMD-SHELL", "pg_isready", "-d", "db_prod"]
interval: 30s
timeout: 60s
retries: 5
start_period: 80s
ports: ports:
- "5432:5432" - "5432:5432"
volumes: volumes:
- db:/var/lib/postgresql/data - db:/var/lib/postgresql/data
- ./mockup:/var/mockup # mount mockup SQL scripts
volumes: volumes:
db: db:
name: postgres

26
server/mockup/data.sql Normal file
View File

@ -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);

19
server/mockup/tables.sql Normal file
View File

@ -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));

42
server/run-ci.sh Executable file
View File

@ -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

12
server/run-init-mockup.sh Executable file
View File

@ -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"

View File

@ -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');

20
server/tests/run-tests.sh Executable file
View File

@ -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