]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test-sd-device: ignore several errors when running on non-host network namespace 22953/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 5 Apr 2022 05:28:20 +0000 (14:28 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 5 Apr 2022 05:33:19 +0000 (14:33 +0900)
Fixes https://github.com/systemd/systemd/commit/93e04eb43bed6cd1cea344f8fb1002b62155fae2#commitcomment-70387367.

src/libsystemd/sd-device/test-sd-device.c

index 560a73ad260b7088095939cf8bdfe4d3c84b4016..7162aac72f5132fe43b621c9b717439f563110fa 100644 (file)
@@ -21,10 +21,11 @@ static void test_sd_device_one(sd_device *d) {
         bool is_block = false;
         dev_t devnum;
         usec_t usec;
-        int i, r;
+        int ifindex, r;
 
         assert_se(sd_device_get_syspath(d, &syspath) >= 0);
         assert_se(path_startswith(syspath, "/sys"));
+        assert_se(sd_device_get_sysname(d, &sysname) >= 0);
 
         log_info("%s(%s)", __func__, syspath);
 
@@ -38,7 +39,31 @@ static void test_sd_device_one(sd_device *d) {
         assert_se(streq(syspath, val));
         dev = sd_device_unref(dev);
 
-        assert_se(sd_device_get_sysname(d, &sysname) >= 0);
+        r = sd_device_get_ifindex(d, &ifindex);
+        if (r >= 0) {
+                assert_se(ifindex > 0);
+
+                r = sd_device_new_from_ifindex(&dev, ifindex);
+                if (r == -ENODEV)
+                        log_device_warning_errno(d, r,
+                                                 "Failed to create sd-device object from ifindex %i. "
+                                                 "Maybe running on a non-host network namespace.", ifindex);
+                else {
+                        assert_se(r >= 0);
+                        assert_se(sd_device_get_syspath(dev, &val) >= 0);
+                        assert_se(streq(syspath, val));
+                        dev = sd_device_unref(dev);
+                }
+
+                /* This does not require the interface really exists on the network namespace.
+                 * Hence, this should always succeed. */
+                assert_se(sd_device_new_from_ifname(&dev, sysname) >= 0);
+                assert_se(sd_device_get_syspath(dev, &val) >= 0);
+                assert_se(streq(syspath, val));
+                dev = sd_device_unref(dev);
+        } else
+                assert_se(r == -ENOENT);
+
         r = sd_device_get_subsystem(d, &subsystem);
         if (r >= 0) {
                 const char *name, *id;
@@ -54,10 +79,17 @@ static void test_sd_device_one(sd_device *d) {
 
                 /* The device ID depends on subsystem. */
                 assert_se(device_get_device_id(d, &id) >= 0);
-                assert_se(sd_device_new_from_device_id(&dev, id) >= 0);
-                assert_se(sd_device_get_syspath(dev, &val) >= 0);
-                assert_se(streq(syspath, val));
-                dev = sd_device_unref(dev);
+                r = sd_device_new_from_device_id(&dev, id);
+                if (r == -ENODEV && ifindex > 0)
+                        log_device_warning_errno(d, r,
+                                                 "Failed to create sd-device object from device ID \"%s\". "
+                                                 "Maybe running on a non-host network namespace.", id);
+                else {
+                        assert_se(r >= 0);
+                        assert_se(sd_device_get_syspath(dev, &val) >= 0);
+                        assert_se(streq(syspath, val));
+                        dev = sd_device_unref(dev);
+                }
 
                 /* These require udev database, and reading database requires device ID. */
                 r = sd_device_get_is_initialized(d);
@@ -122,22 +154,6 @@ static void test_sd_device_one(sd_device *d) {
         } else
                 assert_se(r == -ENOENT);
 
-        r = sd_device_get_ifindex(d, &i);
-        if (r >= 0) {
-                assert_se(i > 0);
-
-                assert_se(sd_device_new_from_ifindex(&dev, i) >= 0);
-                assert_se(sd_device_get_syspath(dev, &val) >= 0);
-                assert_se(streq(syspath, val));
-                dev = sd_device_unref(dev);
-
-                assert_se(sd_device_new_from_ifname(&dev, sysname) >= 0);
-                assert_se(sd_device_get_syspath(dev, &val) >= 0);
-                assert_se(streq(syspath, val));
-                dev = sd_device_unref(dev);
-        } else
-                assert_se(r == -ENOENT);
-
         assert_se(sd_device_get_devpath(d, &val) >= 0);
 
         r = sd_device_get_devtype(d, &val);