# Set up a postgresql database and a redis server for the integration tests. # Set up a PostgreSQL database for the integration tests. # # Preconditions: # - PostgreSQL must be installed on the system. # - The `initdb`, `pg_ctl`, and `psql` commands must be available in the PATH. # # Postconditions: # - A temporary PostgreSQL data directory will be created and initialized. # - PostgreSQL will be started using a Unix socket. # - A PostgreSQL user and database named "sharkey" will be created. create_tmp_psql() { export PGDATA=$(mktemp -d) # Initialize the PostgreSQL data directory initdb -D $PGDATA -U postgres # Start PostgreSQL using Unix sockets pg_ctl -D $PGDATA -l $PGDATA/logfile start -o "-k $PGDATA --listen-addresses=''" until pg_isready -h $PGDATA; do sleep 1; done echo "PostgreSQL started with Unix socket at $PGDATA" # Create a "sharkey" user and database psql -h $PGDATA -c "CREATE USER sharkey" -U postgres psql -h $PGDATA -c "CREATE DATABASE sharkey OWNER sharkey" -U postgres } # Function to find a random unused port # It generates a random port number between 2000 and 65000 # and checks if it is in use. If it is in use, it recursively # calls itself until an unused port is found. function random_unused_port { local port=$(shuf -i 2000-65000 -n 1) netstat -lat | grep $port > /dev/null if [[ $? == 1 ]] ; then echo $port else random_unused_port fi } # Function to create a Redis server on a random unused port # It exports the port number to the REDIS_PORT environment variable # and starts the Redis server with log level set to warning. # It waits until the Redis server is ready to accept connections. create_redis_on_random_port() { export REDIS_PORT=$(random_unused_port) redis-server --port $REDIS_PORT --loglevel warning & until redis-cli -p $REDIS_PORT ping; do sleep 1; done echo "Redis started on port $REDIS_PORT" } pick_sharkey_port() { export SHARKEY_PORT=$(random_unused_port) } # Function to copy the example configuration file and update it with the PostgreSQL and Redis settings. # Arguments: # $1: The destination file path (optional). Defaults to `.config/default.yml`. copy_and_update_config() { local dest_file=.config/default.yml cp .config/example.yml $dest_file yq -i -Y ".db.host = \"$PGDATA\"" $dest_file yq -i -Y ".redis.port = $REDIS_PORT" $dest_file yq -i -Y ".port = $SHARKEY_PORT" $dest_file } # Function to stop the PostgreSQL server; $PGDATA must be set stop_psql() { pg_ctl -D "$PGDATA" stop } # Function to stop the Redis server stop_redis() { redis-cli -p "$REDIS_PORT" shutdown } # Function to stop both PostgreSQL and Redis servers stop_databases() { stop_psql stop_redis } # Function to start a fresh instance of the Sharkey application # It sets up a temporary PostgreSQL database and a Redis server on a random port, # updates the configuration file, and runs the application. start_fresh_sharkey() { create_tmp_psql create_redis_on_random_port pick_sharkey_port trap stop_databases EXIT copy_and_update_config pnpm run migrateandstart } start_fresh_sharkey firefox http://localhost:$SHARKEY_PORT --marionette --new-instance