Skip to content

Commit f814cfc

Browse files
committed
test: skip tests without permissions to create netns
1 parent 40b0c16 commit f814cfc

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

tests/cksuite-all-netns.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ START_TEST(cache_and_clone)
7373
size_t i;
7474
int r;
7575

76+
if (!nltst_netns_has_netns()) {
77+
return;
78+
}
79+
7680
for (i = 0; i < _NL_N_ELEMENTS(links); i++) {
7781
if (links[i].add)
7882
_nltst_add_link(NULL, links[i].ifname, links[i].kind,

tests/nl-test-util.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,26 @@ static struct {
103103
void nltst_netns_fixture_setup(void)
104104
{
105105
ck_assert(!_netns_fixture_global.nsdata);
106-
107106
_netns_fixture_global.nsdata = nltst_netns_enter();
108-
_assert_nltst_netns(_netns_fixture_global.nsdata);
109107
}
110108

111109
void nltst_netns_fixture_teardown(void)
112110
{
113-
_assert_nltst_netns(_netns_fixture_global.nsdata);
114111
_nl_clear_pointer(&_netns_fixture_global.nsdata, nltst_netns_leave);
115112
}
116113

114+
bool nltst_netns_has_netns(void)
115+
{
116+
return !_netns_fixture_global.nsdata;
117+
}
118+
119+
bool nltst_netns_skip_without_netns(void)
120+
{
121+
if (nltst_netns_has_netns())
122+
return false;
123+
printf("SKIP test: no netns\n");
124+
return true;
125+
}
117126
/*****************************************************************************/
118127

119128
static void unshare_user(void)
@@ -149,7 +158,13 @@ static void unshare_user(void)
149158
}
150159
r = fprintf(f, "0 %d 1", uid);
151160
_nltst_assert_errno(r > 0);
152-
_nltst_fclose(f);
161+
r = fclose(f);
162+
if (r != 0 && errno == EPERM) {
163+
/* Seems this can happen during close . Ignore the inability to
164+
* unshare.
165+
* *sigh* */
166+
} else
167+
_nltst_assert_errno(r == 0);
153168

154169
/* Map current GID to root in NS to be created. */
155170
f = fopen("/proc/self/gid_map", "we");
@@ -172,6 +187,9 @@ struct nltst_netns *nltst_netns_enter(void)
172187
unshare_user();
173188

174189
r = unshare(CLONE_NEWNET | CLONE_NEWNS);
190+
if (r == EPERM) {
191+
return NULL;
192+
}
175193
_nltst_assert_errno(r == 0);
176194

177195
/* We need a read-only /sys so that the platform knows there's no udev. */

tests/nl-test-util.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,9 @@ char **_nltst_strtokv(const char *str);
429429

430430
void nltst_netns_fixture_setup(void);
431431
void nltst_netns_fixture_teardown(void);
432+
bool nltst_netns_has_netns(void);
433+
434+
bool nltst_netns_skip_without_netns(void);
432435

433436
struct nltst_netns;
434437

0 commit comments

Comments
 (0)