Skip to content

New module: iproute2 #730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions doc/source/modules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ host

:class:`testinfra.modules.interface.Interface` class

.. attribute:: iproute2

:class:`testinfra.modules.iproute2.IProute2` class

.. attribute:: iptables

:class:`testinfra.modules.iptables.Iptables` class
Expand Down Expand Up @@ -176,6 +180,13 @@ Interface
:undoc-members:
:exclude-members: get_module_class

IProute2
~~~~~~~~~

.. autoclass:: testinfra.modules.iproute2.IProute2
:members:
:undoc-members:
:exclude-members: get_module_class

Iptables
~~~~~~~~~
Expand Down
128 changes: 127 additions & 1 deletion test/test_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ def test_addr_namespace(host):
)
def test_interface(host, family):
# exist
assert host.interface("eth0", family=family).exists
assert host.interface("tap0", family=family).exists
assert not host.interface("does_not_exist", family=family).exists
# addresses
addresses = host.interface.default(family).addresses
Expand All @@ -648,3 +648,129 @@ def test_interface(host, family):
default_itf = host.interface.default(family)
assert default_itf.name == "eth0"
assert default_itf.exists


@pytest.mark.parametrize(
"family",
["inet", None],
)
def test_iproute2_addresses(host, family):
assert host.iproute2.exists

addresses = host.iproute2(family=family).addresses()

assert len(addresses) > 0
assert addresses[0].get("ifname") and addresses[0].get("ifindex")

filtered_addresses = host.iproute2(family=family).addresses(ifname="lo")
assert filtered_addresses[0].get("ifname") == "lo" and len(filtered_addresses) == 1

filtered_addresses2 = host.iproute2(family=family).addresses(local="127.0.0.1")
assert (
filtered_addresses2[0].get("ifname") == "lo" and len(filtered_addresses2) == 1
)


def test_iproute2_links(host):
assert host.iproute2.exists

links = host.iproute2().links()
assert len(links) > 0 and len(links) < 4

assert links[0].get("ifname") and links[0].get("ifindex")


def test_iproute2_routes(host):
assert host.iproute2.exists

routes = host.iproute2().routes()
assert len(routes) > 0

filtered_routes = host.iproute2().routes(
table="local", scope="host", src="127.0.0.1"
)
assert filtered_routes[0].get("protocol") == "kernel" and len(filtered_routes) > 1


def test_iproute2_rules(host):
assert host.iproute2.exists

rules = host.iproute2().rules()
assert len(rules) > 0 and len(rules) < 4
assert rules[0].get("priority") == 0
assert rules[0].get("src") == "all"
assert rules[0].get("table") == "local"

cmd = host.run("ip rule add from 1.2.3.4/32 table 123")
assert cmd.exit_status == 0, f"{cmd.stdout}\n{cmd.stderr}"

rules_123 = host.iproute2().rules(src="1.2.3.4/32")
assert len(rules_123) > 0
assert rules_123[0].get("src") == "1.2.3.4"


def test_iproute2_tunnels(host):
assert host.iproute2.exists

tunnels = host.iproute2().tunnels()
assert len(tunnels) > 0

cmd = host.run("ip tunnel add test mode ipip remote 127.0.0.1")
assert cmd.exit_status == 0, f"{cmd.stdout}\n{cmd.stderr}"

tunnels = host.iproute2().tunnels(ifname="test")
assert len(tunnels) > 0
assert tunnels[0].get("ifname") == "test"
assert tunnels[0].get("mode") == "ip/ip"
assert tunnels[0].get("remote") == "127.0.0.1"


def test_iproute2_vrfs(host):
assert host.iproute2.exists

vrfs = host.iproute2().vrfs()
assert len(vrfs) == 0


def test_iproute2_netns(host):
assert host.iproute2.exists

namespaces = host.iproute2().netns()
assert len(namespaces) == 0

cmd = host.run("ip netns add test")
assert cmd.exit_status == 0, f"{cmd.stdout}\n{cmd.stderr}"

namespaces = host.iproute2().netns()
assert len(namespaces) == 1
assert namespaces[0].get("name") == "test"


def test_iproute2_bridge_vlan(host):
assert host.iproute2.bridge_exists

vlans = host.iproute2().bridge_vlan()
assert len(vlans) == 0


def test_iproute2_bridge_fdb(host):
assert host.iproute2.bridge_exists

fdb = host.iproute2().bridge_fdb()
assert len(fdb) > 0


def test_iproute2_bridge_mdb(host):
assert host.iproute2.bridge_exists

mdb = host.iproute2().bridge_mdb()
assert len(mdb) == 1
assert len(mdb[0].get("mdb")) == 0
assert len(mdb[0].get("router")) == 0


def test_iproute2_bridge_link(host):
assert host.iproute2.bridge_exists

links = host.iproute2().bridge_link()
assert len(links) == 0
1 change: 1 addition & 0 deletions testinfra/modules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"group": "group:Group",
"interface": "interface:Interface",
"iptables": "iptables:Iptables",
"iproute2": "iproute2:IProute2",
"mount_point": "mountpoint:MountPoint",
"package": "package:Package",
"pip": "pip:Pip",
Expand Down
Loading