chore: Define custom docker image for tests

This images is built from the official postgres docker image, but with
updates to optimize the test execution:

- A config file for postgres to increase the performance of the
  test server.
- An initialization script to create a template database with all
  migrations run against it.

There are new make targets to build and publish this docker image.
The image can be pulled with:

    docker pull hashicorpboundary/postgres:13-alpine

To use this image for tests start the container:

    make -C internal/db/docker database-up
This commit is contained in:
Timothy Messier committed 2021-09-09 13:42:00 -04:00
1 parent 811d0b63fd
commit c6cb7c8512
7 files changed
+153

No files matched your search

@@ -0,0 +1,6 @@
FROM postgres:11-alpine
ADD init-db.sh /docker-entrypoint-initdb.d/00-init-db.sh
ADD postgresql.conf /etc/postgresql/postgresql.conf
CMD ["postgres", "-c", "config_file=/etc/postgresql/postgresql.conf"]
@@ -0,0 +1,6 @@
FROM postgres:12-alpine
ADD init-db.sh /docker-entrypoint-initdb.d/00-init-db.sh
ADD postgresql.conf /etc/postgresql/postgresql.conf
CMD ["postgres", "-c", "config_file=/etc/postgresql/postgresql.conf"]
@@ -0,0 +1,6 @@
FROM postgres:13-alpine
ADD init-db.sh /docker-entrypoint-initdb.d/00-init-db.sh
ADD postgresql.conf /etc/postgresql/postgresql.conf
CMD ["postgres", "-c", "config_file=/etc/postgresql/postgresql.conf"]
+6
View File
@@ -0,0 +1,6 @@
FROM postgres:alpine
ADD init-db.sh /docker-entrypoint-initdb.d/00-init-db.sh
ADD postgresql.conf /etc/postgresql/postgresql.conf
CMD ["postgres", "-c", "config_file=/etc/postgresql/postgresql.conf"]
+50
View File
@@ -0,0 +1,50 @@
all: docker-build
CWD := $(shell pwd)
DOCKER_ARGS ?= -d
REGISTRY_NAME?=docker.io/hashicorpboundary
IMAGE_NAME=postgres
IMAGE_TAG ?= $(REGISTRY_NAME)/$(IMAGE_NAME):11-alpine
PG_OPTS ?=
TEST_DB_PORT ?= 5432
# Generate targets from dockerfiles
dockerfiles = $(wildcard Dockerfile.*)
docker-builds = $(patsubst Dockerfile.%,%-build, $(dockerfiles))
docker-publishes = $(patsubst Dockerfile.%,%-publish, $(dockerfiles))
${docker-builds}: %-build:
docker build -t $(REGISTRY_NAME)/$(IMAGE_NAME):$* -f Dockerfile.$* .
docker-build: ${docker-builds}
docker-publish: ${docker-publishes}
${docker-publishes}: %-publish:
docker push $(REGISTRY_NAME)/$(IMAGE_NAME):$*
database-up:
@echo "Using image: $(IMAGE_TAG)"
@echo "Additional postgres configuration: $(PG_OPTS)"
@docker run \
$(DOCKER_ARGS) \
--name boundary-sql-tests \
-p $(TEST_DB_PORT):5432 \
-e POSTGRES_PASSWORD=boundary \
-e POSTGRES_USER=boundary \
-e POSTGRES_DB=boundary \
-e PGDATA=/pgdata \
--mount type=tmpfs,destination=/pgdata \
-v "$(CWD)/../../../internal/db/schema/migrations/postgres":/migrations \
$(IMAGE_TAG) \
-c 'config_file=/etc/postgresql/postgresql.conf' \
$(PG_OPTS) 1> /dev/null
@echo "Test database available at: 127.0.0.1:$(TEST_DB_PORT)"
@echo "For database logs run:"
@echo " docker logs boundary-sql-test"
clean:
docker stop boundary-sql-tests || true
docker rm -v boundary-sql-tests || true
.PHONY: all docker-build database-up ${docker-builds} ${docker-publishes} clean
+44
View File
@@ -0,0 +1,44 @@
#!/usr/bin/env bash
set -e
shopt -s globstar
psql -v "ON_ERROR_STOP=1" --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" -q <<EOSQL
create user postgres superuser login;
create database boundary_template owner $POSTGRES_USER;
EOSQL
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --no-password --dbname boundary_template -q <<EOSQL
create table boundary_schema_version (
version bigint primary key,
dirty boolean not null
);
EOSQL
version=
# Run migrations in order.
for file in $(ls -v /migrations/**/*.sql); do
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --no-password --dbname boundary_template -f "$file"
IFS='/'
read -ra PARTS <<< "$file"
major="10#${PARTS[2]}"
IFS='_'
read -ra MINOR_PARTS <<< "${PARTS[3]}"
minor="10#${MINOR_PARTS[0]}"
let version=${major}*1000+${minor}
IFS=' '
done
echo "setting boundary_schema_version to ${version}";
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --no-password --dbname boundary_template -q <<EOSQL
insert into boundary_schema_version
(version, dirty)
values
(${version}, false);
EOSQL
psql -v "ON_ERROR_STOP=1" --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" -q <<EOSQL
update pg_database set datistemplate = true, datallowconn = false where datname = 'boundary_template';
EOSQL
+35
View File
@@ -0,0 +1,35 @@
listen_addresses = '*' # listen on all addresses so other containers can connect if need be.
max_connections = 200000 # set max connections to the limit to ensure tests can always get a connection.
# Note that this may need to be decreased if <4G are available for the container.
shared_buffers = 256MB # A more reasonable default compared to the 32MB default config.
# This gives postgres more memory for storing results. But given that short lived nature of the
# test databases and the relatively small data sets, there should not be a need to increase it much more then this.
dynamic_shared_memory_type = posix # Use POSIX shared memory allocated using shm_open
fsync = off # Do not flush, can cause data corruption, but since it is for tests and the data is dropped anyway...
synchronous_commit = off # Do not wait for WAL records to be written to disk before reporting success
full_page_writes = off # Also more performant, with the same risks as fsync being off
autovacuum = off # Disable autovacuum, since the test databases and tables are very short-lived, there is no need to vacuum.
log_destination = 'stderr' # Send logs to stderr, most logs are turned off by the following options, but if they are turned on, they will go to stderr
logging_collector = off # Disables the background log collector which is most useful in production environments with logs going to files
log_min_messages = panic # Only log panics to keep the noise and io down
log_min_error_statement = panic # Only log panics to keep the noise and io down
log_min_duration_statement = -1 # Disable slow query logs
log_temp_files = -1 # Disable logs about temp file usage
log_checkpoints = off
log_connections = off
log_disconnections = off
log_duration = off
log_hostname = off
log_lock_waits = off
log_parser_stats = off
log_planner_stats = off
log_executor_stats = off
log_statement_stats = off
log_timezone = 'UTC'
datestyle = 'iso, mdy'
timezone = 'Etc/UTC'
lc_messages = 'en_US.utf8'
lc_monetary = 'en_US.utf8'
lc_numeric = 'en_US.utf8'
lc_time = 'en_US.utf8'
default_text_search_config = 'pg_catalog.english'