Skip to content

Commit

Permalink
Workflow: tweak test-local-subgraph (#2013)
Browse files Browse the repository at this point in the history
**Problem**

At some point, the sdk-core/subgraph test cases started to fail with "socket hang up" error when querying subgraph.

After some research, it was discovered that it is due to the usage of an older version of the "cross-fetch" polyfill which didn't support node 20+, see [here](graffle-js/graffle#628) and [here](graffle-js/graffle#597).

**Solution**

The newer cross-fetch project solved this. The solution is to fix the "resolution" of the cross-fetch in this project to the latest, for now.

Additionally, this PR also:

a) include some minor refactoring around the test setup process,
b) test against both node 18 and 20 for SDK core, and test against node 20 for subgraph.
  • Loading branch information
hellwolf authored Aug 29, 2024
1 parent 04f7b3c commit 3d5df70
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 145 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/call.test-ethereum-contracts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,10 @@ jobs:
- name: Set cache key prefix
id: set-cache-key-prefix
run: |
node_version=${{ matrix.node-version }}
if [ "${{ inputs.run-coverage-tests }}" == false ]; then
v=ethereum-contracts-test-${node_version}-
v=ethereum-contracts-${{ env.DEV_SHELL_NAME }}-test-
else
v=ethereum-contracts-coverage-${node_version}-
v=ethereum-contracts-${{ env.DEV_SHELL_NAME }}-coverage-
fi
echo "cache_key_prefix=$v" >> "$GITHUB_OUTPUT"
shell: bash
Expand Down
47 changes: 18 additions & 29 deletions .github/workflows/call.test-local-subgraph.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ on:
workflow_call:

env:
# FIXME: subgraph test fails with node20
DEV_SHELL_NAME: ci-node18
DEV_SHELL_NAME: ci-node20
ethereum-contracts-working-directory: ./packages/ethereum-contracts
sdk-core-working-directory: ./packages/sdk-core
subgraph-working-directory: ./packages/subgraph

jobs:
subgraph-unit-tests:
name: Run subgraph unit tests
runs-on: ubuntu-latest

env:
subgraph-working-directory: ./packages/subgraph

defaults:
run:
shell: nix develop .#${{ env.DEV_SHELL_NAME }} -c bash -xe {0}
Expand All @@ -27,7 +26,7 @@ jobs:
uses: ./.github/actions/setup-workflow-base
with:
package-name: subgraph
cache-key-prefix: subgraph-unit-tests-
cache-key-prefix: subgraph-${{ env.DEV_SHELL_NAME }}-unit-tests-
dev-shell-name: ${{ env.DEV_SHELL_NAME }}

- name: Install dependencies
Expand All @@ -42,7 +41,7 @@ jobs:
run: |
yarn lint
yarn build
working-directory: ./packages/ethereum-contracts
working-directory: ${{ env.ethereum-contracts-working-directory }}

- name: Run unit tests
if: steps.base.outputs.cache-hit != 'true'
Expand All @@ -54,10 +53,6 @@ jobs:

runs-on: ubuntu-latest

env:
subgraph-working-directory: ./packages/subgraph
sdk-core-working-directory: ./packages/sdk-core

defaults:
run:
shell: nix develop .#${{ env.DEV_SHELL_NAME }} -c bash -xe {0}
Expand All @@ -70,7 +65,7 @@ jobs:
uses: ./.github/actions/setup-workflow-base
with:
package-name: subgraph
cache-key-prefix: subgraph-integration-tests-
cache-key-prefix: subgraph-${{ env.DEV_SHELL_NAME }}-integration-tests-
dev-shell-name: ${{ env.DEV_SHELL_NAME }}

- name: Install dependencies
Expand All @@ -80,41 +75,35 @@ jobs:
- name: Build contracts
if: steps.base.outputs.cache-hit != 'true'
run: yarn build
working-directory: ./packages/ethereum-contracts

- name: Start hardhat node
if: steps.base.outputs.cache-hit != 'true'
run: ./tasks/startHardhatNode.sh start
working-directory: ${{ env.sdk-core-working-directory }}
working-directory: ${{ env.ethereum-contracts-working-directory }}

- name: Build SDK core
if: steps.base.outputs.cache-hit != 'true'
# build sdk-core because subgraph tests use sdk-core
run: yarn build
working-directory: ${{ env.sdk-core-working-directory }}

- name: Start hardhat node
if: steps.base.outputs.cache-hit != 'true'
run: ./tasks/startHardhatNode.sh start
working-directory: ${{ env.sdk-core-working-directory }}

- name: Deploy framework and tokens
if: steps.base.outputs.cache-hit != 'true'
run: npx hardhat run dev-scripts/run-deploy-contracts-and-token.js --network localhost
working-directory: ./packages/ethereum-contracts
working-directory: ${{ env.ethereum-contracts-working-directory }}

- name: Prepare files for local testing
if: steps.base.outputs.cache-hit != 'true'
run: yarn prepare-local
working-directory: ${{ env.subgraph-working-directory }}

- name: Run setup-graph-node
if: steps.base.outputs.cache-hit != 'true'
run: |
chmod +x ./tasks/setup-graph-node.sh
./tasks/setup-graph-node.sh
working-directory: ${{ env.subgraph-working-directory }}

- name: Docker compose
- name: Setup graph node
if: steps.base.outputs.cache-hit != 'true'
run: |
docker rm subgraph_graph-node_1 || true
docker compose up &
./tasks/setup-graph-node.sh &
# wait for it to warm up
sleep 30
working-directory: ${{ env.subgraph-working-directory }}

- name: Run subgraph integration test suite
Expand Down
42 changes: 15 additions & 27 deletions .github/workflows/call.test-sdk-core.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ jobs:
id: set-matrix
run: |
if [ "${{ inputs.run-coverage-tests }}" == "true" ];then
echo "matrix={\"node-version\": [18]}" >> "$GITHUB_OUTPUT"
echo "matrix={\"node-version\": [20]}" >> "$GITHUB_OUTPUT"
else
# FIXME: subgraph test fails with node20
echo "matrix={\"node-version\": [18]}" >> "$GITHUB_OUTPUT"
echo "matrix={\"node-version\": [18, 20]}" >> "$GITHUB_OUTPUT"
fi
test-sdk-core:
Expand Down Expand Up @@ -63,11 +62,10 @@ jobs:
- name: Set cache key prefix
id: set-cache-key-prefix
run: |
node_version=${{ matrix.node-version }}
if [ "${{ inputs.run-coverage-tests }}" == false ]; then
v=sdk-core-test-${node_version}-
v=sdk-core-${{ env.DEV_SHELL_NAME }}-test-
else
v=sdk-core-coverage-${node_version}-
v=sdk-core-${{ env.DEV_SHELL_NAME }}-coverage-
fi
echo "cache_key_prefix=$v" >> "$GITHUB_OUTPUT"
shell: bash
Expand All @@ -87,19 +85,12 @@ jobs:
npx tsc --version
npx hardhat --version
- name: Lint and build essentials
- name: Build contracts
if: steps.base.outputs.cache-hit != 'true'
run: |
yarn lint
yarn build
working-directory: ${{ env.ethereum-contracts-working-directory }}

- name: Start hardhat node
if: steps.base.outputs.cache-hit != 'true'
run: |
./tasks/startHardhatNode.sh start
working-directory: ${{ env.sdk-core-working-directory }}

- name: Build SDK Core
if: steps.base.outputs.cache-hit != 'true'
# build sdk-core because of auto linking to dependency
Expand All @@ -108,7 +99,12 @@ jobs:
yarn build
working-directory: ${{ env.sdk-core-working-directory }}

- name: Deploy Framework and Tokens
- name: Start hardhat node
if: steps.base.outputs.cache-hit != 'true'
run: ./tasks/startHardhatNode.sh start
working-directory: ${{ env.sdk-core-working-directory }}

- name: Deploy framework and tokens
if: steps.base.outputs.cache-hit != 'true'
run: npx hardhat run dev-scripts/run-deploy-contracts-and-token.js --network localhost
working-directory: ${{ env.ethereum-contracts-working-directory }}
Expand All @@ -118,26 +114,18 @@ jobs:
run: yarn prepare-local
working-directory: ${{ env.subgraph-working-directory }}

- name: Run setup-graph-node
if: steps.base.outputs.cache-hit != 'true'
run: |
chmod +x ./tasks/setup-graph-node.sh
./tasks/setup-graph-node.sh
working-directory: ${{ env.subgraph-working-directory }}

- name: Docker compose
- name: Setup graph node
if: steps.base.outputs.cache-hit != 'true'
run: |
docker rm subgraph_graph-node_1 || true
docker compose up &
./tasks/setup-graph-node.sh &
# wait for it to warm up
sleep 30
working-directory: ${{ env.subgraph-working-directory }}

- name: Build and deploy local subgraph
if: steps.base.outputs.cache-hit != 'true'
run: |
yarn build-and-deploy-local
# artificial slow down to give the subgraph time to sync
sleep 30
working-directory: ${{ env.subgraph-working-directory }}

- name: Run test suite
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,11 @@ jobs:
run: yarn prepare-local
working-directory: ./packages/subgraph

- name: "Run setup-graph-node"
run: |
chmod +x ./tasks/setup-graph-node.sh
./tasks/setup-graph-node.sh
working-directory: ./packages/subgraph

- name: "Docker compose"
run: |
docker rm subgraph_graph-node_1 || true
docker compose up &
./tasks/setup-graph-node.sh &
# wait 5 seconds for it to warm up
sleep 5
working-directory: ./packages/subgraph

- name: "Prepare and Deploy Local Subgraph"
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@
"web3": "^1.10.4"
},
"resolutions": {
"mocha": "^10.2.0"
"mocha": "^10.2.0",
"cross-fetch": "^4.0.0"
},
"workspaces": {
"packages": [
Expand Down
5 changes: 4 additions & 1 deletion packages/subgraph/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
includedFiles = [
./package.json
./config
./matchstick.yaml
./schema.graphql
./tsconfig.json
./types
./src
./scripts
# for testing
./docker-compose.yml
./matchstick.yaml
./tests
./tasks
];
};
}
5 changes: 1 addition & 4 deletions packages/subgraph/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
version: "3"
services:
graph-node:
image: graphprotocol/graph-node:v0.35.0
Expand All @@ -11,15 +10,13 @@ services:
depends_on:
- ipfs
- postgres
extra_hosts:
- host.docker.internal:host-gateway
environment:
postgres_host: postgres
postgres_user: graph-node
postgres_pass: let-me-in
postgres_db: graph-node
ipfs: "ipfs:5001"
ethereum: "mainnet:http://host.docker.internal:8545"
ethereum: "mainnet:http://${DOCKER_HOST_IP}:8545"
GRAPH_LOG: info
ipfs:
image: ipfs/go-ipfs:v0.4.23
Expand Down
30 changes: 7 additions & 23 deletions packages/subgraph/tasks/setup-graph-node.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

set -e
set -ex

if ! which docker >/dev/null 2>&1 ; then
echo "Please install 'docker' first"
Expand All @@ -17,26 +17,10 @@ if ! which jq >/dev/null 2>&1; then
exit 1
fi

# Create the graph-node container
docker compose up --no-start graph-node
# this creates the bridged network for the composed ervices (network id: subgraph_default)
docker compose up --no-start
# we then extract the gateway ip address and export it
DOCKER_HOST_IP=$(docker network inspect subgraph_default | jq -r '.[0].IPAM.Config[].Gateway')

# Start graph-node so we can inspect it
docker compose start graph-node

# Identify the container ID
CONTAINER_ID=$(docker container ls | grep graph-node | cut -d' ' -f1)

# Inspect the container to identify the host IP address
HOST_IP=$(docker inspect "$CONTAINER_ID" | jq -r .[0].NetworkSettings.Networks[].Gateway)

echo "Host IP: $HOST_IP"

# Inject the host IP into docker-compose.yml
sed -i -e "s/host.docker.internal/$HOST_IP/g" docker-compose.yml

function stop_graph_node {
# Ensure graph-node is stopped
docker compose stop graph-node
}

trap stop_graph_node EXIT
# docker compose with required variables
DOCKER_HOST_IP=$DOCKER_HOST_IP docker compose up
Loading

0 comments on commit 3d5df70

Please sign in to comment.