Reproducible Docker-based test environment

This commit is contained in:
Petr Baudis 2025-04-23 16:18:13 +02:00
parent e252390985
commit 8983ae13bf
3 changed files with 97 additions and 0 deletions

37
brmbar3/Dockerfile Normal file
View file

@ -0,0 +1,37 @@
FROM debian:bookworm-slim
# Install dependencies including PostgreSQL server (newer version)
RUN apt-get update && apt-get install -y \
python3 \
python3-psycopg2 \
postgresql \
postgresql-client \
sudo \
&& rm -rf /var/lib/apt/lists/*
# Create non-root user for running the application (matching INSTALL.md)
RUN useradd -m -s /bin/bash brmuser
# Give brmuser permissions to manage postgresql service
RUN echo "brmuser ALL=(ALL) NOPASSWD: /usr/sbin/service postgresql start, /usr/sbin/service postgresql stop, /usr/sbin/service postgresql status" > /etc/sudoers.d/brmuser && \
echo "brmuser ALL=(postgres) NOPASSWD: ALL" >> /etc/sudoers.d/brmuser && \
chmod 0440 /etc/sudoers.d/brmuser
# Create working directory
WORKDIR /app
# Copy application files
COPY . /app/
# Setup entrypoint script
COPY docker-entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
# Set proper permissions
RUN chown -R brmuser:brmuser /app
# Switch to non-root user
USER brmuser
ENTRYPOINT ["/entrypoint.sh"]
CMD ["test"]

View file

@ -77,6 +77,13 @@ Software Setup
TODO: Mention the actual commands to execute.
Running tests
-------------
Docker can be used to easily run tests in a reproducible environment:
docker build -t brmbar3 . && docker run --rm brmbar3 test
Troubleshooting
---------------

53
brmbar3/docker-entrypoint.sh Executable file
View file

@ -0,0 +1,53 @@
#!/bin/bash
set -e
# Start PostgreSQL service using sudo
echo "Starting PostgreSQL..."
sudo service postgresql start
# Wait for PostgreSQL to start
echo "Waiting for PostgreSQL to start..."
until sudo -u postgres pg_isready; do
sleep 1
done
echo "PostgreSQL started"
# Create PostgreSQL user if it doesn't exist (with database creation privileges)
if ! sudo -u postgres psql -tAc "SELECT 1 FROM pg_roles WHERE rolname='brmuser'" | grep -q 1; then
echo "Creating PostgreSQL user brmuser with database creation privileges..."
sudo -u postgres createuser -d brmuser
fi
# Drop and recreate database to ensure clean state
echo "Recreating database..."
sudo -u postgres psql -c 'DROP DATABASE IF EXISTS brmbar;'
createdb brmbar
# Load base schema (following INSTALL.md approach)
echo "Loading base schema..."
psql brmbar < /app/SQL
# Load schema files in order, stopping on first error
echo "Loading schema files..."
for f in /app/schema/[0-9]*-*.sql; do
echo "Loading schema file: $f"
if ! psql brmbar -f "$f"; then
echo "Error loading $f - stopping schema load process"
break
fi
done
if [ "$1" == "test" ]; then
echo "Running tests..."
# Load test data
echo "Loading test data..."
psql brmbar < /app/SQL.test
# Run test (as brmuser)
echo "Running python test..."
python3 /app/test--currency-rates.py
else
# Run the specified command
exec "$@"
fi