|
10 | 10 | # See the License for the specific language governing permissions and
|
11 | 11 | # limitations under the License.
|
12 | 12 |
|
| 13 | +import json |
| 14 | + |
13 | 15 | from testinfra.modules.base import Module
|
14 | 16 | from testinfra.utils import cached_property
|
15 | 17 |
|
@@ -48,6 +50,26 @@ def addresses(self):
|
48 | 50 | """
|
49 | 51 | raise NotImplementedError
|
50 | 52 |
|
| 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 | + |
51 | 73 | def __repr__(self):
|
52 | 74 | return "<interface {}>".format(self.name)
|
53 | 75 |
|
@@ -109,6 +131,16 @@ def addresses(self):
|
109 | 131 | addrs.append(splitted[1].split("/", 1)[0])
|
110 | 132 | return addrs
|
111 | 133 |
|
| 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 | + |
112 | 144 | @classmethod
|
113 | 145 | def default(cls, family=None):
|
114 | 146 | _default = cls(None, family=family)
|
|
0 commit comments