Skip to content

Commit

Permalink
test: skip tests without permissions to create netns
Browse files Browse the repository at this point in the history
  • Loading branch information
thom311 committed Jan 12, 2025
1 parent 40b0c16 commit 1ebeb8e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
6 changes: 6 additions & 0 deletions tests/cksuite-all-netns.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ START_TEST(cache_and_clone)
size_t i;
int r;

if (_nltst_skip_no_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,
Expand Down Expand Up @@ -317,6 +320,9 @@ START_TEST(route_1)
_nl_auto_nl_socket struct nl_sock *sk = NULL;
_nl_auto_nl_cache struct nl_cache *cache = NULL;

if (_nltst_skip_no_netns())
return;

if (_nltst_skip_no_iproute2("route_1"))
return;

Expand Down
27 changes: 23 additions & 4 deletions tests/nl-test-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,27 @@ 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_skip_no_netns(void)
{
if (nltst_netns_has_netns())
return false;
printf("skip test due to having no private netns\n");
return true;
}

/*****************************************************************************/

static void unshare_user(void)
Expand Down Expand Up @@ -149,7 +159,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");
Expand All @@ -172,6 +188,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. */
Expand Down
3 changes: 3 additions & 0 deletions tests/nl-test-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_skip_no_netns(void);

struct nltst_netns;

Expand Down

0 comments on commit 1ebeb8e

Please sign in to comment.