Skip to content

Commit 741a735

Browse files
committed
more rules
1 parent 3970515 commit 741a735

File tree

2 files changed

+51
-68
lines changed

2 files changed

+51
-68
lines changed

test/test_modules.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -650,38 +650,48 @@ def test_interface(host, family):
650650
assert default_itf.exists
651651

652652

653-
def test_ip_links(host):
653+
def test_iproute2_links(host):
654654
assert host.ip.exists
655-
iphandle = host.ip()
656655

657-
links = iphandle.links()
656+
links = host.ip.links()
658657
assert len(links) > 0 and len(links) < 4
659658

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

662661

663-
def test_ip_routes(host):
662+
def test_iproute2_routes(host):
664663
assert host.ip.exists
665-
iphandle = host.ip()
666664

667-
routes = iphandle.routes()
665+
routes = host.ip.routes()
668666
assert len(routes) > 0
669667

670668

671-
def test_ip_rules(host):
669+
def test_iproute2_rules(host):
672670
assert host.ip.exists
673-
iphandle = host.ip()
674671

675-
rules = iphandle.rules()
672+
rules = host.ip.rules()
676673
assert len(rules) > 0 and len(rules) < 4
677674
assert rules[0].get("priority") == 0
678675
assert rules[0].get("src") == "all"
679676
assert rules[0].get("table") == "local"
680677

681678

682-
def test_ip_tunnels(host):
679+
def test_iproute2_tunnels(host):
683680
assert host.ip.exists
684-
iphandle = host.ip()
685681

686-
tunnels = iphandle.tunnels()
682+
tunnels = host.ip.tunnels()
687683
assert len(tunnels) == 0
684+
685+
686+
def test_iproute2_vrfs(host):
687+
assert host.ip.exists
688+
689+
vrfs = host.ip.vrfs()
690+
assert len(vrfs) == 0
691+
692+
693+
def test_iproute2_netns(host):
694+
assert host.ip.exists
695+
696+
namespaces = host.ip.netns()
697+
assert len(namespaces) == 0

testinfra/modules/ip.py

Lines changed: 29 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212

1313
import functools
1414
import json
15-
import re
1615

17-
from testinfra.modules.base import Module
16+
from testinfra.modules.base import InstanceModule
1817

19-
class IP(Module):
20-
"""Test network configuration via ip commands
18+
19+
class IP(InstanceModule):
20+
"""Test network configuration via iproute2 commands
2121
2222
>>> host.ip.rules()
2323
@@ -27,7 +27,7 @@ class IP(Module):
2727
host.ip.addresses()
2828
host.ip.tunnels()
2929
30-
Optionally, the protocol family can be provided:
30+
Optionally, the protocol family can be provided to reduce the number of routes returned:
3131
>>> host.ip.routes("inet6", table="main")
3232
...FIX
3333
@@ -36,94 +36,67 @@ class IP(Module):
3636
...FIX
3737
"""
3838

39-
def __init__(self, family=None, netns=None):
39+
def __init__(self, family=None, namespace=None):
4040
self.family = family
41-
self.netns = netns
41+
self.namespace = namespace
4242
super().__init__()
4343

44-
@property
45-
def exists(self):
46-
raise NotImplementedError
47-
48-
def addresses(self):
49-
"""Return the addresses associated with interfaces
50-
"""
51-
raise NotImplementedError
52-
53-
def links(self):
54-
"""Return links and their state
55-
"""
56-
raise NotImplementedError
57-
58-
def routes(self):
59-
"""Return the routes associated with the routing table
60-
"""
61-
raise NotImplementedError
62-
63-
def rules(self):
64-
"""Return all configured ip rules
65-
"""
66-
raise NotImplementedError
67-
68-
def tunnels(self):
69-
"""Return all configured tunnels
70-
"""
71-
raise NotImplementedError
72-
7344
def __repr__(self):
7445
return "<ip>"
7546

76-
@classmethod
77-
def get_module_class(cls, host):
78-
if host.system_info.type == "linux":
79-
return LinuxIP
80-
raise NotImplementedError
81-
82-
class LinuxIP(IP):
8347
@functools.cached_property
8448
def _ip(self):
8549
ip_cmd = self.find_command("ip")
86-
if self.netns is not None:
87-
ip_cmd = f"{ip_cmd} netns exec {self.netns} {ip_cmd}"
50+
if self.namespace is not None:
51+
ip_cmd = f"{ip_cmd} netns exec {self.namespace} {ip_cmd}"
8852
if self.family is not None:
8953
ip_cmd = f"{ip_cmd} -f {self.family}"
9054
return ip_cmd
9155

9256
@property
9357
def exists(self):
94-
return self.run_test("{} -V".format(self._ip), self.name).rc == 0
58+
return self.run_test("{} -V".format(self._ip)).rc == 0
9559

9660
def addresses(self):
97-
"""Return the addresses associated with interfaces
98-
"""
61+
"""Return the addresses associated with interfaces"""
9962
cmd = f"{self._ip} --json address show"
10063
out = self.check_output(cmd)
10164
return json.loads(out)
10265

10366
def links(self):
104-
"""Return links and their state
105-
"""
67+
"""Return links and their state"""
10668
cmd = f"{self._ip} --json link show"
10769
out = self.check_output(cmd)
10870
return json.loads(out)
10971

11072
def routes(self):
111-
"""Return the routes installed
112-
"""
73+
"""Return the routes installed"""
11374
cmd = f"{self._ip} --json route show table all"
11475
out = self.check_output(cmd)
11576
return json.loads(out)
11677

11778
def rules(self):
118-
"""Return the rules our routing policy consists of
119-
"""
79+
"""Return the rules our routing policy consists of"""
12080
cmd = f"{self._ip} --json rule show"
12181
out = self.check_output(cmd)
12282
return json.loads(out)
12383

12484
def tunnels(self):
125-
"""Return all configured tunnels
126-
"""
85+
"""Return all configured tunnels"""
12786
cmd = f"{self._ip} --json tunnel show"
12887
out = self.check_output(cmd)
12988
return json.loads(out)
89+
90+
def vrfs(self):
91+
"""Return all configured vrfs"""
92+
cmd = f"{self._ip} --json vrf show"
93+
out = self.check_output(cmd)
94+
return json.loads(out)
95+
96+
def netns(self):
97+
"""Return all configured network namespaces"""
98+
cmd = f"{self._ip} --json netns show"
99+
out = self.check_output(cmd)
100+
if out is None: # ip netns returns null instead of [] in json mode
101+
return json.loads("[]")
102+
return json.loads(out)

0 commit comments

Comments
 (0)