diff --git a/.github/workflows/ci-server.yaml b/.github/workflows/ci-server.yaml index 82c24cac9a8e6..99ad2dab52ead 100644 --- a/.github/workflows/ci-server.yaml +++ b/.github/workflows/ci-server.yaml @@ -5,7 +5,25 @@ on: - main pull_request_target: jobs: + postgres-job: + runs-on: ubuntu-latest + container: node:10.18-jessie + steps: + - run: echo "Postgres job finished" + services: + postgres: + image: postgres + env: + POSTGRES_HOST: postgres + POSTGRES_PASSWORD: postgrespassword + POSTGRES_PORT: 5432 + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 server-test: + needs: postgres-job runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 @@ -27,3 +45,6 @@ jobs: - name: Server / Run jest tests run: | cd server && yarn test + - name: Server / Run e2e tests + run: | + cd server && yarn test:e2e diff --git a/infra/dev/postgres/init.sql b/infra/dev/postgres/init.sql index c777e4b91c4b7..ec152e4f7e3a6 100644 --- a/infra/dev/postgres/init.sql +++ b/infra/dev/postgres/init.sql @@ -1 +1,5 @@ +-- Create the default database for development CREATE DATABASE "default"; + +-- Create the tests database for e2e testing +CREATE DATABASE "tests"; diff --git a/server/.env.test b/server/.env.test index f88271200bc8c..e77a739cf6a3e 100644 --- a/server/.env.test +++ b/server/.env.test @@ -7,6 +7,6 @@ REFRESH_TOKEN_EXPIRES_IN=30d LOGIN_TOKEN_SECRET=secret_login_token LOGIN_TOKEN_EXPIRES_IN=15m FRONT_AUTH_CALLBACK_URL=http://localhost:3001/auth/callback -PG_DATABASE_URL=postgres://postgres:postgres@localhost:5432/default?connection_limit=1 +PG_DATABASE_URL=postgres://postgres:postgrespassword@localhost:5432/tests?connection_limit=1 STORAGE_TYPE=local STORAGE_LOCAL_PATH=.local-storage diff --git a/server/docker-compose.yml b/server/docker-compose.yml deleted file mode 100644 index a51949c3f8468..0000000000000 --- a/server/docker-compose.yml +++ /dev/null @@ -1,16 +0,0 @@ -# docker-compose.yml -version: '3.8' -services: - test-db: - image: postgres:14.1-alpine - restart: always - environment: - - POSTGRES_USER=postgres - - POSTGRES_PASSWORD=postgres - ports: - - '5432:5432' - volumes: - - test-db:/var/lib/postgresql/data -volumes: - test-db: - driver: local \ No newline at end of file diff --git a/server/scripts/run-integration.sh b/server/scripts/run-integration.sh index 591f7d9528654..56ca62c73ac89 100755 --- a/server/scripts/run-integration.sh +++ b/server/scripts/run-integration.sh @@ -1,6 +1,7 @@ #!/usr/bin/env bash # src/run-integration.sh +# wait-for-it.sh need coreutils to work properly # Check for MacOS if [[ "$OSTYPE" == "darwin"* ]]; then echo "🔵 MacOS detected." @@ -26,25 +27,35 @@ fi DIR="$(cd "$(dirname "$0")" && pwd)" source $DIR/setenv.sh -if [ -z "$(docker ps --filter "name=test-db" --filter "status=running" -q)" ]; then - docker-compose up -d +if [ -z "$(docker ps --filter "name=postgres" --filter "status=running" -q)" ]; then echo '🟡 - Waiting for database to be ready...' echo '🟡 - This may take a while...' echo "${PG_DATABASE_URL}" - $DIR/wait-for-it.sh "${PG_DATABASE_URL}" -- echo '🟢 - Database is ready!' + $DIR/wait-for-it.sh "${PG_DATABASE_URL}" + EXIT_CODE=$? + echo $EXIT_CODE + if [ $EXIT_CODE -ne 0 ]; then + echo '🔴 - Database connection failed!' + echo '🔴 - Please check if the database is running and accessible.' + echo '🔴 - If you are running the database in a container, please check if the container is running and accessible. ("make up" command should be run in "infra/dev" folder)' + echo '🔴 - Otherwise check if your local settings.' + echo '🔴 - Exiting...' + exit 1 + else + echo '🟢 - Database is ready!' + fi else echo "🟢 - Database container is already running." fi -if npx ts-node ./test/check-db.ts | grep -qw 1; then - echo "🟢 - Database is already initialized." -else +npx ts-node ./test/check-db.ts +EXIT_CODE=$? + +if [ $EXIT_CODE -ne 0 ]; then echo '🟡 - Database is not initialized. Running migrations...' npx prisma migrate reset --force && yarn prisma:generate +else + echo "🟢 - Database is already initialized." fi yarn jest --config ./test/jest-e2e.json - -echo '🟡 - Stopping the Docker container...' -docker-compose stop test-db -echo '🟢 - Docker container has been stopped.' diff --git a/server/scripts/setenv.sh b/server/scripts/setenv.sh index 91c019217ef89..29d74b2f4fd1c 100755 --- a/server/scripts/setenv.sh +++ b/server/scripts/setenv.sh @@ -9,7 +9,7 @@ ENV_PATH="${SCRIPT_DIR}/../.env.test" # Check if the file exists if [ -f "${ENV_PATH}" ]; then - echo "🟡 - Loading environment variables from "${ENV_PATH}"..." + echo "🔵 - Loading environment variables from "${ENV_PATH}"..." # Export env vars export $(grep -v '^#' ${ENV_PATH} | xargs) else diff --git a/server/test/check-db.ts b/server/test/check-db.ts index 7202119e0d527..d73833cfedc50 100644 --- a/server/test/check-db.ts +++ b/server/test/check-db.ts @@ -4,15 +4,37 @@ import { PrismaClient } from '@prisma/client'; const prisma = new PrismaClient(); +const schemaDatabaseExists = async (databaseName: string) => { + try { + const result = await prisma.$queryRawUnsafe<[any]>( + `SELECT 1 FROM pg_database WHERE datname = '${databaseName}';`, + ); + + return result.length > 0; + } catch { + return false; + } +}; + async function main() { - const result = - await prisma.$queryRaw`SELECT 1 FROM pg_tables WHERE tablename='_prisma_migrations';`; - console.log(result); + const databaseName = 'tests'; + // Check if schema exists + const databaseExistsResult = await schemaDatabaseExists(databaseName); + + if (!databaseExistsResult) { + throw new Error(`Schema ${databaseName} does not exist`); + } + + // Check if database is initialized + await prisma.$queryRaw`SELECT 1 FROM pg_tables WHERE tablename='_prisma_migrations';`; } main() - .catch((e) => { - throw e; + .then(() => { + process.exit(0); + }) + .catch(() => { + process.exit(1); }) .finally(async () => { await prisma.$disconnect(); diff --git a/server/test/company.e2e-spec.ts b/server/test/company.e2e-wip-spec.ts similarity index 100% rename from server/test/company.e2e-spec.ts rename to server/test/company.e2e-wip-spec.ts