Skip to content

Commit d187821

Browse files
costasdphilpep
authored andcommitted
iproute2: add some documentation
1 parent 35d568c commit d187821

File tree

2 files changed

+59
-20
lines changed

2 files changed

+59
-20
lines changed

doc/source/modules.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ host
5050

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

53+
.. attribute:: iproute2
54+
55+
:class:`testinfra.modules.iproute2.IProute2` class
56+
5357
.. attribute:: iptables
5458

5559
:class:`testinfra.modules.iptables.Iptables` class

testinfra/modules/iproute2.py

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,10 @@
1919
class IProute2(InstanceModule):
2020
"""Test network configuration via iproute2 commands
2121
22-
>>> host.iproute2.rules()
22+
Optional arguments:
23+
- family: force iproute2 tools to use a specific protocol family
24+
- namespace: execute iproute2 tools inside the provided namespace
2325
24-
host.ip.rules(from,to,tos,fwmark,iif,oif,pref, uidrange, ipproto, sport, dport)
25-
host.ip.routes(table, device, scope, proto, src, metric)
26-
host.ip.links()
27-
host.ip.addresses()
28-
host.ip.tunnels()
29-
30-
Optionally, the protocol family can be provided to reduce the number of routes returned:
31-
>>> host.iproute2.routes("inet6", table="main")
32-
...FIX
33-
34-
Optionally, this can work inside a different network namespace:
35-
>>> host.iproute2.routes("inet6", "vpn")
36-
...FIX
3726
"""
3827

3928
def __init__(self, family=None, namespace=None):
@@ -58,7 +47,14 @@ def exists(self):
5847
return self.run_test("{} -V".format(self._ip)).rc == 0
5948

6049
def addresses(self, address=None, ifname=None, local=None):
61-
"""Return the addresses associated with interfaces"""
50+
"""Return the addresses associated with interfaces
51+
52+
Optionally, results can be filtered by:
53+
- address
54+
- ifname
55+
- local
56+
57+
"""
6258
cmd = f"{self._ip} --json address show"
6359
out = self.check_output(cmd)
6460
j = json.loads(out)
@@ -86,7 +82,20 @@ def links(self):
8682
def routes(
8783
self, table="all", device=None, scope=None, proto=None, src=None, metric=None
8884
):
89-
"""Return the routes installed"""
85+
"""Returns the routes installed
86+
87+
Optionally, routes returned can be filtered with the following
88+
selectors. This can be useful in busy routing tables.
89+
90+
Selectors:
91+
- table
92+
- device (maps to ip-route's 'dev' selector)
93+
- scope
94+
- proto
95+
- src
96+
- metric
97+
98+
"""
9099
cmd = f"{self._ip} --json route show "
91100
options = []
92101
if table is not None:
@@ -120,7 +129,25 @@ def rules(
120129
sport=None,
121130
dport=None,
122131
):
123-
"""Return the rules our routing policy consists of"""
132+
"""Returns the rules our routing policy consists of
133+
134+
Optionally, rules returned can be filtered with the following
135+
selectors. This can be useful in busy rulesets.
136+
137+
Selectors:
138+
- src (maps to ip-rule's 'from' selector)
139+
- to
140+
- tos
141+
- fwmark
142+
- iif
143+
- oif
144+
- pref
145+
- uidrange
146+
- ipproto
147+
- sport
148+
- dport
149+
150+
"""
124151
cmd = f"{self._ip} --json rule show "
125152

126153
options = []
@@ -162,7 +189,15 @@ def rules(
162189
return json.loads(out)
163190

164191
def tunnels(self, ifname=None):
165-
"""Return all configured tunnels"""
192+
"""Returns all configured tunnels
193+
194+
Optionally, tunnels returned can be filtered with the interface name.
195+
This can be faster in busy tunnel installations.
196+
197+
Selectors:
198+
- ifname
199+
200+
"""
166201
cmd = f"{self._ip} --json tunnel show "
167202

168203
options = []
@@ -174,13 +209,13 @@ def tunnels(self, ifname=None):
174209
return json.loads(out)
175210

176211
def vrfs(self):
177-
"""Return all configured vrfs"""
212+
"""Returns all configured vrfs"""
178213
cmd = f"{self._ip} --json vrf show"
179214
out = self.check_output(cmd)
180215
return json.loads(out)
181216

182217
def netns(self):
183-
"""Return all configured network namespaces"""
218+
"""Returns all configured network namespaces"""
184219
cmd = f"{self._ip} --json netns show"
185220
out = self.check_output(cmd)
186221
if not out: # ip netns returns null instead of [] in json mode

0 commit comments

Comments
 (0)