diff --git a/Makefile b/Makefile index 3a2e8285d..39b05d12c 100644 --- a/Makefile +++ b/Makefile @@ -32,7 +32,7 @@ test: test-frontend test-backend test-mdbook test-e2e ## Test everything. pre-commit .PHONY: test-e2e -test-e2e: ## End-to-End tests. Needs backend and frontend running. +test-e2e: ## End-to-End tests. Needs install-e2e, backend and frontend running. cd e2e && ./e2e.sh .PHONY: test-frontend diff --git a/ci/Jenkinsfile b/ci/Jenkinsfile index 750c160db..9184cfcd8 100644 --- a/ci/Jenkinsfile +++ b/ci/Jenkinsfile @@ -276,9 +276,9 @@ lock("${env.NODE_NAME}-exclusive") { checkout scm def e2eImage = docker.build("permaplant-e2e:build", "./e2e") try { - e2eImage.inside("-e CI=1 -e E2E_URL=http://pr.permaplant.net") { + e2eImage.inside("-e E2E_URL=http://pr.permaplant.net") { wait_for_pr_db() - sh "make test-e2e" + sh 'make test-e2e' } } catch (err) { echo "Error occurred during the e2eImage.inside block: ${err}" diff --git a/e2e/.env b/e2e/.env deleted file mode 100644 index f87850447..000000000 --- a/e2e/.env +++ /dev/null @@ -1,11 +0,0 @@ -# Used in tests -E2E_URL=localhost:5173 -E2E_USERNAME=Adi -E2E_PASSWORD=1234 - -# Used in clean_db.py -POSTGRES_USER=permaplant -POSTGRES_DB=permaplant -POSTGRES_PASSWORD=permaplant -POSTGRES_HOST=db -POSTGRES_PORT=5432 diff --git a/e2e/.env.sample b/e2e/.env.sample new file mode 100644 index 000000000..74e6a95b0 --- /dev/null +++ b/e2e/.env.sample @@ -0,0 +1,3 @@ +E2E_URL=localhost:5173 +E2E_USERNAME=Adi +E2E_PASSWORD=1234 diff --git a/e2e/README.md b/e2e/README.md index e5bdb0a7b..19c7212f7 100644 --- a/e2e/README.md +++ b/e2e/README.md @@ -1,9 +1,5 @@ # PermaplanT E2E tests -The Jenkins pipeline executes exactly this [Dockerfile](Dockerfile) invoking [e2e.sh](e2e.sh). -It only skips the clean_db.py step, since its not needed in CI. -So running this setup locally should give you fast feedback. - All commands/scripts in this README are executed from this folder (/e2e). ## Directory structure @@ -17,10 +13,24 @@ All commands/scripts in this README are executed from this folder (/e2e). ├── test-results Screenshots, videos, etc. ``` +## Environment Variables + +All environment variables are optional, since they have defaults. + +- `E2E_URL` (Default: localhost:5173) + The url where the app is running. + +- `E2E_USERNAME` (Default: Adi) + The username to login to permaplant. + +- `E2E_PASSWORD` (Default: 1234) + The password to login to permaplant. + ## Quickstart -Make sure you have a virtual environment or you are inside [.devcontainer](../.devcontainer/README.md). -This will install all dependencies and run e2e tests with these [ENV](.env) variables. +- Make sure your app is running. +- Make sure the [ENV](#environment-variables) variables are set according to your desire. +- Make sure you have a virtual environment as this will install all python dependencies. ```sh ./install.sh @@ -48,20 +58,8 @@ docker build -t permaplant-e2e . docker run --network="host" permaplant-e2e ./e2e.sh ``` -If you have a more complicated network/database setup you might need to configure all env variables and use `docker run --network`. - -Lets say: - -- Your app/db are both inside a docker network called `plant` -- The web apps container name is `rudolph` -- The db is at postgres:5433 (for cleaning up after tests) - -```sh -docker run --network="plant" -e TEST_URL=rudolph:5173 -e POSTGRES_HOST=postgres -e POSTGRES_PORT=5433 permaplant-e2e -``` - -There is probably plenty of other setups, we haven't tested all. -Feel free to open an Issue/PR if something is not working. +The Jenkins pipeline performs exactly these two steps. +So running this dockerfile locally should mirror CI. ### Optional arguments diff --git a/e2e/clean_db.py b/e2e/clean_db.py index 5c5ae7378..c86cac6cf 100644 --- a/e2e/clean_db.py +++ b/e2e/clean_db.py @@ -7,14 +7,15 @@ import os import psycopg2 from dotenv import load_dotenv +from urllib.parse import urlparse -load_dotenv(verbose=True) +load_dotenv() -dbname = str(os.getenv("POSTGRES_DB")) -user = str(os.getenv("POSTGRES_USER")) -password = str(os.getenv("POSTGRES_PASSWORD")) -host = str(os.getenv("POSTGRES_HOST")) -port = str(os.getenv("POSTGRES_PORT")) +dbname = str(os.getenv("POSTGRES_DB", "permaplant")) +user = str(os.getenv("POSTGRES_USER", "permaplant")) +password = str(os.getenv("POSTGRES_PASSWORD", "permaplant")) +host = urlparse(os.environ.get("DATABASE_URL", "postgres://permaplant:permaplant@db/permaplant")).hostname +port = str(os.getenv("DATABASE_PORT", "5432")) def delete_maps_with_sut(dbname, user, password, host, port): diff --git a/e2e/e2e.sh b/e2e/e2e.sh index c852a01e0..eda7dec3a 100755 --- a/e2e/e2e.sh +++ b/e2e/e2e.sh @@ -1,10 +1,4 @@ #!/bin/bash -# Only run clean_db script -# when env variable CI is set -if [ -z "$CI" ]; then - python3 clean_db.py -fi - # Run the pytest command python3 -m pytest -n auto --retries 2 --video retain-on-failure --html=test-reports/report.html --self-contained-html --cucumberjson=test-reports/cucumber.json diff --git a/e2e/pages/home.py b/e2e/pages/home.py index f191d3840..ecfb227f1 100644 --- a/e2e/pages/home.py +++ b/e2e/pages/home.py @@ -5,7 +5,7 @@ class HomePage(AbstractPage): """The homepage permaplant""" - URL = os.getenv("E2E_URL") + URL = os.getenv("E2E_URL", "localhost:5173") TITLE: str = 'PermaplanT' HELLO_MSG: str = 'Hello adi' diff --git a/e2e/pages/login.py b/e2e/pages/login.py index 2df5210e7..85f039f44 100644 --- a/e2e/pages/login.py +++ b/e2e/pages/login.py @@ -13,14 +13,14 @@ def __init__(self, page: Page): self.password_field = page.get_by_label("Password") self.username_field = page.get_by_label("Username or email") - def fill_username(self, username=os.getenv("E2E_USERNAME")): + def fill_username(self, username=os.getenv("E2E_USERNAME", "Adi")): """ Fills the username field. Default to ENV variable. """ self.username_field.fill(username) - def fill_password(self, password=os.getenv("E2E_PASSWORD")): + def fill_password(self, password=os.getenv("E2E_PASSWORD", "1234")): """ Fills the password field. Default to ENV variable.