]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: make unit tests relocatable
authorMartin Pitt <martin@piware.de>
Sun, 12 Feb 2017 22:14:43 +0000 (23:14 +0100)
committerMartin Pitt <martin@piware.de>
Mon, 13 Feb 2017 21:31:13 +0000 (22:31 +0100)
It is useful to package test-* binaries and run them as root under
autopkgtest or manually on particular machines. They currently have a
built-in hardcoded absolute path to their test data, which does not work
when running the test programs from any other path than the original
build directory.

By default, make the tests look for their data in
<test_exe_directory>/testdata/ so that they can be called from any
directory (provided that the corresponding test data is installed
correctly). As we don't have a fixed static path in the build tree (as
build and source tree are independent), set $TEST_DIR with "make check"
to point to <srcdir>/test/, as we previously did with an automake
variable.

Makefile.am
src/resolve/test-dns-packet.c
src/shared/tests.c
src/shared/tests.h
src/test/test-cgroup-mask.c
src/test/test-engine.c
src/test/test-execute.c
src/test/test-helper.h
src/test/test-path.c
src/test/test-sched-prio.c

index 3c20c9d4bfe1c3ad2feab791414b51317b0f2361..489cf3c1b73114b199efaa63668df4a9b7117d10 100644 (file)
@@ -150,6 +150,7 @@ endif
 AM_TESTS_ENVIRONMENT = \
        export SYSTEMD_KBD_MODEL_MAP=$(abs_top_srcdir)/src/locale/kbd-model-map; \
        export SYSTEMD_LANGUAGE_FALLBACK_MAP=$(abs_top_srcdir)/src/locale/language-fallback-map; \
+       export TEST_DIR=$(abs_top_srcdir)/test; \
        export PATH=$(abs_top_builddir):$$PATH;
 
 if ENABLE_BASH_COMPLETION
@@ -218,7 +219,6 @@ AM_CPPFLAGS = \
        -DLIBDIR=\"$(libdir)\" \
        -DROOTLIBDIR=\"$(rootlibdir)\" \
        -DROOTLIBEXECDIR=\"$(rootlibexecdir)\" \
-       -DTEST_DIR=\"$(abs_top_srcdir)/test\" \
        -I $(top_srcdir)/src \
        -I $(top_builddir)/src/basic \
        -I $(top_srcdir)/src/basic \
@@ -5637,7 +5637,8 @@ test_dns_packet_SOURCES = \
        $(basic_dns_sources)
 
 test_dns_packet_CPPFLAGS = \
-       $(AM_CPPFLAGS)
+       $(AM_CPPFLAGS) \
+       -I $(top_srcdir)/src/test
 
 test_dns_packet_CFLAGS = \
        $(AM_CFLAGS) \
index 957b8677bad562bfae04f868dc1f20097820e022..3ca7e7849560a3191efa62ddec3a45b2515a3b57 100644 (file)
@@ -31,6 +31,8 @@
 #include "strv.h"
 #include "unaligned.h"
 
+#include "test-helper.h"
+
 #define HASH_KEY SD_ID128_MAKE(d3,1e,48,90,4b,fa,4c,fe,af,9d,d5,a1,d7,2e,8a,b1)
 
 static void verify_rr_copy(DnsResourceRecord *rr) {
@@ -115,7 +117,7 @@ int main(int argc, char **argv) {
                 N = argc - 1;
                 fnames = argv + 1;
         } else {
-                assert_se(glob(TEST_DIR "/test-resolve/*.pkts", GLOB_NOSORT, NULL, &g) == 0);
+                assert_se(glob(TEST_DATA_DIR("/test-resolve/*.pkts"), GLOB_NOSORT, NULL, &g) == 0);
                 N = g.gl_pathc;
                 fnames = g.gl_pathv;
         }
index 409116290dea12d48f29d0b68ffe88c532c3e0b3..70346877251c99adbd5e218ce206b0650cf1ccde 100644 (file)
@@ -17,6 +17,9 @@
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
+#include <alloc-util.h>
+#include <fs-util.h>
+#include <libgen.h>
 #include <stdlib.h>
 #include <util.h>
 
@@ -31,3 +34,13 @@ char* setup_fake_runtime_dir(void) {
 
         return p;
 }
+
+const char* get_exe_relative_testdata_dir(void) {
+        _cleanup_free_ char *exedir = NULL;
+        /* convenience: caller does not need to free result */
+        static char testdir[PATH_MAX];
+
+        assert_se(readlink_and_make_absolute("/proc/self/exe", &exedir) >= 0);
+        assert_se(snprintf(testdir, sizeof(testdir), "%s/testdata", dirname(exedir)) > 0);
+        return testdir;
+}
index 93f09013a1a93ce19ad106e99b2e92bb7825ece8..0100b48937458d5adb6c41250e7f901b5b55f787 100644 (file)
@@ -20,3 +20,4 @@
 ***/
 
 char* setup_fake_runtime_dir(void);
+const char* get_exe_relative_testdata_dir(void);
index a027eb0fd2a20257a3c32fd0a60161431ff22ee9..630587aaf18391c0da5a881618bf90103c660fdb 100644 (file)
@@ -34,7 +34,7 @@ static int test_cgroup_mask(void) {
         int r;
 
         /* Prepare the manager. */
-        assert_se(set_unit_path(TEST_DIR) >= 0);
+        assert_se(set_unit_path(TEST_DATA_DIR("")) >= 0);
         r = manager_new(UNIT_FILE_USER, true, &m);
         if (r == -EPERM || r == -EACCES) {
                 puts("manager_new: Permission denied. Skipping test.");
index a651f6b6831fe497d50b4ac98023ebdeddab79ee..3ec2dfe10ef8234f50e80ad887b2613533ad253c 100644 (file)
@@ -40,7 +40,7 @@ int main(int argc, char *argv[]) {
         assert_se(runtime_dir = setup_fake_runtime_dir());
 
         /* prepare the test */
-        assert_se(set_unit_path(TEST_DIR) >= 0);
+        assert_se(set_unit_path(TEST_DATA_DIR("")) >= 0);
         r = manager_new(UNIT_FILE_USER, true, &m);
         if (MANAGER_SKIP_TEST(r)) {
                 log_notice_errno(r, "Skipping test: manager_new: %m");
index 3a3ab3eb94975764eae6244bd50e2b9773d7f603..145aa37a668606101aa5356211ed9fa1464a6fda 100644 (file)
@@ -516,7 +516,7 @@ int main(int argc, char *argv[]) {
         }
 
         assert_se(setenv("XDG_RUNTIME_DIR", "/tmp/", 1) == 0);
-        assert_se(set_unit_path(TEST_DIR "/test-execute/") >= 0);
+        assert_se(set_unit_path(TEST_DATA_DIR("/test-execute/")) >= 0);
 
         /* Unset VAR1, VAR2 and VAR3 which are used in the PassEnvironment test
          * cases, otherwise (and if they are present in the environment),
index ddb10f88fd1956010a6cc411b89f9073d6d3bf77..4e633ae6d81d17ba7c438aa2b5a93262010627dd 100644 (file)
@@ -20,6 +20,8 @@
 ***/
 
 #include "sd-daemon.h"
+#include "string-util.h"
+#include "tests.h"
 
 #include "macro.h"
 
@@ -39,3 +41,6 @@
                -ENOENT,                                         \
                -ENOMEDIUM /* cannot determine cgroup */         \
                )
+
+#define TEST_DATA_DIR(subdir)                                     \
+        strjoina(getenv("TEST_DIR") ?: get_exe_relative_testdata_dir(), subdir)
index 5e99d478eec6a0cb4af49547d1e087ed3e276a12..d09df3e8a5fe6af87270c8274608b4701201a453 100644 (file)
@@ -263,7 +263,7 @@ int main(int argc, char *argv[]) {
         log_open();
 
         assert_se(runtime_dir = setup_fake_runtime_dir());
-        assert_se(set_unit_path(TEST_DIR "/test-path/") >= 0);
+        assert_se(set_unit_path(TEST_DATA_DIR("/test-path/")) >= 0);
 
         for (test = tests; test && *test; test++) {
                 int r;
index 7b37910c3316e09e4efc8dcfd808763b1b0c33c4..dab64fea575fb0b658b6ae2519dc12ebe1dda05e 100644 (file)
@@ -37,7 +37,7 @@ int main(int argc, char *argv[]) {
         assert_se(runtime_dir = setup_fake_runtime_dir());
 
         /* prepare the test */
-        assert_se(set_unit_path(TEST_DIR) >= 0);
+        assert_se(set_unit_path(TEST_DATA_DIR("")) >= 0);
         r = manager_new(UNIT_FILE_USER, true, &m);
         if (MANAGER_SKIP_TEST(r)) {
                 log_notice_errno(r, "Skipping test: manager_new: %m");