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.
This commit is contained in:
Lennart Poettering 2016-04-08 18:54:05 +02:00
parent 815b09d39b
commit d2120590ff
9 changed files with 94 additions and 3 deletions

View 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 += \

View 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);

33
src/shared/tests.c Normal file
View file

@ -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;
}

22
src/shared/tests.h Normal file
View file

@ -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);

View 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;
}

View file

@ -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);

View 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++) {

View file

@ -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);

View file

@ -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();