Skip to content

Commit 1662d08

Browse files
committed
test: skip tests when having no private netns
Under valgrind in github CI we (currently) seem unable to create the netns. This worked previously, now it no longer does. Handle that by skipping the tests that require a netns.
1 parent 5da4c1e commit 1662d08

File tree

4 files changed

+72
-5
lines changed

4 files changed

+72
-5
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ jobs:
107107
run: |
108108
set -x
109109
export NLTST_SEED_RAND=
110-
CK_FORK=no libtool --mode=execute valgrind --error-exitcode=66 --leak-check=full -s --show-leak-kinds=all ./tests/check-direct
111-
CK_FORK=no libtool --mode=execute valgrind --error-exitcode=66 --leak-check=full -s --show-leak-kinds=all ./tests/check-all
110+
NLTST_IN_CI_VALGRIND=1 CK_FORK=no libtool --mode=execute valgrind --error-exitcode=66 --leak-check=full -s --show-leak-kinds=all ./tests/check-direct
111+
NLTST_IN_CI_VALGRIND=1 CK_FORK=no libtool --mode=execute valgrind --error-exitcode=66 --leak-check=full -s --show-leak-kinds=all ./tests/check-all
112112
shell: bash
113113

114114
- name: Install packages for Release

tests/cksuite-all-netns.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ START_TEST(cache_and_clone)
7373
size_t i;
7474
int r;
7575

76+
if (_nltst_skip_no_netns())
77+
return;
78+
7679
for (i = 0; i < _NL_N_ELEMENTS(links); i++) {
7780
if (links[i].add)
7881
_nltst_add_link(NULL, links[i].ifname, links[i].kind,
@@ -132,11 +135,16 @@ START_TEST(test_create_iface)
132135
_nl_auto_rtnl_link struct rtnl_link *link2 = NULL;
133136
_nl_auto_rtnl_link struct rtnl_link *peer = NULL;
134137
_nltst_auto_delete_link const char *IFNAME_DUMMY = NULL;
135-
_nltst_auto_delete_link const char *IFNAME = "ifname";
138+
_nltst_auto_delete_link const char *IFNAME = NULL;
136139
int ifindex_dummy;
137140
uint32_t u32;
138141
int r;
139142

143+
if (_nltst_skip_no_netns())
144+
return;
145+
146+
IFNAME = "ifname";
147+
140148
switch (TEST_IDX) {
141149
case 0:
142150
link = _nltst_assert(rtnl_link_bridge_alloc());
@@ -317,6 +325,9 @@ START_TEST(route_1)
317325
_nl_auto_nl_socket struct nl_sock *sk = NULL;
318326
_nl_auto_nl_cache struct nl_cache *cache = NULL;
319327

328+
if (_nltst_skip_no_netns())
329+
return;
330+
320331
if (_nltst_skip_no_iproute2("route_1"))
321332
return;
322333

tests/nl-test-util.c

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ uint32_t _nltst_rand_u32(void)
8484

8585
struct nltst_netns {
8686
int canary;
87+
bool is_unshared;
8788
};
8889

8990
/*****************************************************************************/
@@ -114,6 +115,30 @@ void nltst_netns_fixture_teardown(void)
114115
_nl_clear_pointer(&_netns_fixture_global.nsdata, nltst_netns_leave);
115116
}
116117

118+
bool nltst_netns_fixture_is_unshared(void)
119+
{
120+
_assert_nltst_netns(_netns_fixture_global.nsdata);
121+
return _netns_fixture_global.nsdata->is_unshared;
122+
}
123+
124+
bool _nltst_skip_no_netns(void)
125+
{
126+
if (nltst_netns_fixture_is_unshared())
127+
return false;
128+
129+
if (_nltst_in_ci()) {
130+
/* In CI, we exepect normal tests to have a netns, but under
131+
* valgrind this might fail. */
132+
if (!_nl_streq0(getenv("NLTST_IN_CI_VALGRIND"), "1")) {
133+
ck_abort_msg(
134+
"Unable to create private netns under CI (NLTST_IN_CI=1) while not running valgind (NLTST_IN_CI_VALGRIND!=1)");
135+
}
136+
}
137+
138+
printf("skip test due to having no private netns\n");
139+
return true;
140+
}
141+
117142
/*****************************************************************************/
118143

119144
static void unshare_user(void)
@@ -125,6 +150,10 @@ static void unshare_user(void)
125150

126151
/* Become a root in new user NS. */
127152
r = unshare(CLONE_NEWUSER);
153+
if (r != 0 && errno == EPERM) {
154+
/* No permissions? Ignore. Will be handled later. */
155+
return;
156+
}
128157
_nltst_assert_errno(r == 0);
129158

130159
/* Since Linux 3.19 we have to disable setgroups() in order to map users.
@@ -149,14 +178,28 @@ static void unshare_user(void)
149178
}
150179
r = fprintf(f, "0 %d 1", uid);
151180
_nltst_assert_errno(r > 0);
152-
_nltst_fclose(f);
181+
r = fclose(f);
182+
if (r != 0 && errno == EPERM) {
183+
/* Oddly, it seems close() can fail at this point. Ignore it,
184+
* but we probably will be unable to unshare (which we handle
185+
* later).
186+
*/
187+
} else
188+
_nltst_assert_errno(r == 0);
153189

154190
/* Map current GID to root in NS to be created. */
155191
f = fopen("/proc/self/gid_map", "we");
156192
_nltst_assert_errno(f);
157193
r = fprintf(f, "0 %d 1", gid);
158194
_nltst_assert_errno(r > 0);
159-
_nltst_fclose(f);
195+
r = fclose(f);
196+
if (r != 0 && errno == EPERM) {
197+
/* Oddly, it seems close() can fail at this point. Ignore it, but
198+
* we probably will be unable to unshare (which we handle
199+
* later).
200+
*/
201+
} else
202+
_nltst_assert_errno(r == 0);
160203
}
161204

162205
struct nltst_netns *nltst_netns_enter(void)
@@ -172,13 +215,23 @@ struct nltst_netns *nltst_netns_enter(void)
172215
unshare_user();
173216

174217
r = unshare(CLONE_NEWNET | CLONE_NEWNS);
218+
if (r != 0 && errno == EPERM) {
219+
/* The system is probably sandboxed somehow and we are unable
220+
* to create a private netns. That seems questionable, because
221+
* a point of a private netns is to sandbox an application.
222+
* Not having permissions to sandbox sounds bad.
223+
*
224+
* Anyway. We accept this and will later skip some tests. */
225+
return nsdata;
226+
}
175227
_nltst_assert_errno(r == 0);
176228

177229
/* We need a read-only /sys so that the platform knows there's no udev. */
178230
mount(NULL, "/sys", "sysfs", MS_SLAVE, NULL);
179231
r = mount("sys", "/sys", "sysfs", MS_RDONLY, NULL);
180232
_nltst_assert_errno(r == 0);
181233

234+
nsdata->is_unshared = true;
182235
return nsdata;
183236
}
184237

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_fixture_is_unshared(void);
433+
434+
bool _nltst_skip_no_netns(void);
432435

433436
struct nltst_netns;
434437

0 commit comments

Comments
 (0)