Skip to content

Commit

Permalink
feat: add testing in ci
Browse files Browse the repository at this point in the history
  • Loading branch information
janezicmatej committed Jan 24, 2025
1 parent 27b9d90 commit 546f755
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 27 deletions.
42 changes: 42 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
stages:
- build
- test

build:
stage: build
image: node:20.18.1
before_script:
- yarn install --frozen-lockfile
script:
- yarn build

e2e_test:
stage: test
image: node:20.18.1
variables:
POSTGRES_DB: db
POSTGRES_USER: user
POSTGRES_PASSWORD: pass
PGPASSWORD: ${POSTGRES_PASSWORD}
services:
- name: postgres:17
alias: postgres
before_script:
- yarn install --frozen-lockfile
- apt update && apt install -y postgresql-common
- YES=1 /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
- apt -y install postgresql-client-17
- curl -o test/e2e_tests/db/db_btc_testnet https://githubstatic.flare.center/db_btc_testnet
- curl -o test/e2e_tests/db/db_doge_testnet https://githubstatic.flare.center/db_doge_testnet
- curl -o test/e2e_tests/db/db_xrp_testnet https://githubstatic.flare.center/db_xrp_testnet
script:
- yarn test ci

lint:
stage: test
image: node:20.18.1
before_script:
- yarn install --frozen-lockfile
script:
- yarn lint:check
- yarn format:check
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"format:fix": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"-----Testing-----": "echo \"Testing scripts\"",
"makeE2Eexecutable": "chmod +x test/e2e_tests/run_tests.sh",
"test:btc": "./test/e2e_tests/run_tests.sh all btc",
"test": "./test/e2e_tests/run_tests.sh ",
"test:make_db:btc": "./test/e2e_tests/run_tests.sh make_db btc",
"test:run_tests:btc": "./test/e2e_tests/run_tests.sh run_tests btc",
"test:doge": "./test/e2e_tests/run_tests.sh all doge",
Expand Down
2 changes: 1 addition & 1 deletion test/e2e_tests/db/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ services:
POSTGRES_USER: user
POSTGRES_PASSWORD: pass
ports:
- "127.0.0.1:8080:5432"
- "127.0.0.1:8081:5432"
123 changes: 98 additions & 25 deletions test/e2e_tests/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ export DB_DATABASE=db
export DB_USERNAME=user
export DB_PASSWORD=pass
export DB_HOST=127.0.0.1
export DB_PORT=8080
export DB_PORT=8081

export VERIFIER_TYPE=$2
export TESTNET=true

export NUMBER_OF_CONFIRMATIONS=6
Expand All @@ -19,45 +18,119 @@ export API_KEYS=12345


wait_for_pg_ready() {
until docker compose -f test/e2e_tests/db/docker-compose.yaml exec postgres_testing_db pg_isready -h 127.0.0.1 -p 5432 -U db 1> /dev/null; do
until $DOCKER_CMD pg_isready -h $DB_HOST -p 5432 -U db 1> /dev/null; do
sleep 1
done
}

_make_db(){
$DOCKER_CMD dropdb -h $DB_HOST -U user db
wait_for_pg_ready
$DOCKER_CMD createdb -h $DB_HOST -U user -E utf8 -T template0 db
wait_for_pg_ready
$DOCKER_CMD pg_restore -h $DB_HOST --no-owner --role=user -U user --dbname=db /tmp/dbdump
wait_for_pg_ready
}

make_db(){
export VERIFIER_TYPE=$1
DOCKER_CMD="docker compose -f test/e2e_tests/db/docker-compose.yaml exec postgres_testing_db"

if [ "$1" = "all" ] || [ "$1" = "make_db" ]; then
echo ""
echo Making mock DB
echo Making mock DB for $VERIFIER_TYPE

docker compose -f test/e2e_tests/db/docker-compose.yaml up -d
wait_for_pg_ready
docker compose -f test/e2e_tests/db/docker-compose.yaml cp test/e2e_tests/db/db_${VERIFIER_TYPE}_testnet postgres_testing_db:/tmp/dbdump
echo "Copying dump to DB"
wait_for_pg_ready

_make_db
}

commands=(
"docker compose -f test/e2e_tests/db/docker-compose.yaml cp test/e2e_tests/db/db_$2_testnet postgres_testing_db:/tmp/dbdump"
"docker compose -f test/e2e_tests/db/docker-compose.yaml exec postgres_testing_db dropdb -U user db"
"docker compose -f test/e2e_tests/db/docker-compose.yaml exec postgres_testing_db createdb -U user -E utf8 -T template0 db"
"docker compose -f test/e2e_tests/db/docker-compose.yaml exec postgres_testing_db pg_restore --no-owner --role=user -U user --dbname=db /tmp/dbdump"
)

make_db_ci(){
export VERIFIER_TYPE=$1
export DB_HOST=postgres
export DB_PORT=5432
DOCKER_CMD=""

echo ""
echo Copying db_$2_testnet to DB
for command in "${commands[@]}"; do
eval "$command"
wait_for_pg_ready
done
fi
echo Copying dumb to DB

cp test/e2e_tests/db/db_${VERIFIER_TYPE}_testnet /tmp/dbdump

_make_db
}

if [ "$1" = "all" ] || [ "$1" = "run_tests" ]; then
run_tests(){
export VERIFIER_TYPE=$1
echo ""
echo Runing $2 tests
mocha -r ts-node/register --require source-map-support/register "test/e2e_tests/$2/**/*.e2e-spec.ts"
fi

echo Runing $VERIFIER_TYPE tests
mocha -r ts-node/register --require source-map-support/register "test/e2e_tests/${VERIFIER_TYPE}/**/*.e2e-spec.ts"
}

if [ "$1" = "all" ] || [ "$1" = "delete_db" ]; then
delete_db(){
echo ""
echo Stoping and removing DB
docker compose -f test/e2e_tests/db/docker-compose.yaml down
fi
}

show_help() {
echo "
Usage: yarn test COMMAND ARGUMENT
Commands:
run Run e2e tests (create db, run tests, delete db) (needs verifier type as argument, e.g. yarn test run btc)
make_db Create mock DB (needs verifier type as argument, e.g. yarn test make_db btc)
run_tests Run e2e tests (existing db needed) (needs verifier type as argument, e.g. yarn test run_tests btc)
delete_db Delete mock DB
ci Run tests in gitlab ci
Arguments:
btc Bitcoin verifier
doge Dogecoin verifier
xrp Ripple verifier
"
}

main() {
if [ $# -eq 0 ]; then
show_help
exit 1
fi

CMD="$1"
shift
case "$CMD" in
run)
# TODO:(andraz) add validtion (only one argument)
make_db "$@"
run_tests "$@"
delete_db
;;
make_db)
make_db "$@"
;;
run_tests)
run_tests "$@"
;;
delete_db)
delete_db
;;
ci)
# TODO:(andraz) can be improved that it doesn't crash at first error but runs all tests
make_db_ci btc &&
run_tests btc &&
make_db_ci doge &&
run_tests doge &&
make_db_ci xrp &&
run_tests xrp
;;
*)
show_help
;;
esac
}

main "$@"

0 comments on commit 546f755

Please sign in to comment.