2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2016-04-08 18:54:05 +02:00
|
|
|
|
2017-02-12 23:14:43 +01:00
|
|
|
#include <alloc-util.h>
|
|
|
|
#include <fs-util.h>
|
|
|
|
#include <libgen.h>
|
2016-04-08 18:54:05 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <util.h>
|
|
|
|
|
2018-09-12 08:55:02 +02:00
|
|
|
#include "alloc-util.h"
|
2018-09-12 09:45:17 +02:00
|
|
|
#include "env-util.h"
|
2018-09-12 08:55:02 +02:00
|
|
|
#include "fileio.h"
|
2018-09-13 13:34:12 +02:00
|
|
|
#include "log.h"
|
2017-02-15 01:43:51 +01:00
|
|
|
#include "path-util.h"
|
2018-09-12 08:55:02 +02:00
|
|
|
#include "strv.h"
|
|
|
|
#include "tests.h"
|
2016-04-08 18:54:05 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2017-02-12 23:14:43 +01:00
|
|
|
|
2018-09-12 08:55:02 +02:00
|
|
|
static void load_testdata_env(void) {
|
|
|
|
static bool called = false;
|
2018-09-11 02:17:22 +02:00
|
|
|
_cleanup_free_ char *s = NULL;
|
2018-09-12 08:55:02 +02:00
|
|
|
_cleanup_free_ char *envpath = NULL;
|
|
|
|
_cleanup_strv_free_ char **pairs = NULL;
|
|
|
|
char **k, **v;
|
2018-09-11 02:17:22 +02:00
|
|
|
|
2018-09-12 08:55:02 +02:00
|
|
|
if (called)
|
|
|
|
return;
|
|
|
|
called = true;
|
2018-09-11 02:17:22 +02:00
|
|
|
|
|
|
|
assert_se(readlink_and_make_absolute("/proc/self/exe", &s) >= 0);
|
2018-09-12 08:55:02 +02:00
|
|
|
dirname(s);
|
2018-09-11 02:17:22 +02:00
|
|
|
|
2018-09-12 08:55:02 +02:00
|
|
|
envpath = path_join(NULL, s, "systemd-runtest.env");
|
|
|
|
if (load_env_file_pairs(NULL, envpath, NULL, &pairs) < 0)
|
|
|
|
return;
|
2018-09-11 02:17:22 +02:00
|
|
|
|
2018-09-12 08:55:02 +02:00
|
|
|
STRV_FOREACH_PAIR(k, v, pairs)
|
|
|
|
setenv(*k, *v, 0);
|
|
|
|
}
|
|
|
|
|
2018-09-12 08:15:09 +02:00
|
|
|
const char* get_testdata_dir(void) {
|
2017-02-15 08:52:17 +01:00
|
|
|
const char *env;
|
2018-09-12 08:55:02 +02:00
|
|
|
|
|
|
|
load_testdata_env();
|
2017-02-12 23:14:43 +01:00
|
|
|
|
2017-02-15 08:52:17 +01:00
|
|
|
/* if the env var is set, use that */
|
|
|
|
env = getenv("SYSTEMD_TEST_DATA");
|
2018-09-12 08:55:02 +02:00
|
|
|
if (!env)
|
|
|
|
env = SYSTEMD_TEST_DATA;
|
|
|
|
if (access(env, F_OK) < 0) {
|
|
|
|
fprintf(stderr, "ERROR: $SYSTEMD_TEST_DATA directory [%s] does not exist\n", env);
|
|
|
|
exit(EXIT_FAILURE);
|
2017-02-15 08:52:17 +01:00
|
|
|
}
|
|
|
|
|
2018-09-12 08:55:02 +02:00
|
|
|
return env;
|
2017-02-12 23:14:43 +01:00
|
|
|
}
|
2018-09-12 09:23:40 +02:00
|
|
|
|
|
|
|
const char* get_catalog_dir(void) {
|
|
|
|
const char *env;
|
|
|
|
|
|
|
|
load_testdata_env();
|
|
|
|
|
|
|
|
/* if the env var is set, use that */
|
|
|
|
env = getenv("SYSTEMD_CATALOG_DIR");
|
|
|
|
if (!env)
|
|
|
|
env = SYSTEMD_CATALOG_DIR;
|
|
|
|
if (access(env, F_OK) < 0) {
|
|
|
|
fprintf(stderr, "ERROR: $SYSTEMD_CATALOG_DIR directory [%s] does not exist\n", env);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
return env;
|
|
|
|
}
|
2018-09-12 09:45:17 +02:00
|
|
|
|
|
|
|
bool slow_tests_enabled(void) {
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = getenv_bool("SYSTEMD_SLOW_TESTS");
|
|
|
|
if (r >= 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (r != -ENXIO)
|
|
|
|
log_warning_errno(r, "Cannot parse $SYSTEMD_SLOW_TESTS, ignoring.");
|
|
|
|
return SYSTEMD_SLOW_TESTS_DEFAULT;
|
|
|
|
}
|
2018-09-13 13:34:12 +02:00
|
|
|
|
2018-09-13 14:31:13 +02:00
|
|
|
void test_setup_logging(int level) {
|
|
|
|
log_set_max_level(level);
|
|
|
|
log_parse_environment();
|
|
|
|
log_open();
|
|
|
|
}
|
|
|
|
|
2018-09-13 13:34:12 +02:00
|
|
|
int log_tests_skipped(const char *message) {
|
|
|
|
log_notice("%s: %s, skipping tests.",
|
|
|
|
program_invocation_short_name, message);
|
|
|
|
return EXIT_TEST_SKIP;
|
|
|
|
}
|
2018-09-13 16:11:16 +02:00
|
|
|
|
|
|
|
int log_tests_skipped_errno(int r, const char *message) {
|
|
|
|
log_notice_errno(r, "%s: %s, skipping tests: %m",
|
|
|
|
program_invocation_short_name, message);
|
|
|
|
return EXIT_TEST_SKIP;
|
|
|
|
}
|