-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add optional functionality to create extra siteadmin host groups (#49)
- Loading branch information
Showing
6 changed files
with
234 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
import os | ||
from pathlib import Path | ||
from typing import Iterable | ||
import pytest | ||
|
||
|
||
|
@@ -95,3 +97,27 @@ def sample_config(): | |
os.path.dirname(os.path.dirname(__file__)) + "/config.sample.toml" | ||
) as config: | ||
yield config.read() | ||
|
||
|
||
@pytest.fixture | ||
def hostgroup_map_file(tmp_path: Path) -> Iterable[Path]: | ||
contents = hostgroup_map = """ | ||
# This file defines assosiation between siteadm fetched from Nivlheim and hostsgroups in Zabbix. | ||
# A siteadm can be assosiated only with one hostgroup or usergroup. | ||
# Example: <siteadm>:<host/user groupname> | ||
# | ||
#**************************************************************************************** | ||
# ATT: First letter will be capitilazed, leading and trailing spaces will be removed and | ||
# spaces within the hostgroupname will be replaced with "-" by the script automatically | ||
#**************************************************************************************** | ||
# | ||
[email protected]:Hostgroup-user1-primary | ||
# | ||
[email protected]:Hostgroup-user2-primary | ||
[email protected]:Hostgroup-user2-secondary | ||
# | ||
[email protected]:Hostgroup-user3-primary | ||
""" | ||
map_file_path = tmp_path / "siteadmin_hostgroup_map.txt" | ||
map_file_path.write_text(contents) | ||
yield map_file_path |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
from ipaddress import IPv4Address, IPv6Address | ||
import logging | ||
from ipaddress import IPv4Address, IPv6Address | ||
from pathlib import Path | ||
from typing import Dict, List, Set, Tuple, Union | ||
|
||
import pytest | ||
from pytest import LogCaptureFixture | ||
from hypothesis import HealthCheck, given, settings | ||
from hypothesis import strategies as st | ||
|
||
from zabbix_auto_config import utils | ||
from hypothesis import given, settings, strategies as st, HealthCheck, assume | ||
|
||
|
||
@pytest.mark.parametrize( | ||
|
@@ -155,3 +158,75 @@ def test_zac_tags2zabbix_tags( | |
zabbix_tags = utils.zac_tags2zabbix_tags(tags) | ||
for tag in expected: | ||
assert tag in zabbix_tags | ||
|
||
|
||
# Test with the two prefixes we use + no prefix | ||
@pytest.mark.parametrize( | ||
"prefix", | ||
["Templates-", "Siteadmin-"], | ||
) | ||
def test_mapping_values_with_prefix(hostgroup_map_file: Path, prefix: str): | ||
m = utils.read_map_file(hostgroup_map_file) | ||
|
||
# Make sure we read the map file correctly | ||
assert len(m) == 3 | ||
|
||
old_prefix = "Hostgroup-" | ||
new_map = utils.mapping_values_with_prefix( | ||
m, | ||
prefix=prefix, | ||
) | ||
|
||
# Compare new dict to old dict | ||
assert new_map is not m # we should get a new dict | ||
assert len(new_map) == len(m) | ||
assert sum(len(v) for v in new_map.values()) == sum(len(v) for v in m.values()) | ||
|
||
# Check values in new map | ||
assert new_map["[email protected]"] == [f"{prefix}user1-primary"] | ||
assert new_map["[email protected]"] == [ | ||
f"{prefix}user2-primary", | ||
f"{prefix}user2-secondary", | ||
] | ||
assert new_map["[email protected]"] == [f"{prefix}user3-primary"] | ||
|
||
# Check values in old map (they should be untouched) | ||
assert m["[email protected]"] == [f"{old_prefix}user1-primary"] | ||
assert m["[email protected]"] == [ | ||
f"{old_prefix}user2-primary", | ||
f"{old_prefix}user2-secondary", | ||
] | ||
assert m["[email protected]"] == [f"{old_prefix}user3-primary"] | ||
|
||
|
||
def test_mapping_values_with_prefix_no_prefix_arg(caplog: LogCaptureFixture) -> None: | ||
"""Passing an empty string as the prefix should be ignored and logged.""" | ||
res = utils.mapping_values_with_prefix( | ||
{"[email protected]": ["Hostgroup-user1-primary"]}, | ||
prefix="", | ||
) | ||
assert res == {"[email protected]": []} | ||
assert caplog.text.count("WARNING") == 1 | ||
|
||
|
||
def test_mapping_values_with_prefix_no_group_prefix(caplog: LogCaptureFixture) -> None: | ||
"""Passing a group name with no prefix separated by the separator | ||
should be ignored and logged.""" | ||
res = utils.mapping_values_with_prefix( | ||
{"[email protected]": ["Mygroup"]}, | ||
prefix="Foo-", | ||
) | ||
assert res == {"[email protected]": []} | ||
assert caplog.text.count("WARNING") == 1 | ||
|
||
|
||
def test_mapping_values_with_prefix_no_prefix_separator( | ||
caplog: LogCaptureFixture, | ||
) -> None: | ||
"""Passing a prefix with no separator emits a warning (but is otherwise legal).""" | ||
res = utils.mapping_values_with_prefix( | ||
{"[email protected]": ["Hostgroup-user1-primary", "Hostgroup-user1-secondary"]}, | ||
prefix="Foo", | ||
) | ||
assert res == {"[email protected]": ["Foouser1-primary", "Foouser1-secondary"]} | ||
assert caplog.text.count("WARNING") == 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters