Skip to content

Commit

Permalink
iproute2: fix tests after moving Module
Browse files Browse the repository at this point in the history
  • Loading branch information
costasd authored and philpep committed Nov 13, 2023
1 parent fb3301d commit 1f7b1af
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 41 deletions.
42 changes: 24 additions & 18 deletions test/test_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,18 +650,22 @@ def test_interface(host, family):
assert default_itf.exists


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

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

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

filtered_addresses = host.iproute2.addresses(ifname="lo")
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.addresses(local="127.0.0.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
)
Expand All @@ -670,7 +674,7 @@ def test_iproute2_addresses(host):
def test_iproute2_links(host):
assert host.iproute2.exists

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

assert links[0].get("ifname") and links[0].get("ifindex")
Expand All @@ -679,17 +683,19 @@ def test_iproute2_links(host):
def test_iproute2_routes(host):
assert host.iproute2.exists

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

filtered_routes = host.iproute2.routes(table="local", scope="host", src="127.0.0.1")
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()
rules = host.iproute2().rules()
assert len(rules) > 0 and len(rules) < 4
assert rules[0].get("priority") == 0
assert rules[0].get("src") == "all"
Expand All @@ -698,21 +704,21 @@ def test_iproute2_rules(host):
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")
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()
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")
tunnels = host.iproute2().tunnels(ifname="test")
assert len(tunnels) > 0
assert tunnels[0].get("ifname") == "test"
assert tunnels[0].get("mode") == "ip/ip"
Expand All @@ -722,42 +728,42 @@ def test_iproute2_tunnels(host):
def test_iproute2_vrfs(host):
assert host.iproute2.exists

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


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

namespaces = host.iproute2.netns()
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()
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()
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()
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()
mdb = host.iproute2().bridge_mdb()
assert len(mdb) == 1
assert len(mdb[0].get("mdb")) == 0
assert len(mdb[0].get("router")) == 0
Expand All @@ -766,5 +772,5 @@ def test_iproute2_bridge_mdb(host):
def test_iproute2_bridge_link(host):
assert host.iproute2.bridge_exists

links = host.iproute2.bridge_link()
links = host.iproute2().bridge_link()
assert len(links) == 0
54 changes: 31 additions & 23 deletions testinfra/modules/iproute2.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def bridge_exists(self):
def addresses(self, address=None, ifname=None, local=None):
"""Returns the addresses associated with interfaces
>>> host.iproute2.addresses()
>>> host.iproute2().addresses()
[{'ifindex': 1,
'ifname': 'lo',
'flags': ['LOOPBACK', 'UP', 'LOWER_UP'],
Expand Down Expand Up @@ -114,11 +114,12 @@ def addresses(self, address=None, ifname=None, local=None):
* local
"""
raise NotImplementedError

def links(self):
"""Returns links and their state.
>>> host.iproute2.links()
>>> host.iproute2().links()
[{'ifindex': 1,
'ifname': 'lo',
'flags': ['LOOPBACK', 'UP', 'LOWER_UP'],
Expand All @@ -133,13 +134,14 @@ def links(self):
'broadcast': '00:00:00:00:00:00'}]
"""
raise NotImplementedError

def routes(
self, table="all", device=None, scope=None, proto=None, src=None, metric=None
):
"""Returns the routes installed in *all* routing tables.
>>> host.iproute2.routes()
>>> host.iproute2().routes()
[{'dst': '169.254.0.0/16',
'dev': 'wlp4s0',
'scope': 'link',
Expand All @@ -165,6 +167,7 @@ def routes(
* metric
"""
raise NotImplementedError

def rules(
self,
Expand All @@ -182,7 +185,7 @@ def rules(
):
"""Returns the rules our routing policy consists of.
>>> host.iproute2.rules()
>>> host.iproute2().rules()
[{'priority': 0, 'src': 'all', 'table': 'local'},
{'priority': 32765, 'src': '1.2.3.4', 'table': '123'},
{'priority': 32766, 'src': 'all', 'table': 'main'},
Expand All @@ -204,11 +207,12 @@ def rules(
* dport
"""
raise NotImplementedError

def tunnels(self, ifname=None):
"""Returns all configured tunnels
>>> host.iproute2.tunnels()
>>> host.iproute2().tunnels()
[{'ifname': 'test1',
'mode': 'ip/ip',
'remote': '127.0.0.2',
Expand All @@ -220,51 +224,55 @@ def tunnels(self, ifname=None):
* ifname
"""
raise NotImplementedError

def vrfs(self):
"""Returns all configured vrfs"""
cmd = f"{self._ip} --json vrf show"
out = self.check_output(cmd)
return json.loads(out)
raise NotImplementedError

def netns(self):
"""Returns all configured network namespaces
>>> host.iproute2.netns()
>>> host.iproute2().netns()
[{'name': 'test'}]
"""
raise NotImplementedError

def bridge_vlan(self):
"""Returns all configured vlans
>>> host.iproute2.bridge_vlan()
>>> host.iproute2().bridge_vlan()
[]
"""
raise NotImplementedError

def bridge_fdb(self):
"""Returns all configured fdb entries
>>> host.iproute2.bridge_fdb()
>>> host.iproute2().bridge_fdb()
[{'mac': '33:33:00:00:00:01',
'ifname': 'enp0s31f6',
'flags': ['self'],
'state': 'permanent'}]
"""
raise NotImplementedError

def bridge_mdb(self):
"""Returns all configured mdb entries
>>> host.iproute2.bridge_mdb()
>>> host.iproute2().bridge_mdb()
[{'mdb': [], 'router': {}}]
"""
raise NotImplementedError

def bridge_link(self):
"""Returns all configured links
>>> host.iproute2.bridge_link()
>>> host.iproute2().bridge_link()
[]
"""
raise NotImplementedError


class LinuxIProute2(IProute2):
Expand Down Expand Up @@ -307,7 +315,7 @@ def bridge_exists(self):
def addresses(self, address=None, ifname=None, local=None):
"""Returns the addresses associated with interfaces
>>> host.iproute2.addresses()
>>> host.iproute2().addresses()
[{'ifindex': 1,
'ifname': 'lo',
'flags': ['LOOPBACK', 'UP', 'LOWER_UP'],
Expand Down Expand Up @@ -362,7 +370,7 @@ def addresses(self, address=None, ifname=None, local=None):
def links(self):
"""Returns links and their state.
>>> host.iproute2.links()
>>> host.iproute2().links()
[{'ifindex': 1,
'ifname': 'lo',
'flags': ['LOOPBACK', 'UP', 'LOWER_UP'],
Expand All @@ -386,7 +394,7 @@ def routes(
):
"""Returns the routes installed in *all* routing tables.
>>> host.iproute2.routes()
>>> host.iproute2().routes()
[{'dst': '169.254.0.0/16',
'dev': 'wlp4s0',
'scope': 'link',
Expand Down Expand Up @@ -447,7 +455,7 @@ def rules(
):
"""Returns the rules our routing policy consists of.
>>> host.iproute2.rules()
>>> host.iproute2().rules()
[{'priority': 0, 'src': 'all', 'table': 'local'},
{'priority': 32765, 'src': '1.2.3.4', 'table': '123'},
{'priority': 32766, 'src': 'all', 'table': 'main'},
Expand Down Expand Up @@ -512,7 +520,7 @@ def rules(
def tunnels(self, ifname=None):
"""Returns all configured tunnels
>>> host.iproute2.tunnels()
>>> host.iproute2().tunnels()
[{'ifname': 'test1',
'mode': 'ip/ip',
'remote': '127.0.0.2',
Expand Down Expand Up @@ -543,7 +551,7 @@ def vrfs(self):
def netns(self):
"""Returns all configured network namespaces
>>> host.iproute2.netns()
>>> host.iproute2().netns()
[{'name': 'test'}]
"""

Expand All @@ -556,7 +564,7 @@ def netns(self):
def bridge_vlan(self):
"""Returns all configured vlans
>>> host.iproute2.bridge_vlan()
>>> host.iproute2().bridge_vlan()
[]
"""

Expand All @@ -567,7 +575,7 @@ def bridge_vlan(self):
def bridge_fdb(self):
"""Returns all configured fdb entries
>>> host.iproute2.bridge_fdb()
>>> host.iproute2().bridge_fdb()
[{'mac': '33:33:00:00:00:01',
'ifname': 'enp0s31f6',
'flags': ['self'],
Expand All @@ -581,7 +589,7 @@ def bridge_fdb(self):
def bridge_mdb(self):
"""Returns all configured mdb entries
>>> host.iproute2.bridge_mdb()
>>> host.iproute2().bridge_mdb()
[{'mdb': [], 'router': {}}]
"""
Expand All @@ -593,7 +601,7 @@ def bridge_mdb(self):
def bridge_link(self):
"""Returns all configured links
>>> host.iproute2.bridge_link()
>>> host.iproute2().bridge_link()
[]
"""

Expand Down

0 comments on commit 1f7b1af

Please sign in to comment.