monitoring/tests/test_cli.py

89 lines
2.2 KiB
Python

# coding: utf-8
"""Test CLI of the application."""
from runpy import run_module
from click.testing import CliRunner
from pytest import MonkeyPatch, CaptureFixture
from pytest_snapshot.plugin import Snapshot
import obsdsnmp
def printer(*args, **kwargs):
"""Monkeypatch function, that prints args kwargs to stdout.
Args:
args: positional arguments.
kwargs: key-value arguments.
"""
print(args, kwargs) # noqa: WPS421
async def async_printer(*args, **kwargs):
"""Monkeypatch function, that prints args kwargs to stdout.
Args:
args: positional arguments.
kwargs: key-value arguments.
"""
print(args, kwargs) # noqa: WPS421
def test_empty_run(snapshot: Snapshot):
"""Test invoking without arguments.
The app shall give error information.
Args:
snapshot: instance of pytest_snapshot fixture.
"""
runner = CliRunner()
invoke_result = runner.invoke(obsdsnmp.command, [])
assert invoke_result.exit_code == 2
snapshot.assert_match(invoke_result.output, 'empty_run')
def test_help(snapshot: Snapshot):
"""Test invoking help.
The app shall give help.
Args:
snapshot: instance of pytest_snapshot fixture.
"""
runner = CliRunner()
invoke_result = runner.invoke(obsdsnmp.command, ['--help'])
assert invoke_result.exit_code == 0
snapshot.assert_match(invoke_result.output, 'help')
def test_worker(monkeypatch: MonkeyPatch):
"""Test setting interval.
Args:
monkeypatch: pytest monkeypatch fixture.
"""
monkeypatch.setattr(obsdsnmp, 'worker', async_printer)
runner = CliRunner()
invoke_result = runner.invoke(obsdsnmp.command, ['-i', '1', 'test'])
assert invoke_result.exit_code == 0
assert invoke_result.output == '{0} {1}\n'.format((1.0, 'test', None), {})
def test_package(monkeypatch: MonkeyPatch, capsys: CaptureFixture):
"""Test __main__ invokation.
Args:
monkeypatch: pytest monkeypatch fixture.
capsys: capture stdout fixture.
"""
monkeypatch.setattr(obsdsnmp, 'command', printer)
run_module('obsdsnmp')
captured = capsys.readouterr()
assert captured.out == '{0} {1}\n'.format((), {
'auto_envvar_prefix': 'OBSDSNMP',
})