diff --git a/.github/workflows/ci_bats_hub.yaml b/.github/workflows/ci_bats_hub.yaml index f2ec6946b..5182c87f1 100644 --- a/.github/workflows/ci_bats_hub.yaml +++ b/.github/workflows/ci_bats_hub.yaml @@ -34,7 +34,7 @@ jobs: - name: "Install bats dependencies" run: | - sudo apt install daemonize netcat-openbsd + sudo apt install -qq daemonize netcat-openbsd GO111MODULE=on go get github.com/mikefarah/yq/v4 - name: "BATS: build crowdsec" diff --git a/.github/workflows/ci_bats_mariadb.yaml b/.github/workflows/ci_bats_mariadb.yaml index be313a878..026c7fe3d 100644 --- a/.github/workflows/ci_bats_mariadb.yaml +++ b/.github/workflows/ci_bats_mariadb.yaml @@ -41,7 +41,7 @@ jobs: - name: "Install bats dependencies" run: | - sudo apt install daemonize netcat-openbsd + sudo apt install -qq daemonize netcat-openbsd GO111MODULE=on go get github.com/mikefarah/yq/v4 - name: "BATS: build crowdsec" diff --git a/.github/workflows/ci_bats_mysql.yaml b/.github/workflows/ci_bats_mysql.yaml index 9571d11e1..829f0a603 100644 --- a/.github/workflows/ci_bats_mysql.yaml +++ b/.github/workflows/ci_bats_mysql.yaml @@ -41,7 +41,7 @@ jobs: - name: "Install bats dependencies" run: | - sudo apt install daemonize netcat-openbsd + sudo apt install -qq daemonize netcat-openbsd GO111MODULE=on go get github.com/mikefarah/yq/v4 - name: "BATS: build crowdsec" diff --git a/.github/workflows/ci_bats_postgres.yaml b/.github/workflows/ci_bats_postgres.yaml index ebb941b73..d54b8346f 100644 --- a/.github/workflows/ci_bats_postgres.yaml +++ b/.github/workflows/ci_bats_postgres.yaml @@ -46,11 +46,29 @@ jobs: - name: "Install bats dependencies" run: | - sudo apt install daemonize netcat-openbsd + sudo apt install -qq daemonize netcat-openbsd GO111MODULE=on go get github.com/mikefarah/yq/v4 - - name: "BATS: build crowdsec" - run: make bats-clean bats-build + - name: "BATS: build crowdsec (DB_BACKEND: pgx)" + run: make clean bats-build + env: + DB_BACKEND: pgx + POSTGRES_HOST: 127.0.0.1 + POSTGRES_PORT: 5432 + POSTGRES_PASSWORD: ${{ secrets.DATABASE_PASSWORD }} + POSTGRES_USER: postgres + + - name: "BATS: run tests (DB_BACKEND: pgx)" + run: make bats-test + env: + DB_BACKEND: pgx + POSTGRES_HOST: 127.0.0.1 + POSTGRES_PORT: 5432 + POSTGRES_PASSWORD: ${{ secrets.DATABASE_PASSWORD }} + POSTGRES_USER: postgres + + - name: "BATS: build crowdsec (DB_BACKEND: postgres)" + run: make clean bats-build env: DB_BACKEND: postgres POSTGRES_HOST: 127.0.0.1 @@ -58,7 +76,7 @@ jobs: POSTGRES_PASSWORD: ${{ secrets.DATABASE_PASSWORD }} POSTGRES_USER: postgres - - name: "BATS: run tests" + - name: "BATS: run tests (DB_BACKEND: postgres)" run: make bats-test env: DB_BACKEND: postgres diff --git a/.github/workflows/ci_bats_sqlite.yaml b/.github/workflows/ci_bats_sqlite.yaml index e278a41bd..5b23a5efe 100644 --- a/.github/workflows/ci_bats_sqlite.yaml +++ b/.github/workflows/ci_bats_sqlite.yaml @@ -34,7 +34,7 @@ jobs: - name: "Install bats dependencies" run: | - sudo apt install daemonize netcat-openbsd + sudo apt install -qq daemonize netcat-openbsd GO111MODULE=on go get github.com/mikefarah/yq/v4 - name: "BATS: build crowdsec" diff --git a/tests/instance-data b/tests/instance-data index dc0642acf..35db55816 100755 --- a/tests/instance-data +++ b/tests/instance-data @@ -98,8 +98,6 @@ make_init_data() { "${CSCLI}" lapi status "${TEST_DIR}/instance-crowdsec" stop - # we deal with the database separately (might be mysql, postgres) - mkdir -p "${LOCAL_INIT_DIR}" ./instance-db dump "${LOCAL_INIT_DIR}/database" diff --git a/tests/lib/db/instance-pgx b/tests/lib/db/instance-pgx new file mode 100755 index 000000000..204ddaf38 --- /dev/null +++ b/tests/lib/db/instance-pgx @@ -0,0 +1,87 @@ +#!/usr/bin/env bash + +set -eu +script_name=$0 + +die() { + echo >&2 "$@" + exit 1 +} + +POSTGRES_HOST=${POSTGRES_HOST:-127.0.0.1} +POSTGRES_PORT=${POSTGRES_PORT:-5432} +POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-postgres} +POSTGRES_USER=${POSTGRES_USER:-postgres} + +about() { + die "usage: $script_name [ config_yaml | setup | dump | restore ]" +} + +check_postgres_client() { + if ! command -v psql >/dev/null; then + die "missing required program 'psql' as a postgres client (package postgres-client-13 on debian like system)" + fi +} + +exec_sql() { + cmd="${1?Missing required sql command}" + PGPASSWORD="${POSTGRES_PASSWORD}" psql -h "${POSTGRES_HOST}" --user "${POSTGRES_USER}" "--port=${POSTGRES_PORT}" <<< "$cmd" +} + +requirements() { + check_mysql_client +} + +setup() { + exec_sql "DROP DATABASE IF EXISTS crowdsec_test;" + exec_sql "CREATE DATABASE crowdsec_test;" + exec_sql "DROP USER IF EXISTS crowdsec_test;" + exec_sql "CREATE USER crowdsec_test WITH ENCRYPTED PASSWORD 'crowdsec_test';" + exec_sql "GRANT ALL PRIVILEGES ON DATABASE crowdsec_test TO crowdsec_test;" +} + +dump() { + backup_file="${1?Missing file to backup database to}" + PGPASSWORD="${POSTGRES_PASSWORD}" pg_dump -Ft --host "${POSTGRES_HOST}" --port "${POSTGRES_PORT}" --username "${POSTGRES_USER}" --dbname crowdsec_test > "$backup_file" +} + +restore() { + backup_file="${1?missing file to restore database from}" + [ -f "$backup_file" ] || die "Backup file $backup_file doesn't exist" + PGPASSWORD="${POSTGRES_PASSWORD}" pg_restore --username "${POSTGRES_USER}" --host "${POSTGRES_HOST}" --port "${POSTGRES_PORT}" --dbname crowdsec_test --clean < "$backup_file" +} + +config_yaml() { + POSTGRES_PORT=${POSTGRES_PORT} POSTGRES_HOST=${POSTGRES_HOST} yq ' + .db_config.type="pgx"| + .db_config.user="crowdsec_test" | + .db_config.password="crowdsec_test" | + .db_config.db_name="crowdsec_test" | + .db_config.host=strenv(POSTGRES_HOST) | + .db_config.port=env(POSTGRES_PORT) | + .db_config.sslmode="disable" | + del(.db_config.db_path) + ' -i "${CONFIG_YAML}" +} + +[ $# -lt 1 ] && about + +case "$1" in + setup) + setup + ;; + config-yaml) + config_yaml + ;; + dump) + shift + dump "$@" + ;; + restore) + shift + restore "$@" + ;; + *) + about + ;; +esac; diff --git a/tests/lib/db/instance-postgres b/tests/lib/db/instance-postgres index 58c731f1e..4c7ee9c57 100755 --- a/tests/lib/db/instance-postgres +++ b/tests/lib/db/instance-postgres @@ -49,8 +49,6 @@ restore() { backup_file="${1?missing file to restore database from}" [ -f "$backup_file" ] || die "Backup file $backup_file doesn't exist" PGPASSWORD="${POSTGRES_PASSWORD}" pg_restore --username "${POSTGRES_USER}" --host "${POSTGRES_HOST}" --port "${POSTGRES_PORT}" --dbname crowdsec_test --clean < "$backup_file" - # make sure data is ready when crowdsec needs it - sleep 0.3 } config_yaml() {