Skip to content

Commit

Permalink
Make all network unit tests run on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
twangboy committed Aug 28, 2024
1 parent 562732f commit d439a19
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions tests/pytests/unit/utils/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import salt.exceptions
import salt.utils.network
import salt.utils.network as network
import salt.utils.platform
from salt._compat import ipaddress
from tests.support.mock import MagicMock, create_autospec, mock_open, patch

Expand Down Expand Up @@ -1430,9 +1431,12 @@ def test_isportopen_false():
assert ret is False


@pytest.mark.skip_on_windows(reason="Do not run on Windows")
def test_isportopen():
ret = network.isportopen("127.0.0.1", "22")
if salt.utils.platform.is_windows():
port = "135"
else:
port = "22"
ret = network.isportopen("127.0.0.1", port)
assert ret == 0


Expand All @@ -1446,14 +1450,19 @@ def test_get_socket():
assert ret.type == socket.SOCK_STREAM


@pytest.mark.skip_on_windows(reason="Do not run on Windows")
# @pytest.mark.skip_on_windows(reason="Do not run on Windows")
def test_ip_to_host(grains):
if salt.utils.platform.is_windows():
hostname = socket.gethostname()
else:
hostname = "localhost"

ret = network.ip_to_host("127.0.0.1")
if grains["oscodename"] == "Photon":
if grains.get("oscodename") == "Photon":
# Photon returns this for IPv4
assert ret == "ipv6-localhost"
else:
assert ret == "localhost"
assert ret == hostname

ret = network.ip_to_host("2001:a71::1")
assert ret is None
Expand All @@ -1463,22 +1472,22 @@ def test_ip_to_host(grains):
assert ret == "localhost6"
elif grains["os_family"] == "Debian":
if grains["osmajorrelease"] == 12:
assert ret == "localhost"
assert ret == hostname
else:
assert ret == "ip6-localhost"
elif grains["os_family"] == "RedHat":
if grains["oscodename"] == "Photon":
assert ret == "ipv6-localhost"
else:
assert ret == "localhost"
assert ret == hostname
elif grains["os_family"] == "Arch":
if grains.get("osmajorrelease", None) is None:
# running doesn't have osmajorrelease grains
assert ret == "localhost"
assert ret == hostname
else:
assert ret == "ip6-localhost"
else:
assert ret == "localhost"
assert ret == hostname


@pytest.mark.parametrize(
Expand Down Expand Up @@ -1508,11 +1517,10 @@ def test_rpad_ipv4_network(addr, expected):
assert network.rpad_ipv4_network(addr) == expected


@pytest.mark.skip_on_windows(reason="Do not run on Windows")
def test_hw_addr(linux_interfaces_dict, freebsd_interfaces_dict):

with patch(
"salt.utils.network.linux_interfaces",
"salt.utils.network.interfaces",
MagicMock(return_value=linux_interfaces_dict),
):
hw_addrs = network.hw_addr("eth0")
Expand All @@ -1534,11 +1542,10 @@ def test_hw_addr(linux_interfaces_dict, freebsd_interfaces_dict):
)


@pytest.mark.skip_on_windows(reason="Do not run on Windows")
def test_interface_and_ip(linux_interfaces_dict):

with patch(
"salt.utils.network.linux_interfaces",
"salt.utils.network.interfaces",
MagicMock(return_value=linux_interfaces_dict),
):
expected = [
Expand All @@ -1561,11 +1568,10 @@ def test_interface_and_ip(linux_interfaces_dict):
assert ret == 'Interface "dog" not in available interfaces: "eth0", "lo"'


@pytest.mark.skip_on_windows(reason="Do not run on Windows")
def test_subnets(linux_interfaces_dict):

with patch(
"salt.utils.network.linux_interfaces",
"salt.utils.network.interfaces",
MagicMock(return_value=linux_interfaces_dict),
):
ret = network.subnets()
Expand All @@ -1586,17 +1592,16 @@ def test_in_subnet(caplog):
assert not ret


@pytest.mark.skip_on_windows(reason="Do not run on Windows")
def test_ip_addrs(linux_interfaces_dict):
with patch(
"salt.utils.network.linux_interfaces",
"salt.utils.network.interfaces",
MagicMock(return_value=linux_interfaces_dict),
):
ret = network.ip_addrs("eth0")
assert ret == ["10.10.10.56"]

with patch(
"salt.utils.network.linux_interfaces",
"salt.utils.network.interfaces",
MagicMock(return_value=linux_interfaces_dict),
):
ret = network.ip_addrs6("eth0")
Expand Down

0 comments on commit d439a19

Please sign in to comment.