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