]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/test/test-path.c
Merge pull request #16145 from poettering/qrcode-dlopen
[thirdparty/systemd.git] / src / test / test-path.c
index 209eb2e3662cf748d1f2018ba8a3e7b69b51a71f..1075f31bc6c720c7b55ebdbee3217b4a63b9bf0f 100644 (file)
 #include "macro.h"
 #include "manager.h"
 #include "mkdir.h"
+#include "path-util.h"
 #include "rm-rf.h"
 #include "string-util.h"
 #include "strv.h"
-#include "test-helper.h"
 #include "tests.h"
 #include "unit.h"
 #include "util.h"
@@ -31,17 +31,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;
-        }
+        r = enter_cgroup_subroot(NULL);
+        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;
-        }
+        if (manager_errno_skip_test(r))
+                return log_tests_skipped_errno(r, "manager_new");
         assert_se(r >= 0);
         assert_se(manager_startup(tmp, NULL, NULL) >= 0);
 
@@ -65,148 +61,244 @@ static void shutdown_test(Manager *m) {
         manager_free(m);
 }
 
-static void check_stop_unlink(Manager *m, Unit *unit, const char *test_path, const char *service_name) {
+static Service *service_for_path(Manager *m, Path *path, const char *service_name) {
         _cleanup_free_ char *tmp = NULL;
         Unit *service_unit = NULL;
-        Service *service = NULL;
-        usec_t ts;
-        usec_t timeout = 2 * USEC_PER_SEC;
 
         assert_se(m);
-        assert_se(unit);
-        assert_se(test_path);
+        assert_se(path);
 
         if (!service_name) {
-                assert_se(tmp = strreplace(unit->id, ".path", ".service"));
+                assert_se(tmp = strreplace(UNIT(path)->id, ".path", ".service"));
                 service_unit = manager_get_unit(m, tmp);
         } else
                 service_unit = manager_get_unit(m, service_name);
         assert_se(service_unit);
-        service = SERVICE(service_unit);
 
-        ts = now(CLOCK_MONOTONIC);
-        /* We process events until the service related to the path has been successfully started */
-        while (service->result != SERVICE_SUCCESS || service->state != SERVICE_START) {
-                usec_t n;
-                int r;
+        return SERVICE(service_unit);
+}
+
+static void check_states(Manager *m, Path *path, Service *service, PathState path_state, ServiceState service_state) {
+        assert_se(m);
+        assert_se(service);
 
-                r = sd_event_run(m->event, 100 * USEC_PER_MSEC);
-                assert_se(r >= 0);
+        usec_t end = now(CLOCK_MONOTONIC) + 30 * USEC_PER_SEC;
+
+        while (path->result != PATH_SUCCESS || service->result != SERVICE_SUCCESS ||
+               path->state != path_state || service->state != service_state) {
+
+                assert_se(sd_event_run(m->event, 100 * USEC_PER_MSEC) >= 0);
 
                 printf("%s: state = %s; result = %s \n",
-                                service_unit->id,
+                                UNIT(path)->id,
+                                path_state_to_string(path->state),
+                                path_result_to_string(path->result));
+                printf("%s: state = %s; result = %s \n",
+                                UNIT(service)->id,
                                 service_state_to_string(service->state),
                                 service_result_to_string(service->result));
 
-                /* But we timeout if the service has not been started in the allocated time */
-                n = now(CLOCK_MONOTONIC);
-                if (ts + timeout < n) {
-                        log_error("Test timeout when testing %s", unit->id);
+                if (now(CLOCK_MONOTONIC) >= end) {
+                        log_error("Test timeout when testing %s", UNIT(path)->id);
                         exit(EXIT_FAILURE);
                 }
         }
-
-        assert_se(UNIT_VTABLE(unit)->stop(unit) >= 0);
-        (void) rm_rf(test_path, REMOVE_ROOT|REMOVE_PHYSICAL);
 }
 
 static void test_path_exists(Manager *m) {
         const char *test_path = "/tmp/test-path_exists";
         Unit *unit = NULL;
+        Path *path = NULL;
+        Service *service = NULL;
 
         assert_se(m);
 
         assert_se(manager_load_startable_unit_or_warn(m, "path-exists.path", NULL, &unit) >= 0);
-        assert_se(UNIT_VTABLE(unit)->start(unit) >= 0);
+
+        path = PATH(unit);
+        service = service_for_path(m, path, NULL);
+
+        assert_se(unit_start(unit) >= 0);
+        check_states(m, path, service, PATH_WAITING, SERVICE_DEAD);
 
         assert_se(touch(test_path) >= 0);
+        check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING);
+
+        /* Service restarts if file still exists */
+        assert_se(unit_stop(UNIT(service)) >= 0);
+        check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING);
 
-        check_stop_unlink(m, unit, test_path, NULL);
+        assert_se(rm_rf(test_path, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
+        assert_se(unit_stop(UNIT(service)) >= 0);
+        check_states(m, path, service, PATH_WAITING, SERVICE_DEAD);
+
+        assert_se(unit_stop(unit) >= 0);
 }
 
 static void test_path_existsglob(Manager *m) {
         const char *test_path = "/tmp/test-path_existsglobFOOBAR";
         Unit *unit = NULL;
+        Path *path = NULL;
+        Service *service = NULL;
 
         assert_se(m);
+
         assert_se(manager_load_startable_unit_or_warn(m, "path-existsglob.path", NULL, &unit) >= 0);
-        assert_se(UNIT_VTABLE(unit)->start(unit) >= 0);
+
+        path = PATH(unit);
+        service = service_for_path(m, path, NULL);
+
+        assert_se(unit_start(unit) >= 0);
+        check_states(m, path, service, PATH_WAITING, SERVICE_DEAD);
 
         assert_se(touch(test_path) >= 0);
+        check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING);
 
-        check_stop_unlink(m, unit, test_path, NULL);
+        /* Service restarts if file still exists */
+        assert_se(unit_stop(UNIT(service)) >= 0);
+        check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING);
+
+        assert_se(rm_rf(test_path, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
+        assert_se(unit_stop(UNIT(service)) >= 0);
+        check_states(m, path, service, PATH_WAITING, SERVICE_DEAD);
+
+        assert_se(unit_stop(unit) >= 0);
 }
 
 static void test_path_changed(Manager *m) {
         const char *test_path = "/tmp/test-path_changed";
         FILE *f;
         Unit *unit = NULL;
+        Path *path = NULL;
+        Service *service = NULL;
 
         assert_se(m);
 
+        assert_se(manager_load_startable_unit_or_warn(m, "path-changed.path", NULL, &unit) >= 0);
+
+        path = PATH(unit);
+        service = service_for_path(m, path, NULL);
+
+        assert_se(unit_start(unit) >= 0);
+        check_states(m, path, service, PATH_WAITING, SERVICE_DEAD);
+
         assert_se(touch(test_path) >= 0);
+        check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING);
 
-        assert_se(manager_load_startable_unit_or_warn(m, "path-changed.path", NULL, &unit) >= 0);
-        assert_se(UNIT_VTABLE(unit)->start(unit) >= 0);
+        /* Service does not restart if file still exists */
+        assert_se(unit_stop(UNIT(service)) >= 0);
+        check_states(m, path, service, PATH_WAITING, SERVICE_DEAD);
 
         f = fopen(test_path, "w");
         assert_se(f);
         fclose(f);
 
-        check_stop_unlink(m, unit, test_path, NULL);
+        check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING);
+
+        assert_se(unit_stop(UNIT(service)) >= 0);
+        check_states(m, path, service, PATH_WAITING, SERVICE_DEAD);
+
+        (void) rm_rf(test_path, REMOVE_ROOT|REMOVE_PHYSICAL);
+        assert_se(unit_stop(unit) >= 0);
 }
 
 static void test_path_modified(Manager *m) {
         _cleanup_fclose_ FILE *f = NULL;
         const char *test_path = "/tmp/test-path_modified";
         Unit *unit = NULL;
+        Path *path = NULL;
+        Service *service = NULL;
 
         assert_se(m);
 
+        assert_se(manager_load_startable_unit_or_warn(m, "path-modified.path", NULL, &unit) >= 0);
+
+        path = PATH(unit);
+        service = service_for_path(m, path, NULL);
+
+        assert_se(unit_start(unit) >= 0);
+        check_states(m, path, service, PATH_WAITING, SERVICE_DEAD);
+
         assert_se(touch(test_path) >= 0);
+        check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING);
 
-        assert_se(manager_load_startable_unit_or_warn(m, "path-modified.path", NULL, &unit) >= 0);
-        assert_se(UNIT_VTABLE(unit)->start(unit) >= 0);
+        /* Service does not restart if file still exists */
+        assert_se(unit_stop(UNIT(service)) >= 0);
+        check_states(m, path, service, PATH_WAITING, SERVICE_DEAD);
 
         f = fopen(test_path, "w");
         assert_se(f);
         fputs("test", f);
 
-        check_stop_unlink(m, unit, test_path, NULL);
+        check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING);
+
+        assert_se(unit_stop(UNIT(service)) >= 0);
+        check_states(m, path, service, PATH_WAITING, SERVICE_DEAD);
+
+        (void) rm_rf(test_path, REMOVE_ROOT|REMOVE_PHYSICAL);
+        assert_se(unit_stop(unit) >= 0);
 }
 
 static void test_path_unit(Manager *m) {
         const char *test_path = "/tmp/test-path_unit";
         Unit *unit = NULL;
+        Path *path = NULL;
+        Service *service = NULL;
 
         assert_se(m);
 
         assert_se(manager_load_startable_unit_or_warn(m, "path-unit.path", NULL, &unit) >= 0);
-        assert_se(UNIT_VTABLE(unit)->start(unit) >= 0);
+
+        path = PATH(unit);
+        service = service_for_path(m, path, "path-mycustomunit.service");
+
+        assert_se(unit_start(unit) >= 0);
+        check_states(m, path, service, PATH_WAITING, SERVICE_DEAD);
 
         assert_se(touch(test_path) >= 0);
+        check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING);
 
-        check_stop_unlink(m, unit, test_path, "path-mycustomunit.service");
+        assert_se(rm_rf(test_path, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
+        assert_se(unit_stop(UNIT(service)) >= 0);
+        check_states(m, path, service, PATH_WAITING, SERVICE_DEAD);
+
+        assert_se(unit_stop(unit) >= 0);
 }
 
 static void test_path_directorynotempty(Manager *m) {
         const char *test_path = "/tmp/test-path_directorynotempty/";
         Unit *unit = NULL;
+        Path *path = NULL;
+        Service *service = NULL;
 
         assert_se(m);
 
+        assert_se(manager_load_startable_unit_or_warn(m, "path-directorynotempty.path", NULL, &unit) >= 0);
+
+        path = PATH(unit);
+        service = service_for_path(m, path, NULL);
+
         assert_se(access(test_path, F_OK) < 0);
 
-        assert_se(manager_load_startable_unit_or_warn(m, "path-directorynotempty.path", NULL, &unit) >= 0);
-        assert_se(UNIT_VTABLE(unit)->start(unit) >= 0);
+        assert_se(unit_start(unit) >= 0);
+        check_states(m, path, service, PATH_WAITING, SERVICE_DEAD);
 
         /* MakeDirectory default to no */
         assert_se(access(test_path, F_OK) < 0);
 
         assert_se(mkdir_p(test_path, 0755) >= 0);
         assert_se(touch(strjoina(test_path, "test_file")) >= 0);
+        check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING);
+
+        /* Service restarts if directory is still not empty */
+        assert_se(unit_stop(UNIT(service)) >= 0);
+        check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING);
+
+        assert_se(rm_rf(test_path, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
+        assert_se(unit_stop(UNIT(service)) >= 0);
+        check_states(m, path, service, PATH_WAITING, SERVICE_DEAD);
 
-        check_stop_unlink(m, unit, test_path, NULL);
+        assert_se(unit_stop(unit) >= 0);
 }
 
 static void test_path_makedirectory_directorymode(Manager *m) {
@@ -216,10 +308,11 @@ static void test_path_makedirectory_directorymode(Manager *m) {
 
         assert_se(m);
 
+        assert_se(manager_load_startable_unit_or_warn(m, "path-makedirectory.path", NULL, &unit) >= 0);
+
         assert_se(access(test_path, F_OK) < 0);
 
-        assert_se(manager_load_startable_unit_or_warn(m, "path-makedirectory.path", NULL, &unit) >= 0);
-        assert_se(UNIT_VTABLE(unit)->start(unit) >= 0);
+        assert_se(unit_start(unit) >= 0);
 
         /* Check if the directory has been created */
         assert_se(access(test_path, F_OK) >= 0);
@@ -230,7 +323,7 @@ static void test_path_makedirectory_directorymode(Manager *m) {
         assert_se((s.st_mode & S_IRWXG) == 0040);
         assert_se((s.st_mode & S_IRWXO) == 0004);
 
-        assert_se(UNIT_VTABLE(unit)->stop(unit) >= 0);
+        assert_se(unit_stop(unit) >= 0);
         (void) rm_rf(test_path, REMOVE_ROOT|REMOVE_PHYSICAL);
 }
 
@@ -246,25 +339,25 @@ int main(int argc, char *argv[]) {
                 NULL,
         };
 
+        _cleanup_free_ char *test_path = NULL;
         _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL;
-        const test_function_t *test = NULL;
-        Manager *m = NULL;
 
         umask(022);
 
-        log_parse_environment();
-        log_open();
+        test_setup_logging(LOG_INFO);
 
-        assert_se(set_unit_path(get_testdata_dir("/test-path")) >= 0);
+        assert_se(get_testdata_dir("test-path", &test_path) >= 0);
+        assert_se(set_unit_path(test_path) >= 0);
         assert_se(runtime_dir = setup_fake_runtime_dir());
 
-        for (test = tests; test && *test; test++) {
+        for (const test_function_t *test = tests; test && *test; test++) {
+                Manager *m = NULL;
                 int r;
 
                 /* We create a clean environment for each test */
                 r = setup_test(&m);
-                if (r < 0)
-                        return -r;
+                if (r != 0)
+                        return r;
 
                 (*test)(m);