pgx driver tests (#1379)
This commit is contained in:
parent
6491100ef1
commit
7baa5d623b
2
.github/workflows/ci_bats_hub.yaml
vendored
2
.github/workflows/ci_bats_hub.yaml
vendored
|
@ -34,7 +34,7 @@ jobs:
|
||||||
|
|
||||||
- name: "Install bats dependencies"
|
- name: "Install bats dependencies"
|
||||||
run: |
|
run: |
|
||||||
sudo apt install daemonize netcat-openbsd
|
sudo apt install -qq daemonize netcat-openbsd
|
||||||
GO111MODULE=on go get github.com/mikefarah/yq/v4
|
GO111MODULE=on go get github.com/mikefarah/yq/v4
|
||||||
|
|
||||||
- name: "BATS: build crowdsec"
|
- name: "BATS: build crowdsec"
|
||||||
|
|
2
.github/workflows/ci_bats_mariadb.yaml
vendored
2
.github/workflows/ci_bats_mariadb.yaml
vendored
|
@ -41,7 +41,7 @@ jobs:
|
||||||
|
|
||||||
- name: "Install bats dependencies"
|
- name: "Install bats dependencies"
|
||||||
run: |
|
run: |
|
||||||
sudo apt install daemonize netcat-openbsd
|
sudo apt install -qq daemonize netcat-openbsd
|
||||||
GO111MODULE=on go get github.com/mikefarah/yq/v4
|
GO111MODULE=on go get github.com/mikefarah/yq/v4
|
||||||
|
|
||||||
- name: "BATS: build crowdsec"
|
- name: "BATS: build crowdsec"
|
||||||
|
|
2
.github/workflows/ci_bats_mysql.yaml
vendored
2
.github/workflows/ci_bats_mysql.yaml
vendored
|
@ -41,7 +41,7 @@ jobs:
|
||||||
|
|
||||||
- name: "Install bats dependencies"
|
- name: "Install bats dependencies"
|
||||||
run: |
|
run: |
|
||||||
sudo apt install daemonize netcat-openbsd
|
sudo apt install -qq daemonize netcat-openbsd
|
||||||
GO111MODULE=on go get github.com/mikefarah/yq/v4
|
GO111MODULE=on go get github.com/mikefarah/yq/v4
|
||||||
|
|
||||||
- name: "BATS: build crowdsec"
|
- name: "BATS: build crowdsec"
|
||||||
|
|
26
.github/workflows/ci_bats_postgres.yaml
vendored
26
.github/workflows/ci_bats_postgres.yaml
vendored
|
@ -46,11 +46,29 @@ jobs:
|
||||||
|
|
||||||
- name: "Install bats dependencies"
|
- name: "Install bats dependencies"
|
||||||
run: |
|
run: |
|
||||||
sudo apt install daemonize netcat-openbsd
|
sudo apt install -qq daemonize netcat-openbsd
|
||||||
GO111MODULE=on go get github.com/mikefarah/yq/v4
|
GO111MODULE=on go get github.com/mikefarah/yq/v4
|
||||||
|
|
||||||
- name: "BATS: build crowdsec"
|
- name: "BATS: build crowdsec (DB_BACKEND: pgx)"
|
||||||
run: make bats-clean bats-build
|
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:
|
env:
|
||||||
DB_BACKEND: postgres
|
DB_BACKEND: postgres
|
||||||
POSTGRES_HOST: 127.0.0.1
|
POSTGRES_HOST: 127.0.0.1
|
||||||
|
@ -58,7 +76,7 @@ jobs:
|
||||||
POSTGRES_PASSWORD: ${{ secrets.DATABASE_PASSWORD }}
|
POSTGRES_PASSWORD: ${{ secrets.DATABASE_PASSWORD }}
|
||||||
POSTGRES_USER: postgres
|
POSTGRES_USER: postgres
|
||||||
|
|
||||||
- name: "BATS: run tests"
|
- name: "BATS: run tests (DB_BACKEND: postgres)"
|
||||||
run: make bats-test
|
run: make bats-test
|
||||||
env:
|
env:
|
||||||
DB_BACKEND: postgres
|
DB_BACKEND: postgres
|
||||||
|
|
2
.github/workflows/ci_bats_sqlite.yaml
vendored
2
.github/workflows/ci_bats_sqlite.yaml
vendored
|
@ -34,7 +34,7 @@ jobs:
|
||||||
|
|
||||||
- name: "Install bats dependencies"
|
- name: "Install bats dependencies"
|
||||||
run: |
|
run: |
|
||||||
sudo apt install daemonize netcat-openbsd
|
sudo apt install -qq daemonize netcat-openbsd
|
||||||
GO111MODULE=on go get github.com/mikefarah/yq/v4
|
GO111MODULE=on go get github.com/mikefarah/yq/v4
|
||||||
|
|
||||||
- name: "BATS: build crowdsec"
|
- name: "BATS: build crowdsec"
|
||||||
|
|
|
@ -98,8 +98,6 @@ make_init_data() {
|
||||||
"${CSCLI}" lapi status
|
"${CSCLI}" lapi status
|
||||||
"${TEST_DIR}/instance-crowdsec" stop
|
"${TEST_DIR}/instance-crowdsec" stop
|
||||||
|
|
||||||
# we deal with the database separately (might be mysql, postgres)
|
|
||||||
|
|
||||||
mkdir -p "${LOCAL_INIT_DIR}"
|
mkdir -p "${LOCAL_INIT_DIR}"
|
||||||
|
|
||||||
./instance-db dump "${LOCAL_INIT_DIR}/database"
|
./instance-db dump "${LOCAL_INIT_DIR}/database"
|
||||||
|
|
87
tests/lib/db/instance-pgx
Executable file
87
tests/lib/db/instance-pgx
Executable file
|
@ -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 <backup_file> | restore <backup_file> ]"
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
|
@ -49,8 +49,6 @@ restore() {
|
||||||
backup_file="${1?missing file to restore database from}"
|
backup_file="${1?missing file to restore database from}"
|
||||||
[ -f "$backup_file" ] || die "Backup file $backup_file doesn't exist"
|
[ -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"
|
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() {
|
config_yaml() {
|
||||||
|
|
Loading…
Reference in a new issue