mirror of
https://github.com/crowdsecurity/crowdsec.git
synced 2025-05-12 21:05:53 +02:00
pg tests: pass now, waiting for fix (#1393)
* PG* variable names * take DB_BACKEND from file name; reduce duplicated code; configure db_type=sqlite
This commit is contained in:
parent
2f18808d68
commit
3f24bcdbcf
5 changed files with 52 additions and 122 deletions
|
@ -2,16 +2,22 @@
|
|||
|
||||
set -eu
|
||||
script_name=$0
|
||||
DB_BACKEND=$(echo $script_name | cut -d- -f2)
|
||||
export DB_BACKEND
|
||||
|
||||
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}
|
||||
PGHOST=${PGHOST:-127.0.0.1}
|
||||
PGPORT=${PGPORT:-5432}
|
||||
PGPASSWORD=${PGPASSWORD:-postgres}
|
||||
PGUSER=${PGUSER:-postgres}
|
||||
export PGHOST
|
||||
export PGPORT
|
||||
export PGPASSWORD
|
||||
export PGUSER
|
||||
|
||||
about() {
|
||||
die "usage: $script_name [ config_yaml | setup | dump <backup_file> | restore <backup_file> ]"
|
||||
|
@ -25,7 +31,7 @@ check_postgres_client() {
|
|||
|
||||
exec_sql() {
|
||||
cmd="${1?Missing required sql command}"
|
||||
PGPASSWORD="${POSTGRES_PASSWORD}" psql --host "${POSTGRES_HOST}" --user "${POSTGRES_USER}" "--port=${POSTGRES_PORT}" <<< "$cmd"
|
||||
psql <<< "$cmd"
|
||||
}
|
||||
|
||||
requirements() {
|
||||
|
@ -44,23 +50,23 @@ setup() {
|
|||
|
||||
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"
|
||||
pg_dump -Ft --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"
|
||||
pg_restore --dbname crowdsec_test --clean < "$backup_file"
|
||||
}
|
||||
|
||||
config_yaml() {
|
||||
POSTGRES_PORT=${POSTGRES_PORT} POSTGRES_HOST=${POSTGRES_HOST} yq '
|
||||
.db_config.type="postgres"|
|
||||
yq '
|
||||
.db_config.type="$DB_BACKEND"|
|
||||
.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.host=strenv(PGHOST) |
|
||||
.db_config.port=env(PGPORT) |
|
||||
.db_config.sslmode="disable" |
|
||||
del(.db_config.db_path)
|
||||
' -i "${CONFIG_YAML}"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue