Updated patch targets for new module structure

This commit is contained in:
Wouter de Bruijn
2026-02-12 15:22:39 +01:00
parent 22ebeaec1b
commit 14c0b9a479
5 changed files with 79 additions and 35 deletions
+26 -8
View File
@@ -14,8 +14,11 @@ from netbox_zabbix_sync.modules.config import (
def test_load_config_defaults():
"""Test that load_config returns default values when no config file or env vars are present"""
with (
patch("modules.config.load_config_file", return_value=DEFAULT_CONFIG.copy()),
patch("modules.config.load_env_variable", return_value=None),
patch(
"netbox_zabbix_sync.modules.config.load_config_file",
return_value=DEFAULT_CONFIG.copy(),
),
patch("netbox_zabbix_sync.modules.config.load_env_variable", return_value=None),
):
config = load_config()
assert config == DEFAULT_CONFIG
@@ -30,8 +33,11 @@ def test_load_config_file():
mock_config["sync_vms"] = True
with (
patch("modules.config.load_config_file", return_value=mock_config),
patch("modules.config.load_env_variable", return_value=None),
patch(
"netbox_zabbix_sync.modules.config.load_config_file",
return_value=mock_config,
),
patch("netbox_zabbix_sync.modules.config.load_env_variable", return_value=None),
):
config = load_config()
assert config["templates_config_context"] is True
@@ -52,8 +58,14 @@ def test_load_env_variables():
return None
with (
patch("modules.config.load_config_file", return_value=DEFAULT_CONFIG.copy()),
patch("modules.config.load_env_variable", side_effect=mock_load_env),
patch(
"netbox_zabbix_sync.modules.config.load_config_file",
return_value=DEFAULT_CONFIG.copy(),
),
patch(
"netbox_zabbix_sync.modules.config.load_env_variable",
side_effect=mock_load_env,
),
):
config = load_config()
assert config["sync_vms"] is True
@@ -75,8 +87,14 @@ def test_env_vars_override_config_file():
return None
with (
patch("modules.config.load_config_file", return_value=mock_config),
patch("modules.config.load_env_variable", side_effect=mock_load_env),
patch(
"netbox_zabbix_sync.modules.config.load_config_file",
return_value=mock_config,
),
patch(
"netbox_zabbix_sync.modules.config.load_env_variable",
side_effect=mock_load_env,
),
):
config = load_config()
# This should be overridden by the env var
+6 -2
View File
@@ -41,7 +41,9 @@ class TestDeviceDeletion(unittest.TestCase):
self.mock_logger = MagicMock()
# Create PhysicalDevice instance with mocks
with patch("modules.device.config", {"device_cf": "zabbix_hostid"}):
with patch(
"netbox_zabbix_sync.modules.device.config", {"device_cf": "zabbix_hostid"}
):
self.device = PhysicalDevice(
self.mock_nb_device,
self.mock_zabbix,
@@ -147,7 +149,9 @@ class TestDeviceDeletion(unittest.TestCase):
def test_create_journal_entry_when_disabled(self):
"""Test create_journal_entry when journaling is disabled."""
# Setup - create device with journal=False
with patch("modules.device.config", {"device_cf": "zabbix_hostid"}):
with patch(
"netbox_zabbix_sync.modules.device.config", {"device_cf": "zabbix_hostid"}
):
device = PhysicalDevice(
self.mock_nb_device,
self.mock_zabbix,
+6 -2
View File
@@ -262,7 +262,9 @@ class TestHostgroups(unittest.TestCase):
def test_nested_region_hostgroups(self):
"""Test hostgroup generation with nested regions."""
# Mock the build_path function to return a predictable result
with patch("modules.hostgroups.build_path") as mock_build_path:
with patch(
"netbox_zabbix_sync.modules.hostgroups.build_path"
) as mock_build_path:
# Configure the mock to return a list of regions in the path
mock_build_path.return_value = ["ParentRegion", "TestRegion"]
@@ -284,7 +286,9 @@ class TestHostgroups(unittest.TestCase):
def test_nested_sitegroup_hostgroups(self):
"""Test hostgroup generation with nested site groups."""
# Mock the build_path function to return a predictable result
with patch("modules.hostgroups.build_path") as mock_build_path:
with patch(
"netbox_zabbix_sync.modules.hostgroups.build_path"
) as mock_build_path:
# Configure the mock to return a list of site groups in the path
mock_build_path.return_value = ["ParentSiteGroup", "TestSiteGroup"]
+36 -18
View File
@@ -37,7 +37,7 @@ class TestPhysicalDevice(unittest.TestCase):
# Create PhysicalDevice instance with mocks
with patch(
"modules.device.config",
"netbox_zabbix_sync.modules.device.config",
{
"device_cf": "zabbix_hostid",
"template_cf": "zabbix_template",
@@ -76,8 +76,11 @@ class TestPhysicalDevice(unittest.TestCase):
# We need to patch the search function to simulate finding special characters
with (
patch("modules.device.search") as mock_search,
patch("modules.device.config", {"device_cf": "zabbix_hostid"}),
patch("netbox_zabbix_sync.modules.device.search") as mock_search,
patch(
"netbox_zabbix_sync.modules.device.config",
{"device_cf": "zabbix_hostid"},
),
):
# Make the search function return True to simulate special characters
mock_search.return_value = True
@@ -105,7 +108,9 @@ class TestPhysicalDevice(unittest.TestCase):
}
# Create device with the updated mock
with patch("modules.device.config", {"device_cf": "zabbix_hostid"}):
with patch(
"netbox_zabbix_sync.modules.device.config", {"device_cf": "zabbix_hostid"}
):
device = PhysicalDevice(
self.mock_nb_device,
self.mock_zabbix,
@@ -124,7 +129,9 @@ class TestPhysicalDevice(unittest.TestCase):
self.mock_nb_device.config_context = {"zabbix": {"templates": "Template1"}}
# Create device with the updated mock
with patch("modules.device.config", {"device_cf": "zabbix_hostid"}):
with patch(
"netbox_zabbix_sync.modules.device.config", {"device_cf": "zabbix_hostid"}
):
device = PhysicalDevice(
self.mock_nb_device,
self.mock_zabbix,
@@ -143,7 +150,9 @@ class TestPhysicalDevice(unittest.TestCase):
self.mock_nb_device.config_context = {}
# Create device with the updated mock
with patch("modules.device.config", {"device_cf": "zabbix_hostid"}):
with patch(
"netbox_zabbix_sync.modules.device.config", {"device_cf": "zabbix_hostid"}
):
device = PhysicalDevice(
self.mock_nb_device,
self.mock_zabbix,
@@ -162,7 +171,9 @@ class TestPhysicalDevice(unittest.TestCase):
self.mock_nb_device.config_context = {"zabbix": {}}
# Create device with the updated mock
with patch("modules.device.config", {"device_cf": "zabbix_hostid"}):
with patch(
"netbox_zabbix_sync.modules.device.config", {"device_cf": "zabbix_hostid"}
):
device = PhysicalDevice(
self.mock_nb_device,
self.mock_zabbix,
@@ -184,7 +195,10 @@ class TestPhysicalDevice(unittest.TestCase):
with patch.object(
PhysicalDevice, "get_templates_context", return_value=["Template1"]
):
with patch("modules.device.config", {"device_cf": "zabbix_hostid"}):
with patch(
"netbox_zabbix_sync.modules.device.config",
{"device_cf": "zabbix_hostid"},
):
device = PhysicalDevice(
self.mock_nb_device,
self.mock_zabbix,
@@ -211,7 +225,7 @@ class TestPhysicalDevice(unittest.TestCase):
"inventory_sync": False,
}
with patch("modules.device.config", config_patch):
with patch("netbox_zabbix_sync.modules.device.config", config_patch):
device = PhysicalDevice(
self.mock_nb_device,
self.mock_zabbix,
@@ -221,7 +235,7 @@ class TestPhysicalDevice(unittest.TestCase):
)
# Call set_inventory with the config patch still active
with patch("modules.device.config", config_patch):
with patch("netbox_zabbix_sync.modules.device.config", config_patch):
result = device.set_inventory({})
# Check result
@@ -238,7 +252,7 @@ class TestPhysicalDevice(unittest.TestCase):
"inventory_sync": False,
}
with patch("modules.device.config", config_patch):
with patch("netbox_zabbix_sync.modules.device.config", config_patch):
device = PhysicalDevice(
self.mock_nb_device,
self.mock_zabbix,
@@ -248,7 +262,7 @@ class TestPhysicalDevice(unittest.TestCase):
)
# Call set_inventory with the config patch still active
with patch("modules.device.config", config_patch):
with patch("netbox_zabbix_sync.modules.device.config", config_patch):
result = device.set_inventory({})
# Check result
@@ -264,7 +278,7 @@ class TestPhysicalDevice(unittest.TestCase):
"inventory_sync": False,
}
with patch("modules.device.config", config_patch):
with patch("netbox_zabbix_sync.modules.device.config", config_patch):
device = PhysicalDevice(
self.mock_nb_device,
self.mock_zabbix,
@@ -274,7 +288,7 @@ class TestPhysicalDevice(unittest.TestCase):
)
# Call set_inventory with the config patch still active
with patch("modules.device.config", config_patch):
with patch("netbox_zabbix_sync.modules.device.config", config_patch):
result = device.set_inventory({})
# Check result
@@ -291,7 +305,7 @@ class TestPhysicalDevice(unittest.TestCase):
"device_inventory_map": {"name": "name", "serial": "serialno_a"},
}
with patch("modules.device.config", config_patch):
with patch("netbox_zabbix_sync.modules.device.config", config_patch):
device = PhysicalDevice(
self.mock_nb_device,
self.mock_zabbix,
@@ -304,7 +318,7 @@ class TestPhysicalDevice(unittest.TestCase):
mock_device_data = {"name": "test-device", "serial": "ABC123"}
# Call set_inventory with the config patch still active
with patch("modules.device.config", config_patch):
with patch("netbox_zabbix_sync.modules.device.config", config_patch):
result = device.set_inventory(mock_device_data)
# Check result
@@ -320,7 +334,9 @@ class TestPhysicalDevice(unittest.TestCase):
self.mock_nb_device.virtual_chassis = MagicMock()
# Create device with the updated mock
with patch("modules.device.config", {"device_cf": "zabbix_hostid"}):
with patch(
"netbox_zabbix_sync.modules.device.config", {"device_cf": "zabbix_hostid"}
):
device = PhysicalDevice(
self.mock_nb_device,
self.mock_zabbix,
@@ -338,7 +354,9 @@ class TestPhysicalDevice(unittest.TestCase):
self.mock_nb_device.virtual_chassis = None
# Create device with the updated mock
with patch("modules.device.config", {"device_cf": "zabbix_hostid"}):
with patch(
"netbox_zabbix_sync.modules.device.config", {"device_cf": "zabbix_hostid"}
):
device = PhysicalDevice(
self.mock_nb_device,
self.mock_zabbix,
+5 -5
View File
@@ -51,7 +51,7 @@ class TestUsermacroSync(unittest.TestCase):
return device
@patch(
"modules.device.config",
"netbox_zabbix_sync.modules.device.config",
{"usermacro_sync": False, "device_cf": "zabbix_hostid", "tag_sync": False},
)
@patch.object(PhysicalDevice, "_usermacro_map")
@@ -66,10 +66,10 @@ class TestUsermacroSync(unittest.TestCase):
self.assertTrue(result is True or result is None)
@patch(
"modules.device.config",
"netbox_zabbix_sync.modules.device.config",
{"usermacro_sync": True, "device_cf": "zabbix_hostid", "tag_sync": False},
)
@patch("modules.device.ZabbixUsermacros")
@patch("netbox_zabbix_sync.modules.device.ZabbixUsermacros")
@patch.object(PhysicalDevice, "_usermacro_map")
def test_usermacro_sync_true(self, mock_usermacro_map, mock_usermacros_class):
mock_usermacro_map.return_value = self.usermacro_map
@@ -90,10 +90,10 @@ class TestUsermacroSync(unittest.TestCase):
self.assertGreater(len(device.usermacros), 0)
@patch(
"modules.device.config",
"netbox_zabbix_sync.modules.device.config",
{"usermacro_sync": "full", "device_cf": "zabbix_hostid", "tag_sync": False},
)
@patch("modules.device.ZabbixUsermacros")
@patch("netbox_zabbix_sync.modules.device.ZabbixUsermacros")
@patch.object(PhysicalDevice, "_usermacro_map")
def test_usermacro_sync_full(self, mock_usermacro_map, mock_usermacros_class):
mock_usermacro_map.return_value = self.usermacro_map