test: Various build environment fixes
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 8 Apr 2024 17:56:36 +0000 (19:56 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 9 Apr 2024 07:40:03 +0000 (09:40 +0200)
All bets are off in build chroots, so let's handle more cases of
files or executables that might not be available in build chroots.

Specifically, these are all fixes to allow the unit tests to run
in the opensuse build chroot.

src/test/test-acl-util.c
src/test/test-copy.c
src/test/test-date.c
src/test/test-os-util.c
src/test/test-specifier.c

index 331faf1e4b304fbb2cd9420d7ef4b9aaa67c8006..3cb106a93f6f7dfffaec8340758ccc2a89716ac8 100644 (file)
@@ -10,6 +10,7 @@
 #include "fd-util.h"
 #include "format-util.h"
 #include "fs-util.h"
+#include "path-util.h"
 #include "string-util.h"
 #include "tests.h"
 #include "tmpfile-util.h"
@@ -22,6 +23,12 @@ TEST_RET(add_acls_for_user) {
         uid_t uid;
         int r;
 
+        FOREACH_STRING(s, "capsh", "getfacl", "ls") {
+                r = find_executable(s, NULL);
+                if (r < 0)
+                        return log_tests_skipped_errno(r, "Could not find %s binary: %m", s);
+        }
+
         fd = mkostemp_safe(fn);
         assert_se(fd >= 0);
 
@@ -69,11 +76,18 @@ TEST_RET(add_acls_for_user) {
         return 0;
 }
 
-TEST(fd_acl_make_read_only) {
+TEST_RET(fd_acl_make_read_only) {
         _cleanup_(unlink_tempfilep) char fn[] = "/tmp/test-empty.XXXXXX";
         _cleanup_close_ int fd = -EBADF;
         const char *cmd;
         struct stat st;
+        int r;
+
+        FOREACH_STRING(s, "capsh", "getfacl", "ls", "stat") {
+                r = find_executable(s, NULL);
+                if (r < 0)
+                        return log_tests_skipped_errno(r, "Could not find %s binary: %m", s);
+        }
 
         fd = mkostemp_safe(fn);
         assert_se(fd >= 0);
@@ -125,6 +139,8 @@ TEST(fd_acl_make_read_only) {
 
         cmd = strjoina("stat ", fn);
         assert_se(system(cmd) == 0);
+
+        return 0;
 }
 
 DEFINE_TEST_MAIN(LOG_INFO);
index 111c001de6b45405b40af6c130103ccfeca3aa63..ea5d67ff48346fc7ef96ff7c074a58dc0d829174 100644 (file)
@@ -290,7 +290,7 @@ TEST(copy_tree_at_symlink) {
         fd = safe_close(fd);
 }
 
-TEST(copy_bytes) {
+TEST_RET(copy_bytes) {
         _cleanup_close_pair_ int pipefd[2] = EBADF_PAIR;
         _cleanup_close_ int infd = -EBADF;
         int r, r2;
@@ -299,7 +299,8 @@ TEST(copy_bytes) {
         infd = open("/usr/lib/os-release", O_RDONLY|O_CLOEXEC);
         if (infd < 0)
                 infd = open("/etc/os-release", O_RDONLY|O_CLOEXEC);
-        assert_se(infd >= 0);
+        if (infd < 0)
+                return log_tests_skipped_errno(errno, "Could not open /usr/lib/os-release or /etc/os-release: %m");
 
         assert_se(pipe2(pipefd, O_CLOEXEC) == 0);
 
@@ -324,6 +325,8 @@ TEST(copy_bytes) {
 
         r = copy_bytes(pipefd[1], infd, 1, 0);
         assert_se(r == -EBADF);
+
+        return 0;
 }
 
 static void test_copy_bytes_regular_file_one(const char *src, bool try_reflink, uint64_t max_bytes) {
index 162ac342f50bb787debbd64ffd8c05f95aed868a..2b96983e2cb612fd101db7aed2f80e6629e30e2d 100644 (file)
@@ -83,9 +83,11 @@ int main(int argc, char *argv[]) {
         test_one("today");
         test_one("tomorrow");
         test_one_noutc("16:20 UTC");
-        test_one_noutc("16:20 Asia/Seoul");
-        test_one_noutc("tomorrow Asia/Seoul");
-        test_one_noutc("2012-12-30 18:42 Asia/Seoul");
+        if (access("/usr/share/zoneinfo/Asia/Seoul", F_OK) >= 0) {
+                test_one_noutc("16:20 Asia/Seoul");
+                test_one_noutc("tomorrow Asia/Seoul");
+                test_one_noutc("2012-12-30 18:42 Asia/Seoul");
+        }
         test_one_noutc("now");
         test_one_noutc("+2d");
         test_one_noutc("+2y 4d");
@@ -96,9 +98,11 @@ int main(int argc, char *argv[]) {
         test_should_fail("1969-12-31 UTC");
         test_should_fail("-1000y");
         test_should_fail("today UTC UTC");
-        test_should_fail("now Asia/Seoul");
-        test_should_fail("+2d Asia/Seoul");
-        test_should_fail("@1395716396 Asia/Seoul");
+        if (access("/usr/share/zoneinfo/Asia/Seoul", F_OK) >= 0) {
+                test_should_fail("now Asia/Seoul");
+                test_should_fail("+2d Asia/Seoul");
+                test_should_fail("@1395716396 Asia/Seoul");
+        }
 #if SIZEOF_TIME_T == 8
         test_should_pass("9999-12-30 23:59:59 UTC");
         test_should_fail("9999-12-31 00:00:00 UTC");
index e51ab9d29086bb28f7543e3f88e1bd683962cbeb..e6838421709f2ef06ca4f5dbba2b0a3ca2bbb3ce 100644 (file)
@@ -20,10 +20,12 @@ TEST(path_is_os_tree) {
 }
 
 TEST(parse_os_release) {
-        /* Let's assume that we're running in a valid system, so os-release is available */
         _cleanup_free_ char *id = NULL, *id2 = NULL, *name = NULL, *foobar = NULL;
-        ASSERT_EQ(parse_os_release(NULL, "ID", &id), 0);
-        log_info("ID: %s", id);
+
+        if (access("/etc/os-release", F_OK) >= 0 || access("/usr/lib/os-release", F_OK) >= 0) {
+                ASSERT_EQ(parse_os_release(NULL, "ID", &id), 0);
+                log_info("ID: %s", id);
+        }
 
         ASSERT_EQ(setenv("SYSTEMD_OS_RELEASE", "/dev/null", 1), 0);
         ASSERT_EQ(parse_os_release(NULL, "ID", &id2), 0);
index d6a8b79aa4ac6693f4c08b0d5ba0bf40731fa576..e50d4504360728901001e7b0e87c633708ec4806 100644 (file)
@@ -138,6 +138,8 @@ TEST(specifiers) {
                 xsprintf(spec, "%%%c", s->specifier);
 
                 r = specifier_printf(spec, SIZE_MAX, specifier_table, NULL, NULL, &resolved);
+                if (s->specifier == 'A' && r == -EUNATCH) /* os-release might be missing in build chroots */
+                        continue;
                 if (s->specifier == 'm' && IN_SET(r, -EUNATCH, -ENOMEDIUM, -ENOPKG)) /* machine-id might be missing in build chroots */
                         continue;
                 assert_se(r >= 0);