naccyde / rpms / systemd

Forked from rpms/systemd a year ago
Clone
594167
From 01df4d485c8a6f3493a83a2a61572e9f2bccb649 Mon Sep 17 00:00:00 2001
594167
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
594167
Date: Wed, 19 Jan 2022 09:39:55 +0100
594167
Subject: [PATCH] test-load-fragment: add a basic test for
594167
 config_parse_unit_env_file()
594167
594167
(cherry picked from commit 3ef86964ed151aa2464701eef46d665adfd70895)
594167
594167
Related: #2017035
594167
---
594167
 src/test/test-load-fragment.c | 64 +++++++++++++++++++++++++++++++++++
594167
 1 file changed, 64 insertions(+)
594167
594167
diff --git a/src/test/test-load-fragment.c b/src/test/test-load-fragment.c
594167
index 1bd68c7e0a..fbe4744333 100644
594167
--- a/src/test/test-load-fragment.c
594167
+++ b/src/test/test-load-fragment.c
594167
@@ -773,6 +773,70 @@ TEST(config_parse_pass_environ) {
594167
         assert_se(streq(passenv[0], "normal_name"));
594167
 }
594167
 
594167
+TEST(config_parse_unit_env_file) {
594167
+        /* int config_parse_unit_env_file(
594167
+                 const char *unit,
594167
+                 const char *filename,
594167
+                 unsigned line,
594167
+                 const char *section,
594167
+                 unsigned section_line,
594167
+                 const char *lvalue,
594167
+                 int ltype,
594167
+                 const char *rvalue,
594167
+                 void *data,
594167
+                 void *userdata) */
594167
+
594167
+        _cleanup_(manager_freep) Manager *m = NULL;
594167
+        Unit *u;
594167
+        _cleanup_strv_free_ char **files = NULL;
594167
+        int r;
594167
+
594167
+        r = manager_new(UNIT_FILE_USER, MANAGER_TEST_RUN_MINIMAL, &m);
594167
+        if (manager_errno_skip_test(r)) {
594167
+                log_notice_errno(r, "Skipping test: manager_new: %m");
594167
+                return;
594167
+        }
594167
+
594167
+        assert_se(r >= 0);
594167
+        assert_se(manager_startup(m, NULL, NULL, NULL) >= 0);
594167
+
594167
+        assert_se(u = unit_new(m, sizeof(Service)));
594167
+        assert_se(unit_add_name(u, "foobar.service") == 0);
594167
+
594167
+        r = config_parse_unit_env_file(u->id, "fake", 1, "section", 1,
594167
+                                      "EnvironmentFile", 0, "not-absolute",
594167
+                                       &files, u);
594167
+        assert_se(r == 0);
594167
+        assert_se(strv_length(files) == 0);
594167
+
594167
+        r = config_parse_unit_env_file(u->id, "fake", 1, "section", 1,
594167
+                                      "EnvironmentFile", 0, "/absolute1",
594167
+                                       &files, u);
594167
+        assert_se(r == 0);
594167
+        assert_se(strv_length(files) == 1);
594167
+
594167
+        r = config_parse_unit_env_file(u->id, "fake", 1, "section", 1,
594167
+                                      "EnvironmentFile", 0, "/absolute2",
594167
+                                       &files, u);
594167
+        assert_se(r == 0);
594167
+        assert_se(strv_length(files) == 2);
594167
+        assert_se(streq(files[0], "/absolute1"));
594167
+        assert_se(streq(files[1], "/absolute2"));
594167
+
594167
+        r = config_parse_unit_env_file(u->id, "fake", 1, "section", 1,
594167
+                                       "EnvironmentFile", 0, "",
594167
+                                       &files, u);
594167
+        assert_se(r == 0);
594167
+        assert_se(strv_isempty(files));
594167
+
594167
+        r = config_parse_unit_env_file(u->id, "fake", 1, "section", 1,
594167
+                                       "EnvironmentFile", 0, "/path/%n.conf",
594167
+                                       &files, u);
594167
+        assert_se(r == 0);
594167
+        assert_se(strv_length(files) == 1);
594167
+        assert_se(streq(files[0], "/path/foobar.service.conf"));
594167
+}
594167
+
594167
 TEST(unit_dump_config_items) {
594167
         unit_dump_config_items(stdout);
594167
 }