Skip to content

Commit

Permalink
feat: add unit test for filled config
Browse files Browse the repository at this point in the history
  • Loading branch information
aimxhaisse committed Nov 20, 2023
1 parent 94cd415 commit 8b5de16
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 13 deletions.
2 changes: 1 addition & 1 deletion etc/config.local.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ web3signer_url: ~
default_fee_recipient: ~
slack_channel: ~
slack_token: ~
relay_url: ~
relays: ~
liveness_file: ~

watched_keys:
Expand Down
8 changes: 4 additions & 4 deletions eth_validator_watcher/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ class Config:
default_fee_recipient: Optional[str] = os.getenv('ETH_WATCHER_DEFAULT_FEE_RECIPIENT')
slack_channel: Optional[str] = os.getenv('ETH_WATCHER_SLACK_CHANNEL')
slack_token: Optional[str] = os.getenv('ETH_WATCHER_SLACK_TOKEN')
relay_url: Optional[List[str]] = field(default_factory=lambda: os.getenv('ETH_WATCHER_RELAY_URL', '').split(','))
relays: Optional[List[str]] = field(default_factory=lambda: list(filter(bool, os.getenv('ETH_WATCHER_RELAY_URL', '').split(','))))
liveness_file: Optional[str] = os.getenv('ETH_WATCHER_LIVENESS_FILE')

watched_keys: Optional[List[WatchedKeyConfig]] = None
watched_keys: List[WatchedKeyConfig] = field(default_factory=list)


def load_config(config_file: str) -> Config:
Expand Down Expand Up @@ -66,7 +66,7 @@ def get_value(value: Any, key: str) -> Any:
config.slack_channel = get_value(config.slack_channel, 'slack_channel')
config.slack_token = get_value(config.slack_token, 'slack_token')
config.beacon_type = get_value(config.beacon_type, 'beacon_type')
config.relay_url = get_value(config.relay_url, 'relay_url')
config.relays = get_value(config.relays or None, 'relays')
config.liveness_file = get_value(config.liveness_file, 'liveness_file')

# More complex settings that can't.
Expand All @@ -75,7 +75,7 @@ def get_value(value: Any, key: str) -> Any:
public_key=config_key.get('public_key'),
labels=config_key.get('label'),
fee_recipient=config_key.get('fee_recipient', config.default_fee_recipient),
) for config_key in yml.get('watched_keys')
) for config_key in yml.get('watched_keys') or ()
]

return config
2 changes: 1 addition & 1 deletion tests/config/assets/config.empty.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ web3signer_url: ~
default_fee_recipient: ~
slack_channel: ~
slack_token: ~
relay_url: ~
relays: ~
liveness_file: ~
watched_keys: ~
8 changes: 5 additions & 3 deletions tests/config/assets/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ beacon_url: http://localhost:5051/
beacon_type: other
execution_url: http://localhost:8545/
web3signer_url: http://web3signer:9000/
default_fee_recipient: 0x41bF25fC8C52d292bD66D3BCEcd8a919ecB9EF88
default_fee_recipient: '0x41bF25fC8C52d292bD66D3BCEcd8a919ecB9EF88'
slack_channel: '#ethereum-monitoring'
slack_token: 'secret'
relay_url: http://relay1,http://relay2
relays:
- http://relay1
- http://relay2
liveness_file: /tmp/i-am-alive

watched_keys:
- public_key: 0x832b8286f5d6535fd941c6c4ed8b9b20d214fc6aa726ce4fba1c9dbb4f278132646304f550e557231b6932aa02cf08d3
- public_key: '0x832b8286f5d6535fd941c6c4ed8b9b20d214fc6aa726ce4fba1c9dbb4f278132646304f550e557231b6932aa02cf08d3'
labels: ["google"]
fee_recipient: ~
38 changes: 34 additions & 4 deletions tests/config/test_load_config.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,38 @@
from pathlib import Path

from eth_validator_watcher.config import load_config
from eth_validator_watcher.config import load_config, WatchedKeyConfig
from eth_validator_watcher.models import BeaconType
from tests.config import assets


def test_empty_config() -> None:
Path(assets.__file__).parent / ""
config = load_config()
path = Path(assets.__file__).parent / "config.empty.yaml"
config = load_config(path)

assert config.beacon_url is None
assert config.execution_url is None
assert config.web3signer_url is None
assert config.default_fee_recipient is None
assert config.slack_channel is None
assert config.slack_token is None
assert config.beacon_type == BeaconType.OTHER
assert config.relays is None
assert config.liveness_file is None

assert config.watched_keys == []


def test_filled_config() -> None:
path = Path(assets.__file__).parent / "config.yaml"
config = load_config(path)

assert config.beacon_url == 'http://localhost:5051/'
assert config.execution_url == 'http://localhost:8545/'
assert config.web3signer_url == 'http://web3signer:9000/'
assert config.default_fee_recipient == '0x41bF25fC8C52d292bD66D3BCEcd8a919ecB9EF88'
assert config.slack_channel == '#ethereum-monitoring'
assert config.slack_token == 'secret'
assert config.beacon_type == BeaconType.OTHER
assert config.relays == ['http://relay1', 'http://relay2']
assert config.liveness_file == '/tmp/i-am-alive'

assert [k.public_key for k in config.watched_keys] == ['0x832b8286f5d6535fd941c6c4ed8b9b20d214fc6aa726ce4fba1c9dbb4f278132646304f550e557231b6932aa02cf08d3']

0 comments on commit 8b5de16

Please sign in to comment.