summaryrefslogtreecommitdiffstats
path: root/testing/mozbase/moznetwork/tests
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 17:32:43 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 17:32:43 +0000
commit6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch)
treea68f146d7fa01f0134297619fbe7e33db084e0aa /testing/mozbase/moznetwork/tests
parentInitial commit. (diff)
downloadthunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.tar.xz
thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.zip
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'testing/mozbase/moznetwork/tests')
-rw-r--r--testing/mozbase/moznetwork/tests/manifest.ini3
-rw-r--r--testing/mozbase/moznetwork/tests/test_moznetwork.py73
2 files changed, 76 insertions, 0 deletions
diff --git a/testing/mozbase/moznetwork/tests/manifest.ini b/testing/mozbase/moznetwork/tests/manifest.ini
new file mode 100644
index 0000000000..8057892c67
--- /dev/null
+++ b/testing/mozbase/moznetwork/tests/manifest.ini
@@ -0,0 +1,3 @@
+[DEFAULT]
+subsuite = mozbase
+[test_moznetwork.py]
diff --git a/testing/mozbase/moznetwork/tests/test_moznetwork.py b/testing/mozbase/moznetwork/tests/test_moznetwork.py
new file mode 100644
index 0000000000..8a1ee31a2e
--- /dev/null
+++ b/testing/mozbase/moznetwork/tests/test_moznetwork.py
@@ -0,0 +1,73 @@
+#!/usr/bin/env python
+"""
+Unit-Tests for moznetwork
+"""
+
+import re
+import subprocess
+from distutils.spawn import find_executable
+from unittest import mock
+
+import mozinfo
+import moznetwork
+import mozunit
+import pytest
+
+
+@pytest.fixture(scope="session")
+def ip_addresses():
+ """List of IP addresses associated with the host."""
+
+ # Regex to match IPv4 addresses.
+ # 0-255.0-255.0-255.0-255, note order is important here.
+ regexip = re.compile(
+ "((25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)\.){3}"
+ "(25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)"
+ )
+
+ commands = (
+ ["ip", "addr", "show"],
+ ["ifconfig"],
+ ["ipconfig"],
+ # Explicitly search '/sbin' because it doesn't always appear
+ # to be on the $PATH of all systems
+ ["/sbin/ip", "addr", "show"],
+ ["/sbin/ifconfig"],
+ )
+
+ cmd = None
+ for command in commands:
+ if find_executable(command[0]):
+ cmd = command
+ break
+ else:
+ raise OSError(
+ "No program for detecting ip address found! Ensure one of 'ip', "
+ "'ifconfig' or 'ipconfig' exists on your $PATH."
+ )
+
+ ps = subprocess.Popen(cmd, stdout=subprocess.PIPE)
+ standardoutput, _ = ps.communicate()
+
+ # Generate a list of IPs by parsing the output of ip/ifconfig
+ return [x.group() for x in re.finditer(regexip, standardoutput.decode("UTF-8"))]
+
+
+def test_get_ip(ip_addresses):
+ """Attempt to test the IP address returned by
+ moznetwork.get_ip() is valid"""
+ assert moznetwork.get_ip() in ip_addresses
+
+
+@pytest.mark.skipif(mozinfo.isWin, reason="Test is not supported in Windows")
+def test_get_ip_using_get_interface(ip_addresses):
+ """Test that the control flow path for get_ip() using
+ _get_interface_list() is works"""
+ with mock.patch("socket.gethostbyname") as byname:
+ # Force socket.gethostbyname to return None
+ byname.return_value = None
+ assert moznetwork.get_ip() in ip_addresses
+
+
+if __name__ == "__main__":
+ mozunit.main()