Skip to content

Commit

Permalink
Merge pull request #33 from valory-xyz/test/e2e
Browse files Browse the repository at this point in the history
test/e2e
  • Loading branch information
DavidMinarsch authored Oct 11, 2022
2 parents 42c51cd + 0e476be commit 0756c4f
Show file tree
Hide file tree
Showing 13 changed files with 527 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitleaksignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
62103cafc6b909a321cbf6139173c3a8580b94b9:packages/balancer/agents/autonomous_fund/aea-config.yaml:generic-api-key:120
8efa33c0e847f2d75bdca1b0cdf5fd134f6b322b:packages/balancer/skills/autonomous_fund_abci/skill.yaml:generic-api-key:81
c5417dfdd2c3aa5d1d7ab05e7ccbcf6d6c6487aa:packages/balancer/skills/pool_manager_abci/skill.yaml:generic-api-key:79
920341fcf1d179421c4afdbbbb4f9d23235023fd:packages/balancer/contracts/managed_pool_controller/tests/helpers/constants.py:generic-api-key:46
Expand Down
4 changes: 4 additions & 0 deletions packages/balancer/agents/autonomous_fund/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@
# ------------------------------------------------------------------------------

"""Package for balancer/autonomous_fund agent."""
from pathlib import Path


PACKAGE_DIR = Path(__file__).parent
108 changes: 102 additions & 6 deletions packages/balancer/agents/autonomous_fund/aea-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ license: Apache-2.0
description: ''
aea_version: '>=1.19.0, <2.0.0'
fingerprint:
__init__.py: bafybeialdpvrelsxfu7zh5r5s7tg3gpcz3kc33snhifzdlvjj6rssbzspa
__init__.py: bafybeibzqammkemm5yc5hqjzdnpmgr2aw3j7lwni64rc5c4sf7qsnv7ulu
tests/__init__.py: bafybeicmv3jjcqcdnmubfmrgdgeyic3nkm77z4mutsyuo34hpddwupofre
tests/helpers/__init__.py: bafybeiauwuw4a4wxqacqqkkk52ktps2qedadhypvh3ex7uqs7qaqrpygmy
tests/helpers/constants.py: bafybeiaxnzow6b6skwz2unt76n4qqz7vlp6sabyqhfqtijsivssh3l6dza
tests/helpers/docker.py: bafybeif6d6ku7ljgmcwfy6ogo7jm2x6mwxkwsp7gb4lbk3xijduui5humi
tests/helpers/fixtures.py: bafybeiayn2dvqkflpaeizlkyhhqk3gagenlqxo3h5etib7hjpv7jq7htiu
tests/helpers/constants.py: bafybeie2nmfhdlrmnrwp2225cxclkw3dd4cxu4uig7hrhtqyg5t7nvfpje
tests/helpers/data/json_server/data.json: bafybeidueptoi7g44hkdrzkyn326l44qhkbt4bsnidrvkn5y3pkgljmuzy
tests/helpers/docker.py: bafybeiaicurt4s7unh362l26wbmetxc7kgl6fwninwfgusrrbyiaozf3hy
tests/helpers/fixtures.py: bafybeiczpplahcqw7zxi76gnefs5jhnhdpxy4box53evmu6j75tw6assny
tests/test_agents/__init__.py: bafybeicmv3jjcqcdnmubfmrgdgeyic3nkm77z4mutsyuo34hpddwupofre
tests/test_agents/base.py: bafybeig7o56fgyhggurr5tmdfntdwrfh73dalxemlphgdrvfyo6klrjy7i
tests/test_agents/test_autonomous_fund.py: bafybeiap4dwh6o2u37njx6msg7rzbxp5enwqmrks5kwzzwzj6xtmjyjnja
tests/test_contracts/__init__.py: bafybeiel4den34ewkgjdlze2gdw5veayz3xtnnii4tfznjltpq33nsssda
tests/test_contracts/test_managed_pool_controller/__init__.py: bafybeid3fa64f4rskt4tf6hfjxqzid2o5dopuh2f53aegclg6fj5ylso5i
tests/test_contracts/test_managed_pool_controller/test_contract.py: bafybeigp42gxwkeyb3vtmh4oi7kjc7nv2xx3vakyv5qpajqb3mhxxxxyce
Expand All @@ -28,7 +32,7 @@ contracts:
protocols:
- open_aea/signing:1.0.0:bafybeiambqptflge33eemdhis2whik67hjplfnqwieoa6wblzlaf7vuo44
skills:
- balancer/autonomous_fund_abci:0.1.0:bafybeifx2x3g3dhlq3clq2irf5ciqsj2gadyyelfhop7mlfiwj2yx5xri4
- balancer/autonomous_fund_abci:0.1.0:bafybeie5tls5ppxzuqfny2kdzvxqjy3zpa75ylcngjqnlnjccbfra5tafm
- balancer/fear_and_greed_oracle_abci:0.1.0:bafybeicmttoiyfvh2usfyrsuog475rkxoszb3ymh6ue3lnsc3p6q4aky6q
- balancer/pool_manager_abci:0.1.0:bafybeihb5jzxuf7piv44v6ovhxvg222uwf7ke3y6bjxl7jipszqd7ay2bm
- valory/abstract_abci:0.1.0:bafybeigqbgoej3zhsz6e5ibdu5tewv6plh4yzxllguyftbymj5bp2zaxyq
Expand All @@ -44,8 +48,27 @@ default_routing: {}
connection_private_key_paths: {}
private_key_paths: {}
logging_config:
disable_existing_loggers: false
version: 1
disable_existing_loggers: false
formatters:
standard:
format: '[%(asctime)s] [%(levelname)s] %(message)s'
handlers:
logfile:
class: logging.FileHandler
formatter: standard
filename: ${LOG_FILE:str:log.txt}
level: INFO
console:
class: logging.StreamHandler
formatter: standard
stream: ext://sys.stdout
loggers:
aea:
handlers:
- logfile
- console
propagate: false
dependencies:
open-aea-ledger-ethereum: {}
default_connection: null
Expand Down Expand Up @@ -83,3 +106,76 @@ cert_requests:
public_key: ${P2P_PUBLIC_KEY:str:0250827315351f6f4d3777b77d4bfd67280156240313bf221750b36404344bb35d}
save_path: .certs/acn_cosmos_9005.txt
is_abstract: true
---
public_id: balancer/autonomous_fund_abci:0.1.0
type: skill
models:
params:
args:
cleanup_history_depth: 1
cleanup_history_depth_current: null
consensus:
max_participants: 1
drand_public_key: 868f005eb8e6e4ca0a47c8a77ceaa5309a47978a7c71bc5cce96366b5d7a569937c529eeda66c7293784a9402801af31
fear_and_greed_endpoint: https://api.alternative.me/fng/?format=json
fear_and_greed_num_points: 2
finalize_timeout: 60.0
genesis_config:
genesis_time: '2022-09-26T00:00:00.000000000Z'
chain_id: chain-c4daS1
consensus_params:
block:
max_bytes: '22020096'
max_gas: '-1'
time_iota_ms: '1000'
evidence:
max_age_num_blocks: '100000'
max_age_duration: '172800000000000'
max_bytes: '1048576'
validator:
pub_key_types:
- ed25519
version: {}
voting_power: '10'
history_check_timeout: 1205
keeper_allowed_retries: 3
keeper_timeout: 30.0
managed_pool_controller_address: '0xb821BFfE924E18F8B3d92473C5279d60F0Dfc6eA'
max_healthcheck: 120
max_index_change: 0.00040509259
max_index_value: 100
min_index_value: 0
observation_aggregator_function: median
observation_interval: 10
on_chain_service_id: null
pool_weights:
0:
- 60
- 30
- 10
41:
- 33
- 33
- 34
60:
- 10
- 30
- 60
reset_tendermint_after: 2
retry_attempts: 400
retry_timeout: 3
round_timeout_seconds: 30.0
service_id: pool_manager
service_registry_address: null
setup:
safe_contract_address:
- '0x8001bdCf80F8Fb61CdcDA48419A30b430B385ca1'
sleep_time: 1
tendermint_check_sleep_delay: 3
tendermint_com_url: http://localhost:8080
tendermint_max_retries: 5
tendermint_url: http://localhost:26657
validate_timeout: 1205
weight_tolerance: 0.1
weight_update_timespan: 72000
weighted_pool_address: '0x28BF8d29cFA99aE9C3D876210453272f30e4D131'
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,7 @@
WEIGHTED_POOL = "0x28BF8d29cFA99aE9C3D876210453272f30e4D131"
INITIAL_POOL_WEIGHTS = [30, 40, 30] # scaled on 0-100

# this safe contract is configured with the accounts above, and has the threshold set at 3/4.
CONFIGURED_SAFE_INSTANCE = "0x5564550A54EcD43bA8f7c666fff1C4762889A572"
# this safe contract is configured with the accounts above, and has the threshold set at 1/4.
CONFIGURED_SAFE_INSTANCE = "0x8001bdCf80F8Fb61CdcDA48419A30b430B385ca1"

MOCK_API_PATH = "/fng?format=json"
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"/fng": {
"get": {
"name": "Fear and Greed Index",
"data": [
{
"value": "22",
"value_classification": "Extreme Fear",
"timestamp": "1665360000",
"time_until_update": "33681"
},
{
"value": "22",
"value_classification": "Extreme Fear",
"timestamp": "1665273600"
}
],
"metadata": {
"error": null
}
}
}
}
75 changes: 75 additions & 0 deletions packages/balancer/agents/autonomous_fund/tests/helpers/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"""Autonomous Fund Contracts Docker image."""
import logging
import time
from pathlib import Path
from typing import Dict, List

import docker
Expand All @@ -29,6 +30,7 @@
from aea_test_autonomy.docker.base import DockerImage
from docker.models.containers import Container

from packages.balancer.agents.autonomous_fund import PACKAGE_DIR
from packages.balancer.agents.autonomous_fund.tests.helpers.constants import (
MANAGED_POOL_CONTROLLER,
)
Expand Down Expand Up @@ -120,3 +122,76 @@ def wait(self, max_attempts: int = 15, sleep_rate: float = 1.0) -> bool:
)
time.sleep(sleep_rate)
return False


DEFAULT_JSON_SERVER_ADDR = "http://127.0.0.1"
DEFAULT_JSON_SERVER_PORT = 3000
DEFAULT_JSON_DATA_DIR = (
PACKAGE_DIR / "tests" / "helpers" / "data" / "json_server" / "data.json"
)


class MockFearAndGreedApi(DockerImage):
"""Spawn a JSON server to for mocking the Fear and Greed API."""

def __init__(
self,
client: docker.DockerClient,
addr: str = DEFAULT_JSON_SERVER_ADDR,
port: int = DEFAULT_JSON_SERVER_PORT,
json_data: Path = DEFAULT_JSON_DATA_DIR,
):
"""Initialize."""
super().__init__(client)
self.addr = addr
self.port = port
self.json_data = json_data

def create_many(self, nb_containers: int) -> List[Container]:
"""Instantiate the image in many containers, parametrized."""
raise NotImplementedError()

@property
def tag(self) -> str:
"""Get the tag."""
return "ajoelpod/mock-json-server:latest"

def create(self) -> Container:
"""Create the container."""
data = "/usr/src/app/data.json"
volumes = {
str(self.json_data): {
"bind": data,
"mode": "rw",
},
}
ports = {"8000/tcp": ("0.0.0.0", self.port)} # nosec
container = self._client.containers.run(
self.tag,
detach=True,
ports=ports,
volumes=volumes,
extra_hosts={"host.docker.internal": "host-gateway"},
)
return container

def wait(self, max_attempts: int = 15, sleep_rate: float = 1.0) -> bool:
"""
Wait until the image is running.
:param max_attempts: max number of attempts.
:param sleep_rate: the amount of time to sleep between different requests.
:return: True if the wait was successful, False otherwise.
"""
for i in range(max_attempts):
try:
response = requests.get(f"{self.addr}:{self.port}")
enforce(response.status_code == 200, "")
return True
except Exception as e: # pylint: disable=broad-except
logging.error("Exception: %s: %s", type(e).__name__, str(e))
logging.info(
"Attempt %s failed. Retrying in %s seconds...", i, sleep_rate
)
time.sleep(sleep_rate)
return False
28 changes: 28 additions & 0 deletions packages/balancer/agents/autonomous_fund/tests/helpers/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
AutonomousFundNetworkDockerImage,
DEFAULT_HARDHAT_ADDR,
DEFAULT_HARDHAT_PORT,
DEFAULT_JSON_SERVER_ADDR,
DEFAULT_JSON_SERVER_PORT,
MockFearAndGreedApi,
)


Expand Down Expand Up @@ -62,3 +65,28 @@ def _start_network(
use_safe_contracts=cls.USE_SAFE_CONTRACTS,
)
yield from launch_image(image, timeout=timeout, max_attempts=max_attempts)


@pytest.mark.integration
class UseMockFearAndGreedApiBaseTest: # pylint: disable=too-few-public-methods
"""Inherit from this class to use a mock Fear and Greed API."""

MOCK_API_ADDRESS = DEFAULT_JSON_SERVER_ADDR
MOCK_API_PORT = DEFAULT_JSON_SERVER_PORT

@classmethod
@pytest.fixture(autouse=True)
def _start_mock_api(
cls,
timeout: int = 3,
max_attempts: int = 200,
) -> Generator:
"""Start a Fear and Greed API instance."""
client = docker.from_env()
logging.info(f"Launching the Fear and Greed API on port {cls.MOCK_API_PORT}")
image = MockFearAndGreedApi(
client,
addr=cls.MOCK_API_ADDRESS,
port=cls.MOCK_API_PORT,
)
yield from launch_image(image, timeout=timeout, max_attempts=max_attempts)
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2022 Valory AG
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ------------------------------------------------------------------------------

"""Tests package for balancer/autonomous_fund agent."""
Loading

0 comments on commit 0756c4f

Please sign in to comment.