From 463d0d15690c7abe2172a23ae23c5547693dd71f Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 24 Feb 2016 21:24:23 +0100 Subject: core: remove ManagerRunningAs enum Previously, we had two enums ManagerRunningAs and UnitFileScope, that were mostly identical and converted from one to the other all the time. The latter had one more value UNIT_FILE_GLOBAL however. Let's simplify things, and remove ManagerRunningAs and replace it by UnitFileScope everywhere, thus making the translation unnecessary. Introduce two new macros MANAGER_IS_SYSTEM() and MANAGER_IS_USER() to simplify checking if we are running in one or the user context. --- src/test/test-sched-prio.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/test/test-sched-prio.c') diff --git a/src/test/test-sched-prio.c b/src/test/test-sched-prio.c index 7f515b53d8..5083cd53c2 100644 --- a/src/test/test-sched-prio.c +++ b/src/test/test-sched-prio.c @@ -33,7 +33,7 @@ int main(int argc, char *argv[]) { /* prepare the test */ assert_se(set_unit_path(TEST_DIR) >= 0); - r = manager_new(MANAGER_USER, true, &m); + r = manager_new(UNIT_FILE_USER, true, &m); if (MANAGER_SKIP_TEST(r)) { printf("Skipping test: manager_new: %s\n", strerror(-r)); return EXIT_TEST_SKIP; -- cgit v1.2.3-54-g00ecf From d2120590ffdcc041f098667355ca3db5161d25d9 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 8 Apr 2016 18:54:05 +0200 Subject: tests: override XDG_RUNTIME_DIR where we use the user runtime dir 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 | 4 +++- src/basic/rm-rf.h | 9 +++++++++ src/shared/tests.c | 33 +++++++++++++++++++++++++++++++++ src/shared/tests.h | 22 ++++++++++++++++++++++ src/test/test-cgroup-mask.c | 6 ++++++ src/test/test-engine.c | 5 +++++ src/test/test-path.c | 8 ++++++-- src/test/test-sched-prio.c | 5 +++++ src/test/test-unit-file.c | 5 +++++ 9 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 src/shared/tests.c create mode 100644 src/shared/tests.h (limited to 'src/test/test-sched-prio.c') diff --git a/Makefile.am b/Makefile.am index ea6b0340b5..34eaefd0bf 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 += \ diff --git a/src/basic/rm-rf.h b/src/basic/rm-rf.h index 6d03268919..40b5b527d5 100644 --- a/src/basic/rm-rf.h +++ b/src/basic/rm-rf.h @@ -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 index 0000000000..409116290d --- /dev/null +++ b/src/shared/tests.c @@ -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 . +***/ + +#include +#include + +#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 index 0000000000..93f09013a1 --- /dev/null +++ b/src/shared/tests.h @@ -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 . +***/ + +char* setup_fake_runtime_dir(void); diff --git a/src/test/test-cgroup-mask.c b/src/test/test-cgroup-mask.c index 332755cf41..4eb8fcd773 100644 --- a/src/test/test-cgroup-mask.c +++ b/src/test/test-cgroup-mask.c @@ -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; } diff --git a/src/test/test-engine.c b/src/test/test-engine.c index 52b34c03df..361d1e7b0b 100644 --- a/src/test/test-engine.c +++ b/src/test/test-engine.c @@ -23,9 +23,12 @@ #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); diff --git a/src/test/test-path.c b/src/test/test-path.c index e0c8db50ae..435cafd83a 100644 --- a/src/test/test-path.c +++ b/src/test/test-path.c @@ -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++) { diff --git a/src/test/test-sched-prio.c b/src/test/test-sched-prio.c index 5083cd53c2..3e9caafc71 100644 --- a/src/test/test-sched-prio.c +++ b/src/test/test-sched-prio.c @@ -21,9 +21,12 @@ #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); diff --git a/src/test/test-unit-file.c b/src/test/test-unit-file.c index 7f2fee772b..114ddf8478 100644 --- a/src/test/test-unit-file.c +++ b/src/test/test-unit-file.c @@ -35,10 +35,12 @@ #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(); -- cgit v1.2.3-54-g00ecf From f942504e4f74c6d30d7b73cb602517e055f02152 Mon Sep 17 00:00:00 2001 From: Evgeny Vereshchagin Date: Fri, 20 May 2016 16:08:24 +0300 Subject: basic: remove rm_rf_and_free, add rm_rf_physical_and_free, use rm_rf_physical_and_freep in tests (#3292) Some distros don't mount /tmp as tmpfs. For example: https://lists.ubuntu.com/archives/ubuntu-cloud/2016-January/001009.html Some tests: * print 'Attempted to remove disk file system, and we can't allow that.' * don't really cleanup /tmp --- src/basic/rm-rf.h | 6 +++--- src/test/test-cgroup-mask.c | 2 +- src/test/test-engine.c | 2 +- src/test/test-path.c | 2 +- src/test/test-sched-prio.c | 2 +- src/test/test-unit-file.c | 2 +- 6 files changed, 8 insertions(+), 8 deletions(-) (limited to 'src/test/test-sched-prio.c') diff --git a/src/basic/rm-rf.h b/src/basic/rm-rf.h index 40b5b527d5..f693a5bb7c 100644 --- a/src/basic/rm-rf.h +++ b/src/basic/rm-rf.h @@ -32,10 +32,10 @@ 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) { +static inline void rm_rf_physical_and_free(char *p) { if (!p) return; - (void) rm_rf(p, REMOVE_ROOT); + (void) rm_rf(p, REMOVE_ROOT|REMOVE_PHYSICAL); free(p); } -DEFINE_TRIVIAL_CLEANUP_FUNC(char*, rm_rf_and_free); +DEFINE_TRIVIAL_CLEANUP_FUNC(char*, rm_rf_physical_and_free); diff --git a/src/test/test-cgroup-mask.c b/src/test/test-cgroup-mask.c index 4eb8fcd773..4677f7cbd9 100644 --- a/src/test/test-cgroup-mask.c +++ b/src/test/test-cgroup-mask.c @@ -109,7 +109,7 @@ static int test_cgroup_mask(void) { } int main(int argc, char* argv[]) { - _cleanup_(rm_rf_and_freep) char *runtime_dir = NULL; + _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL; int rc = 0; assert_se(runtime_dir = setup_fake_runtime_dir()); diff --git a/src/test/test-engine.c b/src/test/test-engine.c index 361d1e7b0b..23da10fa1a 100644 --- a/src/test/test-engine.c +++ b/src/test/test-engine.c @@ -28,7 +28,7 @@ #include "tests.h" int main(int argc, char *argv[]) { - _cleanup_(rm_rf_and_freep) char *runtime_dir = NULL; + _cleanup_(rm_rf_physical_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; diff --git a/src/test/test-path.c b/src/test/test-path.c index 435cafd83a..62181e22a0 100644 --- a/src/test/test-path.c +++ b/src/test/test-path.c @@ -255,7 +255,7 @@ int main(int argc, char *argv[]) { NULL, }; - _cleanup_(rm_rf_and_freep) char *runtime_dir = NULL; + _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL; const test_function_t *test = NULL; Manager *m = NULL; diff --git a/src/test/test-sched-prio.c b/src/test/test-sched-prio.c index 3e9caafc71..c068f5c39e 100644 --- a/src/test/test-sched-prio.c +++ b/src/test/test-sched-prio.c @@ -26,7 +26,7 @@ #include "tests.h" int main(int argc, char *argv[]) { - _cleanup_(rm_rf_and_freep) char *runtime_dir = NULL; + _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL; Manager *m = NULL; Unit *idle_ok, *idle_bad, *rr_ok, *rr_bad, *rr_sched; Service *ser; diff --git a/src/test/test-unit-file.c b/src/test/test-unit-file.c index c340673c6c..ade0ff2a63 100644 --- a/src/test/test-unit-file.c +++ b/src/test/test-unit-file.c @@ -842,7 +842,7 @@ static void test_config_parse_pass_environ(void) { } int main(int argc, char *argv[]) { - _cleanup_(rm_rf_and_freep) char *runtime_dir = NULL; + _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL; int r; log_parse_environment(); -- cgit v1.2.3-54-g00ecf