diff --git a/prometheuspvesd/cli.py b/prometheuspvesd/cli.py index 30a22d7..4c18666 100644 --- a/prometheuspvesd/cli.py +++ b/prometheuspvesd/cli.py @@ -119,14 +119,13 @@ class PrometheusSD: if not value: self.log.sysexit_with_message(f"Option '{name}' is required but not set") - if config.config["pve"]["token_name"] and not config.config["pve"]["token_value"]: + if ( + not config.config["pve"]["password"] + and not (config.config["pve"]["token_name"] and config.config["pve"]["token_value"]) + ): self.log.sysexit_with_message( - "Option 'pve.token_name' require 'pve.token_value' to be set" - ) - - if not config.config["pve"]["token_name"] and not config.config["pve"]["password"]: - self.log.sysexit_with_message( - "Neither password nor API token have been set for pve authentication" + "Either 'pve.password' or 'pve.token_name' and 'pve.token_value' " + "are required but not set" ) self.logger.info(f"Using config file {config.config_file}") diff --git a/prometheuspvesd/test/fixtures/fixtures.py b/prometheuspvesd/test/fixtures/fixtures.py index 260f81e..bed5716 100644 --- a/prometheuspvesd/test/fixtures/fixtures.py +++ b/prometheuspvesd/test/fixtures/fixtures.py @@ -166,6 +166,8 @@ def defaults(): "password": "", "server": "", "user": "", + "token_name": "", + "token_value": "", "verify_ssl": True }, "service": True, diff --git a/prometheuspvesd/test/unit/test_cli.py b/prometheuspvesd/test/unit/test_cli.py index ccf234e..6133028 100644 --- a/prometheuspvesd/test/unit/test_cli.py +++ b/prometheuspvesd/test/unit/test_cli.py @@ -29,6 +29,65 @@ def test_cli_required_error(mocker, capsys): assert e.value.code == 1 +@pytest.mark.parametrize( + "testinput", [{ + "pve.user": "dummy", + "pve.password": "", + "pve.token_name": "", + "pve.token_value": "" + }, { + "pve.user": "dummy", + "pve.password": "", + "pve.token_name": "dummy", + "pve.token_value": "" + }, { + "pve.user": "dummy", + "pve.password": "", + "pve.token_name": "", + "pve.token_value": "dummy" + }] +) +def test_cli_auth_required_error(mocker, capsys, builtins, testinput): + for key, value in testinput.items(): + builtins[key]["default"] = value + + mocker.patch.dict(Config.SETTINGS, builtins) + mocker.patch.object(ProxmoxClient, "_auth", return_value=mocker.create_autospec(ProxmoxAPI)) + mocker.patch.object(PrometheusSD, "_fetch", return_value=True) + + with pytest.raises(SystemExit) as e: + PrometheusSD() + + stdout, stderr = capsys.readouterr() + assert "Either 'pve.password' or 'pve.token_name' and 'pve.token_value' are required but not set" in stderr + assert e.value.code == 1 + + +@pytest.mark.parametrize( + "testinput", [{ + "pve.password": "dummy", + "pve.token_name": "", + "pve.token_value": "" + }, { + "pve.password": "", + "pve.token_name": "dummy", + "pve.token_value": "dummy" + }] +) +def test_cli_auth_no_error(mocker, capsys, builtins, testinput): + for key, value in testinput.items(): + builtins[key]["default"] = value + + mocker.patch.dict(Config.SETTINGS, builtins) + mocker.patch.object(ProxmoxClient, "_auth", return_value=mocker.create_autospec(ProxmoxAPI)) + mocker.patch.object(PrometheusSD, "_fetch", return_value=True) + + psd = PrometheusSD() + + for key, value in testinput.items(): + assert psd.config.config["pve"][key.split(".")[1]] == value + + def test_cli_config_error(mocker, capsys): mocker.patch( "prometheuspvesd.config.SingleConfig.__init__", diff --git a/pyproject.toml b/pyproject.toml index 6735756..ef025cd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -66,7 +66,7 @@ sections = ["FUTURE", "STDLIB", "THIRDPARTY", "FIRSTPARTY", "LOCALFOLDER"] skip_glob = ["**/.env*", "**/env/*", "**/.venv/*", "**/docs/*"] [tool.pytest.ini_options] -addopts = "prometheuspvesd --cov=prometheuspvesd --cov-report=xml:coverage.xml --cov-report=term --no-cov-on-fail" +addopts = "prometheuspvesd --cov=prometheuspvesd --cov-report=xml:coverage.xml --cov-report=term-missing --no-cov-on-fail --cov-fail-under=80" filterwarnings = [ "ignore::FutureWarning", "ignore::DeprecationWarning",