From 0cf29baac098787daa39efe621bcf987e1e0570d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Wed, 12 Sep 2018 09:45:17 +0200 Subject: [PATCH 01/14] tests: centralize check for slow tests --- src/journal/test-compress-benchmark.c | 15 ++++++--------- src/shared/tests.c | 13 +++++++++++++ src/shared/tests.h | 1 + src/test/test-hashmap-plain.c | 16 +++++----------- src/test/test-watchdog.c | 6 +++--- 5 files changed, 28 insertions(+), 23 deletions(-) diff --git a/src/journal/test-compress-benchmark.c b/src/journal/test-compress-benchmark.c index 411df3fa7a..e3034dae69 100644 --- a/src/journal/test-compress-benchmark.c +++ b/src/journal/test-compress-benchmark.c @@ -8,6 +8,7 @@ #include "process-util.h" #include "random-util.h" #include "string-util.h" +#include "tests.h" #include "util.h" typedef int (compress_t)(const void *src, uint64_t src_size, void *dst, @@ -143,23 +144,19 @@ static void test_compress_decompress(const char* label, const char* type, int main(int argc, char *argv[]) { #if HAVE_XZ || HAVE_LZ4 const char *i; - int r; log_set_max_level(LOG_INFO); + log_parse_environment(); + log_open(); if (argc >= 2) { unsigned x; assert_se(safe_atou(argv[1], &x) >= 0); arg_duration = x * USEC_PER_SEC; - } else { - bool slow; - - r = getenv_bool("SYSTEMD_SLOW_TESTS"); - slow = r >= 0 ? r : SYSTEMD_SLOW_TESTS_DEFAULT; - - arg_duration = slow ? 2 * USEC_PER_SEC : USEC_PER_SEC / 50; - } + } else + arg_duration = slow_tests_enabled() ? + 2 * USEC_PER_SEC : USEC_PER_SEC / 50; if (argc == 3) (void) safe_atozu(argv[2], &arg_start); diff --git a/src/shared/tests.c b/src/shared/tests.c index 94f4629b1b..cb5b7b6dea 100644 --- a/src/shared/tests.c +++ b/src/shared/tests.c @@ -7,6 +7,7 @@ #include #include "alloc-util.h" +#include "env-util.h" #include "fileio.h" #include "path-util.h" #include "strv.h" @@ -76,3 +77,15 @@ const char* get_catalog_dir(void) { } return env; } + +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; +} diff --git a/src/shared/tests.h b/src/shared/tests.h index 0d5e6a8386..44b52f5589 100644 --- a/src/shared/tests.h +++ b/src/shared/tests.h @@ -4,3 +4,4 @@ char* setup_fake_runtime_dir(void); const char* get_testdata_dir(void); const char* get_catalog_dir(void); +bool slow_tests_enabled(void); diff --git a/src/test/test-hashmap-plain.c b/src/test/test-hashmap-plain.c index f80febce76..82837da4f6 100644 --- a/src/test/test-hashmap-plain.c +++ b/src/test/test-hashmap-plain.c @@ -1,15 +1,13 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #include "alloc-util.h" -#include "env-util.h" #include "hashmap.h" #include "log.h" #include "string-util.h" #include "strv.h" +#include "tests.h" #include "util.h" -static bool arg_slow = false; - void test_hashmap_funcs(void); static void test_hashmap_replace(void) { @@ -739,15 +737,16 @@ static void test_hashmap_many(void) { Hashmap *h; unsigned i, j; void *v, *k; + bool slow = slow_tests_enabled(); const struct { const struct hash_ops *ops; unsigned n_entries; } tests[] = { - { .ops = NULL, .n_entries = arg_slow ? 1 << 20 : 240 }, - { .ops = &crippled_hashmap_ops, .n_entries = arg_slow ? 1 << 14 : 140 }, + { .ops = NULL, .n_entries = slow ? 1 << 20 : 240 }, + { .ops = &crippled_hashmap_ops, .n_entries = slow ? 1 << 14 : 140 }, }; - log_info("%s (%s)", __func__, arg_slow ? "slow" : "fast"); + log_info("%s (%s)", __func__, slow ? "slow" : "fast"); for (j = 0; j < ELEMENTSOF(tests); j++) { assert_se(h = hashmap_new(tests[j].ops)); @@ -886,14 +885,9 @@ static void test_hashmap_reserve(void) { } void test_hashmap_funcs(void) { - int r; - log_parse_environment(); log_open(); - r = getenv_bool("SYSTEMD_SLOW_TESTS"); - arg_slow = r >= 0 ? r : SYSTEMD_SLOW_TESTS_DEFAULT; - test_hashmap_copy(); test_hashmap_get_strv(); test_hashmap_move_one(); diff --git a/src/test/test-watchdog.c b/src/test/test-watchdog.c index 2aba3b5a26..d595ae27d5 100644 --- a/src/test/test-watchdog.c +++ b/src/test/test-watchdog.c @@ -3,8 +3,8 @@ #include #include -#include "env-util.h" #include "log.h" +#include "tests.h" #include "watchdog.h" int main(int argc, char *argv[]) { @@ -15,9 +15,9 @@ int main(int argc, char *argv[]) { log_set_max_level(LOG_DEBUG); log_parse_environment(); + log_open(); - r = getenv_bool("SYSTEMD_SLOW_TESTS"); - slow = r >= 0 ? r : SYSTEMD_SLOW_TESTS_DEFAULT; + slow = slow_tests_enabled(); t = slow ? 10 * USEC_PER_SEC : 1 * USEC_PER_SEC; count = slow ? 5 : 3; From f57d003cb6ccda702d2777aa2bfe3907fd683091 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Wed, 12 Sep 2018 09:55:39 +0200 Subject: [PATCH 02/14] test-barrier: just make this a slow test test-barrier was using a custom mechanism to skip itself. Let's just follow the normal scheme. --- src/test/test-barrier.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/test/test-barrier.c b/src/test/test-barrier.c index d2afd92f63..c59fe03e92 100644 --- a/src/test/test-barrier.c +++ b/src/test/test-barrier.c @@ -16,6 +16,7 @@ #include "barrier.h" #include "util.h" +#include "tests.h" /* 20ms to test deadlocks; All timings use multiples of this constant as * alarm/sleep timers. If this timeout is too small for slow machines to perform @@ -419,19 +420,16 @@ TEST_BARRIER(test_barrier_pending_exit, TEST_BARRIER_WAIT_SUCCESS(pid2)); int main(int argc, char *argv[]) { - /* - * This test uses real-time alarms and sleeps to test for CPU races - * explicitly. This is highly fragile if your system is under load. We - * already increased the BASE_TIME value to make the tests more robust, - * but that just makes the test take significantly longer. Hence, - * disable the test by default, so it will not break CI. - */ - if (argc < 2) - return EXIT_TEST_SKIP; - + log_set_max_level(LOG_INFO); log_parse_environment(); log_open(); + if (!slow_tests_enabled()) { + log_notice("%s: slow tests are disabled, exiting.", + program_invocation_short_name); + return EXIT_TEST_SKIP; + } + test_barrier_sync(); test_barrier_wait_next(); test_barrier_wait_next_twice(); From d3da291eb071ce35f530fda690a300e44b99b53e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Wed, 12 Sep 2018 11:02:58 +0200 Subject: [PATCH 03/14] meson: disable "slow tests" too when tests are generally disabled We would have a strange situation where after setting -Dslow-tests=true -Dtests=false we'd get mostly the slow tests (plus some other ones which I'll fix in subsequent commit). Let's simplify things by making -Dtests=false just disable those tests too. --- meson.build | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/meson.build b/meson.build index a97c5fcffc..28fd4da365 100644 --- a/meson.build +++ b/meson.build @@ -270,7 +270,11 @@ pkgconfig = import('pkgconfig') check_compilation_sh = find_program('tools/meson-check-compilation.sh') meson_build_sh = find_program('tools/meson-build.sh') -if get_option('tests') != 'false' +want_tests = get_option('tests') +slow_tests = want_tests != 'false' and get_option('slow-tests') +install_tests = get_option('install-tests') + +if want_tests != 'false' cxx = find_program('c++', required : false) if cxx.found() # Used only for tests @@ -1291,9 +1295,6 @@ conf.set10('ENABLE_NSS', enable_nss) conf.set10('ENABLE_TIMEDATECTL', get_option('timedated') or get_option('timesyncd')) -want_tests = get_option('tests') -install_tests = get_option('install-tests') -slow_tests = get_option('slow-tests') tests = [] fuzzers = [] From 938be08926aa3c05faaca854776f29b9dab72661 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Wed, 12 Sep 2018 11:08:49 +0200 Subject: [PATCH 04/14] meson: disable _all_ tests when -Dtests=false Back in 08318a2c5acb3d0e4243c36461e69a3e45482441, value "false" was enabled for '-Dtests=', but various tests were not conditionalized properly. So even with -Dtests=false -Dslow-tests=false we'd run 120 tests. Let's make this consistent. --- hwdb/meson.build | 8 ++++--- meson.build | 48 ++++++++++++++++++++++++++-------------- src/boot/efi/meson.build | 8 ++++--- src/systemd/meson.build | 12 +++++----- test/meson.build | 28 ++++++++++++++--------- 5 files changed, 66 insertions(+), 38 deletions(-) diff --git a/hwdb/meson.build b/hwdb/meson.build index 158292c712..31ee3e7409 100644 --- a/hwdb/meson.build +++ b/hwdb/meson.build @@ -36,9 +36,11 @@ endif ############################################################ parse_hwdb_py = find_program('parse_hwdb.py') -test('parse-hwdb', - parse_hwdb_py, - timeout : 90) +if want_tests != 'false' + test('parse-hwdb', + parse_hwdb_py, + timeout : 90) +endif ############################################################ diff --git a/meson.build b/meson.build index 28fd4da365..12ddddd3eb 100644 --- a/meson.build +++ b/meson.build @@ -1504,9 +1504,12 @@ foreach tuple : [['myhostname', 'ENABLE_NSS_MYHOSTNAME'], 'rm $DESTDIR@0@/libnss_@1@.so' .format(rootlibdir, module)) - test('dlopen-nss_' + module, - test_dlopen, - args : [nss.full_path()]) # path to dlopen must include a slash + if want_tests != 'false' + test('dlopen-nss_' + module, + test_dlopen, + # path to dlopen must include a slash + args : nss.full_path()) + endif endif endforeach @@ -1747,9 +1750,12 @@ if conf.get('ENABLE_LOGIND') == 1 install : true, install_dir : pamlibdir) - test('dlopen-pam_systemd', - test_dlopen, - args : [pam_systemd.full_path()]) # path to dlopen must include a slash + if want_tests != 'false' + test('dlopen-pam_systemd', + test_dlopen, + # path to dlopen must include a slash + args : [pam_systemd.full_path()]) + endif endif endif @@ -2436,10 +2442,12 @@ if conf.get('ENABLE_TMPFILES') == 1 install_dir : rootbindir) public_programs += exe - test('test-systemd-tmpfiles', - test_systemd_tmpfiles_py, - args : exe.full_path()) - # https://github.com/mesonbuild/meson/issues/2681 + if want_tests != 'false' + test('test-systemd-tmpfiles', + test_systemd_tmpfiles_py, + # https://github.com/mesonbuild/meson/issues/2681 + args : exe.full_path()) + endif endif if conf.get('ENABLE_HWDB') == 1 @@ -2666,7 +2674,9 @@ exe = executable( link_with : [libsystemd], install : install_tests, install_dir : testsdir) -test('test-libsystemd-sym', exe) +if want_tests != 'false' + test('test-libsystemd-sym', exe) +endif exe = executable( 'test-libsystemd-static-sym', @@ -2678,7 +2688,7 @@ exe = executable( build_by_default : static_libsystemd_pic, install : install_tests and static_libsystemd_pic, install_dir : testsdir) -if static_libsystemd_pic +if want_tests != 'false' and static_libsystemd_pic test('test-libsystemd-static-sym', exe) endif @@ -2690,7 +2700,9 @@ exe = executable( link_with : [libudev], install : install_tests, install_dir : testsdir) -test('test-libudev-sym', exe) +if want_tests != 'false' + test('test-libudev-sym', exe) +endif exe = executable( 'test-libudev-static-sym', @@ -2701,7 +2713,7 @@ exe = executable( build_by_default : static_libudev_pic, install : install_tests and static_libudev_pic, install_dir : testsdir) -if static_libudev_pic +if want_tests != 'false' and static_libudev_pic test('test-libudev-static-sym', exe) endif @@ -2791,9 +2803,11 @@ meson_check_help = find_program('tools/meson-check-help.sh') foreach exec : public_programs name = exec.full_path().split('/')[-1] - test('check-help-' + name, - meson_check_help, - args : [exec.full_path()]) + if want_tests != 'false' + test('check-help-' + name, + meson_check_help, + args : [exec.full_path()]) + endif endforeach ############################################################ diff --git a/src/boot/efi/meson.build b/src/boot/efi/meson.build index 1cec5505a0..c5509e73d1 100644 --- a/src/boot/efi/meson.build +++ b/src/boot/efi/meson.build @@ -169,9 +169,11 @@ if have_gnu_efi efi_ldflags + tuple[2] + ['-lefi', '-lgnuefi', libgcc_file_name]) - test('no-undefined-symbols-' + tuple[0], - no_undefined_symbols, - args : [so]) + if want_tests != 'false' + test('no-undefined-symbols-' + tuple[0], + no_undefined_symbols, + args : [so]) + endif stub = custom_target( tuple[1], diff --git a/src/systemd/meson.build b/src/systemd/meson.build index 5f84439a58..212f99cf78 100644 --- a/src/systemd/meson.build +++ b/src/systemd/meson.build @@ -65,10 +65,12 @@ endif foreach header : _systemd_headers foreach opt : opts name = ''.join([header, ':'] + opt) - test('cc-' + name, - check_compilation_sh, - args : cc.cmd_array() + ['-c', '-x'] + opt + - ['-Werror', '-include', - join_paths(meson.current_source_dir(), header)]) + if want_tests != 'false' + test('cc-' + name, + check_compilation_sh, + args : cc.cmd_array() + ['-c', '-x'] + opt + + ['-Werror', '-include', + join_paths(meson.current_source_dir(), header)]) + endif endforeach endforeach diff --git a/test/meson.build b/test/meson.build index 616ffb9728..8bec8dc07c 100644 --- a/test/meson.build +++ b/test/meson.build @@ -211,16 +211,20 @@ endif ############################################################ rule_syntax_check_py = find_program('rule-syntax-check.py') -test('rule-syntax-check', - rule_syntax_check_py, - args : all_rules) +if want_tests != 'false' + test('rule-syntax-check', + rule_syntax_check_py, + args : all_rules) +endif ############################################################ if conf.get('HAVE_SYSV_COMPAT') == 1 sysv_generator_test_py = find_program('sysv-generator-test.py') - test('sysv-generator-test', - sysv_generator_test_py) + if want_tests != 'false' + test('sysv-generator-test', + sysv_generator_test_py) + endif endif ############################################################ @@ -235,17 +239,21 @@ custom_target( if perl.found() udev_test_pl = find_program('udev-test.pl') - test('udev-test', - udev_test_pl) + if want_tests != 'false' + test('udev-test', + udev_test_pl) + endif else message('Skipping udev-test because perl is not available') endif if conf.get('ENABLE_HWDB') == 1 hwdb_test_sh = find_program('hwdb-test.sh') - test('hwdb-test', - hwdb_test_sh, - timeout : 90) + if want_tests != 'false' + test('hwdb-test', + hwdb_test_sh, + timeout : 90) + endif endif subdir('fuzz-regressions') From 08d541ca06c75c8539206c60eba975fb3ecd6460 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Wed, 12 Sep 2018 13:25:45 +0200 Subject: [PATCH 05/14] test: when skipping tests, always print something It is quite confusing if the test "fails" without printing anything at all. A typo in an 'if' statement is also fixed. --- src/libsystemd-network/test-dhcp-server.c | 9 +++++---- src/test/test-boot-timestamps.c | 7 ++++++- src/test/test-capability.c | 17 ++++++++++------- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/libsystemd-network/test-dhcp-server.c b/src/libsystemd-network/test-dhcp-server.c index 815b11e997..3de25b02c2 100644 --- a/src/libsystemd-network/test-dhcp-server.c +++ b/src/libsystemd-network/test-dhcp-server.c @@ -54,9 +54,8 @@ static int test_basic(sd_event *event) { test_pool(&address_lo, 1, 0); r = sd_dhcp_server_start(server); - if (r == -EPERM) - return EXIT_TEST_SKIP; + return log_info_errno(r, "sd_dhcp_server_start failed: %m"); assert_se(r >= 0); assert_se(sd_dhcp_server_start(server) == -EBUSY); @@ -236,8 +235,10 @@ int main(int argc, char *argv[]) { assert_se(sd_event_new(&e) >= 0); r = test_basic(e); - if (r != 0) - return r; + if (r != 0) { + log_notice("%s: skipping tests.", program_invocation_short_name); + return EXIT_TEST_SKIP; + } test_message_handler(); test_client_id_hash(); diff --git a/src/test/test-boot-timestamps.c b/src/test/test-boot-timestamps.c index ef39304b9f..e7deee5d5f 100644 --- a/src/test/test-boot-timestamps.c +++ b/src/test/test-boot-timestamps.c @@ -91,5 +91,10 @@ int main(int argc, char* argv[]) { r = test_boot_timestamps(); assert(r >= 0); - return (p > 0 || q > 0 || r >> 0) ? EXIT_SUCCESS : EXIT_TEST_SKIP; + bool any = p > 0 || q > 0 || r > 0; + if (!any) + log_notice("%s: access to firmware variable not possible, skipping tests.", + program_invocation_short_name); + + return any ? EXIT_SUCCESS : EXIT_TEST_SKIP; } diff --git a/src/test/test-capability.c b/src/test/test-capability.c index af6d808b6d..5e2d21bee9 100644 --- a/src/test/test-capability.c +++ b/src/test/test-capability.c @@ -91,10 +91,9 @@ static int setup_tests(bool *run_ambient) { int r; nobody = getpwnam(NOBODY_USER_NAME); - if (!nobody) { - log_error_errno(errno, "Could not find nobody user: %m"); - return -EXIT_TEST_SKIP; - } + if (!nobody) + return log_error_errno(errno, "Could not find nobody user: %m"); + test_uid = nobody->pw_uid; test_gid = nobody->pw_gid; @@ -229,12 +228,16 @@ int main(int argc, char *argv[]) { log_info("have ambient caps: %s", yes_no(ambient_capabilities_supported())); - if (getuid() != 0) + if (getuid() != 0) { + log_notice("%s: not root, skipping tests.", program_invocation_short_name); return EXIT_TEST_SKIP; + } r = setup_tests(&run_ambient); - if (r < 0) - return -r; + if (r < 0) { + log_notice("%s: skipping tests.", program_invocation_short_name); + return EXIT_TEST_SKIP; + } show_capabilities(); From 964bc0ad600f6d9fb90ed82f6a302988e843336d Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 12 Sep 2018 21:52:31 +0900 Subject: [PATCH 06/14] test: log when skipping tests in more cases Follow-up for the previous commit. --- src/journal/test-compress-benchmark.c | 1 + src/journal/test-compress.c | 1 + src/journal/test-journal-interleaving.c | 4 +++- src/journal/test-journal-stream.c | 4 +++- src/journal/test-journal-verify.c | 4 +++- src/journal/test-journal.c | 4 +++- src/libsystemd/sd-bus/test-bus-gvariant.c | 4 +++- src/libsystemd/sd-bus/test-bus-marshal.c | 4 +++- src/libsystemd/sd-bus/test-bus-match.c | 4 +++- src/network/test-network.c | 4 +++- src/test/test-architecture.c | 4 +++- src/test/test-bpf.c | 5 +++-- src/test/test-ipcrm.c | 3 ++- src/test/test-netlink-manual.c | 18 ++++++++++++------ src/test/test-sigbus.c | 5 ++++- 15 files changed, 50 insertions(+), 19 deletions(-) diff --git a/src/journal/test-compress-benchmark.c b/src/journal/test-compress-benchmark.c index e3034dae69..8701fe7954 100644 --- a/src/journal/test-compress-benchmark.c +++ b/src/journal/test-compress-benchmark.c @@ -173,6 +173,7 @@ int main(int argc, char *argv[]) { } return 0; #else + log_info("No compression feature is enabled, skipping tests."); return EXIT_TEST_SKIP; #endif } diff --git a/src/journal/test-compress.c b/src/journal/test-compress.c index 791c6fdffb..d59a613b39 100644 --- a/src/journal/test-compress.c +++ b/src/journal/test-compress.c @@ -305,6 +305,7 @@ int main(int argc, char *argv[]) { return 0; #else + log_info("/* XZ and LZ4 tests skipped */"); return EXIT_TEST_SKIP; #endif } diff --git a/src/journal/test-journal-interleaving.c b/src/journal/test-journal-interleaving.c index 1f0c9f8f2a..a227dec646 100644 --- a/src/journal/test-journal-interleaving.c +++ b/src/journal/test-journal-interleaving.c @@ -277,8 +277,10 @@ int main(int argc, char *argv[]) { log_set_max_level(LOG_DEBUG); /* journal_file_open requires a valid machine id */ - if (access("/etc/machine-id", F_OK) != 0) + if (access("/etc/machine-id", F_OK) != 0) { + log_info("/etc/machine-id not found, skipping tests."); return EXIT_TEST_SKIP; + } arg_keep = argc > 1; diff --git a/src/journal/test-journal-stream.c b/src/journal/test-journal-stream.c index ae35c91eff..9fe3208100 100644 --- a/src/journal/test-journal-stream.c +++ b/src/journal/test-journal-stream.c @@ -67,8 +67,10 @@ int main(int argc, char *argv[]) { dual_timestamp previous_ts = DUAL_TIMESTAMP_NULL; /* journal_file_open requires a valid machine id */ - if (access("/etc/machine-id", F_OK) != 0) + if (access("/etc/machine-id", F_OK) != 0) { + log_info("/etc/machine-id not found, skipping tests."); return EXIT_TEST_SKIP; + } log_set_max_level(LOG_DEBUG); diff --git a/src/journal/test-journal-verify.c b/src/journal/test-journal-verify.c index c1c3a82c42..ba962cd42e 100644 --- a/src/journal/test-journal-verify.c +++ b/src/journal/test-journal-verify.c @@ -61,8 +61,10 @@ int main(int argc, char *argv[]) { uint64_t p; /* journal_file_open requires a valid machine id */ - if (access("/etc/machine-id", F_OK) != 0) + if (access("/etc/machine-id", F_OK) != 0) { + log_info("/etc/machine-id not found, skipping tests."); return EXIT_TEST_SKIP; + } log_set_max_level(LOG_DEBUG); diff --git a/src/journal/test-journal.c b/src/journal/test-journal.c index 69bdff6760..3cdaad4b40 100644 --- a/src/journal/test-journal.c +++ b/src/journal/test-journal.c @@ -239,8 +239,10 @@ int main(int argc, char *argv[]) { arg_keep = argc > 1; /* journal_file_open requires a valid machine id */ - if (access("/etc/machine-id", F_OK) != 0) + if (access("/etc/machine-id", F_OK) != 0) { + log_info("/etc/machine-id not found, skipping tests."); return EXIT_TEST_SKIP; + } test_non_empty(); test_empty(); diff --git a/src/libsystemd/sd-bus/test-bus-gvariant.c b/src/libsystemd/sd-bus/test-bus-gvariant.c index 33f4d1ee0b..ae3e71e365 100644 --- a/src/libsystemd/sd-bus/test-bus-gvariant.c +++ b/src/libsystemd/sd-bus/test-bus-gvariant.c @@ -121,8 +121,10 @@ static void test_marshal(void) { int r; r = sd_bus_open_user(&bus); - if (r < 0) + if (r < 0) { + log_info("Failed to connect to bus, skipping tests."); exit(EXIT_TEST_SKIP); + } bus->message_version = 2; /* dirty hack to enable gvariant */ diff --git a/src/libsystemd/sd-bus/test-bus-marshal.c b/src/libsystemd/sd-bus/test-bus-marshal.c index cc1b61ce58..47a24bd8ef 100644 --- a/src/libsystemd/sd-bus/test-bus-marshal.c +++ b/src/libsystemd/sd-bus/test-bus-marshal.c @@ -121,8 +121,10 @@ int main(int argc, char *argv[]) { uint64_t u64; r = sd_bus_default_user(&bus); - if (r < 0) + if (r < 0) { + log_info("Failed to connect to bus, skipping tests."); return EXIT_TEST_SKIP; + } r = sd_bus_message_new_method_call(bus, &m, "foobar.waldo", "/", "foobar.waldo", "Piep"); assert_se(r >= 0); diff --git a/src/libsystemd/sd-bus/test-bus-match.c b/src/libsystemd/sd-bus/test-bus-match.c index 2f1d057eb9..05b84d51c3 100644 --- a/src/libsystemd/sd-bus/test-bus-match.c +++ b/src/libsystemd/sd-bus/test-bus-match.c @@ -78,8 +78,10 @@ int main(int argc, char *argv[]) { int r; r = sd_bus_open_user(&bus); - if (r < 0) + if (r < 0) { + log_info("Failed to connect to bus, skipping tests."); return EXIT_TEST_SKIP; + } assert_se(match_add(slots, &root, "arg2='wal\\'do',sender='foo',type='signal',interface='bar.x',", 1) >= 0); assert_se(match_add(slots, &root, "arg2='wal\\'do2',sender='foo',type='signal',interface='bar.x',", 2) >= 0); diff --git a/src/network/test-network.c b/src/network/test-network.c index 31112a8a96..a04257d3fd 100644 --- a/src/network/test-network.c +++ b/src/network/test-network.c @@ -231,8 +231,10 @@ int main(void) { assert_se(manager_new(&manager) >= 0); r = test_load_config(manager); - if (r == -EPERM) + if (r == -EPERM) { + log_info_errno(r, "Skipping tests: %m"); return EXIT_TEST_SKIP; + } assert_se(sd_device_new_from_syspath(&loopback, "/sys/class/net/lo") >= 0); assert_se(loopback); diff --git a/src/test/test-architecture.c b/src/test/test-architecture.c index 6bc0a28a42..81b1e276fc 100644 --- a/src/test/test-architecture.c +++ b/src/test/test-architecture.c @@ -17,8 +17,10 @@ int main(int argc, char *argv[]) { assert_se(architecture_from_string(architecture_to_string(1)) == 1); v = detect_virtualization(); - if (IN_SET(v, -EPERM, -EACCES)) + if (IN_SET(v, -EPERM, -EACCES)) { + log_info_errno(v, "Skipping tests: %m"); return EXIT_TEST_SKIP; + } assert_se(v >= 0); diff --git a/src/test/test-bpf.c b/src/test/test-bpf.c index 6f4a22a1cc..a4975bf13a 100644 --- a/src/test/test-bpf.c +++ b/src/test/test-bpf.c @@ -110,9 +110,10 @@ int main(int argc, char *argv[]) { unit_dump(u, stdout, NULL); r = bpf_firewall_compile(u); - if (IN_SET(r, -ENOTTY, -ENOSYS, -EPERM )) - /* Kernel doesn't support the necessary bpf bits, or masked out via seccomp? */ + if (IN_SET(r, -ENOTTY, -ENOSYS, -EPERM)) { + log_info_errno(r, "Kernel doesn't support the necessary bpf bits, or masked out via seccomp? Skipping tests: %m"); return EXIT_TEST_SKIP; + } assert_se(r >= 0); assert(u->ip_bpf_ingress); diff --git a/src/test/test-ipcrm.c b/src/test/test-ipcrm.c index 6cdf48a490..a57173144b 100644 --- a/src/test/test-ipcrm.c +++ b/src/test/test-ipcrm.c @@ -12,7 +12,8 @@ int main(int argc, char *argv[]) { r = get_user_creds(&name, &uid, NULL, NULL, NULL, 0); if (r < 0) { log_full_errno(r == -ESRCH ? LOG_NOTICE : LOG_ERR, - r, "Failed to resolve \"%s\": %m", name); + r, "Failed to resolve \"%s\"%s: %m", name, + r == -ESRCH ? ", skipping tests" : ""); return r == -ESRCH ? EXIT_TEST_SKIP : EXIT_FAILURE; } diff --git a/src/test/test-netlink-manual.c b/src/test/test-netlink-manual.c index eed610b27a..e887a9a780 100644 --- a/src/test/test-netlink-manual.c +++ b/src/test/test-netlink-manual.c @@ -46,11 +46,21 @@ static int test_tunnel_configure(sd_netlink *rtnl) { /* skip test if module cannot be loaded */ r = load_module("ipip"); - if (r < 0) + if (r < 0) { + log_info_errno(r, "Skipping tests: failed to load module 'ipip': %m"); return EXIT_TEST_SKIP; + } - if (getuid() != 0) + r = load_module("sit"); + if (r < 0) { + log_info_errno(r, "Skipping tests: failed to load module 'sit': %m"); return EXIT_TEST_SKIP; + } + + if (getuid() != 0) { + log_info("Skipping tests: not root"); + return EXIT_TEST_SKIP; + } /* IPIP tunnel */ assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0) >= 0); @@ -76,10 +86,6 @@ static int test_tunnel_configure(sd_netlink *rtnl) { assert_se((m = sd_netlink_message_unref(m)) == NULL); - r = load_module("sit"); - if (r < 0) - return EXIT_TEST_SKIP; - /* sit */ assert_se(sd_rtnl_message_new_link(rtnl, &n, RTM_NEWLINK, 0) >= 0); assert_se(n); diff --git a/src/test/test-sigbus.c b/src/test/test-sigbus.c index c9343364d4..999dbe6be4 100644 --- a/src/test/test-sigbus.c +++ b/src/test/test-sigbus.c @@ -16,11 +16,14 @@ int main(int argc, char *argv[]) { uint8_t *p; #if HAVE_VALGRIND_VALGRIND_H - if (RUNNING_ON_VALGRIND) + if (RUNNING_ON_VALGRIND) { + puts("This test cannot run on valgrind, skipping tests."); return EXIT_TEST_SKIP; + } #endif #ifdef __SANITIZE_ADDRESS__ + puts("Address sanitization is enabled, skipping tests."); return EXIT_TEST_SKIP; #endif sigbus_install(); From fd1939fbe73c59aaa911363cfa4f7be748fb1d2a Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 12 Sep 2018 21:47:56 +0900 Subject: [PATCH 07/14] meson: do not build tests by default when '-Dtests=false' [zj: it is still possible to build tests explicitly by calling ninja -C build test-name. This way we have full flexibility.] --- meson.build | 9 ++++++--- src/test/meson.build | 9 ++++++--- test/meson.build | 2 +- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/meson.build b/meson.build index 12ddddd3eb..694c5cfcdd 100644 --- a/meson.build +++ b/meson.build @@ -1466,7 +1466,8 @@ test_dlopen = executable( test_dlopen_c, include_directories : includes, link_with : [libbasic], - dependencies : [libdl]) + dependencies : [libdl], + build_by_default : want_tests != 'false') foreach tuple : [['myhostname', 'ENABLE_NSS_MYHOSTNAME'], ['systemd', 'ENABLE_NSS_SYSTEMD'], @@ -2672,6 +2673,7 @@ exe = executable( test_libsystemd_sym_c, include_directories : includes, link_with : [libsystemd], + build_by_default : want_tests != 'false', install : install_tests, install_dir : testsdir) if want_tests != 'false' @@ -2685,7 +2687,7 @@ exe = executable( link_with : [install_libsystemd_static], dependencies : [threads], # threads is already included in dependencies on the library, # but does not seem to get propagated. Add here as a work-around. - build_by_default : static_libsystemd_pic, + build_by_default : want_tests != 'false' and static_libsystemd_pic, install : install_tests and static_libsystemd_pic, install_dir : testsdir) if want_tests != 'false' and static_libsystemd_pic @@ -2698,6 +2700,7 @@ exe = executable( include_directories : includes, c_args : ['-Wno-deprecated-declarations'], link_with : [libudev], + build_by_default : want_tests != 'false', install : install_tests, install_dir : testsdir) if want_tests != 'false' @@ -2710,7 +2713,7 @@ exe = executable( include_directories : includes, c_args : ['-Wno-deprecated-declarations'], link_with : [install_libudev_static], - build_by_default : static_libudev_pic, + build_by_default : want_tests != 'false' and static_libudev_pic, install : install_tests and static_libudev_pic, install_dir : testsdir) if want_tests != 'false' and static_libudev_pic diff --git a/src/test/meson.build b/src/test/meson.build index e82c993ab4..86d7b16d45 100644 --- a/src/test/meson.build +++ b/src/test/meson.build @@ -6,7 +6,8 @@ test_hashmap_ordered_c = custom_target( input : [awkscript, 'test-hashmap-plain.c'], output : 'test-hashmap-ordered.c', command : [awk, '-f', '@INPUT0@', '@INPUT1@'], - capture : true) + capture : true, + build_by_default : want_tests != 'false') test_include_dir = include_directories('.') @@ -26,14 +27,16 @@ test_libsystemd_sym_c = custom_target( input : [libsystemd_sym_path] + systemd_headers, output : 'test-libsystemd-sym.c', command : [generate_sym_test_py, libsystemd_sym_path] + systemd_headers, - capture : true) + capture : true, + build_by_default : want_tests != 'false') test_libudev_sym_c = custom_target( 'test-libudev-sym.c', input : [libudev_sym_path, libudev_h_path], output : 'test-libudev-sym.c', command : [generate_sym_test_py, '@INPUT0@', '@INPUT1@'], - capture : true) + capture : true, + build_by_default : want_tests != 'false') test_dlopen_c = files('test-dlopen.c') diff --git a/test/meson.build b/test/meson.build index 8bec8dc07c..9750ff22b9 100644 --- a/test/meson.build +++ b/test/meson.build @@ -235,7 +235,7 @@ custom_target( 'sys', command : [sys_script_py, meson.current_build_dir()], output : 'sys', - build_by_default : true) + build_by_default : want_tests != 'false') if perl.found() udev_test_pl = find_program('udev-test.pl') From 15c5594bec63dbd5c71064439c9ecba46ebad705 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Wed, 12 Sep 2018 16:11:15 +0200 Subject: [PATCH 08/14] NEWS: remove repeated "slightly" --- NEWS | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/NEWS b/NEWS index 242f55af29..fca0ed3762 100644 --- a/NEWS +++ b/NEWS @@ -594,10 +594,9 @@ CHANGES WITH 237: different from what the documentation said, and not particularly useful, as repeated systemd-tmpfiles invocations would not be idempotent and grow such files without bounds. With this release - behaviour has been altered slightly, to match what the documentation - says: lines of this type only have an effect if the indicated files - don't exist yet, and only then the argument string is written to the - file. + behaviour has been altered to match what the documentation says: + lines of this type only have an effect if the indicated files don't + exist yet, and only then the argument string is written to the file. * FUTURE INCOMPATIBILITY: In systemd v238 we intend to slightly change systemd-tmpfiles behaviour: previously, read-only files owned by root From 3b2bdd625a176fd8b75a8bef8f6269322b73bca6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Wed, 12 Sep 2018 16:52:08 +0200 Subject: [PATCH 09/14] meson: always allow compilation of tests binaries --- meson.build | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/meson.build b/meson.build index 694c5cfcdd..e8550ddea8 100644 --- a/meson.build +++ b/meson.build @@ -274,12 +274,10 @@ want_tests = get_option('tests') slow_tests = want_tests != 'false' and get_option('slow-tests') install_tests = get_option('install-tests') -if want_tests != 'false' - cxx = find_program('c++', required : false) - if cxx.found() - # Used only for tests - add_languages('cpp') - endif +cxx = find_program('c++', required : false) +if cxx.found() + # Used only for tests + add_languages('cpp') endif want_ossfuzz = get_option('oss-fuzz') @@ -2640,9 +2638,8 @@ foreach tuple : tests timeout = type.split('=')[1].to_int() type = '' endif - if want_tests == 'false' - message('Not compiling @0@ because tests is set to false'.format(name)) - elif condition == '' or conf.get(condition) == 1 + + if condition == '' or conf.get(condition) == 1 exe = executable( name, sources, @@ -2650,6 +2647,7 @@ foreach tuple : tests link_with : link_with, dependencies : dependencies, c_args : defs, + build_by_default : want_tests != 'false', install_rpath : rootlibexecdir, install : install_tests, install_dir : join_paths(testsdir, type)) @@ -2658,7 +2656,7 @@ foreach tuple : tests message('@0@ is a manual test'.format(name)) elif type == 'unsafe' and want_tests != 'unsafe' message('@0@ is an unsafe test'.format(name)) - else + elif want_tests != 'false' test(name, exe, env : test_env, timeout : timeout) From c1cd674357f921136b1e74ece641288670de33c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Wed, 12 Sep 2018 16:57:06 +0200 Subject: [PATCH 10/14] meson: drop some unneeded parens --- meson.build | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/meson.build b/meson.build index e8550ddea8..a503e3bf00 100644 --- a/meson.build +++ b/meson.build @@ -1753,7 +1753,7 @@ if conf.get('ENABLE_LOGIND') == 1 test('dlopen-pam_systemd', test_dlopen, # path to dlopen must include a slash - args : [pam_systemd.full_path()]) + args : pam_systemd.full_path()) endif endif endif @@ -2484,7 +2484,7 @@ public_programs += exe exe = executable('systemd-udevd', systemd_udevd_sources, include_directories : includes, - c_args : ['-DLOG_REALM=LOG_REALM_UDEV'], + c_args : '-DLOG_REALM=LOG_REALM_UDEV', link_with : [libudev_core, libsystemd_network, libudev_static], @@ -2500,7 +2500,7 @@ public_programs += exe exe = executable('udevadm', udevadm_sources, - c_args : ['-DLOG_REALM=LOG_REALM_UDEV'], + c_args : '-DLOG_REALM=LOG_REALM_UDEV', include_directories : includes, link_with : [libudev_core, libsystemd_network, @@ -2696,7 +2696,7 @@ exe = executable( 'test-libudev-sym', test_libudev_sym_c, include_directories : includes, - c_args : ['-Wno-deprecated-declarations'], + c_args : '-Wno-deprecated-declarations', link_with : [libudev], build_by_default : want_tests != 'false', install : install_tests, @@ -2709,7 +2709,7 @@ exe = executable( 'test-libudev-static-sym', test_libudev_sym_c, include_directories : includes, - c_args : ['-Wno-deprecated-declarations'], + c_args : '-Wno-deprecated-declarations', link_with : [install_libudev_static], build_by_default : want_tests != 'false' and static_libudev_pic, install : install_tests and static_libudev_pic, @@ -2807,7 +2807,7 @@ foreach exec : public_programs if want_tests != 'false' test('check-help-' + name, meson_check_help, - args : [exec.full_path()]) + args : exec.full_path()) endif endforeach From 317bb217d3f21312e58d9efdc6d3739f7895a08c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 13 Sep 2018 13:34:12 +0200 Subject: [PATCH 11/14] tests: add helper to unify skipping a test and exiting --- src/journal/test-compress-benchmark.c | 3 +-- src/journal/test-journal-interleaving.c | 10 ++++------ src/journal/test-journal-stream.c | 7 +++---- src/journal/test-journal-verify.c | 7 +++---- src/journal/test-journal.c | 7 +++---- src/libsystemd-network/test-dhcp-server.c | 7 +++---- src/libsystemd/sd-bus/test-bus-chat.c | 7 +++---- src/libsystemd/sd-bus/test-bus-cleanup.c | 11 ++++------- src/libsystemd/sd-bus/test-bus-creds.c | 7 +++---- src/libsystemd/sd-bus/test-bus-marshal.c | 7 +++---- src/libsystemd/sd-bus/test-bus-match.c | 7 +++---- src/libsystemd/sd-bus/test-bus-track.c | 7 +++---- src/network/test-network.c | 8 ++++---- src/shared/tests.c | 7 +++++++ src/shared/tests.h | 1 + src/test/test-architecture.c | 7 +++---- src/test/test-barrier.c | 7 ++----- src/test/test-boot-timestamps.c | 9 ++++----- src/test/test-bpf.c | 24 ++++++++--------------- src/test/test-capability.c | 15 +++++--------- src/test/test-cgroup-mask.c | 15 +++++++------- src/test/test-engine.c | 6 ++---- src/test/test-execute.c | 12 ++++-------- src/test/test-hash.c | 7 +++---- src/test/test-ipcrm.c | 9 +++++---- src/test/test-namespace.c | 14 ++++++------- src/test/test-netlink-manual.c | 7 +++---- src/test/test-path.c | 12 +++++------- src/test/test-sched-prio.c | 6 ++---- src/test/test-sigbus.c | 21 ++++++++++---------- src/test/test-sleep.c | 7 +++---- src/test/test-unit-file.c | 6 ++---- src/test/test-unit-name.c | 6 ++---- src/test/test-watch-pid.c | 13 ++++-------- 34 files changed, 128 insertions(+), 178 deletions(-) diff --git a/src/journal/test-compress-benchmark.c b/src/journal/test-compress-benchmark.c index 8701fe7954..0633fe6ea4 100644 --- a/src/journal/test-compress-benchmark.c +++ b/src/journal/test-compress-benchmark.c @@ -173,7 +173,6 @@ int main(int argc, char *argv[]) { } return 0; #else - log_info("No compression feature is enabled, skipping tests."); - return EXIT_TEST_SKIP; + return log_tests_skipped("No compression feature is enabled"); #endif } diff --git a/src/journal/test-journal-interleaving.c b/src/journal/test-journal-interleaving.c index a227dec646..82da68e0ea 100644 --- a/src/journal/test-journal-interleaving.c +++ b/src/journal/test-journal-interleaving.c @@ -15,9 +15,9 @@ #include "parse-util.h" #include "rm-rf.h" #include "util.h" +#include "tests.h" -/* This program tests skipping around in a multi-file journal. - */ +/* This program tests skipping around in a multi-file journal. */ static bool arg_keep = false; @@ -277,10 +277,8 @@ int main(int argc, char *argv[]) { log_set_max_level(LOG_DEBUG); /* journal_file_open requires a valid machine id */ - if (access("/etc/machine-id", F_OK) != 0) { - log_info("/etc/machine-id not found, skipping tests."); - return EXIT_TEST_SKIP; - } + if (access("/etc/machine-id", F_OK) != 0) + return log_tests_skipped("/etc/machine-id not found"); arg_keep = argc > 1; diff --git a/src/journal/test-journal-stream.c b/src/journal/test-journal-stream.c index 9fe3208100..d7fdf907c8 100644 --- a/src/journal/test-journal-stream.c +++ b/src/journal/test-journal-stream.c @@ -12,6 +12,7 @@ #include "macro.h" #include "parse-util.h" #include "rm-rf.h" +#include "tests.h" #include "util.h" #define N_ENTRIES 200 @@ -67,10 +68,8 @@ int main(int argc, char *argv[]) { dual_timestamp previous_ts = DUAL_TIMESTAMP_NULL; /* journal_file_open requires a valid machine id */ - if (access("/etc/machine-id", F_OK) != 0) { - log_info("/etc/machine-id not found, skipping tests."); - return EXIT_TEST_SKIP; - } + if (access("/etc/machine-id", F_OK) != 0) + return log_tests_skipped("/etc/machine-id not found"); log_set_max_level(LOG_DEBUG); diff --git a/src/journal/test-journal-verify.c b/src/journal/test-journal-verify.c index ba962cd42e..c2a21cef9c 100644 --- a/src/journal/test-journal-verify.c +++ b/src/journal/test-journal-verify.c @@ -10,6 +10,7 @@ #include "log.h" #include "rm-rf.h" #include "terminal-util.h" +#include "tests.h" #include "util.h" #define N_ENTRIES 6000 @@ -61,10 +62,8 @@ int main(int argc, char *argv[]) { uint64_t p; /* journal_file_open requires a valid machine id */ - if (access("/etc/machine-id", F_OK) != 0) { - log_info("/etc/machine-id not found, skipping tests."); - return EXIT_TEST_SKIP; - } + if (access("/etc/machine-id", F_OK) != 0) + return log_tests_skipped("/etc/machine-id not found"); log_set_max_level(LOG_DEBUG); diff --git a/src/journal/test-journal.c b/src/journal/test-journal.c index 3cdaad4b40..7e188420db 100644 --- a/src/journal/test-journal.c +++ b/src/journal/test-journal.c @@ -8,6 +8,7 @@ #include "journal-vacuum.h" #include "log.h" #include "rm-rf.h" +#include "tests.h" static bool arg_keep = false; @@ -239,10 +240,8 @@ int main(int argc, char *argv[]) { arg_keep = argc > 1; /* journal_file_open requires a valid machine id */ - if (access("/etc/machine-id", F_OK) != 0) { - log_info("/etc/machine-id not found, skipping tests."); - return EXIT_TEST_SKIP; - } + if (access("/etc/machine-id", F_OK) != 0) + return log_tests_skipped("/etc/machine-id not found"); test_non_empty(); test_empty(); diff --git a/src/libsystemd-network/test-dhcp-server.c b/src/libsystemd-network/test-dhcp-server.c index 3de25b02c2..2854e04abc 100644 --- a/src/libsystemd-network/test-dhcp-server.c +++ b/src/libsystemd-network/test-dhcp-server.c @@ -9,6 +9,7 @@ #include "sd-event.h" #include "dhcp-server-internal.h" +#include "tests.h" static void test_pool(struct in_addr *address, unsigned size, int ret) { _cleanup_(sd_dhcp_server_unrefp) sd_dhcp_server *server = NULL; @@ -235,10 +236,8 @@ int main(int argc, char *argv[]) { assert_se(sd_event_new(&e) >= 0); r = test_basic(e); - if (r != 0) { - log_notice("%s: skipping tests.", program_invocation_short_name); - return EXIT_TEST_SKIP; - } + if (r != 0) + return log_tests_skipped("cannot start dhcp server"); test_message_handler(); test_client_id_hash(); diff --git a/src/libsystemd/sd-bus/test-bus-chat.c b/src/libsystemd/sd-bus/test-bus-chat.c index f3ff856e42..28922e6a69 100644 --- a/src/libsystemd/sd-bus/test-bus-chat.c +++ b/src/libsystemd/sd-bus/test-bus-chat.c @@ -16,6 +16,7 @@ #include "format-util.h" #include "log.h" #include "macro.h" +#include "tests.h" #include "util.h" static int match_callback(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { @@ -510,10 +511,8 @@ int main(int argc, char *argv[]) { int q, r; r = server_init(&bus); - if (r < 0) { - log_info("Failed to connect to bus, skipping tests."); - return EXIT_TEST_SKIP; - } + if (r < 0) + return log_tests_skipped("Failed to connect to bus"); log_info("Initialized..."); diff --git a/src/libsystemd/sd-bus/test-bus-cleanup.c b/src/libsystemd/sd-bus/test-bus-cleanup.c index d1d962ebb2..a70f4823db 100644 --- a/src/libsystemd/sd-bus/test-bus-cleanup.c +++ b/src/libsystemd/sd-bus/test-bus-cleanup.c @@ -8,6 +8,7 @@ #include "bus-message.h" #include "bus-util.h" #include "refcnt.h" +#include "tests.h" static void test_bus_new(void) { _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL; @@ -59,17 +60,13 @@ static void test_bus_new_signal(void) { } int main(int argc, char **argv) { - int r; - log_parse_environment(); log_open(); test_bus_new(); - r = test_bus_open(); - if (r < 0) { - log_info("Failed to connect to bus, skipping tests."); - return EXIT_TEST_SKIP; - } + + if (test_bus_open() < 0) + return log_tests_skipped("Failed to connect to bus"); test_bus_new_method_call(); test_bus_new_signal(); diff --git a/src/libsystemd/sd-bus/test-bus-creds.c b/src/libsystemd/sd-bus/test-bus-creds.c index 69f1f3e345..a99fbc3bc5 100644 --- a/src/libsystemd/sd-bus/test-bus-creds.c +++ b/src/libsystemd/sd-bus/test-bus-creds.c @@ -5,6 +5,7 @@ #include "bus-dump.h" #include "bus-util.h" #include "cgroup-util.h" +#include "tests.h" int main(int argc, char *argv[]) { _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; @@ -14,10 +15,8 @@ int main(int argc, char *argv[]) { log_parse_environment(); log_open(); - if (cg_unified_flush() == -ENOMEDIUM) { - log_info("Skipping test: /sys/fs/cgroup/ not available"); - return EXIT_TEST_SKIP; - } + if (cg_unified_flush() == -ENOMEDIUM) + return log_tests_skipped("/sys/fs/cgroup/ not available"); r = sd_bus_creds_new_from_pid(&creds, 0, _SD_BUS_CREDS_ALL); log_full_errno(r < 0 ? LOG_ERR : LOG_DEBUG, r, "sd_bus_creds_new_from_pid: %m"); diff --git a/src/libsystemd/sd-bus/test-bus-marshal.c b/src/libsystemd/sd-bus/test-bus-marshal.c index 47a24bd8ef..22c48e38dc 100644 --- a/src/libsystemd/sd-bus/test-bus-marshal.c +++ b/src/libsystemd/sd-bus/test-bus-marshal.c @@ -21,6 +21,7 @@ #include "fd-util.h" #include "hexdecoct.h" #include "log.h" +#include "tests.h" #include "util.h" static void test_bus_path_encode_unique(void) { @@ -121,10 +122,8 @@ int main(int argc, char *argv[]) { uint64_t u64; r = sd_bus_default_user(&bus); - if (r < 0) { - log_info("Failed to connect to bus, skipping tests."); - return EXIT_TEST_SKIP; - } + if (r < 0) + return log_tests_skipped("Failed to connect to bus"); r = sd_bus_message_new_method_call(bus, &m, "foobar.waldo", "/", "foobar.waldo", "Piep"); assert_se(r >= 0); diff --git a/src/libsystemd/sd-bus/test-bus-match.c b/src/libsystemd/sd-bus/test-bus-match.c index 05b84d51c3..527911c5af 100644 --- a/src/libsystemd/sd-bus/test-bus-match.c +++ b/src/libsystemd/sd-bus/test-bus-match.c @@ -6,6 +6,7 @@ #include "bus-util.h" #include "log.h" #include "macro.h" +#include "tests.h" static bool mask[32]; @@ -78,10 +79,8 @@ int main(int argc, char *argv[]) { int r; r = sd_bus_open_user(&bus); - if (r < 0) { - log_info("Failed to connect to bus, skipping tests."); - return EXIT_TEST_SKIP; - } + if (r < 0) + return log_tests_skipped("Failed to connect to bus"); assert_se(match_add(slots, &root, "arg2='wal\\'do',sender='foo',type='signal',interface='bar.x',", 1) >= 0); assert_se(match_add(slots, &root, "arg2='wal\\'do2',sender='foo',type='signal',interface='bar.x',", 2) >= 0); diff --git a/src/libsystemd/sd-bus/test-bus-track.c b/src/libsystemd/sd-bus/test-bus-track.c index b75703f14f..8a3dcf1654 100644 --- a/src/libsystemd/sd-bus/test-bus-track.c +++ b/src/libsystemd/sd-bus/test-bus-track.c @@ -6,6 +6,7 @@ #include "sd-bus.h" #include "macro.h" +#include "tests.h" static bool track_cb_called_x = false; static bool track_cb_called_y = false; @@ -51,10 +52,8 @@ int main(int argc, char *argv[]) { assert_se(r >= 0); r = sd_bus_open_user(&a); - if (IN_SET(r, -ECONNREFUSED, -ENOENT)) { - log_info("Failed to connect to bus, skipping tests."); - return EXIT_TEST_SKIP; - } + if (IN_SET(r, -ECONNREFUSED, -ENOENT)) + return log_tests_skipped("Failed to connect to bus"); assert_se(r >= 0); r = sd_bus_attach_event(a, event, SD_EVENT_PRIORITY_NORMAL); diff --git a/src/network/test-network.c b/src/network/test-network.c index a04257d3fd..e8c10bc15c 100644 --- a/src/network/test-network.c +++ b/src/network/test-network.c @@ -10,6 +10,7 @@ #include "network-internal.h" #include "networkd-manager.h" #include "string-util.h" +#include "tests.h" static void test_deserialize_in_addr(void) { _cleanup_free_ struct in_addr *addresses = NULL; @@ -231,10 +232,9 @@ int main(void) { assert_se(manager_new(&manager) >= 0); r = test_load_config(manager); - if (r == -EPERM) { - log_info_errno(r, "Skipping tests: %m"); - return EXIT_TEST_SKIP; - } + if (r == -EPERM) + return log_tests_skipped("Cannot load configuration"); + assert_se(r == 0); assert_se(sd_device_new_from_syspath(&loopback, "/sys/class/net/lo") >= 0); assert_se(loopback); diff --git a/src/shared/tests.c b/src/shared/tests.c index cb5b7b6dea..884f34bc72 100644 --- a/src/shared/tests.c +++ b/src/shared/tests.c @@ -9,6 +9,7 @@ #include "alloc-util.h" #include "env-util.h" #include "fileio.h" +#include "log.h" #include "path-util.h" #include "strv.h" #include "tests.h" @@ -89,3 +90,9 @@ bool slow_tests_enabled(void) { log_warning_errno(r, "Cannot parse $SYSTEMD_SLOW_TESTS, ignoring."); return SYSTEMD_SLOW_TESTS_DEFAULT; } + +int log_tests_skipped(const char *message) { + log_notice("%s: %s, skipping tests.", + program_invocation_short_name, message); + return EXIT_TEST_SKIP; +} diff --git a/src/shared/tests.h b/src/shared/tests.h index 44b52f5589..67d5e1ce4a 100644 --- a/src/shared/tests.h +++ b/src/shared/tests.h @@ -5,3 +5,4 @@ char* setup_fake_runtime_dir(void); const char* get_testdata_dir(void); const char* get_catalog_dir(void); bool slow_tests_enabled(void); +int log_tests_skipped(const char *message); diff --git a/src/test/test-architecture.c b/src/test/test-architecture.c index 81b1e276fc..6cd64b60b2 100644 --- a/src/test/test-architecture.c +++ b/src/test/test-architecture.c @@ -2,6 +2,7 @@ #include "architecture.h" #include "log.h" +#include "tests.h" #include "util.h" #include "virt.h" @@ -17,10 +18,8 @@ int main(int argc, char *argv[]) { assert_se(architecture_from_string(architecture_to_string(1)) == 1); v = detect_virtualization(); - if (IN_SET(v, -EPERM, -EACCES)) { - log_info_errno(v, "Skipping tests: %m"); - return EXIT_TEST_SKIP; - } + if (IN_SET(v, -EPERM, -EACCES)) + return log_tests_skipped("Cannot detect virtualization"); assert_se(v >= 0); diff --git a/src/test/test-barrier.c b/src/test/test-barrier.c index c59fe03e92..2a735695a1 100644 --- a/src/test/test-barrier.c +++ b/src/test/test-barrier.c @@ -424,11 +424,8 @@ int main(int argc, char *argv[]) { log_parse_environment(); log_open(); - if (!slow_tests_enabled()) { - log_notice("%s: slow tests are disabled, exiting.", - program_invocation_short_name); - return EXIT_TEST_SKIP; - } + if (!slow_tests_enabled()) + return log_tests_skipped("slow tests are disabled"); test_barrier_sync(); test_barrier_wait_next(); diff --git a/src/test/test-boot-timestamps.c b/src/test/test-boot-timestamps.c index e7deee5d5f..578aa5b304 100644 --- a/src/test/test-boot-timestamps.c +++ b/src/test/test-boot-timestamps.c @@ -4,6 +4,7 @@ #include "boot-timestamps.h" #include "efivars.h" #include "log.h" +#include "tests.h" #include "util.h" static int test_acpi_fpdt(void) { @@ -91,10 +92,8 @@ int main(int argc, char* argv[]) { r = test_boot_timestamps(); assert(r >= 0); - bool any = p > 0 || q > 0 || r > 0; - if (!any) - log_notice("%s: access to firmware variable not possible, skipping tests.", - program_invocation_short_name); + if (p == 0 && q == 0 && r == 0) + return log_tests_skipped("access to firmware variables not possible"); - return any ? EXIT_SUCCESS : EXIT_TEST_SKIP; + return EXIT_SUCCESS; } diff --git a/src/test/test-bpf.c b/src/test/test-bpf.c index a4975bf13a..a055fea802 100644 --- a/src/test/test-bpf.c +++ b/src/test/test-bpf.c @@ -33,10 +33,8 @@ int main(int argc, char *argv[]) { log_open(); r = enter_cgroup_subroot(); - if (r == -ENOMEDIUM) { - log_notice("cgroupfs not available, skipping tests"); - return EXIT_TEST_SKIP; - } + if (r == -ENOMEDIUM) + return log_tests_skipped("cgroupfs not available"); assert_se(set_unit_path(get_testdata_dir()) >= 0); assert_se(runtime_dir = setup_fake_runtime_dir()); @@ -47,16 +45,12 @@ int main(int argc, char *argv[]) { r = bpf_program_add_instructions(p, exit_insn, ELEMENTSOF(exit_insn)); assert(r == 0); - if (getuid() != 0) { - log_notice("Not running as root, skipping kernel related tests."); - return EXIT_TEST_SKIP; - } + if (getuid() != 0) + return log_tests_skipped("not running as root"); r = bpf_firewall_supported(); - if (r == BPF_FIREWALL_UNSUPPORTED) { - log_notice("BPF firewalling not supported, skipping"); - return EXIT_TEST_SKIP; - } + if (r == BPF_FIREWALL_UNSUPPORTED) + return log_tests_skipped("BPF firewalling not supported"); assert_se(r > 0); if (r == BPF_FIREWALL_SUPPORTED_WITH_MULTI) @@ -110,10 +104,8 @@ int main(int argc, char *argv[]) { unit_dump(u, stdout, NULL); r = bpf_firewall_compile(u); - if (IN_SET(r, -ENOTTY, -ENOSYS, -EPERM)) { - log_info_errno(r, "Kernel doesn't support the necessary bpf bits, or masked out via seccomp? Skipping tests: %m"); - return EXIT_TEST_SKIP; - } + if (IN_SET(r, -ENOTTY, -ENOSYS, -EPERM)) + return log_tests_skipped("Kernel doesn't support the necessary bpf bits (masked out via seccomp?)"); assert_se(r >= 0); assert(u->ip_bpf_ingress); diff --git a/src/test/test-capability.c b/src/test/test-capability.c index 5e2d21bee9..79ddca9554 100644 --- a/src/test/test-capability.c +++ b/src/test/test-capability.c @@ -14,6 +14,7 @@ #include "fileio.h" #include "macro.h" #include "parse-util.h" +#include "tests.h" #include "util.h" static uid_t test_uid = -1; @@ -217,7 +218,6 @@ static void test_set_ambient_caps(void) { } int main(int argc, char *argv[]) { - int r; bool run_ambient; test_last_cap_file(); @@ -228,16 +228,11 @@ int main(int argc, char *argv[]) { log_info("have ambient caps: %s", yes_no(ambient_capabilities_supported())); - if (getuid() != 0) { - log_notice("%s: not root, skipping tests.", program_invocation_short_name); - return EXIT_TEST_SKIP; - } + if (getuid() != 0) + return log_tests_skipped("not running as root"); - r = setup_tests(&run_ambient); - if (r < 0) { - log_notice("%s: skipping tests.", program_invocation_short_name); - return EXIT_TEST_SKIP; - } + if (setup_tests(&run_ambient) < 0) + return log_tests_skipped("setup failed"); show_capabilities(); diff --git a/src/test/test-cgroup-mask.c b/src/test/test-cgroup-mask.c index 8dc1639720..0dd673e3e5 100644 --- a/src/test/test-cgroup-mask.c +++ b/src/test/test-cgroup-mask.c @@ -17,19 +17,18 @@ static int test_cgroup_mask(void) { int r; r = enter_cgroup_subroot(); - if (r == -ENOMEDIUM) { - puts("Skipping test: cgroupfs not available"); - return EXIT_TEST_SKIP; - } + if (r == -ENOMEDIUM) + return log_tests_skipped("cgroupfs not available"); /* Prepare the manager. */ assert_se(set_unit_path(get_testdata_dir()) >= 0); assert_se(runtime_dir = setup_fake_runtime_dir()); r = manager_new(UNIT_FILE_USER, MANAGER_TEST_RUN_BASIC, &m); if (IN_SET(r, -EPERM, -EACCES)) { - puts("manager_new: Permission denied. Skipping test."); - return EXIT_TEST_SKIP; + log_error_errno(r, "manager_new: %m"); + return log_tests_skipped("cannot create manager"); } + assert_se(r >= 0); /* Turn off all kinds of default accouning, so that we can @@ -117,13 +116,13 @@ static void test_cg_mask_to_string(void) { } int main(int argc, char* argv[]) { - int rc = 0; + int rc = EXIT_SUCCESS; log_parse_environment(); log_open(); - TEST_REQ_RUNNING_SYSTEMD(rc = test_cgroup_mask()); test_cg_mask_to_string(); + 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 0f3e244dc1..f7435bd4fb 100644 --- a/src/test/test-engine.c +++ b/src/test/test-engine.c @@ -23,10 +23,8 @@ int main(int argc, char *argv[]) { log_open(); r = enter_cgroup_subroot(); - if (r == -ENOMEDIUM) { - log_notice_errno(r, "Skipping test: cgroupfs not available"); - return EXIT_TEST_SKIP; - } + if (r == -ENOMEDIUM) + return log_tests_skipped("cgroupfs not available"); /* prepare the test */ assert_se(set_unit_path(get_testdata_dir()) >= 0); diff --git a/src/test/test-execute.c b/src/test/test-execute.c index b37e601753..0d2f35734e 100644 --- a/src/test/test-execute.c +++ b/src/test/test-execute.c @@ -733,16 +733,12 @@ int main(int argc, char *argv[]) { (void) unsetenv("SHELL"); /* It is needed otherwise cgroup creation fails */ - if (getuid() != 0) { - puts("Skipping test: not root"); - return EXIT_TEST_SKIP; - } + if (getuid() != 0) + return log_tests_skipped("not root"); r = enter_cgroup_subroot(); - if (r == -ENOMEDIUM) { - puts("Skipping test: cgroupfs not available"); - return EXIT_TEST_SKIP; - } + if (r == -ENOMEDIUM) + return log_tests_skipped("cgroupfs not available"); assert_se(runtime_dir = setup_fake_runtime_dir()); test_execute_path = path_join(NULL, get_testdata_dir(), "test-execute"); diff --git a/src/test/test-hash.c b/src/test/test-hash.c index f5bc131846..ea56b74d0f 100644 --- a/src/test/test-hash.c +++ b/src/test/test-hash.c @@ -7,6 +7,7 @@ #include "log.h" #include "string-util.h" #include "khash.h" +#include "tests.h" int main(int argc, char *argv[]) { _cleanup_(khash_unrefp) khash *h = NULL, *copy = NULL; @@ -20,10 +21,8 @@ int main(int argc, char *argv[]) { r = khash_supported(); assert_se(r >= 0); - if (r == 0) { - puts("khash not supported on this kernel, skipping"); - return EXIT_TEST_SKIP; - } + if (r == 0) + return log_tests_skipped("khash not supported on this kernel"); assert_se(khash_new(&h, "foobar") == -EOPNOTSUPP); /* undefined hash function */ diff --git a/src/test/test-ipcrm.c b/src/test/test-ipcrm.c index a57173144b..1f6d7b4351 100644 --- a/src/test/test-ipcrm.c +++ b/src/test/test-ipcrm.c @@ -2,6 +2,7 @@ #include "clean-ipc.h" #include "user-util.h" +#include "tests.h" #include "util.h" int main(int argc, char *argv[]) { @@ -10,11 +11,11 @@ int main(int argc, char *argv[]) { const char* name = argv[1] ?: NOBODY_USER_NAME; r = get_user_creds(&name, &uid, NULL, NULL, NULL, 0); + if (r == -ESRCH) + return log_tests_skipped("Failed to resolve user"); if (r < 0) { - log_full_errno(r == -ESRCH ? LOG_NOTICE : LOG_ERR, - r, "Failed to resolve \"%s\"%s: %m", name, - r == -ESRCH ? ", skipping tests" : ""); - return r == -ESRCH ? EXIT_TEST_SKIP : EXIT_FAILURE; + log_error_errno(r, "Failed to resolve \"%s\": %m", name); + return EXIT_FAILURE; } r = clean_ipc_by_uid(uid); diff --git a/src/test/test-namespace.c b/src/test/test-namespace.c index b202739719..318bd5bad4 100644 --- a/src/test/test-namespace.c +++ b/src/test/test-namespace.c @@ -7,6 +7,7 @@ #include "namespace.h" #include "process-util.h" #include "string-util.h" +#include "tests.h" #include "util.h" static void test_tmpdir(const char *id, const char *A, const char *B) { @@ -46,16 +47,14 @@ static void test_tmpdir(const char *id, const char *A, const char *B) { assert_se(rmdir(b) >= 0); } -static void test_netns(void) { +static int test_netns(void) { _cleanup_close_pair_ int s[2] = { -1, -1 }; pid_t pid1, pid2, pid3; int r, n = 0; siginfo_t si; - if (geteuid() > 0) { - log_info("Skipping test: not root"); - exit(EXIT_TEST_SKIP); - } + if (geteuid() > 0) + return log_tests_skipped("not root"); assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, s) >= 0); @@ -102,6 +101,7 @@ static void test_netns(void) { n += si.si_status; assert_se(n == 1); + return EXIT_SUCCESS; } int main(int argc, char *argv[]) { @@ -128,7 +128,5 @@ int main(int argc, char *argv[]) { test_tmpdir("sys-devices-pci0000:00-0000:00:1a.0-usb3-3\\x2d1-3\\x2d1:1.0-bluetooth-hci0.device", z, zz); - test_netns(); - - return 0; + return test_netns(); } diff --git a/src/test/test-netlink-manual.c b/src/test/test-netlink-manual.c index e887a9a780..39826dfdb9 100644 --- a/src/test/test-netlink-manual.c +++ b/src/test/test-netlink-manual.c @@ -10,6 +10,7 @@ #include "macro.h" #include "module-util.h" +#include "tests.h" #include "util.h" static int load_module(const char *mod_name) { @@ -57,10 +58,8 @@ static int test_tunnel_configure(sd_netlink *rtnl) { return EXIT_TEST_SKIP; } - if (getuid() != 0) { - log_info("Skipping tests: not root"); - return EXIT_TEST_SKIP; - } + if (getuid() != 0) + return log_tests_skipped("not root"); /* IPIP tunnel */ assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0) >= 0); diff --git a/src/test/test-path.c b/src/test/test-path.c index 5e78a560ff..7e664ec849 100644 --- a/src/test/test-path.c +++ b/src/test/test-path.c @@ -33,15 +33,13 @@ static int setup_test(Manager **m) { assert_se(m); r = enter_cgroup_subroot(); - if (r == -ENOMEDIUM) { - log_notice_errno(r, "Skipping test: cgroupfs not available"); - return -EXIT_TEST_SKIP; - } + if (r == -ENOMEDIUM) + return log_tests_skipped("cgroupfs not available"); r = manager_new(UNIT_FILE_USER, MANAGER_TEST_RUN_BASIC, &tmp); if (MANAGER_SKIP_TEST(r)) { log_notice_errno(r, "Skipping test: manager_new: %m"); - return -EXIT_TEST_SKIP; + return EXIT_TEST_SKIP; } assert_se(r >= 0); assert_se(manager_startup(tmp, NULL, NULL) >= 0); @@ -266,8 +264,8 @@ int main(int argc, char *argv[]) { /* We create a clean environment for each test */ r = setup_test(&m); - if (r < 0) - return -r; + if (r != 0) + return r; (*test)(m); diff --git a/src/test/test-sched-prio.c b/src/test/test-sched-prio.c index 60012e47d2..c594336325 100644 --- a/src/test/test-sched-prio.c +++ b/src/test/test-sched-prio.c @@ -20,10 +20,8 @@ int main(int argc, char *argv[]) { int r; r = enter_cgroup_subroot(); - if (r == -ENOMEDIUM) { - log_notice_errno(r, "Skipping test: cgroupfs not available"); - return EXIT_TEST_SKIP; - } + if (r == -ENOMEDIUM) + return log_tests_skipped("cgroupfs not available"); /* prepare the test */ assert_se(set_unit_path(get_testdata_dir()) >= 0); diff --git a/src/test/test-sigbus.c b/src/test/test-sigbus.c index 999dbe6be4..518e48e1ec 100644 --- a/src/test/test-sigbus.c +++ b/src/test/test-sigbus.c @@ -2,12 +2,14 @@ #include +#if HAVE_VALGRIND_VALGRIND_H +# include +#endif + #include "fd-util.h" #include "sigbus.h" +#include "tests.h" #include "util.h" -#if HAVE_VALGRIND_VALGRIND_H -#include -#endif int main(int argc, char *argv[]) { _cleanup_close_ int fd = -1; @@ -15,17 +17,14 @@ int main(int argc, char *argv[]) { void *addr = NULL; uint8_t *p; +#ifdef __SANITIZE_ADDRESS__ + return log_tests_skipped("address-sanitizer is enabled"); +#endif #if HAVE_VALGRIND_VALGRIND_H - if (RUNNING_ON_VALGRIND) { - puts("This test cannot run on valgrind, skipping tests."); - return EXIT_TEST_SKIP; - } + if (RUNNING_ON_VALGRIND) + return log_tests_skipped("This test cannot run on valgrind"); #endif -#ifdef __SANITIZE_ADDRESS__ - puts("Address sanitization is enabled, skipping tests."); - return EXIT_TEST_SKIP; -#endif sigbus_install(); assert_se(sigbus_pop(&addr) == 0); diff --git a/src/test/test-sleep.c b/src/test/test-sleep.c index a8ad302f71..0198d34f19 100644 --- a/src/test/test-sleep.c +++ b/src/test/test-sleep.c @@ -8,6 +8,7 @@ #include "log.h" #include "sleep-config.h" #include "strv.h" +#include "tests.h" #include "util.h" static void test_parse_sleep_config(void) { @@ -26,10 +27,8 @@ static int test_fiemap(const char *path) { if (fd < 0) return log_error_errno(errno, "failed to open %s: %m", path); r = read_fiemap(fd, &fiemap); - if (r == -EOPNOTSUPP) { - log_info("Skipping test, not supported"); - exit(EXIT_TEST_SKIP); - } + if (r == -EOPNOTSUPP) + exit(log_tests_skipped("Not supported")); if (r < 0) return log_error_errno(r, "Unable to read extent map for '%s': %m", path); log_info("extent map information for %s:", path); diff --git a/src/test/test-unit-file.c b/src/test/test-unit-file.c index 09b0179fa1..0b84d4c4ed 100644 --- a/src/test/test-unit-file.c +++ b/src/test/test-unit-file.c @@ -900,10 +900,8 @@ int main(int argc, char *argv[]) { log_open(); r = enter_cgroup_subroot(); - if (r == -ENOMEDIUM) { - log_notice_errno(r, "Skipping test: cgroupfs not available"); - return EXIT_TEST_SKIP; - } + if (r == -ENOMEDIUM) + return log_tests_skipped("cgroupfs not available"); assert_se(runtime_dir = setup_fake_runtime_dir()); diff --git a/src/test/test-unit-name.c b/src/test/test-unit-name.c index 2b00ef8cb7..7fd3f82d4d 100644 --- a/src/test/test-unit-name.c +++ b/src/test/test-unit-name.c @@ -815,10 +815,8 @@ int main(int argc, char* argv[]) { log_open(); r = enter_cgroup_subroot(); - if (r == -ENOMEDIUM) { - log_notice_errno(r, "Skipping test: cgroupfs not available"); - return EXIT_TEST_SKIP; - } + if (r == -ENOMEDIUM) + return log_tests_skipped("cgroupfs not available"); assert_se(runtime_dir = setup_fake_runtime_dir()); diff --git a/src/test/test-watch-pid.c b/src/test/test-watch-pid.c index 4e349ab927..615ded186b 100644 --- a/src/test/test-watch-pid.c +++ b/src/test/test-watch-pid.c @@ -17,16 +17,11 @@ int main(int argc, char *argv[]) { log_parse_environment(); log_open(); - if (getuid() != 0) { - log_notice("Not running as root, skipping kernel related tests."); - return EXIT_TEST_SKIP; - } - + if (getuid() != 0) + return log_tests_skipped("not root"); r = enter_cgroup_subroot(); - if (r == -ENOMEDIUM) { - log_notice("cgroupfs not available, skipping tests"); - return EXIT_TEST_SKIP; - } + if (r == -ENOMEDIUM) + return log_tests_skipped("cgroupfs not available"); assert_se(set_unit_path(get_testdata_dir()) >= 0); assert_se(runtime_dir = setup_fake_runtime_dir()); From 8b81c382c390d69e48d653c357b4d820bfc149a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 13 Sep 2018 13:35:03 +0200 Subject: [PATCH 12/14] test-condition: make function return void We don't look at the result anyway. --- src/test/test-condition.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/test/test-condition.c b/src/test/test-condition.c index 7ce6ee80ea..678fc426fe 100644 --- a/src/test/test-condition.c +++ b/src/test/test-condition.c @@ -113,7 +113,7 @@ static void test_condition_test_path(void) { condition_free(condition); } -static int test_condition_test_control_group_controller(void) { +static void test_condition_test_control_group_controller(void) { Condition *condition; CGroupMask system_mask; CGroupController controller; @@ -123,7 +123,7 @@ static int test_condition_test_control_group_controller(void) { r = cg_unified_flush(); if (r < 0) { log_notice_errno(r, "Skipping ConditionControlGroupController tests: %m"); - return EXIT_TEST_SKIP; + return; } /* Invalid controllers are ignored */ @@ -180,8 +180,6 @@ static int test_condition_test_control_group_controller(void) { assert_se(condition); assert_se(!condition_test(condition)); condition_free(condition); - - return EXIT_SUCCESS; } static void test_condition_test_ac_power(void) { From 6d7c403324091b0ac0797dbd26b9fe61d4aea9a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 13 Sep 2018 14:31:13 +0200 Subject: [PATCH 13/14] tests: use a helper function to parse environment and open logging The advantages are that we save a few lines, and that we can override logging using environment variables in more test executables. --- src/fuzz/fuzz-main.c | 5 ++--- src/journal/test-catalog.c | 4 +--- src/journal/test-compress-benchmark.c | 7 ++----- src/journal/test-compress.c | 3 ++- src/journal/test-journal-enum.c | 3 ++- src/journal/test-journal-init.c | 3 ++- src/journal/test-journal-interleaving.c | 2 +- src/journal/test-journal-match.c | 3 ++- src/journal/test-journal-stream.c | 2 +- src/journal/test-journal-verify.c | 2 +- src/journal/test-journal.c | 8 +++++--- src/libsystemd-network/test-acd.c | 5 ++--- src/libsystemd-network/test-dhcp-client.c | 5 ++--- src/libsystemd-network/test-dhcp-server.c | 4 +--- src/libsystemd-network/test-dhcp6-client.c | 5 ++--- src/libsystemd-network/test-ipv4ll-manual.c | 5 ++--- src/libsystemd-network/test-ipv4ll.c | 5 ++--- src/libsystemd-network/test-ndisc-ra.c | 5 ++--- src/libsystemd-network/test-ndisc-rs.c | 5 ++--- src/libsystemd/sd-bus/test-bus-chat.c | 2 ++ src/libsystemd/sd-bus/test-bus-cleanup.c | 3 +-- src/libsystemd/sd-bus/test-bus-creds.c | 4 +--- src/libsystemd/sd-bus/test-bus-gvariant.c | 2 ++ src/libsystemd/sd-bus/test-bus-marshal.c | 2 ++ src/libsystemd/sd-bus/test-bus-match.c | 2 ++ src/libsystemd/sd-bus/test-bus-track.c | 2 ++ src/libsystemd/sd-event/test-event.c | 5 ++--- src/libsystemd/sd-netlink/test-local-addresses.c | 5 ++--- src/network/test-network.c | 2 ++ src/network/test-routing-policy-rule.c | 5 ++--- src/nspawn/test-patch-uid.c | 5 ++--- src/shared/tests.c | 6 ++++++ src/shared/tests.h | 1 + src/test/test-architecture.c | 2 ++ src/test/test-barrier.c | 4 +--- src/test/test-boot-timestamps.c | 3 +-- src/test/test-bpf.c | 4 +--- src/test/test-bus-util.c | 5 ++--- src/test/test-capability.c | 5 ++--- src/test/test-cgroup-mask.c | 3 +-- src/test/test-cgroup-util.c | 5 ++--- src/test/test-condition.c | 5 ++--- src/test/test-conf-files.c | 5 ++--- src/test/test-copy.c | 3 ++- src/test/test-date.c | 5 ++--- src/test/test-dissect-image.c | 3 ++- src/test/test-dns-domain.c | 5 ++--- src/test/test-engine.c | 4 +--- src/test/test-escape.c | 5 ++--- src/test/test-exec-util.c | 5 ++--- src/test/test-execute.c | 4 +--- src/test/test-fd-util.c | 3 ++- src/test/test-fileio.c | 5 ++--- src/test/test-firewall-util.c | 3 ++- src/test/test-hash.c | 2 +- src/test/test-install-root.c | 3 ++- src/test/test-install.c | 4 ++-- src/test/test-ipcrm.c | 2 ++ src/test/test-journal-importer.c | 3 +-- src/test/test-loopback.c | 5 ++--- src/test/test-mount-util.c | 3 ++- src/test/test-namespace.c | 3 +-- src/test/test-netlink-manual.c | 2 ++ src/test/test-ns.c | 3 ++- src/test/test-os-util.c | 5 ++--- src/test/test-path-lookup.c | 5 ++--- src/test/test-path-util.c | 5 ++--- src/test/test-path.c | 3 +-- src/test/test-process-util.c | 5 ++--- src/test/test-random-util.c | 5 ++--- src/test/test-sched-prio.c | 2 ++ src/test/test-seccomp.c | 3 ++- src/test/test-selinux.c | 4 ++-- src/test/test-sigbus.c | 2 ++ src/test/test-sleep.c | 3 +-- src/test/test-socket-util.c | 3 ++- src/test/test-specifier.c | 3 ++- src/test/test-tmpfiles.c | 4 ++-- src/test/test-umount.c | 4 +--- src/test/test-unit-file.c | 3 +-- src/test/test-unit-name.c | 3 +-- src/test/test-watch-pid.c | 4 +--- src/test/test-watchdog.c | 4 +--- src/test/test-xattr-util.c | 5 ++--- src/timesync/test-timesync.c | 4 ++-- 85 files changed, 153 insertions(+), 169 deletions(-) diff --git a/src/fuzz/fuzz-main.c b/src/fuzz/fuzz-main.c index d549dc95ff..caf7a27ef1 100644 --- a/src/fuzz/fuzz-main.c +++ b/src/fuzz/fuzz-main.c @@ -4,6 +4,7 @@ #include "log.h" #include "fileio.h" #include "fuzz.h" +#include "tests.h" /* This is a test driver for the systemd fuzzers that provides main function * for regression testing outside of oss-fuzz (https://github.com/google/oss-fuzz) @@ -16,9 +17,7 @@ int main(int argc, char **argv) { size_t size; char *name; - log_set_max_level(LOG_DEBUG); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); for (i = 1; i < argc; i++) { _cleanup_free_ char *buf = NULL; diff --git a/src/journal/test-catalog.c b/src/journal/test-catalog.c index d9ee557b9c..368ac749d0 100644 --- a/src/journal/test-catalog.c +++ b/src/journal/test-catalog.c @@ -206,9 +206,7 @@ int main(int argc, char *argv[]) { setlocale(LC_ALL, "de_DE.UTF-8"); - log_set_max_level(LOG_DEBUG); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); /* If test-catalog is located at the build directory, then use catalogs in that. * If it is not, e.g. installed by systemd-tests package, then use installed catalogs. */ diff --git a/src/journal/test-compress-benchmark.c b/src/journal/test-compress-benchmark.c index 0633fe6ea4..7f13b611e6 100644 --- a/src/journal/test-compress-benchmark.c +++ b/src/journal/test-compress-benchmark.c @@ -143,11 +143,7 @@ static void test_compress_decompress(const char* label, const char* type, int main(int argc, char *argv[]) { #if HAVE_XZ || HAVE_LZ4 - const char *i; - - log_set_max_level(LOG_INFO); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_INFO); if (argc >= 2) { unsigned x; @@ -163,6 +159,7 @@ int main(int argc, char *argv[]) { else arg_start = getpid_cached(); + const char *i; NULSTR_FOREACH(i, "zeros\0simple\0random\0") { #if HAVE_XZ test_compress_decompress("XZ", i, compress_blob_xz, decompress_blob_xz); diff --git a/src/journal/test-compress.c b/src/journal/test-compress.c index d59a613b39..7addf318d6 100644 --- a/src/journal/test-compress.c +++ b/src/journal/test-compress.c @@ -12,6 +12,7 @@ #include "macro.h" #include "path-util.h" #include "random-util.h" +#include "tests.h" #include "util.h" #if HAVE_XZ @@ -253,7 +254,7 @@ int main(int argc, char *argv[]) { memcpy(huge, "HUGE=", 5); char_array_0(huge); - log_set_max_level(LOG_DEBUG); + test_setup_logging(LOG_DEBUG); random_bytes(data + 7, sizeof(data) - 7); diff --git a/src/journal/test-journal-enum.c b/src/journal/test-journal-enum.c index b25a983498..f74b49d501 100644 --- a/src/journal/test-journal-enum.c +++ b/src/journal/test-journal-enum.c @@ -7,12 +7,13 @@ #include "journal-internal.h" #include "log.h" #include "macro.h" +#include "tests.h" int main(int argc, char *argv[]) { unsigned n = 0; _cleanup_(sd_journal_closep) sd_journal*j = NULL; - log_set_max_level(LOG_DEBUG); + test_setup_logging(LOG_DEBUG); assert_se(sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY) >= 0); diff --git a/src/journal/test-journal-init.c b/src/journal/test-journal-init.c index a43672b6e1..860baca383 100644 --- a/src/journal/test-journal-init.c +++ b/src/journal/test-journal-init.c @@ -5,6 +5,7 @@ #include "log.h" #include "parse-util.h" #include "rm-rf.h" +#include "tests.h" #include "util.h" int main(int argc, char *argv[]) { @@ -12,7 +13,7 @@ int main(int argc, char *argv[]) { int r, i, I = 100; char t[] = "/tmp/journal-stream-XXXXXX"; - log_set_max_level(LOG_DEBUG); + test_setup_logging(LOG_DEBUG); if (argc >= 2) { r = safe_atoi(argv[1], &I); diff --git a/src/journal/test-journal-interleaving.c b/src/journal/test-journal-interleaving.c index 82da68e0ea..7b098b4ba2 100644 --- a/src/journal/test-journal-interleaving.c +++ b/src/journal/test-journal-interleaving.c @@ -274,7 +274,7 @@ static void test_sequence_numbers(void) { } int main(int argc, char *argv[]) { - log_set_max_level(LOG_DEBUG); + test_setup_logging(LOG_DEBUG); /* journal_file_open requires a valid machine id */ if (access("/etc/machine-id", F_OK) != 0) diff --git a/src/journal/test-journal-match.c b/src/journal/test-journal-match.c index 4e5ad1791a..ba415fcc38 100644 --- a/src/journal/test-journal-match.c +++ b/src/journal/test-journal-match.c @@ -8,13 +8,14 @@ #include "journal-internal.h" #include "log.h" #include "string-util.h" +#include "tests.h" #include "util.h" int main(int argc, char *argv[]) { _cleanup_(sd_journal_closep) sd_journal*j = NULL; _cleanup_free_ char *t; - log_set_max_level(LOG_DEBUG); + test_setup_logging(LOG_DEBUG); assert_se(sd_journal_open(&j, 0) >= 0); diff --git a/src/journal/test-journal-stream.c b/src/journal/test-journal-stream.c index d7fdf907c8..226c30f80a 100644 --- a/src/journal/test-journal-stream.c +++ b/src/journal/test-journal-stream.c @@ -71,7 +71,7 @@ int main(int argc, char *argv[]) { if (access("/etc/machine-id", F_OK) != 0) return log_tests_skipped("/etc/machine-id not found"); - log_set_max_level(LOG_DEBUG); + test_setup_logging(LOG_DEBUG); assert_se(mkdtemp(t)); assert_se(chdir(t) >= 0); diff --git a/src/journal/test-journal-verify.c b/src/journal/test-journal-verify.c index c2a21cef9c..8d6b441213 100644 --- a/src/journal/test-journal-verify.c +++ b/src/journal/test-journal-verify.c @@ -65,7 +65,7 @@ int main(int argc, char *argv[]) { if (access("/etc/machine-id", F_OK) != 0) return log_tests_skipped("/etc/machine-id not found"); - log_set_max_level(LOG_DEBUG); + test_setup_logging(LOG_DEBUG); assert_se(mkdtemp(t)); assert_se(chdir(t) >= 0); diff --git a/src/journal/test-journal.c b/src/journal/test-journal.c index 7e188420db..34f202c81d 100644 --- a/src/journal/test-journal.c +++ b/src/journal/test-journal.c @@ -22,7 +22,7 @@ static void test_non_empty(void) { sd_id128_t fake_boot_id; char t[] = "/tmp/journal-XXXXXX"; - log_set_max_level(LOG_DEBUG); + test_setup_logging(LOG_DEBUG); assert_se(mkdtemp(t)); assert_se(chdir(t) >= 0); @@ -113,7 +113,7 @@ static void test_empty(void) { JournalFile *f1, *f2, *f3, *f4; char t[] = "/tmp/journal-XXXXXX"; - log_set_max_level(LOG_DEBUG); + test_setup_logging(LOG_DEBUG); assert_se(mkdtemp(t)); assert_se(chdir(t) >= 0); @@ -165,7 +165,7 @@ static bool check_compressed(uint64_t compress_threshold, uint64_t data_size) { assert_se(data_size <= sizeof(data)); - log_set_max_level(LOG_DEBUG); + test_setup_logging(LOG_DEBUG); assert_se(mkdtemp(t)); assert_se(chdir(t) >= 0); @@ -239,6 +239,8 @@ static void test_min_compress_size(void) { int main(int argc, char *argv[]) { arg_keep = argc > 1; + test_setup_logging(LOG_INFO); + /* journal_file_open requires a valid machine id */ if (access("/etc/machine-id", F_OK) != 0) return log_tests_skipped("/etc/machine-id not found"); diff --git a/src/libsystemd-network/test-acd.c b/src/libsystemd-network/test-acd.c index 079e760996..302eea2c30 100644 --- a/src/libsystemd-network/test-acd.c +++ b/src/libsystemd-network/test-acd.c @@ -13,6 +13,7 @@ #include "in-addr-util.h" #include "netlink-util.h" +#include "tests.h" #include "util.h" static void acd_handler(sd_ipv4acd *acd, int event, void *userdata) { @@ -83,9 +84,7 @@ static int test_acd(const char *ifname, const char *address) { } int main(int argc, char *argv[]) { - log_set_max_level(LOG_DEBUG); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); if (argc == 3) return test_acd(argv[1], argv[2]); diff --git a/src/libsystemd-network/test-dhcp-client.c b/src/libsystemd-network/test-dhcp-client.c index 0e257633b8..30dc36140f 100644 --- a/src/libsystemd-network/test-dhcp-client.c +++ b/src/libsystemd-network/test-dhcp-client.c @@ -16,6 +16,7 @@ #include "dhcp-internal.h" #include "dhcp-protocol.h" #include "fd-util.h" +#include "tests.h" #include "util.h" static uint8_t mac_addr[] = {'A', 'B', 'C', '1', '2', '3'}; @@ -524,9 +525,7 @@ static void test_addr_acq(sd_event *e) { int main(int argc, char *argv[]) { _cleanup_(sd_event_unrefp) sd_event *e; - log_set_max_level(LOG_DEBUG); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); assert_se(sd_event_new(&e) >= 0); diff --git a/src/libsystemd-network/test-dhcp-server.c b/src/libsystemd-network/test-dhcp-server.c index 2854e04abc..ea998939bc 100644 --- a/src/libsystemd-network/test-dhcp-server.c +++ b/src/libsystemd-network/test-dhcp-server.c @@ -229,9 +229,7 @@ int main(int argc, char *argv[]) { _cleanup_(sd_event_unrefp) sd_event *e; int r; - log_set_max_level(LOG_DEBUG); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); assert_se(sd_event_new(&e) >= 0); diff --git a/src/libsystemd-network/test-dhcp6-client.c b/src/libsystemd-network/test-dhcp6-client.c index 27c0002fe2..5e20580783 100644 --- a/src/libsystemd-network/test-dhcp6-client.c +++ b/src/libsystemd-network/test-dhcp6-client.c @@ -19,6 +19,7 @@ #include "fd-util.h" #include "macro.h" #include "socket-util.h" +#include "tests.h" #include "virt.h" static struct ether_addr mac_addr = { @@ -910,9 +911,7 @@ int main(int argc, char *argv[]) { assert_se(sd_event_new(&e) >= 0); - log_set_max_level(LOG_DEBUG); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); test_client_basic(e); test_option(e); diff --git a/src/libsystemd-network/test-ipv4ll-manual.c b/src/libsystemd-network/test-ipv4ll-manual.c index 125133f039..fd827ff401 100644 --- a/src/libsystemd-network/test-ipv4ll-manual.c +++ b/src/libsystemd-network/test-ipv4ll-manual.c @@ -15,6 +15,7 @@ #include "netlink-util.h" #include "parse-util.h" #include "string-util.h" +#include "tests.h" #include "util.h" static void ll_handler(sd_ipv4ll *ll, int event, void *userdata) { @@ -95,9 +96,7 @@ static int test_ll(const char *ifname, const char *seed) { } int main(int argc, char *argv[]) { - log_set_max_level(LOG_DEBUG); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); if (argc == 2) return test_ll(argv[1], NULL); diff --git a/src/libsystemd-network/test-ipv4ll.c b/src/libsystemd-network/test-ipv4ll.c index ee9cce02a8..d9c803f899 100644 --- a/src/libsystemd-network/test-ipv4ll.c +++ b/src/libsystemd-network/test-ipv4ll.c @@ -15,6 +15,7 @@ #include "arp-util.h" #include "fd-util.h" #include "socket-util.h" +#include "tests.h" #include "util.h" static bool verbose = false; @@ -193,9 +194,7 @@ static void test_basic_request(sd_event *e) { int main(int argc, char *argv[]) { _cleanup_(sd_event_unrefp) sd_event *e = NULL; - log_set_max_level(LOG_DEBUG); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); assert_se(sd_event_new(&e) >= 0); diff --git a/src/libsystemd-network/test-ndisc-ra.c b/src/libsystemd-network/test-ndisc-ra.c index d5a0237663..c8870fad0d 100644 --- a/src/libsystemd-network/test-ndisc-ra.c +++ b/src/libsystemd-network/test-ndisc-ra.c @@ -13,6 +13,7 @@ #include "icmp6-util.h" #include "socket-util.h" #include "strv.h" +#include "tests.h" static struct ether_addr mac_addr = { .ether_addr_octet = { 0x78, 0x2b, 0xcb, 0xb3, 0x6d, 0x53 } @@ -357,9 +358,7 @@ static void test_ra(void) { int main(int argc, char *argv[]) { - log_set_max_level(LOG_DEBUG); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); test_radv_prefix(); test_radv(); diff --git a/src/libsystemd-network/test-ndisc-rs.c b/src/libsystemd-network/test-ndisc-rs.c index b9d0e7dc90..70f289bcb5 100644 --- a/src/libsystemd-network/test-ndisc-rs.c +++ b/src/libsystemd-network/test-ndisc-rs.c @@ -14,6 +14,7 @@ #include "socket-util.h" #include "strv.h" #include "ndisc-internal.h" +#include "tests.h" static struct ether_addr mac_addr = { .ether_addr_octet = {'A', 'B', 'C', '1', '2', '3'} @@ -407,9 +408,7 @@ static void test_timeout(void) { int main(int argc, char *argv[]) { - log_set_max_level(LOG_DEBUG); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); test_rs(); test_timeout(); diff --git a/src/libsystemd/sd-bus/test-bus-chat.c b/src/libsystemd/sd-bus/test-bus-chat.c index 28922e6a69..2ba6eaee7d 100644 --- a/src/libsystemd/sd-bus/test-bus-chat.c +++ b/src/libsystemd/sd-bus/test-bus-chat.c @@ -510,6 +510,8 @@ int main(int argc, char *argv[]) { void *p; int q, r; + test_setup_logging(LOG_INFO); + r = server_init(&bus); if (r < 0) return log_tests_skipped("Failed to connect to bus"); diff --git a/src/libsystemd/sd-bus/test-bus-cleanup.c b/src/libsystemd/sd-bus/test-bus-cleanup.c index a70f4823db..975d3f97dd 100644 --- a/src/libsystemd/sd-bus/test-bus-cleanup.c +++ b/src/libsystemd/sd-bus/test-bus-cleanup.c @@ -60,8 +60,7 @@ static void test_bus_new_signal(void) { } int main(int argc, char **argv) { - log_parse_environment(); - log_open(); + test_setup_logging(LOG_INFO); test_bus_new(); diff --git a/src/libsystemd/sd-bus/test-bus-creds.c b/src/libsystemd/sd-bus/test-bus-creds.c index a99fbc3bc5..c02c459663 100644 --- a/src/libsystemd/sd-bus/test-bus-creds.c +++ b/src/libsystemd/sd-bus/test-bus-creds.c @@ -11,9 +11,7 @@ int main(int argc, char *argv[]) { _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; int r; - log_set_max_level(LOG_DEBUG); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); if (cg_unified_flush() == -ENOMEDIUM) return log_tests_skipped("/sys/fs/cgroup/ not available"); diff --git a/src/libsystemd/sd-bus/test-bus-gvariant.c b/src/libsystemd/sd-bus/test-bus-gvariant.c index ae3e71e365..648fea76e0 100644 --- a/src/libsystemd/sd-bus/test-bus-gvariant.c +++ b/src/libsystemd/sd-bus/test-bus-gvariant.c @@ -13,6 +13,7 @@ #include "bus-message.h" #include "bus-util.h" #include "macro.h" +#include "tests.h" #include "util.h" static void test_bus_gvariant_is_fixed_size(void) { @@ -199,6 +200,7 @@ static void test_marshal(void) { } int main(int argc, char *argv[]) { + test_setup_logging(LOG_INFO); test_bus_gvariant_is_fixed_size(); test_bus_gvariant_get_size(); diff --git a/src/libsystemd/sd-bus/test-bus-marshal.c b/src/libsystemd/sd-bus/test-bus-marshal.c index 22c48e38dc..7e113b179e 100644 --- a/src/libsystemd/sd-bus/test-bus-marshal.c +++ b/src/libsystemd/sd-bus/test-bus-marshal.c @@ -121,6 +121,8 @@ int main(int argc, char *argv[]) { double dbl; uint64_t u64; + test_setup_logging(LOG_INFO); + r = sd_bus_default_user(&bus); if (r < 0) return log_tests_skipped("Failed to connect to bus"); diff --git a/src/libsystemd/sd-bus/test-bus-match.c b/src/libsystemd/sd-bus/test-bus-match.c index 527911c5af..c56b39437b 100644 --- a/src/libsystemd/sd-bus/test-bus-match.c +++ b/src/libsystemd/sd-bus/test-bus-match.c @@ -78,6 +78,8 @@ int main(int argc, char *argv[]) { sd_bus_slot slots[19]; int r; + test_setup_logging(LOG_INFO); + r = sd_bus_open_user(&bus); if (r < 0) return log_tests_skipped("Failed to connect to bus"); diff --git a/src/libsystemd/sd-bus/test-bus-track.c b/src/libsystemd/sd-bus/test-bus-track.c index 8a3dcf1654..a2782cd1d5 100644 --- a/src/libsystemd/sd-bus/test-bus-track.c +++ b/src/libsystemd/sd-bus/test-bus-track.c @@ -48,6 +48,8 @@ int main(int argc, char *argv[]) { const char *unique; int r; + test_setup_logging(LOG_INFO); + r = sd_event_default(&event); assert_se(r >= 0); diff --git a/src/libsystemd/sd-event/test-event.c b/src/libsystemd/sd-event/test-event.c index 1f08b4d4b8..50074a56be 100644 --- a/src/libsystemd/sd-event/test-event.c +++ b/src/libsystemd/sd-event/test-event.c @@ -16,6 +16,7 @@ #include "signal-util.h" #include "stdio-util.h" #include "string-util.h" +#include "tests.h" #include "util.h" static int prepare_handler(sd_event_source *s, void *userdata) { @@ -481,9 +482,7 @@ static void test_inotify(unsigned n_create_events) { } int main(int argc, char *argv[]) { - - log_set_max_level(LOG_DEBUG); - log_parse_environment(); + test_setup_logging(LOG_DEBUG); test_basic(); test_sd_event_now(); diff --git a/src/libsystemd/sd-netlink/test-local-addresses.c b/src/libsystemd/sd-netlink/test-local-addresses.c index cb05d05a89..17114265d7 100644 --- a/src/libsystemd/sd-netlink/test-local-addresses.c +++ b/src/libsystemd/sd-netlink/test-local-addresses.c @@ -4,6 +4,7 @@ #include "alloc-util.h" #include "in-addr-util.h" #include "local-addresses.h" +#include "tests.h" static void print_local_addresses(struct local_address *a, unsigned n) { unsigned i; @@ -20,9 +21,7 @@ int main(int argc, char *argv[]) { struct local_address *a; int n; - log_set_max_level(LOG_DEBUG); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); a = NULL; n = local_addresses(NULL, 0, AF_UNSPEC, &a); diff --git a/src/network/test-network.c b/src/network/test-network.c index e8c10bc15c..6e169e0fca 100644 --- a/src/network/test-network.c +++ b/src/network/test-network.c @@ -224,6 +224,8 @@ int main(void) { _cleanup_(sd_device_unrefp) sd_device *loopback = NULL; int ifindex, r; + test_setup_logging(LOG_INFO); + test_deserialize_in_addr(); test_deserialize_dhcp_routes(); test_address_equality(); diff --git a/src/network/test-routing-policy-rule.c b/src/network/test-routing-policy-rule.c index 9920f516b7..d112471587 100644 --- a/src/network/test-routing-policy-rule.c +++ b/src/network/test-routing-policy-rule.c @@ -9,6 +9,7 @@ #include "network-internal.h" #include "networkd-manager.h" #include "string-util.h" +#include "tests.h" static void test_rule_serialization(const char *title, const char *ruleset, const char *expected) { char pattern[] = "/tmp/systemd-test-routing-policy-rule.XXXXXX", @@ -57,9 +58,7 @@ static void test_rule_serialization(const char *title, const char *ruleset, cons int main(int argc, char **argv) { _cleanup_free_ char *p = NULL; - log_set_max_level(LOG_DEBUG); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); test_rule_serialization("basic parsing", "RULE=from=1.2.3.4/32 to=2.3.4.5/32 tos=5 fwmark=1/2 table=10", NULL); diff --git a/src/nspawn/test-patch-uid.c b/src/nspawn/test-patch-uid.c index 8e29d3e806..b50f0990d8 100644 --- a/src/nspawn/test-patch-uid.c +++ b/src/nspawn/test-patch-uid.c @@ -5,15 +5,14 @@ #include "log.h" #include "nspawn-patch-uid.h" #include "user-util.h" +#include "tests.h" #include "util.h" int main(int argc, char *argv[]) { uid_t shift, range; int r; - log_set_max_level(LOG_DEBUG); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); if (argc != 4) { log_error("Expected PATH SHIFT RANGE parameters."); diff --git a/src/shared/tests.c b/src/shared/tests.c index 884f34bc72..8d70c02802 100644 --- a/src/shared/tests.c +++ b/src/shared/tests.c @@ -91,6 +91,12 @@ bool slow_tests_enabled(void) { return SYSTEMD_SLOW_TESTS_DEFAULT; } +void test_setup_logging(int level) { + log_set_max_level(level); + log_parse_environment(); + log_open(); +} + int log_tests_skipped(const char *message) { log_notice("%s: %s, skipping tests.", program_invocation_short_name, message); diff --git a/src/shared/tests.h b/src/shared/tests.h index 67d5e1ce4a..f35a526737 100644 --- a/src/shared/tests.h +++ b/src/shared/tests.h @@ -5,4 +5,5 @@ char* setup_fake_runtime_dir(void); const char* get_testdata_dir(void); const char* get_catalog_dir(void); bool slow_tests_enabled(void); +void test_setup_logging(int level); int log_tests_skipped(const char *message); diff --git a/src/test/test-architecture.c b/src/test/test-architecture.c index 6cd64b60b2..8c43bfc750 100644 --- a/src/test/test-architecture.c +++ b/src/test/test-architecture.c @@ -10,6 +10,8 @@ int main(int argc, char *argv[]) { int a, v; const char *p; + test_setup_logging(LOG_INFO); + assert_se(architecture_from_string("") < 0); assert_se(architecture_from_string(NULL) < 0); assert_se(architecture_from_string("hoge") < 0); diff --git a/src/test/test-barrier.c b/src/test/test-barrier.c index 2a735695a1..6ae84cd6fc 100644 --- a/src/test/test-barrier.c +++ b/src/test/test-barrier.c @@ -420,9 +420,7 @@ TEST_BARRIER(test_barrier_pending_exit, TEST_BARRIER_WAIT_SUCCESS(pid2)); int main(int argc, char *argv[]) { - log_set_max_level(LOG_INFO); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_INFO); if (!slow_tests_enabled()) return log_tests_skipped("slow tests are disabled"); diff --git a/src/test/test-boot-timestamps.c b/src/test/test-boot-timestamps.c index 578aa5b304..d45ca8c920 100644 --- a/src/test/test-boot-timestamps.c +++ b/src/test/test-boot-timestamps.c @@ -82,8 +82,7 @@ static int test_boot_timestamps(void) { int main(int argc, char* argv[]) { int p, q, r; - log_set_max_level(LOG_DEBUG); - log_parse_environment(); + test_setup_logging(LOG_DEBUG); p = test_acpi_fpdt(); assert(p >= 0); diff --git a/src/test/test-bpf.c b/src/test/test-bpf.c index a055fea802..2fb7968dfd 100644 --- a/src/test/test-bpf.c +++ b/src/test/test-bpf.c @@ -28,9 +28,7 @@ int main(int argc, char *argv[]) { char log_buf[65535]; int r; - log_set_max_level(LOG_DEBUG); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); r = enter_cgroup_subroot(); if (r == -ENOMEDIUM) diff --git a/src/test/test-bus-util.c b/src/test/test-bus-util.c index 791b3928fe..789d19cf7f 100644 --- a/src/test/test-bus-util.c +++ b/src/test/test-bus-util.c @@ -2,6 +2,7 @@ #include "bus-util.h" #include "log.h" +#include "tests.h" static void test_name_async(unsigned n_messages) { _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; @@ -78,9 +79,7 @@ static void test_destroy_callback(void) { } int main(int argc, char **argv) { - log_set_max_level(LOG_DEBUG); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); test_name_async(0); test_name_async(20); diff --git a/src/test/test-capability.c b/src/test/test-capability.c index 79ddca9554..730dbf8cbd 100644 --- a/src/test/test-capability.c +++ b/src/test/test-capability.c @@ -220,12 +220,11 @@ static void test_set_ambient_caps(void) { int main(int argc, char *argv[]) { bool run_ambient; + test_setup_logging(LOG_INFO); + test_last_cap_file(); test_last_cap_probe(); - log_parse_environment(); - log_open(); - log_info("have ambient caps: %s", yes_no(ambient_capabilities_supported())); if (getuid() != 0) diff --git a/src/test/test-cgroup-mask.c b/src/test/test-cgroup-mask.c index 0dd673e3e5..bab27edf54 100644 --- a/src/test/test-cgroup-mask.c +++ b/src/test/test-cgroup-mask.c @@ -118,8 +118,7 @@ static void test_cg_mask_to_string(void) { int main(int argc, char* argv[]) { int rc = EXIT_SUCCESS; - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); test_cg_mask_to_string(); TEST_REQ_RUNNING_SYSTEMD(rc = test_cgroup_mask()); diff --git a/src/test/test-cgroup-util.c b/src/test/test-cgroup-util.c index d49356315e..95ca41fba5 100644 --- a/src/test/test-cgroup-util.c +++ b/src/test/test-cgroup-util.c @@ -14,6 +14,7 @@ #include "string-util.h" #include "strv.h" #include "test-helper.h" +#include "tests.h" #include "user-util.h" #include "util.h" @@ -447,9 +448,7 @@ static void test_cg_get_keyed_attribute(void) { } int main(void) { - log_set_max_level(LOG_DEBUG); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); test_path_decode_unit(); test_path_get_unit(); diff --git a/src/test/test-condition.c b/src/test/test-condition.c index 678fc426fe..5c2d00af88 100644 --- a/src/test/test-condition.c +++ b/src/test/test-condition.c @@ -26,6 +26,7 @@ #include "strv.h" #include "tomoyo-util.h" #include "user-util.h" +#include "tests.h" #include "util.h" #include "virt.h" @@ -673,9 +674,7 @@ static void test_condition_test_group(void) { } int main(int argc, char *argv[]) { - log_set_max_level(LOG_DEBUG); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); test_condition_test_path(); test_condition_test_ac_power(); diff --git a/src/test/test-conf-files.c b/src/test/test-conf-files.c index 2ec2dfc261..b69046c9c1 100644 --- a/src/test/test-conf-files.c +++ b/src/test/test-conf-files.c @@ -16,6 +16,7 @@ #include "rm-rf.h" #include "string-util.h" #include "strv.h" +#include "tests.h" #include "user-util.h" #include "util.h" @@ -93,9 +94,7 @@ static void test_conf_files_list(bool use_root) { } int main(int argc, char **argv) { - log_set_max_level(LOG_DEBUG); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); test_conf_files_list(false); test_conf_files_list(true); diff --git a/src/test/test-copy.c b/src/test/test-copy.c index 2e8d251ac1..4e265374ab 100644 --- a/src/test/test-copy.c +++ b/src/test/test-copy.c @@ -14,6 +14,7 @@ #include "rm-rf.h" #include "string-util.h" #include "strv.h" +#include "tests.h" #include "user-util.h" #include "util.h" @@ -254,7 +255,7 @@ static void test_copy_atomic(void) { } int main(int argc, char *argv[]) { - log_set_max_level(LOG_DEBUG); + test_setup_logging(LOG_DEBUG); test_copy_file(); test_copy_file_fd(); diff --git a/src/test/test-date.c b/src/test/test-date.c index 99b6f2eb9e..cba51e225c 100644 --- a/src/test/test-date.c +++ b/src/test/test-date.c @@ -4,6 +4,7 @@ #include "alloc-util.h" #include "string-util.h" +#include "tests.h" #include "util.h" static void test_should_pass(const char *p) { @@ -66,9 +67,7 @@ static void test_one_noutc(const char *p) { } int main(int argc, char *argv[]) { - log_set_max_level(LOG_DEBUG); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); test_one("17:41"); test_one("18:42:44"); diff --git a/src/test/test-dissect-image.c b/src/test/test-dissect-image.c index a0a909baf0..7b32e8373f 100644 --- a/src/test/test-dissect-image.c +++ b/src/test/test-dissect-image.c @@ -7,13 +7,14 @@ #include "log.h" #include "loop-util.h" #include "string-util.h" +#include "tests.h" int main(int argc, char *argv[]) { _cleanup_(loop_device_unrefp) LoopDevice *d = NULL; _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL; int r, i; - log_set_max_level(LOG_DEBUG); + test_setup_logging(LOG_DEBUG); if (argc < 2) { log_error("Requires one command line argument."); diff --git a/src/test/test-dns-domain.c b/src/test/test-dns-domain.c index 7fa887e4d2..cbfe5ef390 100644 --- a/src/test/test-dns-domain.c +++ b/src/test/test-dns-domain.c @@ -4,6 +4,7 @@ #include "dns-domain.h" #include "macro.h" #include "string-util.h" +#include "tests.h" static void test_dns_label_unescape_one(const char *what, const char *expect, size_t buffer_sz, int ret) { char buffer[buffer_sz]; @@ -687,9 +688,7 @@ static void test_dns_name_is_valid_or_address(void) { } int main(int argc, char *argv[]) { - log_set_max_level(LOG_DEBUG); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); test_dns_label_unescape(); test_dns_label_unescape_suffix(); diff --git a/src/test/test-engine.c b/src/test/test-engine.c index f7435bd4fb..0483c5b698 100644 --- a/src/test/test-engine.c +++ b/src/test/test-engine.c @@ -18,9 +18,7 @@ int main(int argc, char *argv[]) { Job *j; int r; - log_set_max_level(LOG_DEBUG); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); r = enter_cgroup_subroot(); if (r == -ENOMEDIUM) diff --git a/src/test/test-escape.c b/src/test/test-escape.c index 650a9a058d..4ee4aa974d 100644 --- a/src/test/test-escape.c +++ b/src/test/test-escape.c @@ -3,6 +3,7 @@ #include "alloc-util.h" #include "escape.h" #include "macro.h" +#include "tests.h" static void test_cescape(void) { _cleanup_free_ char *escaped; @@ -119,9 +120,7 @@ static void test_shell_maybe_quote(void) { } int main(int argc, char *argv[]) { - log_set_max_level(LOG_DEBUG); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); test_cescape(); test_cunescape(); diff --git a/src/test/test-exec-util.c b/src/test/test-exec-util.c index cfc8b5f88e..d346a4d6f7 100644 --- a/src/test/test-exec-util.c +++ b/src/test/test-exec-util.c @@ -19,6 +19,7 @@ #include "rm-rf.h" #include "string-util.h" #include "strv.h" +#include "tests.h" static int here = 0, here2 = 0, here3 = 0; void *ignore_stdout_args[] = {&here, &here2, &here3}; @@ -334,9 +335,7 @@ static void test_environment_gathering(void) { } int main(int argc, char *argv[]) { - log_set_max_level(LOG_DEBUG); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); test_execute_directory(true); test_execute_directory(false); diff --git a/src/test/test-execute.c b/src/test/test-execute.c index 0d2f35734e..15967f76c2 100644 --- a/src/test/test-execute.c +++ b/src/test/test-execute.c @@ -724,9 +724,7 @@ int main(int argc, char *argv[]) { }; int r; - log_set_max_level(LOG_DEBUG); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); (void) unsetenv("USER"); (void) unsetenv("LOGNAME"); diff --git a/src/test/test-fd-util.c b/src/test/test-fd-util.c index a04403d748..157dc88320 100644 --- a/src/test/test-fd-util.c +++ b/src/test/test-fd-util.c @@ -12,6 +12,7 @@ #include "random-util.h" #include "string-util.h" #include "util.h" +#include "tests.h" static void test_close_many(void) { int fds[3]; @@ -316,7 +317,7 @@ static void test_read_nr_open(void) { int main(int argc, char *argv[]) { - log_set_max_level(LOG_DEBUG); + test_setup_logging(LOG_DEBUG); test_close_many(); test_close_nointr(); diff --git a/src/test/test-fileio.c b/src/test/test-fileio.c index 14ba075144..aa38a7d29a 100644 --- a/src/test/test-fileio.c +++ b/src/test/test-fileio.c @@ -16,6 +16,7 @@ #include "process-util.h" #include "string-util.h" #include "strv.h" +#include "tests.h" #include "util.h" static void test_parse_env_file(void) { @@ -710,9 +711,7 @@ static void test_read_line3(void) { } int main(int argc, char *argv[]) { - log_set_max_level(LOG_DEBUG); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); test_parse_env_file(); test_parse_multiline_env_file(); diff --git a/src/test/test-firewall-util.c b/src/test/test-firewall-util.c index 1b62590b49..1788e8d1ca 100644 --- a/src/test/test-firewall-util.c +++ b/src/test/test-firewall-util.c @@ -2,12 +2,13 @@ #include "firewall-util.h" #include "log.h" +#include "tests.h" #define MAKE_IN_ADDR_UNION(a,b,c,d) (union in_addr_union) { .in.s_addr = htobe32((uint32_t) (a) << 24 | (uint32_t) (b) << 16 | (uint32_t) (c) << 8 | (uint32_t) (d))} int main(int argc, char *argv[]) { int r; - log_set_max_level(LOG_DEBUG); + test_setup_logging(LOG_DEBUG); r = fw_add_masquerade(true, AF_INET, 0, NULL, 0, "foobar", NULL, 0); if (r < 0) diff --git a/src/test/test-hash.c b/src/test/test-hash.c index ea56b74d0f..44d1044bf3 100644 --- a/src/test/test-hash.c +++ b/src/test/test-hash.c @@ -14,7 +14,7 @@ int main(int argc, char *argv[]) { _cleanup_free_ char *s = NULL; int r; - log_set_max_level(LOG_DEBUG); + test_setup_logging(LOG_DEBUG); assert_se(khash_new(&h, NULL) == -EINVAL); assert_se(khash_new(&h, "") == -EINVAL); diff --git a/src/test/test-install-root.c b/src/test/test-install-root.c index fe1ca5b16f..c0956fa4bb 100644 --- a/src/test/test-install-root.c +++ b/src/test/test-install-root.c @@ -7,6 +7,7 @@ #include "rm-rf.h" #include "special.h" #include "string-util.h" +#include "tests.h" static void test_basic_mask_and_enable(const char *root) { const char *p; @@ -14,7 +15,7 @@ static void test_basic_mask_and_enable(const char *root) { UnitFileChange *changes = NULL; size_t n_changes = 0; - log_set_max_level(LOG_DEBUG); + test_setup_logging(LOG_DEBUG); assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "a.service", NULL) == -ENOENT); assert_se(unit_file_get_state(UNIT_FILE_SYSTEM, root, "b.service", NULL) == -ENOENT); diff --git a/src/test/test-install.c b/src/test/test-install.c index 7dfc7e4272..62daaccd62 100644 --- a/src/test/test-install.c +++ b/src/test/test-install.c @@ -4,6 +4,7 @@ #include #include "install.h" +#include "tests.h" static void dump_changes(UnitFileChange *c, unsigned n) { unsigned i; @@ -29,8 +30,7 @@ int main(int argc, char* argv[]) { size_t n_changes = 0; UnitFileState state = 0; - log_set_max_level(LOG_DEBUG); - log_parse_environment(); + test_setup_logging(LOG_DEBUG); h = hashmap_new(&string_hash_ops); r = unit_file_get_list(UNIT_FILE_SYSTEM, NULL, h, NULL, NULL); diff --git a/src/test/test-ipcrm.c b/src/test/test-ipcrm.c index 1f6d7b4351..4b658a0bdb 100644 --- a/src/test/test-ipcrm.c +++ b/src/test/test-ipcrm.c @@ -10,6 +10,8 @@ int main(int argc, char *argv[]) { int r; const char* name = argv[1] ?: NOBODY_USER_NAME; + test_setup_logging(LOG_INFO); + r = get_user_creds(&name, &uid, NULL, NULL, NULL, 0); if (r == -ESRCH) return log_tests_skipped("Failed to resolve user"); diff --git a/src/test/test-journal-importer.c b/src/test/test-journal-importer.c index 8f09d5ad2f..c1ceb0bbd8 100644 --- a/src/test/test-journal-importer.c +++ b/src/test/test-journal-importer.c @@ -69,8 +69,7 @@ static void test_bad_input(void) { } int main(int argc, char **argv) { - log_set_max_level(LOG_DEBUG); - log_parse_environment(); + test_setup_logging(LOG_DEBUG); test_basic_parsing(); test_bad_input(); diff --git a/src/test/test-loopback.c b/src/test/test-loopback.c index eaea9e4c76..89b760fae4 100644 --- a/src/test/test-loopback.c +++ b/src/test/test-loopback.c @@ -5,13 +5,12 @@ #include "log.h" #include "loopback-setup.h" +#include "tests.h" int main(int argc, char* argv[]) { int r; - log_open(); - log_set_max_level(LOG_DEBUG); - log_parse_environment(); + test_setup_logging(LOG_DEBUG); r = loopback_setup(); if (r < 0) diff --git a/src/test/test-mount-util.c b/src/test/test-mount-util.c index c10e1681fb..56e385aa11 100644 --- a/src/test/test-mount-util.c +++ b/src/test/test-mount-util.c @@ -13,6 +13,7 @@ #include "path-util.h" #include "rm-rf.h" #include "string-util.h" +#include "tests.h" static void test_mount_propagation_flags(const char *name, int ret, unsigned long expected) { long unsigned flags; @@ -295,7 +296,7 @@ static void test_mount_option_mangle(void) { int main(int argc, char *argv[]) { - log_set_max_level(LOG_DEBUG); + test_setup_logging(LOG_DEBUG); test_mount_propagation_flags("shared", 0, MS_SHARED); test_mount_propagation_flags("slave", 0, MS_SLAVE); diff --git a/src/test/test-namespace.c b/src/test/test-namespace.c index 318bd5bad4..ab8f05076d 100644 --- a/src/test/test-namespace.c +++ b/src/test/test-namespace.c @@ -109,8 +109,7 @@ int main(int argc, char *argv[]) { char boot_id[SD_ID128_STRING_MAX]; _cleanup_free_ char *x = NULL, *y = NULL, *z = NULL, *zz = NULL; - log_parse_environment(); - log_open(); + test_setup_logging(LOG_INFO); assert_se(sd_id128_get_boot(&bid) >= 0); sd_id128_to_string(bid, boot_id); diff --git a/src/test/test-netlink-manual.c b/src/test/test-netlink-manual.c index 39826dfdb9..a471456e0c 100644 --- a/src/test/test-netlink-manual.c +++ b/src/test/test-netlink-manual.c @@ -118,6 +118,8 @@ int main(int argc, char *argv[]) { sd_netlink *rtnl; int r; + test_setup_logging(LOG_INFO); + assert_se(sd_netlink_open(&rtnl) >= 0); assert_se(rtnl); diff --git a/src/test/test-ns.c b/src/test/test-ns.c index 4ab70f2306..d3dbb54ca1 100644 --- a/src/test/test-ns.c +++ b/src/test/test-ns.c @@ -6,6 +6,7 @@ #include "log.h" #include "namespace.h" +#include "tests.h" int main(int argc, char *argv[]) { const char * const writable[] = { @@ -43,7 +44,7 @@ int main(int argc, char *argv[]) { char tmp_dir[] = "/tmp/systemd-private-XXXXXX", var_tmp_dir[] = "/var/tmp/systemd-private-XXXXXX"; - log_set_max_level(LOG_DEBUG); + test_setup_logging(LOG_DEBUG); assert_se(mkdtemp(tmp_dir)); assert_se(mkdtemp(var_tmp_dir)); diff --git a/src/test/test-os-util.c b/src/test/test-os-util.c index 8d8b52d7f6..c215a2e99e 100644 --- a/src/test/test-os-util.c +++ b/src/test/test-os-util.c @@ -4,6 +4,7 @@ #include "log.h" #include "os-util.h" +#include "tests.h" static void test_path_is_os_tree(void) { assert_se(path_is_os_tree("/") > 0); @@ -12,9 +13,7 @@ static void test_path_is_os_tree(void) { } int main(int argc, char *argv[]) { - log_set_max_level(LOG_DEBUG); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); test_path_is_os_tree(); diff --git a/src/test/test-path-lookup.c b/src/test/test-path-lookup.c index 892293cb10..029dc8e906 100644 --- a/src/test/test-path-lookup.c +++ b/src/test/test-path-lookup.c @@ -8,6 +8,7 @@ #include "rm-rf.h" #include "string-util.h" #include "strv.h" +#include "tests.h" static void test_paths(UnitFileScope scope) { char template[] = "/tmp/test-path-lookup.XXXXXXX"; @@ -76,9 +77,7 @@ static void print_generator_binary_paths(UnitFileScope scope) { } int main(int argc, char **argv) { - log_set_max_level(LOG_DEBUG); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); test_paths(UNIT_FILE_SYSTEM); test_paths(UNIT_FILE_USER); diff --git a/src/test/test-path-util.c b/src/test/test-path-util.c index 35b27bcedd..9ec42aae7b 100644 --- a/src/test/test-path-util.c +++ b/src/test/test-path-util.c @@ -12,6 +12,7 @@ #include "stat-util.h" #include "string-util.h" #include "strv.h" +#include "tests.h" #include "util.h" #define test_path_compare(a, b, result) { \ @@ -506,9 +507,7 @@ static void test_empty_or_root(void) { } int main(int argc, char **argv) { - log_set_max_level(LOG_DEBUG); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); test_path(); test_path_equal_root(); diff --git a/src/test/test-path.c b/src/test/test-path.c index 7e664ec849..025b3295a0 100644 --- a/src/test/test-path.c +++ b/src/test/test-path.c @@ -252,8 +252,7 @@ int main(int argc, char *argv[]) { umask(022); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_INFO); test_path = path_join(NULL, get_testdata_dir(), "test-path"); assert_se(set_unit_path(test_path) >= 0); diff --git a/src/test/test-process-util.c b/src/test/test-process-util.c index 1b3b357913..d396c29b06 100644 --- a/src/test/test-process-util.c +++ b/src/test/test-process-util.c @@ -24,6 +24,7 @@ #include "string-util.h" #include "terminal-util.h" #include "test-helper.h" +#include "tests.h" #include "util.h" #include "virt.h" @@ -587,9 +588,7 @@ static void test_ioprio_class_from_to_string(void) { } int main(int argc, char *argv[]) { - log_set_max_level(LOG_DEBUG); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); saved_argc = argc; saved_argv = argv; diff --git a/src/test/test-random-util.c b/src/test/test-random-util.c index 70301a7782..9652a0af05 100644 --- a/src/test/test-random-util.c +++ b/src/test/test-random-util.c @@ -3,6 +3,7 @@ #include "hexdecoct.h" #include "random-util.h" #include "log.h" +#include "tests.h" static void test_acquire_random_bytes(bool high_quality_required) { uint8_t buf[16] = {}; @@ -51,9 +52,7 @@ static void test_rdrand64(void) { } int main(int argc, char **argv) { - log_set_max_level(LOG_DEBUG); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); test_acquire_random_bytes(false); test_acquire_random_bytes(true); diff --git a/src/test/test-sched-prio.c b/src/test/test-sched-prio.c index c594336325..f6ca192f07 100644 --- a/src/test/test-sched-prio.c +++ b/src/test/test-sched-prio.c @@ -19,6 +19,8 @@ int main(int argc, char *argv[]) { Service *ser; int r; + test_setup_logging(LOG_INFO); + r = enter_cgroup_subroot(); if (r == -ENOMEDIUM) return log_tests_skipped("cgroupfs not available"); diff --git a/src/test/test-seccomp.c b/src/test/test-seccomp.c index d82cb5c1c5..e05710c00d 100644 --- a/src/test/test-seccomp.c +++ b/src/test/test-seccomp.c @@ -20,6 +20,7 @@ #include "seccomp-util.h" #include "set.h" #include "string-util.h" +#include "tests.h" #include "util.h" #include "virt.h" @@ -668,7 +669,7 @@ static void test_filter_sets_ordered(void) { int main(int argc, char *argv[]) { - log_set_max_level(LOG_DEBUG); + test_setup_logging(LOG_DEBUG); test_seccomp_arch_to_string(); test_architecture_table(); diff --git a/src/test/test-selinux.c b/src/test/test-selinux.c index 6caeb843f3..59b4f71946 100644 --- a/src/test/test-selinux.c +++ b/src/test/test-selinux.c @@ -7,6 +7,7 @@ #include "log.h" #include "selinux-util.h" #include "string-util.h" +#include "tests.h" #include "time-util.h" #include "util.h" @@ -92,8 +93,7 @@ int main(int argc, char **argv) { if (argc >= 2) path = argv[1]; - log_set_max_level(LOG_DEBUG); - log_parse_environment(); + test_setup_logging(LOG_DEBUG); test_testing(); test_loading(); diff --git a/src/test/test-sigbus.c b/src/test/test-sigbus.c index 518e48e1ec..33c9d42e9e 100644 --- a/src/test/test-sigbus.c +++ b/src/test/test-sigbus.c @@ -17,6 +17,8 @@ int main(int argc, char *argv[]) { void *addr = NULL; uint8_t *p; + test_setup_logging(LOG_INFO); + #ifdef __SANITIZE_ADDRESS__ return log_tests_skipped("address-sanitizer is enabled"); #endif diff --git a/src/test/test-sleep.c b/src/test/test-sleep.c index 0198d34f19..2ce79f8345 100644 --- a/src/test/test-sleep.c +++ b/src/test/test-sleep.c @@ -80,8 +80,7 @@ static void test_sleep(void) { int main(int argc, char* argv[]) { int i, r = 0, k; - log_parse_environment(); - log_open(); + test_setup_logging(LOG_INFO); if (getuid() != 0) log_warning("This program is unlikely to work for unprivileged users"); diff --git a/src/test/test-socket-util.c b/src/test/test-socket-util.c index 19c5395b92..df18a2a83c 100644 --- a/src/test/test-socket-util.c +++ b/src/test/test-socket-util.c @@ -17,6 +17,7 @@ #include "socket-util.h" #include "string-util.h" #include "util.h" +#include "tests.h" static void test_ifname_valid(void) { assert(ifname_valid("foo")); @@ -698,7 +699,7 @@ static void test_send_emptydata(void) { int main(int argc, char *argv[]) { - log_set_max_level(LOG_DEBUG); + test_setup_logging(LOG_DEBUG); test_ifname_valid(); diff --git a/src/test/test-specifier.c b/src/test/test-specifier.c index 9c7c352b51..a0ffdf6cb6 100644 --- a/src/test/test-specifier.c +++ b/src/test/test-specifier.c @@ -5,6 +5,7 @@ #include "specifier.h" #include "string-util.h" #include "strv.h" +#include "tests.h" static void test_specifier_escape_one(const char *a, const char *b) { _cleanup_free_ char *x = NULL; @@ -39,7 +40,7 @@ static void test_specifier_escape_strv(void) { } int main(int argc, char *argv[]) { - log_set_max_level(LOG_DEBUG); + test_setup_logging(LOG_DEBUG); test_specifier_escape(); test_specifier_escape_strv(); diff --git a/src/test/test-tmpfiles.c b/src/test/test-tmpfiles.c index 3817790233..7f288e54ce 100644 --- a/src/test/test-tmpfiles.c +++ b/src/test/test-tmpfiles.c @@ -13,6 +13,7 @@ #include "log.h" #include "process-util.h" #include "string-util.h" +#include "tests.h" #include "util.h" int main(int argc, char** argv) { @@ -21,8 +22,7 @@ int main(int argc, char** argv) { const char *p = argv[1] ?: "/tmp"; char *pattern; - log_set_max_level(LOG_DEBUG); - log_parse_environment(); + test_setup_logging(LOG_DEBUG); pattern = strjoina(p, "/systemd-test-XXXXXX"); diff --git a/src/test/test-umount.c b/src/test/test-umount.c index c068f7a0f0..1b243d03ef 100644 --- a/src/test/test-umount.c +++ b/src/test/test-umount.c @@ -53,9 +53,7 @@ static void test_swap_list(const char *fname) { } int main(int argc, char **argv) { - log_set_max_level(LOG_DEBUG); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); test_mount_points_list(NULL); test_mount_points_list("/test-umount/empty.mountinfo"); diff --git a/src/test/test-unit-file.c b/src/test/test-unit-file.c index 0b84d4c4ed..7132146827 100644 --- a/src/test/test-unit-file.c +++ b/src/test/test-unit-file.c @@ -896,8 +896,7 @@ int main(int argc, char *argv[]) { _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL; int r; - log_parse_environment(); - log_open(); + test_setup_logging(LOG_INFO); r = enter_cgroup_subroot(); if (r == -ENOMEDIUM) diff --git a/src/test/test-unit-name.c b/src/test/test-unit-name.c index 7fd3f82d4d..b1600db534 100644 --- a/src/test/test-unit-name.c +++ b/src/test/test-unit-name.c @@ -811,8 +811,7 @@ int main(int argc, char* argv[]) { _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL; int r, rc = 0; - log_parse_environment(); - log_open(); + test_setup_logging(LOG_INFO); r = enter_cgroup_subroot(); if (r == -ENOMEDIUM) diff --git a/src/test/test-watch-pid.c b/src/test/test-watch-pid.c index 615ded186b..03378ecf08 100644 --- a/src/test/test-watch-pid.c +++ b/src/test/test-watch-pid.c @@ -13,9 +13,7 @@ int main(int argc, char *argv[]) { Unit *a, *b, *c, *u; int r; - log_set_max_level(LOG_DEBUG); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); if (getuid() != 0) return log_tests_skipped("not root"); diff --git a/src/test/test-watchdog.c b/src/test/test-watchdog.c index d595ae27d5..ab66d5c49d 100644 --- a/src/test/test-watchdog.c +++ b/src/test/test-watchdog.c @@ -13,9 +13,7 @@ int main(int argc, char *argv[]) { int r; bool slow; - log_set_max_level(LOG_DEBUG); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); slow = slow_tests_enabled(); diff --git a/src/test/test-xattr-util.c b/src/test/test-xattr-util.c index 72720dccb8..2d93e65be2 100644 --- a/src/test/test-xattr-util.c +++ b/src/test/test-xattr-util.c @@ -12,6 +12,7 @@ #include "fs-util.h" #include "macro.h" #include "string-util.h" +#include "tests.h" #include "xattr-util.h" static void test_fgetxattrat_fake(void) { @@ -78,9 +79,7 @@ static void test_getcrtime(void) { } int main(void) { - log_set_max_level(LOG_DEBUG); - log_parse_environment(); - log_open(); + test_setup_logging(LOG_DEBUG); test_fgetxattrat_fake(); test_getcrtime(); diff --git a/src/timesync/test-timesync.c b/src/timesync/test-timesync.c index a045eeeec1..bd03a1dd90 100644 --- a/src/timesync/test-timesync.c +++ b/src/timesync/test-timesync.c @@ -5,6 +5,7 @@ #include "log.h" #include "macro.h" #include "timesyncd-conf.h" +#include "tests.h" static void test_manager_parse_string(void) { /* Make sure that NTP_SERVERS is configured to something @@ -25,8 +26,7 @@ static void test_manager_parse_string(void) { } int main(int argc, char **argv) { - log_set_max_level(LOG_DEBUG); - log_parse_environment(); + test_setup_logging(LOG_DEBUG); test_manager_parse_string(); From 730d989acc5cd913ffbf747570713c10f46516c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 13 Sep 2018 16:11:16 +0200 Subject: [PATCH 14/14] tests: add a helper function to skip with errno --- src/libsystemd/sd-bus/test-bus-gvariant.c | 15 +++++++-------- src/shared/tests.c | 6 ++++++ src/shared/tests.h | 1 + src/test/test-engine.c | 6 ++---- src/test/test-execute.c | 6 ++---- src/test/test-netlink-manual.c | 12 ++++-------- src/test/test-path.c | 6 ++---- src/test/test-sched-prio.c | 6 ++---- src/test/test-unit-file.c | 7 ++----- src/test/test-unit-name.c | 6 ++---- 10 files changed, 30 insertions(+), 41 deletions(-) diff --git a/src/libsystemd/sd-bus/test-bus-gvariant.c b/src/libsystemd/sd-bus/test-bus-gvariant.c index 648fea76e0..ae418efa8b 100644 --- a/src/libsystemd/sd-bus/test-bus-gvariant.c +++ b/src/libsystemd/sd-bus/test-bus-gvariant.c @@ -114,18 +114,16 @@ static void test_bus_gvariant_get_alignment(void) { assert_se(bus_gvariant_get_alignment("((t)(t))") == 8); } -static void test_marshal(void) { +static int test_marshal(void) { _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *n = NULL; _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; - _cleanup_free_ void *blob; + _cleanup_free_ void *blob = NULL; size_t sz; int r; r = sd_bus_open_user(&bus); - if (r < 0) { - log_info("Failed to connect to bus, skipping tests."); - exit(EXIT_TEST_SKIP); - } + if (r < 0) + return log_tests_skipped_errno(r, "Failed to connect to bus"); bus->message_version = 2; /* dirty hack to enable gvariant */ @@ -197,6 +195,8 @@ static void test_marshal(void) { assert_se(sd_bus_message_seal(m, 4712, 0) >= 0); assert_se(bus_message_dump(m, NULL, BUS_MESSAGE_DUMP_WITH_HEADER) >= 0); + + return EXIT_SUCCESS; } int main(int argc, char *argv[]) { @@ -205,7 +205,6 @@ int main(int argc, char *argv[]) { test_bus_gvariant_is_fixed_size(); test_bus_gvariant_get_size(); test_bus_gvariant_get_alignment(); - test_marshal(); - return 0; + return test_marshal(); } diff --git a/src/shared/tests.c b/src/shared/tests.c index 8d70c02802..10fe6a088c 100644 --- a/src/shared/tests.c +++ b/src/shared/tests.c @@ -102,3 +102,9 @@ int log_tests_skipped(const char *message) { program_invocation_short_name, message); return EXIT_TEST_SKIP; } + +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; +} diff --git a/src/shared/tests.h b/src/shared/tests.h index f35a526737..549959edf2 100644 --- a/src/shared/tests.h +++ b/src/shared/tests.h @@ -7,3 +7,4 @@ const char* get_catalog_dir(void); bool slow_tests_enabled(void); void test_setup_logging(int level); int log_tests_skipped(const char *message); +int log_tests_skipped_errno(int r, const char *message); diff --git a/src/test/test-engine.c b/src/test/test-engine.c index 0483c5b698..0673d36b62 100644 --- a/src/test/test-engine.c +++ b/src/test/test-engine.c @@ -28,10 +28,8 @@ int main(int argc, char *argv[]) { assert_se(set_unit_path(get_testdata_dir()) >= 0); assert_se(runtime_dir = setup_fake_runtime_dir()); r = manager_new(UNIT_FILE_USER, MANAGER_TEST_RUN_BASIC, &m); - if (MANAGER_SKIP_TEST(r)) { - log_notice_errno(r, "Skipping test: manager_new: %m"); - return EXIT_TEST_SKIP; - } + if (MANAGER_SKIP_TEST(r)) + return log_tests_skipped_errno(r, "manager_new"); assert_se(r >= 0); assert_se(manager_startup(m, NULL, NULL) >= 0); diff --git a/src/test/test-execute.c b/src/test/test-execute.c index 15967f76c2..c3ea5f6469 100644 --- a/src/test/test-execute.c +++ b/src/test/test-execute.c @@ -664,10 +664,8 @@ static int run_tests(UnitFileScope scope, const test_function_t *tests) { assert_se(tests); r = manager_new(scope, MANAGER_TEST_RUN_BASIC, &m); - if (MANAGER_SKIP_TEST(r)) { - log_notice_errno(r, "Skipping test: manager_new: %m"); - return EXIT_TEST_SKIP; - } + if (MANAGER_SKIP_TEST(r)) + return log_tests_skipped_errno(r, "manager_new"); assert_se(r >= 0); assert_se(manager_startup(m, NULL, NULL) >= 0); diff --git a/src/test/test-netlink-manual.c b/src/test/test-netlink-manual.c index a471456e0c..1ebe8d1972 100644 --- a/src/test/test-netlink-manual.c +++ b/src/test/test-netlink-manual.c @@ -47,16 +47,12 @@ static int test_tunnel_configure(sd_netlink *rtnl) { /* skip test if module cannot be loaded */ r = load_module("ipip"); - if (r < 0) { - log_info_errno(r, "Skipping tests: failed to load module 'ipip': %m"); - return EXIT_TEST_SKIP; - } + if (r < 0) + return log_tests_skipped_errno(r, "failed to load module 'ipip'"); r = load_module("sit"); - if (r < 0) { - log_info_errno(r, "Skipping tests: failed to load module 'sit': %m"); - return EXIT_TEST_SKIP; - } + if (r < 0) + return log_tests_skipped_errno(r, "failed to load module 'sit'"); if (getuid() != 0) return log_tests_skipped("not root"); diff --git a/src/test/test-path.c b/src/test/test-path.c index 025b3295a0..0b5537b364 100644 --- a/src/test/test-path.c +++ b/src/test/test-path.c @@ -37,10 +37,8 @@ static int setup_test(Manager **m) { return log_tests_skipped("cgroupfs not available"); r = manager_new(UNIT_FILE_USER, MANAGER_TEST_RUN_BASIC, &tmp); - if (MANAGER_SKIP_TEST(r)) { - log_notice_errno(r, "Skipping test: manager_new: %m"); - return EXIT_TEST_SKIP; - } + if (MANAGER_SKIP_TEST(r)) + return log_tests_skipped_errno(r, "manager_new"); assert_se(r >= 0); assert_se(manager_startup(tmp, NULL, NULL) >= 0); diff --git a/src/test/test-sched-prio.c b/src/test/test-sched-prio.c index f6ca192f07..1aa178182b 100644 --- a/src/test/test-sched-prio.c +++ b/src/test/test-sched-prio.c @@ -29,10 +29,8 @@ int main(int argc, char *argv[]) { assert_se(set_unit_path(get_testdata_dir()) >= 0); assert_se(runtime_dir = setup_fake_runtime_dir()); r = manager_new(UNIT_FILE_USER, MANAGER_TEST_RUN_BASIC, &m); - if (MANAGER_SKIP_TEST(r)) { - log_notice_errno(r, "Skipping test: manager_new: %m"); - return EXIT_TEST_SKIP; - } + if (MANAGER_SKIP_TEST(r)) + return log_tests_skipped_errno(r, "manager_new"); assert_se(r >= 0); assert_se(manager_startup(m, NULL, NULL) >= 0); diff --git a/src/test/test-unit-file.c b/src/test/test-unit-file.c index 7132146827..cb074aaa4d 100644 --- a/src/test/test-unit-file.c +++ b/src/test/test-unit-file.c @@ -39,11 +39,8 @@ static int test_unit_file_get_set(void) { assert_se(h); r = unit_file_get_list(UNIT_FILE_SYSTEM, NULL, h, NULL, NULL); - - if (IN_SET(r, -EPERM, -EACCES)) { - log_notice_errno(r, "Skipping test: unit_file_get_list: %m"); - return EXIT_TEST_SKIP; - } + if (IN_SET(r, -EPERM, -EACCES)) + return log_tests_skipped_errno(r, "unit_file_get_list"); log_full_errno(r == 0 ? LOG_INFO : LOG_ERR, r, "unit_file_get_list: %m"); diff --git a/src/test/test-unit-name.c b/src/test/test-unit-name.c index b1600db534..157c900276 100644 --- a/src/test/test-unit-name.c +++ b/src/test/test-unit-name.c @@ -205,10 +205,8 @@ static int test_unit_printf(void) { assert_se(get_shell(&shell) >= 0); r = manager_new(UNIT_FILE_USER, MANAGER_TEST_RUN_MINIMAL, &m); - if (MANAGER_SKIP_TEST(r)) { - log_notice_errno(r, "Skipping test: manager_new: %m"); - return EXIT_TEST_SKIP; - } + if (MANAGER_SKIP_TEST(r)) + return log_tests_skipped_errno(r, "manager_new"); assert_se(r == 0); #define expect(unit, pattern, expected) \