]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tests: override XDG_RUNTIME_DIR where we use the user runtime dir
authorLennart Poettering <lennart@poettering.net>
Fri, 8 Apr 2016 16:54:05 +0000 (18:54 +0200)
committerLennart Poettering <lennart@poettering.net>
Tue, 12 Apr 2016 11:43:33 +0000 (13:43 +0200)
We don#t really support systems where XDG_RUNTIME_DIR is not supported for
systemd --user. Hence, let's always set our own XDG_RUNTIME_DIR for tests that
involve systemd --user, so that we know it is set, and that it doesn't polute
the user's actual runtime dir.

Makefile.am
src/basic/rm-rf.h
src/shared/tests.c [new file with mode: 0644]
src/shared/tests.h [new file with mode: 0644]
src/test/test-cgroup-mask.c
src/test/test-engine.c
src/test/test-path.c
src/test/test-sched-prio.c
src/test/test-unit-file.c

index ea6b0340b51b117aa3b258a62680e87d82e0de8c..34eaefd0bf40897b9399cf70b7006cf7b91256ed 100644 (file)
@@ -1036,7 +1036,9 @@ libshared_la_SOURCES = \
        src/shared/machine-pool.c \
        src/shared/machine-pool.h \
        src/shared/resolve-util.c \
-       src/shared/resolve-util.h
+       src/shared/resolve-util.h \
+       src/shared/tests.h \
+       src/shared/tests.c
 
 if HAVE_UTMP
 libshared_la_SOURCES += \
index 6d032689190266f9f6429d42f0deb9558c8fc631..40b5b527d57265e98f7994dbdbea8eae82ab5f78 100644 (file)
@@ -30,3 +30,12 @@ typedef enum RemoveFlags {
 
 int rm_rf_children(int fd, RemoveFlags flags, struct stat *root_dev);
 int rm_rf(const char *path, RemoveFlags flags);
+
+/* Useful for usage with _cleanup_(), destroys a directory and frees the pointer */
+static inline void rm_rf_and_free(char *p) {
+        if (!p)
+                return;
+        (void) rm_rf(p, REMOVE_ROOT);
+        free(p);
+}
+DEFINE_TRIVIAL_CLEANUP_FUNC(char*, rm_rf_and_free);
diff --git a/src/shared/tests.c b/src/shared/tests.c
new file mode 100644 (file)
index 0000000..4091162
--- /dev/null
@@ -0,0 +1,33 @@
+/***
+  This file is part of systemd.
+
+  Copyright 2016 Lennart Poettering
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <stdlib.h>
+#include <util.h>
+
+#include "tests.h"
+
+char* setup_fake_runtime_dir(void) {
+        char t[] = "/tmp/fake-xdg-runtime-XXXXXX", *p;
+
+        assert_se(mkdtemp(t));
+        assert_se(setenv("XDG_RUNTIME_DIR", t, 1) >= 0);
+        assert_se(p = strdup(t));
+
+        return p;
+}
diff --git a/src/shared/tests.h b/src/shared/tests.h
new file mode 100644 (file)
index 0000000..93f0901
--- /dev/null
@@ -0,0 +1,22 @@
+#pragma once
+
+/***
+  This file is part of systemd.
+
+  Copyright 2016 Lennart Poettering
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+char* setup_fake_runtime_dir(void);
index 332755cf41162eb4609f0bd132f0a2be5c50d87c..4eb8fcd77340048785b9d66b4436bdbe3f722fe4 100644 (file)
@@ -21,7 +21,9 @@
 
 #include "macro.h"
 #include "manager.h"
+#include "rm-rf.h"
 #include "test-helper.h"
+#include "tests.h"
 #include "unit.h"
 
 static int test_cgroup_mask(void) {
@@ -107,7 +109,11 @@ static int test_cgroup_mask(void) {
 }
 
 int main(int argc, char* argv[]) {
+        _cleanup_(rm_rf_and_freep) char *runtime_dir = NULL;
         int rc = 0;
+
+        assert_se(runtime_dir = setup_fake_runtime_dir());
         TEST_REQ_RUNNING_SYSTEMD(rc = test_cgroup_mask());
+
         return rc;
 }
index 52b34c03df8b0d581d16a3d0eab3b586819d351c..361d1e7b0beacdd799569890b09e9fb75a6092ca 100644 (file)
 
 #include "bus-util.h"
 #include "manager.h"
+#include "rm-rf.h"
 #include "test-helper.h"
+#include "tests.h"
 
 int main(int argc, char *argv[]) {
+        _cleanup_(rm_rf_and_freep) char *runtime_dir = NULL;
         _cleanup_(sd_bus_error_free) sd_bus_error err = SD_BUS_ERROR_NULL;
         Manager *m = NULL;
         Unit *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL, *g = NULL, *h = NULL;
@@ -34,6 +37,8 @@ int main(int argc, char *argv[]) {
         Job *j;
         int r;
 
+        assert_se(runtime_dir = setup_fake_runtime_dir());
+
         /* prepare the test */
         assert_se(set_unit_path(TEST_DIR) >= 0);
         r = manager_new(UNIT_FILE_USER, true, &m);
index e0c8db50ae866a78eb7658571055a6d588a97a4b..435cafd83afc5b13ad3bc8ba451fbdc91eff8af0 100644 (file)
@@ -30,6 +30,7 @@
 #include "string-util.h"
 #include "strv.h"
 #include "test-helper.h"
+#include "tests.h"
 #include "unit.h"
 #include "util.h"
 
@@ -243,7 +244,7 @@ static void test_path_makedirectory_directorymode(Manager *m) {
 }
 
 int main(int argc, char *argv[]) {
-        test_function_t tests[] = {
+        static const test_function_t tests[] = {
                 test_path_exists,
                 test_path_existsglob,
                 test_path_changed,
@@ -253,12 +254,15 @@ int main(int argc, char *argv[]) {
                 test_path_makedirectory_directorymode,
                 NULL,
         };
-        test_function_t *test = NULL;
+
+        _cleanup_(rm_rf_and_freep) char *runtime_dir = NULL;
+        const test_function_t *test = NULL;
         Manager *m = NULL;
 
         log_parse_environment();
         log_open();
 
+        assert_se(runtime_dir = setup_fake_runtime_dir());
         assert_se(set_unit_path(TEST_DIR "/test-path/") >= 0);
 
         for (test = tests; test && *test; test++) {
index 5083cd53c238680df705a33145bb84860878971b..3e9caafc710dd143fea8e50f0a9637c48c32f5b6 100644 (file)
 
 #include "macro.h"
 #include "manager.h"
+#include "rm-rf.h"
 #include "test-helper.h"
+#include "tests.h"
 
 int main(int argc, char *argv[]) {
+        _cleanup_(rm_rf_and_freep) char *runtime_dir = NULL;
         Manager *m = NULL;
         Unit *idle_ok, *idle_bad, *rr_ok, *rr_bad, *rr_sched;
         Service *ser;
@@ -31,6 +34,8 @@ int main(int argc, char *argv[]) {
         FDSet *fdset = NULL;
         int r;
 
+        assert_se(runtime_dir = setup_fake_runtime_dir());
+
         /* prepare the test */
         assert_se(set_unit_path(TEST_DIR) >= 0);
         r = manager_new(UNIT_FILE_USER, true, &m);
index 7f2fee772ba64717d73353c5a12d0eb3175f4303..114ddf8478e6fdf9111f067b1549985e2fb2d1c6 100644 (file)
 #include "install.h"
 #include "load-fragment.h"
 #include "macro.h"
+#include "rm-rf.h"
 #include "specifier.h"
 #include "string-util.h"
 #include "strv.h"
 #include "test-helper.h"
+#include "tests.h"
 #include "user-util.h"
 #include "util.h"
 
@@ -840,11 +842,14 @@ static void test_config_parse_pass_environ(void) {
 }
 
 int main(int argc, char *argv[]) {
+        _cleanup_(rm_rf_and_freep) char *runtime_dir = NULL;
         int r;
 
         log_parse_environment();
         log_open();
 
+        assert_se(runtime_dir = setup_fake_runtime_dir());
+
         r = test_unit_file_get_set();
         test_config_parse_exec();
         test_config_parse_capability_set();