From a9954873d9f49cb274c8db5d117fd83a5265e6b8 Mon Sep 17 00:00:00 2001 From: Thales Lima Date: Thu, 27 Jun 2024 16:02:19 +0200 Subject: [PATCH] Wrap testing module inside a docker-compose --- .gitignore | 5 ++-- docker-compose.yaml | 15 ---------- testing/.env.default | 4 +++ testing/Dockerfile | 33 +++++++++++++++++++++ testing/README.md | 57 ++++++++++++++----------------------- testing/docker-compose.yaml | 31 ++++++++++++++++++++ testing/runner.py | 2 +- 7 files changed, 94 insertions(+), 53 deletions(-) delete mode 100644 docker-compose.yaml create mode 100644 testing/.env.default create mode 100644 testing/Dockerfile create mode 100644 testing/docker-compose.yaml diff --git a/.gitignore b/.gitignore index 9c89335..3b908fb 100644 --- a/.gitignore +++ b/.gitignore @@ -7,7 +7,7 @@ target/ # Substreams spkg files are build artifacts *.spkg -.env +*/.env .vscode .idea *.log @@ -18,4 +18,5 @@ substreams/ethereum-template/Cargo.lock .DS_Store -tycho-indexer \ No newline at end of file +tycho-indexer +substreams/my_substream \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml deleted file mode 100644 index 7c93ea8..0000000 --- a/docker-compose.yaml +++ /dev/null @@ -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: \ No newline at end of file diff --git a/testing/.env.default b/testing/.env.default new file mode 100644 index 0000000..21eb434 --- /dev/null +++ b/testing/.env.default @@ -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" \ No newline at end of file diff --git a/testing/Dockerfile b/testing/Dockerfile new file mode 100644 index 0000000..ff7c6cc --- /dev/null +++ b/testing/Dockerfile @@ -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"] \ No newline at end of file diff --git a/testing/README.md b/testing/README.md index 7f64c80..7355128 100644 --- a/testing/README.md +++ b/testing/README.md @@ -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 -``` - -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` +docker compose run app +``` \ No newline at end of file diff --git a/testing/docker-compose.yaml b/testing/docker-compose.yaml new file mode 100644 index 0000000..c618ad4 --- /dev/null +++ b/testing/docker-compose.yaml @@ -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: \ No newline at end of file diff --git a/testing/runner.py b/testing/runner.py index a9136ed..6c767d3 100644 --- a/testing/runner.py +++ b/testing/runner.py @@ -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: