Wrap testing module inside a docker-compose

This commit is contained in:
Thales Lima
2024-06-27 16:02:19 +02:00
parent b598a12592
commit a9954873d9
7 changed files with 94 additions and 53 deletions

3
.gitignore vendored
View File

@@ -7,7 +7,7 @@ target/
# Substreams spkg files are build artifacts
*.spkg
.env
*/.env
.vscode
.idea
*.log
@@ -19,3 +19,4 @@ substreams/ethereum-template/Cargo.lock
.DS_Store
tycho-indexer
substreams/my_substream

View File

@@ -1,15 +0,0 @@
version: '3.1'
services:
db:
image: ghcr.io/dbsystel/postgresql-partman:15-5
restart: "always"
environment:
POSTGRESQL_PASSWORD: mypassword
POSTGRESQL_DATABASE: tycho_indexer_0
POSTGRESQL_USERNAME: postgres
ports:
- "5431:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
volumes:
postgres_data:

4
testing/.env.default Normal file
View File

@@ -0,0 +1,4 @@
SUBSTREAMS_PATH=../substreams/ethereum-curve
RPC_URL=https://mainnet.infura.io/v3/your-infura-key
DATABASE_URL: "postgres://postgres:mypassword@db:5432/tycho_indexer_0"
SUBSTREAMS_API_TOKEN="changeme"

33
testing/Dockerfile Normal file
View File

@@ -0,0 +1,33 @@
# Use an official Python runtime as a parent image
FROM --platform=linux/amd64 continuumio/miniconda3:24.4.0-0
# Set the working directory in the container to /app
WORKDIR /app
# Add current directory code to /app in container
ADD . /app/testing
RUN chmod +x /app/testing/tycho-indexer
# Create a new conda environment and install pip
RUN conda create -n myenv pip python=3.9
# Install any needed packages specified in requirements.txt
RUN echo "source activate myenv" >~/.bashrc
ENV PATH /opt/conda/envs/myenv/bin:$PATH
RUN apt-get update \
&& apt-get -y install libpq-dev gcc \
&& pip install psycopg2 \
&& apt-get clean
RUN /bin/bash -c "source activate myenv && pip install --no-cache-dir -r testing/requirements.txt"
# Make port 80 available to the world outside this container
EXPOSE 80
# Install the substreams cli
RUN wget -c https://github.com/streamingfast/substreams/releases/download/v1.8.0/substreams_linux_x86_64.tar.gz -O - | tar xzf - substreams
RUN mv substreams /usr/local/bin/substreams && chmod +x /usr/local/bin/substreams
# Run the command to start your application
CMD ["python", "testing/cli.py", "--test_yaml_path", "/app/substreams/my_substream/test_assets.yaml", "--with_binary_logs"]

View File

@@ -1,53 +1,40 @@
# Substreams Testing
We know testing your Substreams modules can be a pain. So, our goal here is to provide a quick and easy way to run end-to-end tests.
This package provides a comprehensive testing suite for Substreams modules. The testing suite is designed to facilitate end-to-end testing, ensuring that your Substreams modules function as expected.
Our script will build the `.spkg` for your Substreams module, index the specified block range, and check if the expected state has been correctly indexed in PostgreSQL.
## Overview
## How to Test
The testing suite builds the `.spkg` for your Substreams module, indexes a specified block range, and verifies that the expected state has been correctly indexed in PostgreSQL.
Here's what you need to do:
## Prerequisites
1. Get the latest version of our indexer.
2. Define your tests and expected state.
3. Run the testing script.
- Latest version of our indexer, Tycho. Please contact us to obtain the latest version. Once acquired, place it in the `/testing/` directory.
- Docker installed on your machine.
### Get the Latest Version of Tycho (Indexer)
## Test Configuration
We don't have a direct download link yet. Please reach out to us to get the latest version. Once you have it, place it in the `/testing/` folder.
Tests are defined in a `yaml` file. A template can be found at `substreams/ethereum-template/test_assets.yaml`. The configuration file should include:
### Define Your Tests
- The target Substreams config file.
- The expected protocol types.
- The tests to be run.
Define all your tests in a `yaml` file. You can find a template in `substreams/ethereum-template/test_assets.yaml`.
Each test will index all blocks between `start-block` and `stop-block` and verify that the indexed state matches the expected state.
You'll need to:
## Running Tests
- Point to the target substreams config file.
- Specify the expected protocol types.
- Define your tests.
### Step 1: Export Environment Variables
For each test, the script will index all blocks between `start-block` and `stop-block` and check that the indexed state matches the expected state.
Export the required environment variables for the execution. You can find the available environment variables in the `.env.default` file.
Please create a `.env` file in the `testing` directory and set the required environment variables.
### Run the Script
The variable SUBSTREAMS_PATH should be a relative reference to the directory containing the Substreams module that you want to test.
Once everything is set up, run our script using the CLI.
Example: `SUBSTREAMS_PATH=../substreams/ethereum-curve`
First, you need a local postgres database
### Step 2: Build and the Testing Script
Run the testing script using Docker Compose:
```bash
docker-compose up -d db
docker compose run app
```
Then export an environment variable for RPC connection
```bash
export RPC_URL=your-chain-rpc
```
And finally run the testing script
```bash
python testing/cli.py --test_yaml_path "./substreams/your-substreams-folder/test_assets.yaml"
```
You can get the available CLI flags using `--help`

View File

@@ -0,0 +1,31 @@
version: '3.1'
services:
db:
image: ghcr.io/dbsystel/postgresql-partman:15-5
restart: "always"
environment:
POSTGRESQL_PASSWORD: mypassword
POSTGRESQL_DATABASE: tycho_indexer_0
POSTGRESQL_USERNAME: postgres
ports:
- "5431:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
app:
build:
context: .
dockerfile: Dockerfile
volumes:
- ${SUBSTREAMS_PATH}:/app/substreams/my_substream
- ../substreams:/app/substreams
- ../proto:/app/proto
- ./tycho-indexer:/app/testing/tycho-indexer
- ./runner.py:/app/testing/runner.py
ports:
- "80:80"
depends_on:
- db
env_file:
- ".env"
volumes:
postgres_data:

View File

@@ -61,7 +61,7 @@ class TestRunner:
print(f"❗️ {test['name']} failed: {result.message}")
self.tycho_runner.empty_database(
"postgres://postgres:mypassword@localhost:5431"
"postgres://postgres:mypassword@db:5432"
)
def validate_state(self, expected_state: dict, stop_block: int) -> TestResult: