Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: db strategy on docker tests #178

Merged
merged 5 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .codepipeline/docker/config.default.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//========== Database Configuration ==========================
Config::set("database", [
"hostname" => getenv('DB_HOST') ?: "mysqldb",
"port" => getenv('DB_PORT') ?: "",
"port" => getenv('DB_PORT') ?: "3306",
"username" => getenv('DB_USERNAME') ?: "cmfive",
"password" => getenv('DB_PASSWORD') ?: "cmfive",
"database" => getenv('DB_DATABASE') ?: "cmfive",
Expand Down
9 changes: 9 additions & 0 deletions .codepipeline/docker/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ if [ ! -f config.php ]; then
cp /bootstrap/config.default.php config.php
fi

# Add custom config
if [ -n "$CUSTOM_CONFIG" ]; then
echo "➕ Adding custom config"
# Remove existing custom config between markers
sed -i '/# BEGIN CUSTOM CONFIG/,/# END CUSTOM CONFIG/d' config.php
# Add new custom config
echo -e "\n# BEGIN CUSTOM CONFIG\n${CUSTOM_CONFIG}\n# END CUSTOM CONFIG" >> config.php
fi

#Ensure necessary directories have the correct permissions
echo "Setting permissions"
chmod ugo=rwX -R cache/ storage/ uploads/
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ The following options can be used with the Docker image. You may choose to use f
- **DB_DATABASE:** The name of the database
- **DB_USERNAME:** The username to connect to the database
- **DB_PASSWORD:** The password to connect to the database
- **CUSTOM_COFIG:** (optional) Custom configuration to add to the config.php file.
- **ENVIRONMENT:** (optional) The environment to run in (development, production). Defaults to production.
- **INSTALL_CORE_BRANCH:** (optional) The branch of the cmfive-core repository to switch to while the container is starting. If not specified it will use the built-in core. Note: If this method is used, the theme will not be compiled automatically for the specified branch.

Expand Down
25 changes: 14 additions & 11 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ services:
container_name: mysql-8
hostname: mysql-8
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_USER=cmfive
- MYSQL_PASSWORD=cmfive
- MYSQL_DATABASE=cmfive
- TZ=Australia/Sydney
MYSQL_ROOT_PASSWORD: root
MYSQL_USER: cmfive
MYSQL_PASSWORD: cmfive
MYSQL_DATABASE: cmfive
TZ: Australia/Sydney
volumes:
- dbdata:/var/lib/mysql:delegated
networks:
Expand All @@ -28,12 +28,15 @@ services:
container_name: cmfive
hostname: nginx-php8.1
environment:
- TZ=Australia/Sydney
- DB_HOST=mysqldb
- DB_DATABASE=cmfive
- DB_USERNAME=cmfive
- DB_PASSWORD=cmfive
- ENVIRONMENT=development
TZ: Australia/Sydney
DB_HOST: mysqldb
DB_DATABASE: cmfive
DB_USERNAME: cmfive
DB_PASSWORD: cmfive
DB_PORT: 3306
ENVIRONMENT: development
CUSTOM_CONFIG: |
Config::set('tests', ['testrunner' => 'ENABLED']);
volumes:
- ./:/var/www/html:rw
ports:
Expand Down
71 changes: 53 additions & 18 deletions test/playwright/docker_run.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
#!/bin/sh

# Exit on error
set -e

# This script is used to run the Playwright tests in a container
# usage: ./docker_run.sh
# Or to refresh the Cmfive Docker containers before running the tests
# usage: ./docker_run.sh --fresh

# Bootstrap this script in the Playwright container
if [ -z "$IS_PLAYWRIGHT_CONTAINER" ]; then
# Explicitly prep DB for testing:
echo "Backup and snaphsot DB with migrations"
if docker exec -t cmfive bash -c "php cmfive.php testDB setup"; then
echo "Success: Setup test DB"
else
echo "Warning: Failed to setup test DB"
fi

# Get the directory of the script
TESTDIR=$(dirname -- "$( readlink -f -- "$0"; )";)

Expand All @@ -23,16 +34,32 @@ if [ -z "$IS_PLAYWRIGHT_CONTAINER" ]; then
docker compose up -d --wait
fi

docker build -t playwright-cosine -f $TESTDIR/playwright.Dockerfile $PROJECTDIR

set +e

docker run -it --rm \
-e IS_PLAYWRIGHT_CONTAINER=1 \
-v $PROJECTDIR:/cmfive-boilerplate \
-v ms-playwright-data-cmfive:/ms-playwright \
--ipc=host \
--network=host \
--user $(id -u):$(id -g) \
--cap-add=SYS_ADMIN \
mcr.microsoft.com/playwright:v1.45.1-jammy \
/cmfive-boilerplate/test/playwright/docker_run.sh
-e IS_PLAYWRIGHT_CONTAINER=1 \
-e LANG=en_AU.UTF-8 \
-e LC_ALL=en_AU.UTF-8 \
-v ms-playwright-data-cmfive:/ms-playwright \
-v $PROJECTDIR:/cmfive-boilerplate \
--ipc=host \
--network=host \
--cap-add=SYS_ADMIN \
playwright-cosine \
"/cmfive-boilerplate/test/playwright/docker_run.sh"

DOCKER_EXIT_CODE=$?

set -e

# OK, now we are done with the Playwright container
# Consider cleanup:
# We can hold the DB as-is, for further manual testing.
# But should clean up migration artefacts, to avoid git diff.
bash cleanupTestMigrations.sh
# but no: npm run cleanup

# Open ./test-results in the default file manager
if [ -d $TESTDIR/test-results ]; then
Expand All @@ -42,7 +69,13 @@ if [ -z "$IS_PLAYWRIGHT_CONTAINER" ]; then
open $TESTDIR/test-results
fi
fi


if [ $DOCKER_EXIT_CODE -ne 0 ]; then
echo "Playwright container exited with code $DOCKER_EXIT_CODE"
exit $DOCKER_EXIT_CODE
fi

echo "Success: Playwright tests completed"
exit
fi

Expand All @@ -65,20 +98,22 @@ wait_for_response() {
fi
echo ""
echo "$url responded"
}
}

cd /cmfive-boilerplate/test/playwright
wait_for_response "http://localhost:3000" 60

locale
date

cd /cmfive-boilerplate/test/playwright
npm i
npx playwright install
npm run setup
npm run build
npm run cleanup

wait_for_response "http://localhost:3000" 30
# Run the tests, recommended 1 worker for Playwright for memory usage
export WORKERS=1

export WORKERS=3
export RETRIES=2
# Retry failed tests, recommended 0 retries because of data consistency
export RETRIES=0

npm run test

17 changes: 17 additions & 0 deletions test/playwright/playwright.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM mcr.microsoft.com/playwright:v1.45.1-jammy
# Australianise things:
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y locales && \
locale-gen en_AU.UTF-8 && \
update-locale en_AU.UTF-8 && \
LANG=en_AU.UTF-8 && \
LC_ALL=en_AU.UTF-8 && \
locale

RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install -yq tzdata && \
ln -fs /usr/share/zoneinfo/Australia/Sydney /etc/localtime && \
dpkg-reconfigure -f noninteractive tzdata && \
date

# Shift into test environment
WORKDIR /cmfive-boilerplate/test/playwright
Loading