summaryrefslogtreecommitdiffstats
path: root/tests/py
diff options
context:
space:
mode:
authorPhil Sutter <phil@nwl.cc>2021-08-10 15:40:06 +0200
committerPhil Sutter <phil@nwl.cc>2021-08-11 12:18:49 +0200
commit26e6b6e9018c342b58a11e5f5b713a0212e03415 (patch)
treeb596aab600541e358406d91cdc0c5eb56d4a2a33 /tests/py
parentd9957a5c57a11df8233ea4a7c91c579af511e4b6 (diff)
tests/py: Make netns spawning more robust
On systems without unshare Python module, try to call unshare binary with oneself as parameters. Signed-off-by: Phil Sutter <phil@nwl.cc>
Diffstat (limited to 'tests/py')
-rwxr-xr-xtests/py/nft-test.py40
1 files changed, 34 insertions, 6 deletions
diff --git a/tests/py/nft-test.py b/tests/py/nft-test.py
index 15e74d8b..f8f9341c 100755
--- a/tests/py/nft-test.py
+++ b/tests/py/nft-test.py
@@ -1349,6 +1349,33 @@ def run_test_file(filename, force_all_family_option, specific_file):
return [tests, passed, total_warning, total_error, total_unit_run]
+def spawn_netns():
+ # prefer unshare module
+ try:
+ import unshare
+ unshare.unshare(unshare.CLONE_NEWNET)
+ return True
+ except:
+ pass
+
+ # sledgehammer style:
+ # - call ourselves prefixed by 'unshare -n' if found
+ # - pass extra --no-netns parameter to avoid another recursion
+ try:
+ import shutil
+
+ unshare = shutil.which("unshare")
+ if unshare is None:
+ return False
+
+ sys.argv.append("--no-netns")
+ if debug_option:
+ print("calling: ", [unshare, "-n", sys.executable] + sys.argv)
+ os.execv(unshare, [unshare, "-n", sys.executable] + sys.argv)
+ except:
+ pass
+
+ return False
def main():
parser = argparse.ArgumentParser(description='Run nft tests')
@@ -1376,6 +1403,10 @@ def main():
parser.add_argument('-l', '--library', default=None,
help='path to libntables.so.1, overrides --host')
+ parser.add_argument('-N', '--no-netns', action='store_true',
+ dest='no_netns',
+ help='Do not run in own network namespace')
+
parser.add_argument('-s', '--schema', action='store_true',
dest='enable_schema',
help='verify json input/output against schema')
@@ -1400,15 +1431,12 @@ def main():
print("You need to be root to run this, sorry")
return
+ if not args.no_netns and not spawn_netns():
+ print_warning("cannot run in own namespace, connectivity might break")
+
# Change working directory to repository root
os.chdir(TESTS_PATH + "/../..")
- try:
- import unshare
- unshare.unshare(unshare.CLONE_NEWNET)
- except:
- print_warning("cannot run in own namespace, connectivity might break")
-
check_lib_path = True
if args.library is None:
if args.host: