|
3 | 3 | import os |
4 | 4 | import shlex |
5 | 5 | from pathlib import Path |
6 | | -from lib.py import ip |
| 6 | +from lib.py import KsftSkipEx |
| 7 | +from lib.py import cmd, ip |
7 | 8 | from lib.py import NetNS, NetdevSimDev |
8 | 9 | from .remote import Remote |
9 | 10 |
|
@@ -118,6 +119,8 @@ def __init__(self, src_path): |
118 | 119 | self.ifname = self.dev['ifname'] |
119 | 120 | self.ifindex = self.dev['ifindex'] |
120 | 121 |
|
| 122 | + self._required_cmd = {} |
| 123 | + |
121 | 124 | def create_local(self): |
122 | 125 | self._netns = NetNS() |
123 | 126 | self._ns = NetdevSimDev() |
@@ -160,3 +163,27 @@ def __del__(self): |
160 | 163 | if self.remote: |
161 | 164 | del self.remote |
162 | 165 | self.remote = None |
| 166 | + |
| 167 | + def require_v4(self): |
| 168 | + if not self.v4 or not self.remote_v4: |
| 169 | + raise KsftSkipEx("Test requires IPv4 connectivity") |
| 170 | + |
| 171 | + def require_v6(self): |
| 172 | + if not self.v6 or not self.remote_v6: |
| 173 | + raise KsftSkipEx("Test requires IPv6 connectivity") |
| 174 | + |
| 175 | + def _require_cmd(self, comm, key, host=None): |
| 176 | + cached = self._required_cmd.get(comm, {}) |
| 177 | + if cached.get(key) is None: |
| 178 | + cached[key] = cmd("command -v -- " + comm, fail=False, |
| 179 | + shell=True, host=host).ret == 0 |
| 180 | + self._required_cmd[comm] = cached |
| 181 | + return cached[key] |
| 182 | + |
| 183 | + def require_cmd(self, comm, local=True, remote=False): |
| 184 | + if local: |
| 185 | + if not self._require_cmd(comm, "local"): |
| 186 | + raise KsftSkipEx("Test requires command: " + comm) |
| 187 | + if remote: |
| 188 | + if not self._require_cmd(comm, "remote"): |
| 189 | + raise KsftSkipEx("Test requires (remote) command: " + comm) |
0 commit comments