From 14c0b9a479c3a0b6483ef8a8ac2109fbbe3c5bf8 Mon Sep 17 00:00:00 2001 From: Wouter de Bruijn Date: Thu, 12 Feb 2026 15:22:39 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Updated=20patch=20targets=20for=20n?= =?UTF-8?q?ew=20module=20structure?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_configuration_parsing.py | 34 +++++++++++++----- tests/test_device_deletion.py | 8 +++-- tests/test_hostgroups.py | 8 +++-- tests/test_physical_device.py | 54 +++++++++++++++++++---------- tests/test_usermacros.py | 10 +++--- 5 files changed, 79 insertions(+), 35 deletions(-) diff --git a/tests/test_configuration_parsing.py b/tests/test_configuration_parsing.py index 3da7e3a..e2b1293 100644 --- a/tests/test_configuration_parsing.py +++ b/tests/test_configuration_parsing.py @@ -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 diff --git a/tests/test_device_deletion.py b/tests/test_device_deletion.py index 6bba9d8..0d78f36 100644 --- a/tests/test_device_deletion.py +++ b/tests/test_device_deletion.py @@ -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, diff --git a/tests/test_hostgroups.py b/tests/test_hostgroups.py index 1eab424..4bab4aa 100644 --- a/tests/test_hostgroups.py +++ b/tests/test_hostgroups.py @@ -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"] diff --git a/tests/test_physical_device.py b/tests/test_physical_device.py index 5ef1ce6..0ee6ffc 100644 --- a/tests/test_physical_device.py +++ b/tests/test_physical_device.py @@ -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, diff --git a/tests/test_usermacros.py b/tests/test_usermacros.py index ca4d6a1..45576fc 100644 --- a/tests/test_usermacros.py +++ b/tests/test_usermacros.py @@ -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