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

test: add foundation for e2e testing using docker #41

Merged
merged 7 commits into from
Sep 27, 2023
Merged
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
28 changes: 28 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: test

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v3
- name: docker-test
run: docker-compose up --abort-on-container-exit --exit-code-from tester
- name: docker-logs
run: docker-compose ps
- name: docker-logs-db
run: docker-compose logs db
- name: docker-logs-ledger
run: docker-compose logs ledger
- name: docker-logs-indexer
run: docker-compose logs indexer
- name: docker-logs-tester
run: docker-compose logs tester
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# indexer

Blockchain indexer and database

## E2E Testing

This project uses docker for e2e testing.

Build docker containers (this only needs to be run once or after making changes):

```sh
docker-compose build
```

Run docker containers (and continue running non-tester containers after test scripts):

```sh
docker-compose up
```

Run docker containers (and stop all containers after test scripts):

```sh
docker-compose up --abort-on-container-exit --exit-code-from tester
```

Stop and remove containers (clean up previous run before running again):

```sh
docker-compose down
```
64 changes: 64 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
services:
db:
image: postgres:14
container_name: db_container
restart: always
user: postgres
environment:
POSTGRES_PASSWORD: password
network_mode: host
expose:
- 5432
healthcheck:
test: ["CMD-SHELL", "pg_isready"]
interval: 1s
timeout: 10s
retries: 100

ledger:
build:
context: .
dockerfile: docker/ledger.Dockerfile
container_name: ledger_container
entrypoint: ["/bin/sh", "-c", "./scripts/ledger_start.sh"]
network_mode: host
expose:
- 1317
- 26657
healthcheck:
test: ["CMD-SHELL", "curl -f http://127.0.0.1:26657 || exit 1"]
interval: 1s
timeout: 10s
retries: 100

indexer:
build:
context: .
dockerfile: docker/indexer.Dockerfile
container_name: indexer_container
environment:
DATABASE_URL: postgres://postgres:password@localhost:5432
REGEN_API: http://localhost:1317
REGEN_RPC: http://localhost:26657
entrypoint: ["/bin/sh", "-c", "./docker/scripts/indexer_start.sh"]
network_mode: host
depends_on:
db:
condition: service_healthy
ledger:
condition: service_healthy

tester:
build:
context: .
dockerfile: docker/tester.Dockerfile
container_name: tester_container
entrypoint: ["/bin/sh", "-c", "./scripts/tester_start.sh"]
network_mode: host
depends_on:
db:
condition: service_healthy
ledger:
condition: service_healthy
indexer:
condition: service_started
55 changes: 55 additions & 0 deletions docker/data/ledger_group.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"group_members": [
{
"group_id": "1",
"member": {
"added_at": "2023-01-01T00:00:00Z",
"address": "regen1l2pwmzk96ftmmt5egpjulyqtneygmmzndf7csk",
"metadata": "",
"weight": "1"
}
},
{
"group_id": "1",
"member": {
"added_at": "2023-01-01T00:00:00Z",
"address": "regen14v5z5yyl5unnyu6q3ele8ze9jev6y0m7tx6gct",
"metadata": "",
"weight": "1"
}
}
],
"group_policies": [
{
"address": "regen1afk9zr2hn2jsac63h4hm60vl9z3e5u69gndzf7c99cqge3vzwjzs475lmr",
"admin": "regen1l2pwmzk96ftmmt5egpjulyqtneygmmzndf7csk",
"created_at": "2023-01-01T00:00:00Z",
"decision_policy": {
"@type": "/cosmos.group.v1.ThresholdDecisionPolicy",
"threshold": "1",
"windows": {
"min_execution_period": "0s",
"voting_period": "20s"
}
},
"group_id": "1",
"metadata": "",
"version": "1"
}
],
"group_policy_seq": "1",
"group_seq": "1",
"groups": [
{
"admin": "regen1l2pwmzk96ftmmt5egpjulyqtneygmmzndf7csk",
"created_at": "2023-01-01T00:00:00Z",
"id": "1",
"metadata": "",
"total_weight": "2",
"version": "1"
}
],
"proposal_seq": "0",
"proposals": [],
"votes": []
}
21 changes: 21 additions & 0 deletions docker/indexer.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM python:3.9

# Install dependencies
RUN apt-get update
RUN apt-get install libpq-dev postgresql-client -y

# Set working directory
WORKDIR /home/indexer

# Copy source code
COPY . .

# Install python dependencies
RUN pip install poetry
RUN pip install load_dotenv
RUN pip install psycopg2
RUN pip install sentry_sdk
RUN pip install tenacity
Copy link
Contributor

Choose a reason for hiding this comment

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

Lines 15-18 shouldn't be necessary, poetry install should pick these up since we included them as dependencies via poetry as a package manager

Copy link
Contributor

@wgwz wgwz Sep 27, 2023

Choose a reason for hiding this comment

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

i.e. https://github.com/regen-network/indexer/blob/main/poetry.lock#L258

poetry.lock and poetry install are basically like package.lock and npm install in the python world

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks! Yea, for some reason I was hitting errors that these were not installed but that was awhile back and seems to be working fine without these lines now. Not sure why that was an issue previously.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah! Nvm, still hitting the same errors when starting the containers:

ModuleNotFoundError: No module named 'dotenv'

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm going to leave them for now but maybe there is something we can do to prevent needing them in the future.

Copy link
Contributor

Choose a reason for hiding this comment

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

huh, that's weird


# Install indexer
RUN poetry install
67 changes: 67 additions & 0 deletions docker/ledger.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
FROM golang:1.19

# Install dependencies
RUN apt-get update
RUN apt-get install jq -y

# Set ledger version
ENV GIT_CHECKOUT='v5.1.2'

# Clone regen ledger
RUN git clone https://github.com/regen-network/regen-ledger/ /home/ledger

# Set working directory
WORKDIR /home/ledger

# Use provided version
RUN git checkout $GIT_CHECKOUT

# Build regen binary
RUN make install

# Setup moniker, chain, homedir
RUN regen --chain-id regen-local init validator

# Set configuration
RUN regen config chain-id regen-local
RUN regen config keyring-backend test

# Update stake to uregen
RUN sed -i "s/stake/uregen/g" /root/.regen/config/genesis.json

# Add accounts
RUN printf "trouble alarm laptop turn call stem lend brown play planet grocery survey smooth seed describe hood praise whale smile repeat dry sauce front future\n\n" | regen keys --keyring-backend test add validator -i
RUN printf "cool trust waste core unusual report duck amazing fault juice wish century across ghost cigar diary correct draw glimpse face crush rapid quit equip\n\n" | regen keys --keyring-backend test add user1 -i
RUN printf "music debris chicken erode flag law demise over fall always put bounce ring school dumb ivory spin saddle ostrich better seminar heart beach kingdom\n\n" | regen keys --keyring-backend test add user2 -i

# Set up validator
RUN regen add-genesis-account validator 1000000000uregen --keyring-backend test
RUN regen gentx validator 1000000uregen

# Set up user acounts
RUN regen add-genesis-account user1 1000000000uregen --keyring-backend test
RUN regen add-genesis-account user2 1000000000uregen --keyring-backend test

# Prepare genesis file
RUN regen collect-gentxs

# Set minimum gas price
RUN sed -i "s/minimum-gas-prices = \"\"/minimum-gas-prices = \"0uregen\"/" /root/.regen/config/app.toml

# Set cors allow all origins
RUN sed -i "s/cors_allowed_origins = \[\]/cors_allowed_origins = [\"*\"]/" /root/.regen/config/config.toml

# Copy genesis state files
COPY docker/data /home/ledger/data

# Add group state to genesis
RUN jq '.app_state.group |= . + input' /root/.regen/config/genesis.json /home/ledger/data/ledger_group.json > genesis-tmp.json

# Overwrite genesis file with updated genesis file
RUN mv -f genesis-tmp.json /root/.regen/config/genesis.json

# Copy regen start script
COPY docker/scripts/ledger_start.sh /home/ledger/scripts/

# Make start script executable
RUN ["chmod", "+x", "/home/ledger/scripts/ledger_start.sh"]
24 changes: 24 additions & 0 deletions docker/scripts/indexer_init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

# run migrations
(cd sql && ./run_all_migrations.sh)

# workaround for indexer starting with new chain
psql "$DATABASE_URL" -c "INSERT INTO chain (
num,
chain_id
) VALUES (
1,
'regen-local'
)"
psql "$DATABASE_URL" -c "INSERT INTO block (
chain_num,
height,
data,
time
) VALUES (
1,
0,
'{}',
now()
)"
18 changes: 18 additions & 0 deletions docker/scripts/indexer_start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

INDEXER_INITIALIZED="INDEXER_INITIALIZED"

# initialize indexer if not yet initialized
if [ ! -e $INDEXER_INITIALIZED ]; then

# set indexer initialized
touch $INDEXER_INITIALIZED

echo "First start, running init script..."

# run indexer init script
/home/indexer/docker/scripts/indexer_init.sh
fi

# start indexer
python main.py
4 changes: 4 additions & 0 deletions docker/scripts/ledger_start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

# start regen with api and unsafe CORS enabled
regen start --api.enable=true --api.enabled-unsafe-cors=true
13 changes: 13 additions & 0 deletions docker/scripts/tester_start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

set -eo pipefail

# wait for indexer to start
sleep 5

# run tester test scripts
/home/tester/scripts/test_index_proposals.sh
/home/tester/scripts/test_index_votes.sh

# exit without error
exit 0
52 changes: 52 additions & 0 deletions docker/tester.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
FROM golang:1.19

# Install dependencies
RUN apt-get update
RUN apt-get install jq libpq-dev postgresql-client -y

# Set ledger version
ENV GIT_CHECKOUT='v5.1.2'

# Set database url
ENV DATABASE_URL='postgres://postgres:password@localhost:5432/postgres'

# Set test addresses
ENV TEST_USER_ADDRESS_1=regen1l2pwmzk96ftmmt5egpjulyqtneygmmzndf7csk
ENV TEST_USER_ADDRESS_2=regen14v5z5yyl5unnyu6q3ele8ze9jev6y0m7tx6gct
ENV TEST_POLICY_ADDRESS_1=regen1afk9zr2hn2jsac63h4hm60vl9z3e5u69gndzf7c99cqge3vzwjzs475lmr

# Set transaction flags
ENV REGEN_TX_FLAGS="--keyring-backend test --chain-id regen-local --yes"

# Clone regen ledger
RUN git clone https://github.com/regen-network/regen-ledger/ /home/tester

# Set working directory
WORKDIR /home/tester

# Use provided version
RUN git checkout $GIT_CHECKOUT

# Build regen binary
RUN make install

# Set configuration
RUN regen config chain-id regen-local
RUN regen config keyring-backend test

# Add accounts
RUN printf "cool trust waste core unusual report duck amazing fault juice wish century across ghost cigar diary correct draw glimpse face crush rapid quit equip\n\n" | regen keys --keyring-backend test add user1 -i
RUN printf "music debris chicken erode flag law demise over fall always put bounce ring school dumb ivory spin saddle ostrich better seminar heart beach kingdom\n\n" | regen keys --keyring-backend test add user2 -i

# Copy tester start script
COPY docker/scripts/tester_start.sh /home/tester/scripts/

# Copy tester test scripts
COPY docker/tester/ /home/tester/scripts/

# Make start script executable
RUN ["chmod", "+x", "/home/tester/scripts/tester_start.sh"]

# Make test scripts executable
RUN ["chmod", "+x", "/home/tester/scripts/test_index_proposals.sh"]
RUN ["chmod", "+x", "/home/tester/scripts/test_index_votes.sh"]
Loading