crowdsec/tests/lib/db/instance-mysql
mmetc 0667552132
bats: postgres tests (#1377)
* split CI/bats in sqlite+mysql+mariadb+hub
* renamed db and user 'crowdsec' to 'crowdsec-test'; moved backend-specific scripts to lib/db, etc.
* postgres tests
* force delay between ipv6 tests (for myisam)
* force delay after pg_restore to ensure data is written
2022-03-21 15:51:05 +01:00

93 lines
2.4 KiB
Bash
Executable file

#!/usr/bin/env bash
set -eu
script_name=$0
die() {
echo >&2 "$@"
exit 1
}
MYSQL_HOST=${MYSQL_HOST:-127.0.0.1}
MYSQL_PORT=${MYSQL_PORT:-3306}
MYSQL_PASSWORD=${MYSQL_PASSWORD:-password}
MYSQL_USER=${MYSQL_USER:-root}
about() {
die "usage: $script_name [ config_yaml | setup | dump <backup_file> | restore <backup_file> ]"
}
check_mysql_client() {
if ! command -v mysql >/dev/null; then
die "missing required program 'mysql' as a mysql client (package mariadb-client-core-10.6 on debian like system)"
fi
}
exec_sql() {
cmd="${1?Missing required sql command}"
mysql -h "${MYSQL_HOST}" -u "${MYSQL_USER}" "--port=${MYSQL_PORT}" "-p${MYSQL_PASSWORD}" <<< "$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' IDENTIFIED BY 'crowdsec_test';"
exec_sql "GRANT ALL PRIVILEGES ON crowdsec_test.* TO 'crowdsec_test';"
}
dump() {
backup_file="${1?Missing file to backup database to}"
COLUMN_STATISTICS=
if mysqldump --column-statistics 2>&1 | grep -v 'unknown option'; then
COLUMN_STATISTICS='--column-statistics=0'
fi
mysqldump $COLUMN_STATISTICS "-h${MYSQL_HOST}" "-u${MYSQL_USER}" "-p${MYSQL_PASSWORD}" --databases 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"
mysql -h "${MYSQL_HOST}" -u "${MYSQL_USER}" "--port=${MYSQL_PORT}" "-p${MYSQL_PASSWORD}" < "$backup_file"
exec_sql "CREATE USER IF NOT EXISTS 'crowdsec_test' IDENTIFIED BY 'crowdsec_test';"
exec_sql "GRANT ALL PRIVILEGES ON crowdsec_test.* TO 'crowdsec_test';"
}
config_yaml() {
MYSQL_PORT=${MYSQL_PORT} MYSQL_HOST=${MYSQL_HOST} yq '
.db_config.type="mysql"|
.db_config.user="crowdsec_test" |
.db_config.password="crowdsec_test" |
.db_config.db_name="crowdsec_test" |
.db_config.host=strenv(MYSQL_HOST) |
.db_config.port=env(MYSQL_PORT) |
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;