Skip to content

Commit

Permalink
test: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ajasnosz committed Dec 19, 2024
1 parent 945235f commit 9750fd0
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 61 deletions.
2 changes: 1 addition & 1 deletion splunk_connect_for_snmp/snmp/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
120 changes: 60 additions & 60 deletions test/snmp/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")

0 comments on commit 9750fd0

Please sign in to comment.