From a030ac36d3ddffb8b1c3b51343123fdeae47548b Mon Sep 17 00:00:00 2001 From: Lubomir Rintel Date: Fri, 15 May 2015 11:46:43 +0200 Subject: [PATCH] tests: allow running the link test without a writeable sysfs This fixes the test run in a container/our NETNS/NEWUSER test jail. (cherry picked from commit f9dd7f0d849d277cf224c10d069d689764cd2973) --- src/platform/tests/test-common.c | 24 ++++++++++++----- src/platform/tests/test-common.h | 1 + src/platform/tests/test-link.c | 44 ++++++++++++++++++-------------- 3 files changed, 44 insertions(+), 25 deletions(-) diff --git a/src/platform/tests/test-common.c b/src/platform/tests/test-common.c index 2eda47cc8b..203a6e18b1 100644 --- a/src/platform/tests/test-common.c +++ b/src/platform/tests/test-common.c @@ -19,6 +19,13 @@ nmtst_platform_is_root_test () NM_PRAGMA_WARNING_REENABLE } +gboolean +nmtst_platform_is_sysfs_writable () +{ + return !nmtst_platform_is_root_test () + || (access ("/sys/devices", W_OK) == 0); +} + SignalData * add_signal_full (const char *name, NMPlatformSignalChangeType change_type, GCallback callback, int ifindex, const char *ifname) { @@ -325,12 +332,17 @@ main (int argc, char **argv) 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", "sysfs", MS_BIND, NULL) != 0) { - errsv = errno; - g_error ("mount(\"/sys\") failed with %s (%d)", strerror (errsv), errsv); + /* Read-write remount failed. Never mind, we're probably just a root in + * our user NS. */ + if (umount ("/sys/devices") != 0) { + errsv = errno; + g_error ("umount(\"/sys/devices\") failed with %s (%d)", strerror (errsv), errsv); + } + } else { + if (mount ("/sys/devices/devices", "/sys/devices", "sysfs", MS_BIND, NULL) != 0) { + errsv = errno; + g_error ("mount(\"/sys\") failed with %s (%d)", strerror (errsv), errsv); + } } } diff --git a/src/platform/tests/test-common.h b/src/platform/tests/test-common.h index 56e27f6581..563d9fb4f6 100644 --- a/src/platform/tests/test-common.h +++ b/src/platform/tests/test-common.h @@ -29,6 +29,7 @@ typedef struct { } SignalData; gboolean nmtst_platform_is_root_test (void); +gboolean nmtst_platform_is_sysfs_writable (void); SignalData *add_signal_full (const char *name, NMPlatformSignalChangeType change_type, GCallback callback, int ifindex, const char *ifname); #define add_signal(name, change_type, callback) add_signal_full (name, change_type, (GCallback) callback, 0, NULL) diff --git a/src/platform/tests/test-link.c b/src/platform/tests/test-link.c index bbd42946af..102fe795f7 100644 --- a/src/platform/tests/test-link.c +++ b/src/platform/tests/test-link.c @@ -219,12 +219,14 @@ test_slave (int master, int type, SignalData *master_changed) /* Set slave option */ switch (type) { case NM_LINK_TYPE_BRIDGE: - g_assert (nm_platform_slave_set_option (NM_PLATFORM_GET, ifindex, "priority", "789")); - no_error (); - value = nm_platform_slave_get_option (NM_PLATFORM_GET, ifindex, "priority"); - no_error (); - g_assert_cmpstr (value, ==, "789"); - g_free (value); + if (nmtst_platform_is_sysfs_writable ()) { + g_assert (nm_platform_slave_set_option (NM_PLATFORM_GET, ifindex, "priority", "789")); + no_error (); + value = nm_platform_slave_get_option (NM_PLATFORM_GET, ifindex, "priority"); + no_error (); + g_assert_cmpstr (value, ==, "789"); + g_free (value); + } break; default: break; @@ -294,21 +296,25 @@ test_software (NMLinkType link_type, const char *link_typename) /* Set master option */ switch (link_type) { case NM_LINK_TYPE_BRIDGE: - g_assert (nm_platform_master_set_option (NM_PLATFORM_GET, ifindex, "forward_delay", "789")); - no_error (); - value = nm_platform_master_get_option (NM_PLATFORM_GET, ifindex, "forward_delay"); - no_error (); - g_assert_cmpstr (value, ==, "789"); - g_free (value); + if (nmtst_platform_is_sysfs_writable ()) { + g_assert (nm_platform_master_set_option (NM_PLATFORM_GET, ifindex, "forward_delay", "789")); + no_error (); + value = nm_platform_master_get_option (NM_PLATFORM_GET, ifindex, "forward_delay"); + no_error (); + g_assert_cmpstr (value, ==, "789"); + g_free (value); + } break; case NM_LINK_TYPE_BOND: - g_assert (nm_platform_master_set_option (NM_PLATFORM_GET, ifindex, "mode", "active-backup")); - no_error (); - value = nm_platform_master_get_option (NM_PLATFORM_GET, ifindex, "mode"); - no_error (); - /* When reading back, the output looks slightly different. */ - g_assert (g_str_has_prefix (value, "active-backup")); - g_free (value); + if (nmtst_platform_is_sysfs_writable ()) { + g_assert (nm_platform_master_set_option (NM_PLATFORM_GET, ifindex, "mode", "active-backup")); + no_error (); + value = nm_platform_master_get_option (NM_PLATFORM_GET, ifindex, "mode"); + no_error (); + /* When reading back, the output looks slightly different. */ + g_assert (g_str_has_prefix (value, "active-backup")); + g_free (value); + } break; default: break;