Skip to content

Commit

Permalink
Basic database test
Browse files Browse the repository at this point in the history
  • Loading branch information
dboreham committed Feb 14, 2024
1 parent c36f1e4 commit 7b5f720
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
services:

database:
image: cerc/test-database-container:local
restart: always
Expand All @@ -11,6 +12,9 @@ services:
POSTGRES_INITDB_ARGS: "-E UTF8 --locale=C"
ports:
- "5432"

test-client:
image: cerc/test-database-client:local

volumes:
db-data:
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM ubuntu:latest

RUN apt-get update && export DEBIAN_FRONTEND=noninteractive && export DEBCONF_NOWARNINGS="yes" && \
apt-get install -y software-properties-common && \
apt-get install -y postgresql-client && \
apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

EXPOSE 80

COPY run.sh /app/run.sh

ENTRYPOINT ["/app/run.sh"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash
# Build cerc/test-container
source ${CERC_CONTAINER_BASE_DIR}/build-base.sh
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
docker build -t cerc/test-database-client:local -f ${SCRIPT_DIR}/Dockerfile ${build_command_args} $SCRIPT_DIR
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env bash
set -e
if [ -n "$CERC_SCRIPT_DEBUG" ]; then
set -x
fi

# TODO derive this from config
database_url="postgresql://test-user:password@localhost:5432/test-db"
psql_command="psql ${database_url}"

does_test_data_exist () {
query_result=$(${psql_command} -t -c "select count(*) from test_table_1 where key_column = 'test_key_1';" | head -1 | tr -d ' ')
if [[ "${query_result}" == "1" ]]; then
return 0
else
return 1
fi
}

create_test_data () {
${psql_command} -c "create table test_table_1 (key_column text, value_column text, primary key(key_column));"
${psql_command} -c "insert into test_table_1 values ('test_key_1', 'test_value_1');"
}

wait_forever() {
# Loop to keep docker/k8s happy since this is the container entrypoint
while :; do sleep 600; done
}

# Check if the test database content exists already
if does_test_data_exist; then
# If so, log saying so. Test harness will look for this log output
echo "Database test client: test data already exists"
else
# Otherwise log saying the content was not present
echo "Database test client: test data does not exist"
echo "Database test client: creating test data"
# then create it
create_test_data
fi

wait_forever
1 change: 1 addition & 0 deletions stack_orchestrator/data/stacks/test-database/stack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ description: "A test database stack"
repos:
containers:
- cerc/test-database-container
- cerc/test-database-client
pods:
- test-database

0 comments on commit 7b5f720

Please sign in to comment.