Make TestPidOf1 unit test more robust
authorThomas Jarosch <thomas.jarosch@intra2net.com>
Tue, 30 Dec 2025 10:58:20 +0000 (11:58 +0100)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Wed, 31 Dec 2025 10:44:51 +0000 (11:44 +0100)
The test expects to find PID 1 by searching for "init" or "systemd".

While some LXC container is running:
- PID 1 runs systemd (not traditional init)
- Another process (e.g., PID 41150) may have "init" in its cmdline

This causes pid_of("init") to return PID 41150 instead of PID 1,
and the test would fail because PID 1 is not in the result.

The fix checks if PID 1 is found after the "init" search, and if not,
falls back to searching for "systemd" to find PID 1.

test/test_filefunc.cpp

index f3fa0c5..583de35 100644 (file)
@@ -313,17 +313,17 @@ BOOST_AUTO_TEST_CASE(TestPidOf1)
     bool res= pid_of("init", pid_list);
     BOOST_CHECK_EQUAL( true, res);
     
-    if (pid_list.empty())
+    // Check if PID 1 was found. If not, try systemd.
+    std::vector< pid_t >::const_iterator pos1 =
+        std::find( pid_list.begin(), pid_list.end(), 1);
+
+    if (pos1 == pid_list.end())
     {
         res= pid_of("systemd", pid_list);
         BOOST_CHECK_EQUAL( true, res);
+        pos1 = std::find( pid_list.begin(), pid_list.end(), 1);
     }
     
-    BOOST_CHECK_EQUAL( false, pid_list.empty() );
-
-    std::vector< pid_t >::const_iterator pos1 =
-        std::find( pid_list.begin(), pid_list.end(), 1);
-
     BOOST_CHECK( pos1 != pid_list.end() );
 } // eo TestPidOf1()