From 77e32722cb76e9290d1055865b5c493e105daf4d Mon Sep 17 00:00:00 2001 From: Dan Van Atta Date: Sat, 25 Nov 2023 14:22:11 -0800 Subject: [PATCH] Automatically start a DB on docker via gradle (#12124) `./gradlew testWithDatabase` now automatically starts a database, as does running a lobby. There is no longer a need to run the 'start-database.sh' script for gradle tasks to work. --- .build/setup-database | 21 ---------- .docker/docker-compose.yml | 13 +++++++ .docker/init.sql | 5 +++ .github/workflows/verify.yml | 20 +++------- build.gradle | 7 ++++ docs/development/README.md | 39 +++++++------------ spitfire-server/dropwizard-server/README.md | 2 - .../dropwizard-server/build.gradle | 2 + verify | 7 ++-- 9 files changed, 48 insertions(+), 68 deletions(-) delete mode 100755 .build/setup-database create mode 100644 .docker/docker-compose.yml create mode 100644 .docker/init.sql delete mode 100644 spitfire-server/dropwizard-server/README.md diff --git a/.build/setup-database b/.build/setup-database deleted file mode 100755 index 6df285936ac..00000000000 --- a/.build/setup-database +++ /dev/null @@ -1,21 +0,0 @@ -#!/bin/bash - -set -eux - -function main() { - export PGPASSWORD=postgres - createDatabase "lobby_db" "lobby_user" "lobby" - createDatabase "error_report" "error_report_user" "error_report" - ./gradlew flywayMigrateLobbyDb flywayMigrateErrorReportDb -} - -function createDatabase() { - local dbName="$1" - local dbUser="$2" - local password="$3" - - echo "create user ${dbUser} with password '${password}'" | psql -h localhost -U postgres -d postgres - echo "create database ${dbName} owner ${dbUser}" | psql -h localhost -U postgres -d postgres -} - -main diff --git a/.docker/docker-compose.yml b/.docker/docker-compose.yml new file mode 100644 index 00000000000..75cc0d279aa --- /dev/null +++ b/.docker/docker-compose.yml @@ -0,0 +1,13 @@ +version: '3' +services: + database: + image: postgres:10 + environment: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres + POSTGRES_DB: postgres + volumes: + - ./init.sql:/docker-entrypoint-initdb.d/01-init.sql + ports: + - 5432:5432 + diff --git a/.docker/init.sql b/.docker/init.sql new file mode 100644 index 00000000000..d63bf5c3a85 --- /dev/null +++ b/.docker/init.sql @@ -0,0 +1,5 @@ +create user lobby_user password 'lobby'; +create database lobby_db owner lobby_user; + +create user error_report_user password 'error_report'; +create database error_report owner error_report_user; diff --git a/.github/workflows/verify.yml b/.github/workflows/verify.yml index 64c516b0c64..c24672886a7 100644 --- a/.github/workflows/verify.yml +++ b/.github/workflows/verify.yml @@ -8,20 +8,6 @@ on: jobs: build: runs-on: ubuntu-latest - services: - postgres: - image: postgres:10 - env: - POSTGRES_USER: postgres - POSTGRES_PASSWORD: postgres - POSTGRES_DB: postgres - ports: - - 5432:5432 - options: >- - --health-cmd pg_isready - --health-interval 10s - --health-timeout 5s - --health-retries 5 steps: - uses: actions/checkout@v4 - name: Set up JDK 11 @@ -29,5 +15,9 @@ jobs: with: distribution: temurin java-version: 11 + - uses: isbang/compose-action@v1.5.1 + with: + compose-file: ".docker/docker-compose.yml" - name: Run Build Checks - run: ./verify + run: ./verify --exclude-task composeUp + diff --git a/build.gradle b/build.gradle index 6045c07c0d5..be7d14b2784 100644 --- a/build.gradle +++ b/build.gradle @@ -3,9 +3,12 @@ plugins { id 'com.github.ben-manes.versions' version '0.50.0' id 'io.franzbecker.gradle-lombok' version '5.0.0' apply false id 'com.diffplug.spotless' version '6.22.0' apply false + id 'com.avast.gradle.docker-compose' version '0.17.5' } apply plugin: 'eclipse' +apply plugin: 'docker-compose' + ext { schemasDir = file('config/triplea/schemas') @@ -206,6 +209,7 @@ subprojects { description = 'Runs integration tests.' group = 'verification' + dependsOn(":composeUp") dependsOn(":spitfire-server:database:flywayMigrateLobbyDb") dependsOn(":spitfire-server:database:flywayMigrateErrorReportDb") @@ -223,4 +227,7 @@ subprojects { // - 'check' should run ALL validations, tests, spotlessChecks, everything.. check.dependsOn testAll + dockerCompose { + useComposeFiles = ['.docker/docker-compose.yml' ] + } } diff --git a/docs/development/README.md b/docs/development/README.md index 76560f6ba34..71f2e5c443b 100644 --- a/docs/development/README.md +++ b/docs/development/README.md @@ -40,23 +40,18 @@ For more detailed steps on building the project with CLI, see: IDE setup has the needed configurations for the project to compile. There are checked in launchers that IDEA should automatically find. Look in 'run configurations' to launch the game-client. -## Running all tests locally before PR (CLI) +## Running build checks locally (CLI) +Verify will run all checks (including additional custom checks): ``` -./game-app/run/check +./verify ``` -## Run Tests +Tests are split between those that need a database (which runs on docker), vs those that do not. -``` -./gradlew test -``` - -## Run Tests that need a running database - -``` -./gradlew testWithDatabase -``` +- `./gradlew test`: Runs all tests that do not require database +- `./gradlew testWithDatabase`: Runs tests that require a local database +- `./gradlew allTest`: Runs all tests ## Run Formatting @@ -68,7 +63,7 @@ from IDE. Everything can also be formatted from CLI: ./gradew spotlessApply ``` -PR builds will fail if the code is not formatted with spotless. +PR builds will fail if the code is not formatted. ## Code Conventions (Style Guide) @@ -84,24 +79,16 @@ Please be sure to check these out so that you can fit the general style of the p Local database is needed to run the servers (lobby). ```bash -# requires docker to be installed -./spitfire-server/database/start_docker_db +## start database +./gradlew composeUp ``` -This will launch a postgres database on docker and will install the latest -TripleA schema with a small sample dataset for local testing. - -After the database is launched you can: -- run the full set of tests -- launch a local lobby - ### Launch local lobby Lobby can be launched via the checked-in run configurations from IDE, or from CLI: ```bash ./gradlew :spitfire-server:dropwizard-server:run ``` - To connect to local lobby, from the game client: - 'settings > testing > local lobby' - play online @@ -134,7 +121,7 @@ The deployment code is inside of the '[/infrastructure](./infrastructure)' folde We use [ansible](https://www.ansible.com/) to execute deployments. You can test out deployment code by first launching a virtual machine and then running a deployment -against that virtual machine. See '[infrastructure/vagrant/README.md](./infrastructure/vagrant/README.md)' +against that virtual machine. See '[infrastructure/vagrant/REAMDE.md](./infrastructure/vagrant/REAMDE.md)' for more information. # Pitfalls and Pain Points to be aware of @@ -177,7 +164,7 @@ In short: ### Google formatter does not work -IDEA needs a tweak to overcome a JDk9 problem. See the IDE setup for the needed config that needs -to be added to your vmoptions file. +IDEA needs a tweak to overcome a JDk9 problem. The google java format plugin should show a warning dialog about +this if it is a problem. diff --git a/spitfire-server/dropwizard-server/README.md b/spitfire-server/dropwizard-server/README.md deleted file mode 100644 index b6f6ce9ce74..00000000000 --- a/spitfire-server/dropwizard-server/README.md +++ /dev/null @@ -1,2 +0,0 @@ -Sub-project to make setting up a generic -dropwizard server easier to reduce boilerplate code. diff --git a/spitfire-server/dropwizard-server/build.gradle b/spitfire-server/dropwizard-server/build.gradle index a919b26140b..76ffee02e96 100644 --- a/spitfire-server/dropwizard-server/build.gradle +++ b/spitfire-server/dropwizard-server/build.gradle @@ -9,6 +9,8 @@ ext { releasesDir = file("$buildDir/releases") } +run.dependsOn rootProject.composeUp + jar { manifest { attributes 'Main-Class': mainClassName diff --git a/verify b/verify index 779e15bb08a..06deefcb25a 100755 --- a/verify +++ b/verify @@ -19,9 +19,9 @@ function checkDependency { fi } -# Installs python -# Installs docker & docker-compose using pip -# Adds current user to 'docker' group (allows for sudo-less docker) +# Installs Docker and dependencies +# Docker is installed through python, we install python & pip first, then docker. +# Last, we add current user to the 'docker' group to enable sudo-less docker. function installDocker { echo "Installing python and pip, a dependency of docker" if hash yum; then @@ -49,7 +49,6 @@ set -o pipefail set -eu checkDependency "docker" || installDocker -"$scriptDir/.build/setup-database" "$scriptDir/gradlew" check $@ "$scriptDir/.build/find-unused-dbunit-datasets" "$scriptDir/.build/code-convention-checks/check-custom-style" spitfire-server