From f814cfc7aa27f72394ab8b15720896d6dc92f935 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sun, 12 Jan 2025 10:54:59 +0100 Subject: [PATCH] test: skip tests without permissions to create netns --- tests/cksuite-all-netns.c | 4 ++++ tests/nl-test-util.c | 26 ++++++++++++++++++++++---- tests/nl-test-util.h | 3 +++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/tests/cksuite-all-netns.c b/tests/cksuite-all-netns.c index 1948c3e8..17b0f9a2 100644 --- a/tests/cksuite-all-netns.c +++ b/tests/cksuite-all-netns.c @@ -73,6 +73,10 @@ START_TEST(cache_and_clone) size_t i; int r; + if (!nltst_netns_has_netns()) { + return; + } + for (i = 0; i < _NL_N_ELEMENTS(links); i++) { if (links[i].add) _nltst_add_link(NULL, links[i].ifname, links[i].kind, diff --git a/tests/nl-test-util.c b/tests/nl-test-util.c index dc8dc5ad..aa032f2a 100644 --- a/tests/nl-test-util.c +++ b/tests/nl-test-util.c @@ -103,17 +103,26 @@ static struct { void nltst_netns_fixture_setup(void) { ck_assert(!_netns_fixture_global.nsdata); - _netns_fixture_global.nsdata = nltst_netns_enter(); - _assert_nltst_netns(_netns_fixture_global.nsdata); } void nltst_netns_fixture_teardown(void) { - _assert_nltst_netns(_netns_fixture_global.nsdata); _nl_clear_pointer(&_netns_fixture_global.nsdata, nltst_netns_leave); } +bool nltst_netns_has_netns(void) +{ + return !_netns_fixture_global.nsdata; +} + +bool nltst_netns_skip_without_netns(void) +{ + if (nltst_netns_has_netns()) + return false; + printf("SKIP test: no netns\n"); + return true; +} /*****************************************************************************/ static void unshare_user(void) @@ -149,7 +158,13 @@ static void unshare_user(void) } r = fprintf(f, "0 %d 1", uid); _nltst_assert_errno(r > 0); - _nltst_fclose(f); + r = fclose(f); + if (r != 0 && errno == EPERM) { + /* Seems this can happen during close . Ignore the inability to + * unshare. + * *sigh* */ + } else + _nltst_assert_errno(r == 0); /* Map current GID to root in NS to be created. */ f = fopen("/proc/self/gid_map", "we"); @@ -172,6 +187,9 @@ struct nltst_netns *nltst_netns_enter(void) unshare_user(); r = unshare(CLONE_NEWNET | CLONE_NEWNS); + if (r == EPERM) { + return NULL; + } _nltst_assert_errno(r == 0); /* We need a read-only /sys so that the platform knows there's no udev. */ diff --git a/tests/nl-test-util.h b/tests/nl-test-util.h index 981228b4..e8a81dba 100644 --- a/tests/nl-test-util.h +++ b/tests/nl-test-util.h @@ -429,6 +429,9 @@ char **_nltst_strtokv(const char *str); void nltst_netns_fixture_setup(void); void nltst_netns_fixture_teardown(void); +bool nltst_netns_has_netns(void); + +bool nltst_netns_skip_without_netns(void); struct nltst_netns;