From ba98fde885b214a21f75642ad23fcb075aca0c32 Mon Sep 17 00:00:00 2001 From: Elio Bischof Date: Wed, 21 Jun 2023 19:32:17 +0200 Subject: [PATCH] specify environments explicitly --- .gitignore | 4 ++-- CONTRIBUTING.md | 42 ++++++++++++++++++++----------------- acceptance/setup.sh | 23 +------------------- apps/login/.env.integration | 1 + apps/login/mock/Dockerfile | 2 +- apps/login/package.json | 9 ++++---- pnpm-lock.yaml | 12 +++++++++++ 7 files changed, 45 insertions(+), 48 deletions(-) create mode 100644 apps/login/.env.integration diff --git a/.gitignore b/.gitignore index cf3fda36846..850558cd595 100644 --- a/.gitignore +++ b/.gitignore @@ -7,8 +7,8 @@ dist dist-ssr *.local .env -apps/login/.env.local.?.bak -apps/login/.env.local.??.bak +apps/login/.env.local +apps/login/.env.acceptance .cache server/dist public/dist diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 7d7e052c6f1..a3f0e7767b4 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -38,21 +38,6 @@ However, it might be easier to develop against your ZITADEL Cloud instance if you don't have docker installed or have limited resources on your local machine. -### Testing - -You can execute the following commands `pnpm test` for a single test run or `pnpm test:watch` in the following directories: - -- apps/login -- packages/zitadel-client -- packages/zitadel-server -- packages/zitadel-react -- packages/zitadel-next -- The projects root directory: all tests in the project are executed - -In apps/login, these commands also spin up the application and a ZITADEL gRPC API mock server to run integration tests using [Cypress](https://www.cypress.io/) against them. -If you want to run the integration tests standalone against an environment of your choice, navigate to ./apps/login, create your .env.local file and run `pnpm test:integration:run` or `pnpm test:integration:open`. -Then you need to lifecycle the mock process using the command `pnpm mock` or the more fine grained commands `pnpm mock:build`, `pnpm mock:build:nocache`, `pnpm mock:run` and `pnpm mock:destroy`. - ### Developing Against Your Local ZITADEL Instance ```sh @@ -64,15 +49,18 @@ docker compose --file ./acceptance/docker-compose.yaml pull # Run ZITADEL and configure ./apps/login/.env.local docker compose --file ./acceptance/docker-compose.yaml run setup + +# Configure your shell to use the environment variables written to ./apps/login/.env.acceptance +source ./apps/login/.env.acceptance ``` ### Developing Against Your ZITADEL Cloud Instance -Create the file ./apps/login/.env.local with the following content: +Configure your shell by exporting the following environment variables: ```sh -ZITADEL_API_URL= -ZITADEL_ORG_ID= -ZITADEL_SERVICE_USER_TOKEN= +export ZITADEL_API_URL= +export ZITADEL_ORG_ID= +export ZITADEL_SERVICE_USER_TOKEN= ``` ### Setting up local environment @@ -90,4 +78,20 @@ pnpm dev The application is now available at `http://localhost:3000` +### Testing + +You can execute the following commands `pnpm test` for a single test run or `pnpm test:watch` in the following directories: + +- apps/login +- packages/zitadel-client +- packages/zitadel-server +- packages/zitadel-react +- packages/zitadel-next +- The projects root directory: all tests in the project are executed + +In apps/login, these commands also spin up the application and a ZITADEL gRPC API mock server to run integration tests using [Cypress](https://www.cypress.io/) against them. +If you want to run the integration tests standalone against an environment of your choice, navigate to ./apps/login, [configure your shell as you like](# Developing Against Your ZITADEL Cloud Instance) and run `pnpm test:integration:run` or `pnpm test:integration:open`. +Then you need to lifecycle the mock process using the command `pnpm mock` or the more fine grained commands `pnpm mock:build`, `pnpm mock:build:nocache`, `pnpm mock:run` and `pnpm mock:destroy`. + + That's it! 🎉 diff --git a/acceptance/setup.sh b/acceptance/setup.sh index 95f0bb4fd01..d3db4f25547 100755 --- a/acceptance/setup.sh +++ b/acceptance/setup.sh @@ -11,7 +11,7 @@ echo "Using audience ${AUDIENCE} for which the key is used." SERVICE=${SERVICE:-$AUDIENCE} echo "Using the service ${SERVICE} to connect to ZITADEL. For example in docker compose this can differ from the audience." -WRITE_ENVIRONMENT_FILE=${WRITE_ENVIRONMENT_FILE:-$(dirname "$0")/../apps/login/.env.local} +WRITE_ENVIRONMENT_FILE=${WRITE_ENVIRONMENT_FILE:-$(dirname "$0")/../apps/login/.env.acceptance} echo "Writing environment file to ${WRITE_ENVIRONMENT_FILE} when done." AUDIENCE_HOST="$(echo $AUDIENCE | cut -d/ -f3)" @@ -44,27 +44,6 @@ echo "${ORG_RESPONSE}" | jq ORG_ID=$(echo -n ${ORG_RESPONSE} | jq --raw-output '.org.id') echo "Extracted default org id ${ORG_ID}" -ENVIRONMENT_BACKUP_FILE=${WRITE_ENVIRONMENT_FILE} -# If the original file already exists, rename it -if [[ -e ${WRITE_ENVIRONMENT_FILE} ]]; then - if grep -q 'localhost' ${WRITE_ENVIRONMENT_FILE}; then - echo "Current environment file ${WRITE_ENVIRONMENT_FILE} contains localhost. Overwriting:" - cat ${WRITE_ENVIRONMENT_FILE} - else - i=0 - # If a backup file already exists, increment counter until a free filename is found - while [[ -e ${ENVIRONMENT_BACKUP_FILE}.${i}.bak ]]; do - let "i++" - if [[ ${i} -eq 50 ]]; then - echo "Warning: Too many backup files (limit is 50), overwriting ${ENVIRONMENT_BACKUP_FILE}.${i}.bak" - break - fi - done - mv ${WRITE_ENVIRONMENT_FILE} ${ENVIRONMENT_BACKUP_FILE}.${i}.bak - echo "Renamed existing environment file to ${ENVIRONMENT_BACKUP_FILE}.${i}.bak" - fi -fi - echo "ZITADEL_API_URL=${AUDIENCE} ZITADEL_ORG_ID=${ORG_ID} ZITADEL_SERVICE_USER_TOKEN=${TOKEN}" > ${WRITE_ENVIRONMENT_FILE} diff --git a/apps/login/.env.integration b/apps/login/.env.integration new file mode 100644 index 00000000000..44e8f7b6bf8 --- /dev/null +++ b/apps/login/.env.integration @@ -0,0 +1 @@ +ZITADEL_API_URL=http://localhost:22222 \ No newline at end of file diff --git a/apps/login/mock/Dockerfile b/apps/login/mock/Dockerfile index c6477e2db02..e951cfc99d5 100644 --- a/apps/login/mock/Dockerfile +++ b/apps/login/mock/Dockerfile @@ -17,4 +17,4 @@ RUN go install github.com/eliobischof/grpc-mock/cmd/grpc-mock@4de05333a51075cb3a COPY --from=config / . -ENTRYPOINT [ "sh", "-c", "grpc-mock -proto $(tr '\n' ',' < ./mocked-services.cfg) -stub-dir ./initial-stubs" ] +ENTRYPOINT [ "sh", "-c", "grpc-mock -v 1 -proto $(tr '\n' ',' < ./mocked-services.cfg) -stub-dir ./initial-stubs" ] diff --git a/apps/login/package.json b/apps/login/package.json index e2bd308bf82..c03618c7af1 100644 --- a/apps/login/package.json +++ b/apps/login/package.json @@ -3,12 +3,12 @@ "private": true, "scripts": { "dev": "next dev", - "test": "concurrently --group --timings --kill-others-on-fail 'npm:test:unit' 'npm:test:integration'", + "test": "concurrently --timings --kill-others-on-fail 'npm:test:unit' 'npm:test:integration'", "test:watch": "concurrently --kill-others 'npm:test:unit:watch' 'npm:test:integration:watch'", "test:unit": "jest --config ./__test__/jest.config.ts", "test:unit:watch": "pnpm test:unit --watch", - "test:integration": "export ZITADEL_API_URL=http://localhost:22222; pnpm mock:build && concurrently --group --names 'mock,test' --success command-test --kill-others 'pnpm:mock' 'start-server-and-test start http://localhost:3000 \"test:integration:run\"'", - "test:integration:watch": "concurrently --names 'mock,test' --kill-others 'pnpm:mock' 'ZITADEL_API_URL=http://localhost:22222 start-server-and-test dev http://localhost:3000 \"pnpm nodemon -e js,jsx,ts,tsx,css,scss --ignore \\\"__test__/**\\\" --exec \\\"pnpm test:integration:run\\\"\"'", + "test:integration": "pnpm mock:build && concurrently --names 'mock,test' --success command-test --kill-others 'pnpm:mock' 'env-cmd -f ./.env.integration start-server-and-test start http://localhost:3000 \"test:integration:run\"'", + "test:integration:watch": "concurrently --names 'mock,test' --kill-others 'pnpm:mock' 'env-cmd -f ./.env.integration start-server-and-test dev http://localhost:3000 \"pnpm nodemon -e js,jsx,ts,tsx,css,scss --ignore \\\"__test__/**\\\" --exec \\\"pnpm test:integration:run\\\"\"'", "test:integration:run": "cypress run --config-file ./cypress/cypress.config.ts --quiet", "test:integration:open": "cypress open --config-file ./cypress/cypress.config.ts", "mock": "pnpm mock:build && pnpm mock:run", @@ -69,6 +69,7 @@ "concurrently": "^8.1.0", "cypress": "^12.14.0", "del-cli": "5.0.0", + "env-cmd": "^10.1.0", "eslint-config-zitadel": "workspace:*", "grpc-tools": "1.11.3", "jest": "^29.5.0", @@ -87,4 +88,4 @@ "typescript": "5.0.4", "zitadel-tailwind-config": "workspace:*" } -} +} \ No newline at end of file diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d8a14db19b8..b259c0f3b98 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -135,6 +135,9 @@ importers: del-cli: specifier: 5.0.0 version: 5.0.0 + env-cmd: + specifier: ^10.1.0 + version: 10.1.0 eslint-config-zitadel: specifier: workspace:* version: link:../../packages/eslint-config-zitadel @@ -3394,6 +3397,15 @@ packages: engines: {node: '>=0.12'} dev: true + /env-cmd@10.1.0: + resolution: {integrity: sha512-mMdWTT9XKN7yNth/6N6g2GuKuJTsKMDHlQFUDacb/heQRRWOTIZ42t1rMHnQu4jYxU1ajdTeJM+9eEETlqToMA==} + engines: {node: '>=8.0.0'} + hasBin: true + dependencies: + commander: 4.1.1 + cross-spawn: 7.0.3 + dev: true + /error-ex@1.3.2: resolution: {integrity: sha512-7dFHNmqeFSEt2ZBsCriorKnn3Z2pj+fd9kmI6QoWw4//DL+icEBfc0U7qJCisqrTsKTjw4fNFy2pW9OqStD84g==} dependencies: