Ensure that --metrics-port is specified when --prometheus is used when running ursula.

Added/Cleaned up some tests
pull/2245/head
derekpierre 2020-09-11 12:11:24 -04:00
parent 0180bc8625
commit 7a9c57dcb4
4 changed files with 47 additions and 5 deletions

View File

@ -384,6 +384,11 @@ def run(general_config, character_options, config_file, interactive, dry_run, me
prometheus_config: 'PrometheusMetricsConfig' = None
if prometheus:
# ensure metrics port is provided
if not metrics_port:
raise click.BadOptionUsage(option_name='metrics-port',
message='--metrics-port is required when using --prometheus')
# Locally scoped to prevent import without prometheus explicitly installed
from nucypher.utilities.prometheus.metrics import PrometheusMetricsConfig
prometheus_config = PrometheusMetricsConfig(port=metrics_port,

View File

@ -57,9 +57,15 @@ class PrometheusMetricsConfig:
def __init__(self,
port: int,
metrics_prefix: str,
listen_address: str,
listen_address: str = '', # default to localhost ip
collection_interval: int = 10,
start_now: bool = False):
if not port:
raise ValueError('port must be provided')
if not metrics_prefix:
raise ValueError('metrics prefix must be provided')
self.port = port
self.metrics_prefix = metrics_prefix
self.listen_address = listen_address

View File

@ -20,6 +20,7 @@ from unittest import mock
import pytest
import pytest_twisted as pt
from click import BadOptionUsage
from twisted.internet import threads
from nucypher import utilities
@ -72,6 +73,26 @@ def test_ursula_rest_host_determination(click_runner, mocker):
assert isinstance(result.exception, UnknownIPAddress)
@pt.inlineCallbacks
def test_ursula_run_with_prometheus_but_no_metrics_port(click_runner):
args = ('ursula', 'run', # Stat Ursula Command
'--debug', # Display log output; Do not attach console
'--federated-only', # Operating Mode
'--dev', # Run in development mode (ephemeral node)
'--dry-run', # Disable twisted reactor in subprocess
'--lonely', # Do not load seednodes
'--prometheus' # Specify collection of prometheus metrics
)
result = yield threads.deferToThread(click_runner.invoke,
nucypher_cli, args,
catch_exceptions=False)
assert result.exit_code != 0
expected_error = f"Error: --metrics-port is required when using --prometheus"
assert expected_error in result.output
@pt.inlineCallbacks
def test_run_lone_federated_default_development_ursula(click_runner):
deploy_port = select_test_port()

View File

@ -44,27 +44,37 @@ TEST_PREFIX = 'test_prefix'
def test_prometheus_metrics_config():
listen_address = '111.111.111.111'
port = 2020
# no port
with pytest.raises(ValueError):
PrometheusMetricsConfig(port=None, metrics_prefix=TEST_PREFIX)
# no prefix
with pytest.raises(ValueError):
PrometheusMetricsConfig(port=port, metrics_prefix=None)
prometheus_config = PrometheusMetricsConfig(port=port,
metrics_prefix=TEST_PREFIX,
listen_address=listen_address)
metrics_prefix=TEST_PREFIX)
assert prometheus_config.port == 2020
assert prometheus_config.metrics_prefix == TEST_PREFIX
assert prometheus_config.listen_address == listen_address
assert prometheus_config.listen_address == ''
# defaults
assert prometheus_config.collection_interval == 10
assert not prometheus_config.start_now
assert prometheus_config.listen_address == ''
# non-defaults
collection_interval = 5
listen_address = '111.111.111.111'
prometheus_config = PrometheusMetricsConfig(port=port,
metrics_prefix=TEST_PREFIX,
listen_address=listen_address,
collection_interval=collection_interval,
start_now=True)
assert prometheus_config.listen_address == listen_address
assert prometheus_config.collection_interval == collection_interval
assert prometheus_config.start_now