Skip to content

Commit

Permalink
🪚 OmniGraph™ Better docker setup (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
janjakubnanista authored Dec 4, 2023
1 parent 8d2c311 commit 3f431da
Show file tree
Hide file tree
Showing 14 changed files with 164 additions and 119 deletions.
35 changes: 18 additions & 17 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ FROM node:18.16.0 as base

ENV YARN_CACHE_FOLDER=/tmp/yarn_cache

# We'll need a mock NPM_TOKEN to execute any yarn commands
ENV NPM_TOKEN=

# Update the system packages
RUN apt-get update
RUN apt-get install -y \
# Get the envsubst command (see below)
gettext-base
gettext-base \
# Get the json utilities
jq

# .-.-. .-.-. .-.-. .-.-. .-.-. .-.-. .-.-. .-.-
# / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \
Expand All @@ -28,11 +33,6 @@ RUN apt-get install -y \
# `-' `-`-' `-`-' `-`-' `-`-' `-`-' `-`-' `-`-'
FROM base as builder

# The name of the package we're building here
#
# e.g. @layerzerolabs/ua-utils-evm-hardhat
ARG PACKAGE

WORKDIR /app

# We'll only use this turbo to prune the workspace, we don't care what version we use here
Expand All @@ -47,7 +47,14 @@ COPY . .
#
# See more here https://turbo.build/repo/docs/reference/command-line-reference/prune
# And here https://turbo.build/repo/docs/handbook/deploying-with-docker
RUN turbo prune $PACKAGE --docker
#
# There's an open issue on turbo github to support pruning
# without scope, in the meantime we'll use yarn workspaces info
# in combination with jq to get a full list of the workspace packages
# and prefix them with --scope
#
# See https://github.com/vercel/turbo/issues/4074
RUN turbo prune $(yarn workspaces --silent info | jq -r 'keys | map("--scope " + .) | join(" ")') --docker

# .-.-. .-.-. .-.-. .-.-. .-.-. .-.-. .-.-. .-.-
# / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \
Expand Down Expand Up @@ -99,25 +106,19 @@ RUN \
# / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \
# `-' `-`-' `-`-' `-`-' `-`-' `-`-' `-`-' `-`-'
#
# Image that builds the package
# Image that prepares the project for development
#
# .-.-. .-.-. .-.-. .-.-. .-.-. .-.-. .-.-. .-.-
# / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \ \ / / \
# `-' `-`-' `-`-' `-`-' `-`-' `-`-' `-`-' `-`-'
FROM dependencies as build
FROM dependencies as development

# The name of the package we're building here
#
# e.g. @layerzerolabs/ua-utils-evm-hardhat
ARG PACKAGE
ENV NPM_TOKEN=

WORKDIR /app

# For some reason we're missing tsconfig.json when using turbo prune
COPY tsconfig.json ./

# Now we grab the full source code from the builder step
COPY --from=builder /app/out/full/ .

# And finally we build the package
RUN yarn build --filter=$PACKAGE...
COPY --from=builder /app/out/full/ .
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,46 @@ This project is built using `turborepo`. The above commands are just aliases to
yarn dev --filter=create-lz-oapp...
```

### Running tests

The tests are by default executed in a containerized environment that sets up two `hardhat` nodes accessible from within the environment:

- `http://network-britney:8545`
- `http://network-vengaboys:8545`

You can run the whole test suite within this environment by running:

```bash
yarn test
```

#### Refining tested packages

To only run a specific test suite, you can define `DOCKER_COMPOSE_RUN_TESTS_TURBO_ARGS` environment variable before running the tests. This variable will be passed to the underlying `turbo` command and can contain any arguments that this command understands, for example:

```bash
# To only run tests for @layerzerolabs/ua-utils-evm-hardhat-test package
DOCKER_COMPOSE_RUN_TESTS_TURBO_ARGS=--filter=ua-utils-evm-hardhat-test yarn test
```

#### Rebuilding containers

`docker compose` will by default reuse images for containers it has already built. If by any chance you are seeing code changes not being reflected in your test runs, you can force docker to rebuild the images by defining `DOCKER_COMPOSE_RUN_TESTS_ARGS` environment variable. This variable will be passed to the underlying `docker compose run` command and can contain any arguments that this command understands, for example:

```bash
DOCKER_COMPOSE_RUN_TESTS_ARGS=--build yarn test
```

#### Container logs

To monitor the container logs you'll need to run:

```bash
docker compose logs -f
```

This allows you to monitor logs coming from e.g. the `hardhat` nodes

### Troubleshooting

#### Problems with committing
Expand Down
31 changes: 30 additions & 1 deletion docker-compose.templates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,34 @@ services:
build:
context: .
args:
PACKAGE:
# In order not to embed the NPM_TOKEN to the image, we'll pass it as an build argument
#
# This works in conjunction with the adjacent Dockerfile that:
#
# - interpolates .npmrc with this argument
# - installs the depenendencies
# - deletes the interpolated file
#
# This all needs to be done within one layer so that there is no way of reverse engineering
# the Dockerfile and getting the NPM_TOKEN out
NPM_TOKEN: $NPM_TOKEN
# We'll provide a single testing MNEMONIC for the project so that the test EVM nodes
# account are in sync with the hardhat accounts
environment:
- MNEMONIC='test test test test test test test test test test test test'

# EVM node for testing purposes
evm-node:
extends:
service: project
command: ["npx", "turbo", "start", "--filter", "test-evm-node"]
healthcheck:
interval: 2s
retries: 10
test: ["CMD", "curl", "-f", "http://0.0.0.0:8545/"]

# Service that can build this package
tests:
extends:
service: project
working_dir: /app/packages/ua-utils-evm-hardhat-test
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,28 @@ services:
network-vengaboys:
extends:
file: docker-compose.templates.yaml
service: node
service: evm-node

network-britney:
extends:
file: docker-compose.templates.yaml
service: node
service: evm-node

# ~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~
#
# Bootstrap script that deploys all the networks
# The actual tests
#
# .oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.
bootstrap:
tests:
extends:
file: docker-compose.templates.yaml
service: package
service: project
depends_on:
network-vengaboys:
condition: service_healthy
network-britney:
condition: service_healthy
# Can't say I love the fact that the deploy scripts are inlined here
# but at least it's all in this file that bootstraps the whole environment
command:
- /bin/bash
- -c
- |
npx hardhat --network vengaboys deploy --reset
npx hardhat --network britney deploy --reset
# ~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~^~
#
# The actual tests
#
# .oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.oOo.
tests:
extends:
file: docker-compose.templates.yaml
service: package
depends_on:
bootstrap:
condition: service_completed_successfully
command: ["npx", "hardhat", "test"]
volumes:
- ./node_modules/.cache/turbo:/app/node_modules/.cache/turbo
- ./packages:/app/packages
command: "npx turbo test $DOCKER_COMPOSE_RUN_TESTS_TURBO_ARGS"
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"dev": "npx turbo run dev",
"lint": "npx turbo run lint",
"prepare": "husky install",
"test": "npx turbo run test"
"test": "docker compose run --rm $DOCKER_COMPOSE_RUN_TESTS_ARGS tests"
},
"lint-staged": {
"**/*": "npx prettier --write --ignore-unknown"
Expand Down
21 changes: 21 additions & 0 deletions packages/test-evm-node/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import assert from 'assert'
import { HardhatUserConfig } from 'hardhat/types'

const MNEMONIC = process.env.MNEMONIC
assert(MNEMONIC, `Missing MNEMONIC environment variable`)

/**
* This is a dummy hardhat config that enables us to test
* hardhat functionality without mocking too much
*/
const config: HardhatUserConfig = {
networks: {
hardhat: {
accounts: {
mnemonic: MNEMONIC,
},
},
},
}

export default config
21 changes: 21 additions & 0 deletions packages/test-evm-node/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "@layerzerolabs/test-evm-node",
"version": "0.0.1",
"private": true,
"description": "EVM node for E2E testing purposes",
"repository": {
"type": "git",
"url": "git+https://github.com/LayerZero-Labs/lz-utils.git",
"directory": "packages/test-evm-node"
},
"license": "MIT",
"scripts": {
"lint": "npx eslint '**/*.{js,ts,json}'",
"start": "npx hardhat node --hostname 0.0.0.0"
},
"devDependencies": {
"hardhat": "^2.19.0",
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
}
}
9 changes: 9 additions & 0 deletions packages/test-evm-node/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"exclude": ["dist", "node_modules"],
"include": ["src", "*.config.ts"],
"compilerOptions": {
"module": "commonjs",
"types": ["node"]
}
}
31 changes: 2 additions & 29 deletions packages/ua-utils-evm-hardhat-test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,6 @@

## Development

This package provides integration tests for `@layerzerolabs/utils-evm-hardhat` executed within a containerized setup. To run the test suite, simply run:
This package provides integration tests for `@layerzerolabs/utils-evm-hardhat` executed within a containerized setup. These tests are dependent on two networks that are bootstrapped in the root `docker-compose.yaml` and will only run when executed within that environment.

```bash
# You can use the alias command from this package directory
yarn test

# Or use turbo and run from project root
yarn test --filter=utils-evm-hardhat-test

# Or just use the actual test command from this package directory
docker compose run --rm tests
```

In case you're running the tests from the project root, it might sometimes be useful to rebuild the containers
(for example when adding/removing dependencies) and as a lazy developer, you might not be happy about `cd`ing to the package directory
and run the command from there. For that usecase the `$DOCKER_COMPOSE_RUN_TESTS_ARGS` environment variable has been added:

```bash
# To rebuild the containers before running tests from the project root
DOCKER_COMPOSE_RUN_TESTS_ARGS=--build yarn test --filter=utils-evm-hardhat-test
```

To monitor the container logs, you'll need to `cd` into the package directory and run:

```bash
docker compose logs -f
```

This allows you to see any logs coming from the containers, including detailed logs from the `hardhat` nodes
.
Please see the root [`README.md`](../../README.md) for more information.
35 changes: 0 additions & 35 deletions packages/ua-utils-evm-hardhat-test/docker-compose.templates.yaml

This file was deleted.

9 changes: 3 additions & 6 deletions packages/ua-utils-evm-hardhat-test/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import 'hardhat-deploy'
import 'hardhat-deploy-ethers'
import assert from 'assert'
import { withLayerZeroArtifacts } from '@layerzerolabs/utils-evm-hardhat'
import { EndpointId } from '@layerzerolabs/lz-definitions'
import { HardhatUserConfig } from 'hardhat/types'

const MNEMONIC = 'test test test test test test test test test test test test'
const MNEMONIC = process.env.MNEMONIC
assert(MNEMONIC, `Missing MNEMONIC environment variable`)

/**
* This is a dummy hardhat config that enables us to test
Expand All @@ -15,11 +17,6 @@ const config: HardhatUserConfig = {
version: '0.8.19',
},
networks: {
hardhat: {
accounts: {
mnemonic: MNEMONIC,
},
},
vengaboys: {
eid: EndpointId.ETHEREUM_MAINNET,
url: 'http://network-vengaboys:8545',
Expand Down
3 changes: 2 additions & 1 deletion packages/ua-utils-evm-hardhat-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"license": "MIT",
"scripts": {
"lint": "npx eslint '**/*.{js,ts,json}'",
"test": "docker compose run --rm $DOCKER_COMPOSE_RUN_TESTS_ARGS tests"
"pretest": "npx hardhat --network vengaboys deploy --reset && npx hardhat --network britney deploy --reset",
"test": "npx hardhat test"
},
"devDependencies": {
"@ethersproject/abstract-signer": "^5.7.0",
Expand Down
Loading

0 comments on commit 3f431da

Please sign in to comment.