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 f9dd7f0d84)
This commit is contained in:
Lubomir Rintel 2015-05-15 11:46:43 +02:00 committed by Thomas Haller
parent 90eb13a111
commit a030ac36d3
3 changed files with 44 additions and 25 deletions

View file

@ -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);
}
}
}

View file

@ -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)

View file

@ -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;