Skip to content

Commit

Permalink
fix: replace deprecated ruamel.yaml.safe_load (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoxys authored Mar 6, 2022
1 parent c6b6390 commit 5963668
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/content/configuration/defaults.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ exclude_tags: []
pve:
server:
user:
password
password:
auth_timeout: 5
verify_ssl: true

Expand Down
2 changes: 1 addition & 1 deletion prometheuspvesd/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def _set_config(self):
with open(config, "r", encoding="utf8") as stream:
s = stream.read()
try:
file_dict = ruamel.yaml.safe_load(s)
file_dict = ruamel.yaml.YAML(typ="safe", pure=True).load(s)
except (
ruamel.yaml.composer.ComposerError, ruamel.yaml.scanner.ScannerError
) as e:
Expand Down
24 changes: 24 additions & 0 deletions prometheuspvesd/test/data/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
logging:
level: warning
format: console

metrics:
enabled: true
address: "127.0.0.1"
port: 8000

output_file: dummy
loop_delay: 300
service: true

exclude_state: []
exclude_vmid: []
exclude_tags: []

pve:
server: proxmox.example.com
user: root
password: secure
auth_timeout: 5
verify_ssl: true
4 changes: 0 additions & 4 deletions prometheuspvesd/test/data/config_error.yml

This file was deleted.

Empty file.
6 changes: 3 additions & 3 deletions prometheuspvesd/test/fixtures/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ def defaults():
"output_file": "dummy",
"pve": {
"auth_timeout": 5,
"password": "dummypass",
"server": "dummyserver",
"user": "dummyuser",
"password": "",
"server": "",
"user": "",
"verify_ssl": True
},
"service": True,
Expand Down
35 changes: 35 additions & 0 deletions prometheuspvesd/test/unit/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""Test Config class."""
import pytest
import ruamel.yaml

import prometheuspvesd.exception
from prometheuspvesd.config import Config

pytest_plugins = [
"prometheuspvesd.test.fixtures.fixtures",
]


def test_yaml_config(mocker, defaults):
mocker.patch(
"prometheuspvesd.config.default_config_file", "./prometheuspvesd/test/data/config.yml"
)
config = Config()

defaults["pve"]["user"] = "root"
defaults["pve"]["password"] = "secure"
defaults["pve"]["server"] = "proxmox.example.com"

assert config.config == defaults


def test_yaml_config_error(mocker, capsys):
mocker.patch(
"prometheuspvesd.config.default_config_file", "./prometheuspvesd/test/data/config.yml"
)
mocker.patch.object(ruamel.yaml.YAML, "load", side_effect=ruamel.yaml.composer.ComposerError)

with pytest.raises(prometheuspvesd.exception.ConfigError) as e:
Config()

assert "Unable to read config file ./prometheuspvesd/test/data/config.yml" in str(e.value)

0 comments on commit 5963668

Please sign in to comment.