Skip to content

Commit

Permalink
fix: convert file mode string to ocal (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoxys authored Mar 28, 2022
1 parent da048eb commit ba2da53
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
2 changes: 1 addition & 1 deletion prometheuspvesd/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def _write(self, host_list: HostList):
json.dump(output, tf, indent=4)

shutil.move(temp_file.name, self.config.config["output_file"])
chmod(self.config.config["output_file"], self.config.config["output_file_mode"])
chmod(self.config.config["output_file"], int(self.config.config["output_file_mode"], 8))

def _terminate(self, signal, frame):
self.log.sysexit_with_message("Terminating", code=0)
Expand Down
2 changes: 1 addition & 1 deletion prometheuspvesd/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class Config():
"type": environs.Env().str
},
"output_file_mode": {
"default": "0644",
"default": "0640",
"env": "OUTPUT_FILE_MODE",
"file": True,
"type": environs.Env().str
Expand Down
35 changes: 35 additions & 0 deletions prometheuspvesd/test/fixtures/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import environs
import pytest

from prometheuspvesd.model import Host
from prometheuspvesd.model import HostList


@pytest.fixture
def builtins():
Expand Down Expand Up @@ -293,3 +296,35 @@ def networks():
}
},
]


@pytest.fixture
def inventory():
hostlist = HostList()
hostlist.add_host(Host("101", "host1", "129.168.0.1", False, "qemu"))
hostlist.add_host(Host("202", "host2", "129.168.0.2", False, "qemu"))

return hostlist


@pytest.fixture
def labels():
return [{
"targets": ["host1"],
"labels": {
"__meta_pve_ipv4": "129.168.0.1",
"__meta_pve_ipv6": "False",
"__meta_pve_name": "host1",
"__meta_pve_type": "qemu",
"__meta_pve_vmid": "101"
}
}, {
"targets": ["host2"],
"labels": {
"__meta_pve_ipv4": "129.168.0.2",
"__meta_pve_ipv6": "False",
"__meta_pve_name": "host2",
"__meta_pve_type": "qemu",
"__meta_pve_vmid": "202"
}
}]
18 changes: 18 additions & 0 deletions prometheuspvesd/test/unit/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Test CLI class."""
import json

import pytest
from proxmoxer import ProxmoxAPI

Expand Down Expand Up @@ -66,3 +68,19 @@ def test_cli_api_error(mocker, builtins, capsys):
stdout, stderr = capsys.readouterr()
assert "Proxmoxer API error: Dummy API Exception" in stderr
assert e.value.code == 1


def test_cli_write(mocker, tmp_path, builtins, inventory, labels):
temp = tmp_path / "temp.txt"
out = tmp_path / "out.txt"

builtins["output_file"]["default"] = out.as_posix()

mocker.patch.dict(Config.SETTINGS, builtins)
mocker.patch.object(Discovery, "_auth", return_value=mocker.create_autospec(ProxmoxAPI))
mocker.patch.object(Discovery, "propagate", return_value=inventory)
mocker.patch("tempfile.NamedTemporaryFile", return_value=temp.open("w"))

psd = PrometheusSD()
assert json.loads(out.read_text()) == labels
assert oct(out.stat().st_mode & 0o777) == oct(int(psd.config.config["output_file_mode"], 8))

0 comments on commit ba2da53

Please sign in to comment.