summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLubomir Rintel <lkundrak@v3.sk>2015-04-19 09:12:37 +0200
committerThomas Haller <thaller@redhat.com>2015-05-11 19:28:13 +0200
commitd6aef9c188468224d6e1dd670844c3f0e7482c35 (patch)
tree62b28d84019fe85eaa5081f3c7bfd739a5c9c6c7
parent21fef6a3573f18c2490aa65815b020ccf92f893d (diff)
downloadNetworkManager-d6aef9c188468224d6e1dd670844c3f0e7482c35.tar.gz
platform/test: unshare the netns namespace so that root tests don't mess with the system
Mount a private sysfs instance. Otherwise gudev sees the devices from the parent netns as opposed to our netns. We do, however need a writable /sys/devices subtree for testing the bridge code. There doesn't seem to be any other way to get a writable subtree of a read-only filesystem than remounting it with no parameters after the initial mount. We use this to get a writable sysfs instance and then bindmount it so that it fits properly in the sysfs hierarchy. Co-Authored-By: Thomas Haller <thaller@redhat.com>
-rw-r--r--src/platform/tests/test-common.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/platform/tests/test-common.c b/src/platform/tests/test-common.c
index a4a2f21915..bb94debc78 100644
--- a/src/platform/tests/test-common.c
+++ b/src/platform/tests/test-common.c
@@ -1,5 +1,8 @@
#include "config.h"
+#include <sys/mount.h>
+#include <sched.h>
+
#include "test-common.h"
#include "nm-test-utils.h"
@@ -279,6 +282,41 @@ main (int argc, char **argv)
#endif
}
+ if (nmtst_platform_is_root_test () && !g_getenv ("NMTST_NO_UNSHARE")) {
+ int errsv;
+
+ if (unshare (CLONE_NEWNET | CLONE_NEWNS) != 0) {
+ errsv = errno;
+ g_error ("unshare(CLONE_NEWNET|CLONE_NEWNS) failed with %s (%d)", strerror (errsv), errsv);
+ }
+
+ /* Mount our /sys instance, so that gudev sees only our devices.
+ * Needs to be read-only, because we don't run udev. */
+ if (mount (NULL, "/sys", NULL, MS_SLAVE, NULL) != 0) {
+ errsv = errno;
+ g_error ("mount(\"/\", MS_SLAVE) failed with %s (%d)", strerror (errsv), errsv);
+ }
+ if (mount ("sys", "/sys", "sysfs", MS_RDONLY, NULL) != 0) {
+ errsv = errno;
+ g_error ("mount(\"/sys\") failed with %s (%d)", strerror (errsv), errsv);
+ }
+
+ /* Create a writable /sys/devices tree. This makes it possible to run tests
+ * that modify values via sysfs (such as bridge forward delay). */
+ if (mount ("sys", "/sys/devices", "sysfs", 0, NULL) != 0) {
+ errsv = errno;
+ g_error ("mount(\"/sys/devices\") failed with %s (%d)", strerror (errsv), errsv);
+ }
+ if (mount (NULL, "/sys/devices", "sysfs", MS_REMOUNT, NULL) != 0) {
+ errsv = errno;
+ g_error ("remount(\"/sys/devices\") failed with %s (%d)", strerror (errsv), errsv);
+ }
+ if (mount ("/sys/devices/devices", "/sys/devices", NULL, MS_BIND, NULL) != 0) {
+ errsv = errno;
+ g_error ("mount(\"/sys\") failed with %s (%d)", strerror (errsv), errsv);
+ }
+ }
+
SETUP ();
setup_tests ();