add cli tests for auth config
This commit is contained in:
parent
6a254f76f1
commit
ff489210f3
|
@ -119,14 +119,13 @@ class PrometheusSD:
|
||||||
if not value:
|
if not value:
|
||||||
self.log.sysexit_with_message(f"Option '{name}' is required but not set")
|
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(
|
self.log.sysexit_with_message(
|
||||||
"Option 'pve.token_name' require 'pve.token_value' to be set"
|
"Either 'pve.password' or 'pve.token_name' and 'pve.token_value' "
|
||||||
)
|
"are required but not 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"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
self.logger.info(f"Using config file {config.config_file}")
|
self.logger.info(f"Using config file {config.config_file}")
|
||||||
|
|
|
@ -166,6 +166,8 @@ def defaults():
|
||||||
"password": "",
|
"password": "",
|
||||||
"server": "",
|
"server": "",
|
||||||
"user": "",
|
"user": "",
|
||||||
|
"token_name": "",
|
||||||
|
"token_value": "",
|
||||||
"verify_ssl": True
|
"verify_ssl": True
|
||||||
},
|
},
|
||||||
"service": True,
|
"service": True,
|
||||||
|
|
|
@ -29,6 +29,65 @@ def test_cli_required_error(mocker, capsys):
|
||||||
assert e.value.code == 1
|
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):
|
def test_cli_config_error(mocker, capsys):
|
||||||
mocker.patch(
|
mocker.patch(
|
||||||
"prometheuspvesd.config.SingleConfig.__init__",
|
"prometheuspvesd.config.SingleConfig.__init__",
|
||||||
|
|
|
@ -66,7 +66,7 @@ sections = ["FUTURE", "STDLIB", "THIRDPARTY", "FIRSTPARTY", "LOCALFOLDER"]
|
||||||
skip_glob = ["**/.env*", "**/env/*", "**/.venv/*", "**/docs/*"]
|
skip_glob = ["**/.env*", "**/env/*", "**/.venv/*", "**/docs/*"]
|
||||||
|
|
||||||
[tool.pytest.ini_options]
|
[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 = [
|
filterwarnings = [
|
||||||
"ignore::FutureWarning",
|
"ignore::FutureWarning",
|
||||||
"ignore::DeprecationWarning",
|
"ignore::DeprecationWarning",
|
||||||
|
|
Loading…
Reference in New Issue