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

Add docker-based setup for running test workflows #274

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.env
docker-data/
output/
6 changes: 6 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM n8nio/base:20
COPY ./entrypoint.sh /entrypoint.sh
RUN chown -R node /entrypoint.sh
USER node
RUN mkdir -p ~/.pnpm-store && pnpm config set store-dir ~/.pnpm-store --global
ENTRYPOINT ["/entrypoint.sh"]
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,58 @@
# test-workflows
n8n workflows used for testing nodes

## Docker config

Copy `dot-env.example` to `.env` and add the encryption key there. Also make
sure the n8n repository is checked out at `../n8n`. Then you can run the tests
with:


```
COMMAND=test docker compose up --exit-code-from n8n
```

This will create some files and folders in `docker-data/` and an `output/`
folder. The tests will write the results to `output/test-results.json`.

`output/dot-n8n/` will be mounted to `~/.n8n/` and will contain the database,
which is useful for debugging or when you have started the dev-server (see
below).

### Running dev server for creating workflows

The following command will start a dev-server:

```
COMMAND=dev docker compose up --exit-code-from n8n
```

With this server up, you can create your workflows and/or credentials. Then,
with a new terminal, start a session in the container with:

```
docker compose exec n8n /bin/sh
```

This allows you to export workflows to `/output/`:

```
/n8n/packages/cli/bin/n8n export:workflow --backup --id zFePuiBkF9nlktTH --pretty --separate --output /output/workflow
```

This creates the `output/workflow/` directory and exports the workflow
specified there. Any other `n8n` CLI command works similarly.

### Creating snapshots

For snapshot, a separate helper command has been created:

```
COMMAND="snapshot SNAPSHOT_ID" dc up --exit-code-from n8n

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
COMMAND="snapshot SNAPSHOT_ID" dc up --exit-code-from n8n
COMMAND="snapshot SNAPSHOT_ID" docker compose up --exit-code-from n8n

```

You can use multiple, comma-separated snapshot ids like so:

```
COMMAND="snapshot 251,252" dc up --exit-code-from n8n

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
COMMAND="snapshot 251,252" dc up --exit-code-from n8n
COMMAND="snapshot 251,252" docker compose up --exit-code-from n8n

```
24 changes: 24 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
services:
n8n:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we want everything in docker here? I thought what we wanted was to run only external services in docker.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Locally, I'd also love to run n8n in docker, so that I don't need to tear down my n8n home instance. Of course I can do that differently, but this means that you don't forget when running your tests.
Do you think we should remove it from here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plus: this makes it easy to create your test-workflows inside the docker; makes it easy to connect over the local docker-network, etc.

build:
context: .
dockerfile: Dockerfile
env_file: ".env"
volumes:
- ../n8n:/n8n
- ./:/test-workflows
- ./output:/output
- ./docker-data/dot-n8n:/home/node/.n8n

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to add this variable to make it work for me, without it it fails on git hooks which are disabled for docker

environment:
      - DOCKER_BUILD=true 

ports:
- 5678:5678
depends_on:
- pgvector
command: ${COMMAND:-test}
pgvector:
image: pgvector/pgvector:pg16
volumes:
- ./docker-data/pgvector:/var/lib/postgresql/data
environment:
- POSTGRES_PASSWORD=password
- POSTGRES_USER=pgvector
- POSTGRES_DB=pgvector
10 changes: 10 additions & 0 deletions dot-env-example
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
ENCRYPTION_KEY=test-workflow-encryption-key

## Set the logging level to 'debug'
#N8N_LOG_LEVEL=debug
#
## Set log output to both console and a log file
#N8N_LOG_OUTPUT=console,file
#
## Set a save location for the log file
#N8N_LOG_FILE_LOCATION=/output/n8n.log
50 changes: 50 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/sh
set -e
mkdir ~/.n8n 2>/dev/null || true
echo '{ "encryptionKey": "'$ENCRYPTION_KEY'" }' > ~/.n8n/config

cd /n8n

export DOCKER_BUILD=true

echo installing dependencies
#rm -rf node_modules
rm -rf **/node_modules **/dist **/.turbo
echo | pnpm clean || echo "skipping clean"
# doing the echo to make the command skip asking questions...
echo | pnpm install

echo building n8n
pnpm build
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could save some time by skipping frontend build using pnpm build:backend

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by building inside docker (instead of directly on the CI VM), we can't use the turbo build cache. We also can't reuse a built version of n8n from a previous step in the CI.

perhaps we should update this script to assume that n8n is already built when these tests start?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I can remove that step!


# Follows the set-up steps from Notion.
echo preparing test data
cp assets/* /tmp
cp node_modules/pdf-parse/test/data/04-valid.pdf /tmp
cp node_modules/pdf-parse/test/data/05-versions-space.pdf /tmp

packages/cli/bin/n8n import:workflow --separate --input=/test-workflows/workflows
packages/cli/bin/n8n import:credentials --input=/test-workflows/credentials.json

# Check the command argument
case "$1" in
"snapshot")
echo "Creating snapshots"
mkdir -p /output/snapshots
if [ -z "$2" ]; then
echo "No IDs specified. Using default ID 252."
SNAPSHOT_IDS_ARG=""
else
SNAPSHOT_IDS_ARG="--ids=$2"
fi
packages/cli/bin/n8n executeBatch --shallow --retries=0 --snapshot=/output/snapshots --debug $SNAPSHOT_IDS_ARGz
;;
"dev")
echo "Starting development server"
pnpm run dev
;;
"test"|*)
echo "Running tests"
packages/cli/bin/n8n executeBatch --concurrency=16 --shortOutput --debug --compare=../test-workflows/snapshots --output=/output/test-results.json --shallow --skipList=../test-workflows/skipList.txt
;;
esac
2 changes: 1 addition & 1 deletion skipList.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1,4,5,10,19,20,21,22,26,27,28,29,30,31,33,34,38,39,40,41,42,43,45,46,47,49,50,51,54,56,57,59,64,65,66,68,69,72,73,74,75,76,77,78,79,80,82,85,89,92,94,102,106,109,110,112,113,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,134,133,135,136,137,138,139,141,142,144,145,146,147,148,149,151,157,158,159,160,163,164,165,167,168,169,170,171,173,176,179,180,183,184,185,186,187,188,189,190,191,192,193,196,197,198,199,200,201,204,206,207,208,214,215,217,218,219,220,221,222,224,225,226,227,252
1,4,5,10,19,20,21,22,26,27,28,29,30,31,33,34,38,39,40,41,42,43,45,46,47,49,50,51,54,56,57,59,64,65,66,68,69,72,73,74,75,76,77,78,79,80,82,85,89,92,94,102,106,109,110,112,113,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,134,133,135,136,137,138,139,141,142,144,145,146,147,148,149,151,157,158,159,160,163,164,165,167,168,169,170,171,173,176,179,180,183,184,185,186,187,188,189,190,191,192,193,196,197,198,199,200,201,204,206,207,208,214,215,217,218,219,220,221,222,224,225,226,227

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you 🙏