Skip to content

Commit

Permalink
test: add unit tests [run-int-tests]
Browse files Browse the repository at this point in the history
  • Loading branch information
ajasnosz committed Dec 18, 2024
1 parent b207d63 commit 0dcc3d0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
11 changes: 8 additions & 3 deletions splunk_connect_for_snmp/snmp/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,7 @@ def trap(self, work):
remotemibs = set()
metrics = {}

# IPv4 addresses from IPv6 socket have added ::ffff: prefix, which is removed
if IPv6_ENABLED and "." in work["host"]:
work["host"] = work["host"].split(":")[-1]
work["host"] = format_ipv4_address(work["host"])

for w in work["data"]:

Expand Down Expand Up @@ -207,3 +205,10 @@ def trap(self, work):
"detectchange": False,
"sourcetype": SPLUNK_SOURCETYPE_TRAPS,
}


def format_ipv4_address(host):
# IPv4 addresses from IPv6 socket have added ::ffff: prefix, which is removed
if IPv6_ENABLED and "." in host:
return host.split(":")[-1]
return host
24 changes: 24 additions & 0 deletions test/snmp/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,27 @@ def test_trap_reverse_dns_lookup(
},
result,
)

@patch("splunk_connect_for_snmp.snmp.tasks.IPv6_ENABLED", True)
def test_format_ipv4_address(self, ipv6_enabled):
from splunk_connect_for_snmp.snmp.tasks import format_ipv4_address

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.tasks.IPv6_ENABLED", False)
def test_format_ipv4_address_disabled(self, ipv6_enabled):
from splunk_connect_for_snmp.snmp.tasks import format_ipv4_address

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.tasks.IPv6_ENABLED", True)
def test_format_ipv4_address_ipv6(self, ipv6_enabled):
from splunk_connect_for_snmp.snmp.tasks import format_ipv4_address

ip_address = " fd02::b24a:409e:a35e:b580"
host = format_ipv4_address(ip_address)
self.assertEqual(host, " fd02::b24a:409e:a35e:b580")

0 comments on commit 0dcc3d0

Please sign in to comment.