Skip to content

Commit ca0f8bd

Browse files
BenoitKnechtphilpep
authored andcommitted
modules/interface: Add routes() function
Returns the routes associated with the interface, optionally filtered by scope (`host`, `link` or `global`). Signed-off-by: Benoît Knecht <[email protected]>
1 parent b545297 commit ca0f8bd

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

testinfra/modules/interface.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
# See the License for the specific language governing permissions and
1111
# limitations under the License.
1212

13+
import json
14+
1315
from testinfra.modules.base import Module
1416
from testinfra.utils import cached_property
1517

@@ -48,6 +50,26 @@ def addresses(self):
4850
"""
4951
raise NotImplementedError
5052

53+
def routes(self, scope=None):
54+
"""Return the routes associated with the interface, optionally filtered by scope
55+
("host", "link" or "global").
56+
57+
>>> host.interface("eth0").routes()
58+
[{'dst': 'default',
59+
'flags': [],
60+
'gateway': '192.0.2.1',
61+
'metric': 3003,
62+
'prefsrc': '192.0.2.5',
63+
'protocol': 'dhcp'},
64+
{'dst': '192.0.2.0/24',
65+
'flags': [],
66+
'metric': 3003,
67+
'prefsrc': '192.0.2.5',
68+
'protocol': 'dhcp',
69+
'scope': 'link'}]
70+
"""
71+
raise NotImplementedError
72+
5173
def __repr__(self):
5274
return "<interface {}>".format(self.name)
5375

@@ -109,6 +131,16 @@ def addresses(self):
109131
addrs.append(splitted[1].split("/", 1)[0])
110132
return addrs
111133

134+
def routes(self, scope=None):
135+
cmd = f"{self._ip} --json route list dev %s"
136+
137+
if scope is None:
138+
out = self.check_output(cmd, self.name)
139+
else:
140+
out = self.check_output(cmd + " scope %s", self.name, scope)
141+
142+
return json.loads(out)
143+
112144
@classmethod
113145
def default(cls, family=None):
114146
_default = cls(None, family=family)

0 commit comments

Comments
 (0)