From 9750fd07e4bf15d2574ff1e02a1a0129f77e9343 Mon Sep 17 00:00:00 2001 From: ajasnosz Date: Thu, 19 Dec 2024 11:28:53 +0100 Subject: [PATCH] test: add unit tests --- splunk_connect_for_snmp/snmp/tasks.py | 2 +- test/snmp/test_tasks.py | 120 +++++++++++++------------- 2 files changed, 61 insertions(+), 61 deletions(-) diff --git a/splunk_connect_for_snmp/snmp/tasks.py b/splunk_connect_for_snmp/snmp/tasks.py index e30c843ab..d80647263 100644 --- a/splunk_connect_for_snmp/snmp/tasks.py +++ b/splunk_connect_for_snmp/snmp/tasks.py @@ -207,7 +207,7 @@ def trap(self, work): } -def format_ipv4_address(host): +def format_ipv4_address(host: str) -> str: # IPv4 addresses from IPv6 socket have added ::ffff: prefix, which is removed if IPv6_ENABLED and "." in host: return host.split(":")[-1] diff --git a/test/snmp/test_tasks.py b/test/snmp/test_tasks.py index 0a782fdc4..54db54b09 100644 --- a/test/snmp/test_tasks.py +++ b/test/snmp/test_tasks.py @@ -6,6 +6,66 @@ @patch("pymongo.MongoClient") class TestTasks(TestCase): + @patch("splunk_connect_for_snmp.snmp.tasks.IPv6_ENABLED", True) + # @patch("splunk_connect_for_snmp.snmp.manager.Poller.__init__") + # @patch("time.time") + def test_format_ipv4_address( + self, + # m_time, + # m_poller, + ipv6_enabled, + m_mongo_client, + ): + # m_poller.return_value = None + from splunk_connect_for_snmp.snmp.tasks import format_ipv4_address + + # m_mongo_client.return_value = MagicMock() + # m_time.return_value = 1640692955.365186 + + ip_address = "::ffff:172.31.20.76" + host = format_ipv4_address(ip_address) + self.assertEqual(host, "172.31.20.76") + + # @patch("splunk_connect_for_snmp.snmp.manager.Poller.__init__") + # @patch("time.time") + # @patch("splunk_connect_for_snmp.snmp.tasks.IPv6_ENABLED", False) + def test_format_ipv4_address_disabled( + self, + # m_time, + # m_poller, + # ipv6_enabled, + m_mongo_client, + ): + # m_poller.return_value = None + from splunk_connect_for_snmp.snmp.tasks import format_ipv4_address + + # m_mongo_client.return_value = MagicMock() + # m_time.return_value = 1640692955.365186 + + ip_address = "::ffff:172.31.20.76" + host = format_ipv4_address(ip_address) + self.assertEqual(host, "::ffff:172.31.20.76") + + # @patch("splunk_connect_for_snmp.snmp.manager.Poller.__init__") + # @patch("time.time") + # @patch("splunk_connect_for_snmp.snmp.tasks.IPv6_ENABLED", True) + def test_format_ipv4_address_ipv6( + self, + # m_time, + # m_poller, + # ipv6_enabled, + m_mongo_client, + ): + # m_poller.return_value = None + from splunk_connect_for_snmp.snmp.tasks import format_ipv4_address + + # m_mongo_client.return_value = MagicMock() + # m_time.return_value = 1640692955.365186 + + ip_address = "fd02::b24a:409e:a35e:b580" + host = format_ipv4_address(ip_address) + self.assertEqual(host, "fd02::b24a:409e:a35e:b580") + @patch("splunk_connect_for_snmp.snmp.manager.get_inventory") @patch("splunk_connect_for_snmp.snmp.manager.Poller.__init__") @patch("splunk_connect_for_snmp.snmp.manager.Poller.do_work") @@ -289,63 +349,3 @@ def test_trap_reverse_dns_lookup( }, result, ) - - @patch("splunk_connect_for_snmp.snmp.tasks.IPv6_ENABLED", True) - # @patch("splunk_connect_for_snmp.snmp.manager.Poller.__init__") - @patch("time.time") - def test_format_ipv4_address( - self, - m_time, - # m_poller, - ipv6_enabled, - m_mongo_client, - ): - # m_poller.return_value = None - from splunk_connect_for_snmp.snmp.tasks import format_ipv4_address - - m_mongo_client.return_value = MagicMock() - m_time.return_value = 1640692955.365186 - - ip_address = "::ffff:172.31.20.76" - host = format_ipv4_address(ip_address) - self.assertEqual(host, "172.31.20.76") - - # @patch("splunk_connect_for_snmp.snmp.manager.Poller.__init__") - @patch("time.time") - # @patch("splunk_connect_for_snmp.snmp.tasks.IPv6_ENABLED", False) - def test_format_ipv4_address_disabled( - self, - m_time, - # m_poller, - # ipv6_enabled, - m_mongo_client, - ): - # m_poller.return_value = None - from splunk_connect_for_snmp.snmp.tasks import format_ipv4_address - - m_mongo_client.return_value = MagicMock() - # m_time.return_value = 1640692955.365186 - - ip_address = "::ffff:172.31.20.76" - host = format_ipv4_address(ip_address) - self.assertEqual(host, "::ffff:172.31.20.76") - - # @patch("splunk_connect_for_snmp.snmp.manager.Poller.__init__") - @patch("time.time") - # @patch("splunk_connect_for_snmp.snmp.tasks.IPv6_ENABLED", True) - def test_format_ipv4_address_ipv6( - self, - m_time, - # m_poller, - # ipv6_enabled, - m_mongo_client, - ): - # m_poller.return_value = None - from splunk_connect_for_snmp.snmp.tasks import format_ipv4_address - - m_mongo_client.return_value = MagicMock() - m_time.return_value = 1640692955.365186 - - ip_address = "fd02::b24a:409e:a35e:b580" - host = format_ipv4_address(ip_address) - self.assertEqual(host, "fd02::b24a:409e:a35e:b580")