Skip to content

Commit

Permalink
Add flow for testing discovery provider (#4116)
Browse files Browse the repository at this point in the history
  • Loading branch information
cheran-senthil committed Oct 19, 2022
1 parent 3663240 commit b6f8311
Show file tree
Hide file tree
Showing 8 changed files with 204 additions and 104 deletions.
85 changes: 73 additions & 12 deletions dev-tools/audius-compose
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import json
import pathlib
import subprocess
import string
import random

import click
import dotenv
Expand Down Expand Up @@ -147,8 +149,7 @@ def build(
[
"docker",
"compose",
"--project-directory",
protocol_dir,
f"--project-directory={protocol_dir}",
"build",
],
)
Expand Down Expand Up @@ -179,8 +180,7 @@ def up(
[
"docker",
"compose",
"--project-directory",
protocol_dir,
f"--project-directory={protocol_dir}",
"up",
"--build",
"-d",
Expand All @@ -195,14 +195,78 @@ def down(protocol_dir):
[
"docker",
"compose",
"--project-directory",
protocol_dir,
f"--project-directory={protocol_dir}",
"down",
"-v",
],
)


@cli.command()
@click.argument("service")
@click.argument("args", nargs=-1, type=click.UNPROCESSED)
@click.pass_obj
def test(protocol_dir, service, args):
generate_env(
protocol_dir,
0, # amount of replicas don't matter here
0, # amount of replicas don't matter here
0, # amount of replicas don't matter here
False,
)

project_name = f"audius-compose-{''.join(random.choice(string.ascii_lowercase) for i in range(9))}"

no_deps = False
if service == "discovery-provider":
no_deps = args and not any(arg.startswith("integration_tests") for arg in args)

try:
subprocess.run(
[
"docker",
"compose",
f"--project-name={project_name}",
f"--project-directory={protocol_dir}",
f"--file={protocol_dir / 'docker-compose.test.yml'}",
"build",
],
)

subprocess.run(
[
"docker",
"compose",
f"--project-name={project_name}",
f"--project-directory={protocol_dir}",
f"--file={protocol_dir / 'docker-compose.test.yml'}",
"run",
"--rm",
*(["--no-deps"] if no_deps else []),
f"test-{service}",
*args,
],
)
finally:
subprocess.run(
[
"docker",
"compose",
f"--project-name={project_name}",
f"--project-directory={protocol_dir}",
f"--file={protocol_dir / 'docker-compose.test.yml'}",
"down",
"--timeout=1",
"-v",
],
)

subprocess.run(
f'docker rmi $(docker images --filter "reference={project_name}-*" -q)',
shell=True,
)


@cli.command()
@click.argument("service")
@click.argument("command")
Expand All @@ -218,8 +282,7 @@ def exec(protocol_dir, service, command, args):
[
"docker",
"compose",
"--project-directory",
protocol_dir,
f"--project-directory={protocol_dir}",
"exec",
"--index",
str(index),
Expand All @@ -241,8 +304,7 @@ def ps(protocol_dir):
[
"docker",
"compose",
"--project-directory",
protocol_dir,
f"--project-directory={protocol_dir}",
"ps",
"--format=json",
],
Expand Down Expand Up @@ -317,8 +379,7 @@ def logs(protocol_dir, service):
[
"docker",
"compose",
"--project-directory",
protocol_dir,
f"--project-directory={protocol_dir}",
"logs",
service,
],
Expand Down
19 changes: 4 additions & 15 deletions discovery-provider/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,18 @@ curl http://audius-protocol-discovery-provider-1:5000/health_check

## Testing

Unit & integration tests are run through a side-car docker container alongside the main audius stack.

First, ensure your developer stack is running

```bash
audius-compose up

# If running elasticsearch tests, ensure the stack is run with an elasticsearch container
audius-compose up --elasticsearch-replicas 1
```

Run tests

```bash
# Run all tests
docker exec -it audius-protocol-discovery-provider-test-1 pytest
audius-compose test discovery-provider

# Run unit tests
docker exec -it audius-protocol-discovery-provider-test-1 pytest src
audius-compose test discovery-provider src

# Run integration tests
docker exec -it audius-protocol-discovery-provider-test-1 pytest integration_tests
audius-compose test discovery-provider integration_tests

# A single test
docker exec -it audius-protocol-discovery-provider-test-1 pytest src/api/v1/api_unit_test.py
audius-compose test discovery-provider src/api/v1/api_unit_test.py
```
6 changes: 6 additions & 0 deletions discovery-provider/scripts/init-db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ adduser --disabled-password postgres
mkdir /run/postgresql /db
chown -R postgres:postgres /run/postgresql /db
chmod 700 /db
sudo -u postgres pg_ctl init -D /db
echo "host all all 0.0.0.0/0 md5" >>/db/pg_hba.conf
echo "listen_addresses = '*'" >>/db/postgresql.conf
sudo -u postgres pg_ctl start -D /db -o "-c shared_preload_libraries=pg_stat_statements"
sudo -u postgres createdb audius_discovery
sudo -u postgres pg_ctl stop -D /db
16 changes: 3 additions & 13 deletions discovery-provider/scripts/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,14 @@ if [ -z "$audius_redis_url" ]; then
fi

if [ -z "$audius_db_url" ]; then
if [ -z "$(ls -A /db)" ]; then
chown -R postgres:postgres /db
chmod 700 /db
sudo -u postgres pg_ctl init -D /db
echo "host all all 0.0.0.0/0 md5" >>/db/pg_hba.conf
echo "listen_addresses = '*'" >>/db/postgresql.conf
sudo -u postgres pg_ctl start -D /db -o "-c shared_preload_libraries=pg_stat_statements"
sudo -u postgres createdb audius_discovery
else
sudo -u postgres pg_ctl start -D /db -o "-c shared_preload_libraries=pg_stat_statements"
fi
sudo -u postgres pg_ctl start -D /db -o "-c shared_preload_libraries=pg_stat_statements"
export WAIT_HOSTS="localhost:5432"
/wait

sudo -u postgres psql -c "ALTER USER postgres PASSWORD '${postgres_password:-postgres}';"

export audius_db_url="postgresql+psycopg2://postgres:${postgres_password:-postgres}@localhost:5432/audius_discovery"
export audius_db_url_read_replica="postgresql+psycopg2://postgres:${postgres_password:-postgres}@localhost:5432/audius_discovery"
export WAIT_HOSTS="localhost:5432"
/wait
fi

export PYTHONUNBUFFERED=1
Expand Down
21 changes: 21 additions & 0 deletions discovery-provider/scripts/test_entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash

echo "Starting postgres"
sudo -u postgres pg_ctl start -D /db -o "-c shared_preload_libraries=pg_stat_statements"
sudo -u postgres psql -c "ALTER USER postgres PASSWORD 'postgres';"
export audius_db_url="postgresql+psycopg2://postgres:postgres@localhost:5432/audius_discovery"
export audius_db_url_read_replica="postgresql+psycopg2://postgres:postgres@localhost:5432/audius_discovery"

echo "Starting redis"
redis-server --daemonize yes
export audius_redis_url="redis://localhost:6379/00"

if nslookup "discovery-provider-elasticsearch" >/dev/null 2>&1; then
export audius_elasticsearch_url="http://discovery-provider-elasticsearch:9200"
export audius_elasticsearch_run_indexer="true"
fi

echo "Waiting for redis and postgres"
WAIT_HOSTS="localhost:6379,localhost:5432" /wait

pytest "$@"
14 changes: 0 additions & 14 deletions discovery-provider/scripts/test_setup.sh

This file was deleted.

97 changes: 97 additions & 0 deletions docker-compose.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Do not expose any ports in this file to allow running multiple tests at the same time

version: "3.9"

x-logging:
&default-logging
options:
max-size: "50m"
max-file: "3"
driver: json-file

services:
# TODO: remove duplication between docker-compose.yml and docker-compose.test.yml

poa-ganache:
build:
context: contracts
dockerfile: Dockerfile.dev
args:
bootstrapSPIds: ${BOOTSTRAP_SP_IDS}
bootstrapSPDelegateWallets: ${BOOTSTRAP_SP_DELEGATE_WALLETS}
bootstrapSPOwnerWallets: ${BOOTSTRAP_SP_OWNER_WALLETS}
command: bash /tmp/dev-tools/startup/poa-ganache.sh
volumes:
- poa-contracts-abis:/usr/src/app/build/contracts
- ./dev-tools:/tmp/dev-tools
logging: *default-logging
deploy:
mode: global

eth-ganache:
build:
context: eth-contracts
dockerfile: Dockerfile.dev
args:
CONTENT_NODE_VERSION: ${CONTENT_NODE_VERSION}
DISCOVERY_NODE_VERSION: ${DISCOVERY_NODE_VERSION}
command: bash /tmp/dev-tools/startup/eth-ganache.sh
volumes:
- eth-contracts-abis:/usr/src/app/build/contracts
- ./dev-tools:/tmp/dev-tools
logging: *default-logging
deploy:
mode: global

solana-test-validator:
build:
context: solana-programs
dockerfile: Dockerfile.dev
args:
AUDIUS_ETH_REGISTRY_PRIVATE_KEY: "${SOLANA_AUDIUS_ETH_REGISTRY_SECRET_KEY}"
TRACK_LISTEN_COUNT_PRIVATE_KEY: "${SOLANA_TRACK_LISTEN_COUNT_SECRET_KEY}"
CLAIMABLE_TOKENS_PRIVATE_KEY: "${SOLANA_CLAIMABLE_TOKENS_SECRET_KEY}"
REWARD_MANAGER_PRIVATE_KEY: "${SOLANA_REWARD_MANAGER_SECRET_KEY}"
AUDIUS_DATA_PRIVATE_KEY: "${SOLANA_AUDIUS_DATA_SECRET_KEY}"
owner_private_key: "${SOLANA_OWNER_SECRET_KEY}"
feepayer_private_key: "${SOLANA_FEEPAYER_SECRET_KEY}"
token_private_key: "${SOLANA_TOKEN_MINT_SECRET_KEY}"
admin_authority_private_key: "${SOLANA_ADMIN_AUTHORITY_SECRET_KEY}"
admin_account_private_key: "${SOLANA_ADMIN_ACCOUNT_SECRET_KEY}"
signer_group_private_key: "${SOLANA_SIGNER_GROUP_SECRET_KEY}"
valid_signer_private_key: "${SOLANA_VALID_SIGNER_SECRET_KEY}"
reward_manager_pda_private_key: "${SOLANA_REWARD_MANAGER_PDA_SECRET_KEY}"
reward_manager_token_pda_private_key: "${SOLANA_REWARD_MANAGER_TOKEN_PDA_SECRET_KEY}"
valid_signer_eth_address: "${ETH_VALID_SIGNER_ADDRESS}"
volumes:
- solana-programs-idl:/usr/src/app/anchor/audius-data/idl
logging: *default-logging
deploy:
mode: global

# discovery-provider

discovery-provider-elasticsearch:
extends:
file: docker-compose.yml
service: discovery-provider-elasticsearch
deploy:
mode: global
replicas: 1

test-discovery-provider:
extends:
file: docker-compose.yml
service: discovery-provider
entrypoint: scripts/test_entrypoint.sh
command: ""
depends_on:
discovery-provider-elasticsearch:
condition: service_healthy
deploy:
mode: global

volumes:
poa-contracts-abis:
eth-contracts-abis:
solana-programs-idl:

0 comments on commit b6f8311

Please sign in to comment.