richardphibel / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
594167
From b8ebf512d4afa0f64503a96c2b58b12d9de531fb Mon Sep 17 00:00:00 2001
594167
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
594167
Date: Mon, 7 Mar 2022 19:22:01 +0100
594167
Subject: [PATCH] test-os-util: add basic tests for os-release parsing
594167
594167
(cherry picked from commit 80e72f80bc407753582b421421c13ca50b675027)
594167
594167
Related: #2082131
594167
---
594167
 src/test/test-os-util.c | 62 +++++++++++++++++++++++++++++++++++++++++
594167
 1 file changed, 62 insertions(+)
594167
594167
diff --git a/src/test/test-os-util.c b/src/test/test-os-util.c
594167
index eb5466259a..5f82748783 100644
594167
--- a/src/test/test-os-util.c
594167
+++ b/src/test/test-os-util.c
594167
@@ -2,8 +2,11 @@
594167
 
594167
 #include <errno.h>
594167
 
594167
+#include "fs-util.h"
594167
 #include "log.h"
594167
 #include "os-util.h"
594167
+#include "string-util.h"
594167
+#include "strv.h"
594167
 #include "tests.h"
594167
 
594167
 TEST(path_is_os_tree) {
594167
@@ -12,4 +15,63 @@ TEST(path_is_os_tree) {
594167
         assert_se(path_is_os_tree("/idontexist") == -ENOENT);
594167
 }
594167
 
594167
+TEST(parse_os_release) {
594167
+        /* Let's assume that we're running in a valid system, so os-release is available */
594167
+        _cleanup_free_ char *id = NULL, *id2 = NULL, *name = NULL, *foobar = NULL;
594167
+        assert_se(parse_os_release(NULL, "ID", &id) == 1);
594167
+        log_info("ID: %s", id);
594167
+
594167
+        assert_se(setenv("SYSTEMD_OS_RELEASE", "/dev/null", 1) == 0);
594167
+        assert_se(parse_os_release(NULL, "ID", &id2) == 0);
594167
+        log_info("ID: %s", strnull(id2));
594167
+
594167
+        _cleanup_(unlink_tempfilep) char tmpfile[] = "/tmp/test-os-util.XXXXXX";
594167
+        assert_se(write_tmpfile(tmpfile,
594167
+                                "ID=the-id  \n"
594167
+                                "NAME=the-name") == 0);
594167
+
594167
+        assert_se(setenv("SYSTEMD_OS_RELEASE", tmpfile, 1) == 0);
594167
+        assert_se(parse_os_release(NULL, "ID", &id, "NAME", &name) == 2);
594167
+        log_info("ID: %s NAME: %s", id, name);
594167
+        assert_se(streq(id, "the-id"));
594167
+        assert_se(streq(name, "the-name"));
594167
+
594167
+        _cleanup_(unlink_tempfilep) char tmpfile2[] = "/tmp/test-os-util.XXXXXX";
594167
+        assert_se(write_tmpfile(tmpfile2,
594167
+                                "ID=\"ignored\"  \n"
594167
+                                "ID=\"the-id\"  \n"
594167
+                                "NAME='the-name'") == 0);
594167
+
594167
+        assert_se(setenv("SYSTEMD_OS_RELEASE", tmpfile2, 1) == 0);
594167
+        // FIXME: we return 3, which means that the return value is useless in face of repeats
594167
+        assert_se(parse_os_release(NULL, "ID", &id, "NAME", &name) == 3);
594167
+        log_info("ID: %s NAME: %s", id, name);
594167
+        assert_se(streq(id, "the-id"));
594167
+        assert_se(streq(name, "the-name"));
594167
+
594167
+        assert_se(parse_os_release(NULL, "FOOBAR", &foobar) == 0);
594167
+        log_info("FOOBAR: %s", strnull(foobar));
594167
+        assert_se(foobar == NULL);
594167
+
594167
+        assert_se(unsetenv("SYSTEMD_OS_RELEASE") == 0);
594167
+}
594167
+
594167
+TEST(load_os_release_pairs) {
594167
+        _cleanup_(unlink_tempfilep) char tmpfile[] = "/tmp/test-os-util.XXXXXX";
594167
+        assert_se(write_tmpfile(tmpfile,
594167
+                                "ID=\"ignored\"  \n"
594167
+                                "ID=\"the-id\"  \n"
594167
+                                "NAME='the-name'") == 0);
594167
+
594167
+        assert_se(setenv("SYSTEMD_OS_RELEASE", tmpfile, 1) == 0);
594167
+
594167
+        _cleanup_strv_free_ char **pairs = NULL;
594167
+        assert_se(load_os_release_pairs(NULL, &pairs) == 0);
594167
+        assert_se(strv_equal(pairs, STRV_MAKE("ID", "ignored", // FIXME
594167
+                                              "ID", "the-id",
594167
+                                              "NAME", "the-name")));
594167
+
594167
+        assert_se(unsetenv("SYSTEMD_OS_RELEASE") == 0);
594167
+}
594167
+
594167
 DEFINE_TEST_MAIN(LOG_DEBUG);