diff --git a/tests/test-instance.c b/tests/test-instance.c index 70b90d2f..c73369d7 100644 --- a/tests/test-instance.c +++ b/tests/test-instance.c @@ -30,6 +30,7 @@ #include "flatpak.h" #include "flatpak-instance-private.h" +#include "flatpak-run-private.h" #include "libglnx/libglnx.h" @@ -41,29 +42,64 @@ test_gc (void) g_autoptr(GBytes) bytes = NULL; g_autoptr(GError) error = NULL; g_autoptr(GPtrArray) instances = NULL; - g_autofree char *base_dir = flatpak_instance_get_instances_directory (); - g_autofree char *alive_dir = g_build_filename (base_dir, "1", NULL); - g_autofree char *alive_lock = g_build_filename (alive_dir, ".ref", NULL); - g_autofree char *dead_dir = g_build_filename (base_dir, "2", NULL); - g_autofree char *dead_lock = g_build_filename (dead_dir, ".ref", NULL); + g_autofree char *instances_dir = flatpak_instance_get_instances_directory (); g_autofree char *hold_lock = g_test_build_filename (G_TEST_BUILT, "hold-lock", NULL); + g_autofree char *alive_instance_dir = NULL; + g_autofree char *alive_instance_info = NULL; + g_autofree char *alive_instance_lock = NULL; + g_autofree char *alive_dead_instance_dir = NULL; + g_autofree char *alive_dead_instance_info = NULL; + g_autofree char *alive_dead_instance_lock = NULL; + g_autofree char *dead_instance_dir = NULL; + g_autofree char *dead_instance_info = NULL; + g_autofree char *dead_instance_lock = NULL; struct utimbuf a_while_ago = {}; - const char *hold_lock_argv[] = { "hold-lock", "--lock-file", ".ref", NULL }; + const char *hold_lock_argv[] = + { + "/hold-lock", + "--lock-file", + "/.ref", + NULL + }; GPid pid = -1; int stdout_fd = -1; int wstatus = 0; FlatpakInstance *instance; struct stat stat_buf; - g_assert_no_errno (g_mkdir_with_parents (alive_dir, 0700)); - g_assert_no_errno (g_mkdir_with_parents (dead_dir, 0700)); - g_file_set_contents (alive_lock, "", 0, &error); + /* com.example.Alive has one instance, #1, running. + * A second instance, #2, was running until recently but has exited. */ + + alive_instance_dir = g_build_filename (instances_dir, "1", NULL); + g_assert_no_errno (g_mkdir_with_parents (alive_instance_dir, 0700)); + alive_instance_info = g_build_filename (alive_instance_dir, "info", NULL); + g_file_set_contents (alive_instance_info, + "[" FLATPAK_METADATA_GROUP_APPLICATION "]\n" + FLATPAK_METADATA_KEY_NAME "=com.example.Alive\n", + -1, &error); g_assert_no_error (error); - g_file_set_contents (dead_lock, "", 0, &error); + alive_instance_lock = g_build_filename (alive_instance_dir, ".ref", NULL); + g_file_set_contents (alive_instance_lock, "", 0, &error); g_assert_no_error (error); + alive_dead_instance_dir = g_build_filename (instances_dir, "2", NULL); + g_assert_no_errno (g_mkdir_with_parents (alive_dead_instance_dir, 0700)); + alive_dead_instance_info = g_build_filename (alive_dead_instance_dir, "info", NULL); + g_file_set_contents (alive_dead_instance_info, + "[" FLATPAK_METADATA_GROUP_APPLICATION "]\n" + FLATPAK_METADATA_KEY_NAME "=com.example.Alive\n", + -1, &error); + g_assert_no_error (error); + alive_dead_instance_lock = g_build_filename (alive_dead_instance_dir, ".ref", NULL); + g_file_set_contents (alive_dead_instance_lock, "", 0, &error); + g_assert_no_error (error); + + /* This represents the running instance #1. We have to do this + * out-of-process because the locks we use are process-oriented, + * so the locks we take during GC would not conflict with locks held + * by our own process. */ hold_lock_argv[0] = hold_lock; - hold_lock_argv[2] = alive_lock; + hold_lock_argv[2] = alive_instance_lock; g_spawn_async_with_pipes (NULL, (gchar **) hold_lock_argv, NULL, @@ -79,20 +115,38 @@ test_gc (void) g_assert_cmpint (pid, >, 1); g_assert_cmpint (stdout_fd, >=, 0); + /* com.example.Dead has no instances running. + * Instance #4 was running until recently but has exited. */ + + dead_instance_dir = g_build_filename (instances_dir, "4", NULL); + g_assert_no_errno (g_mkdir_with_parents (dead_instance_dir, 0700)); + dead_instance_info = g_build_filename (dead_instance_dir, "info", NULL); + g_file_set_contents (dead_instance_info, + "[" FLATPAK_METADATA_GROUP_APPLICATION "]\n" + FLATPAK_METADATA_KEY_NAME "=com.example.Dead\n", + -1, &error); + g_assert_no_error (error); + dead_instance_lock = g_build_filename (dead_instance_dir, ".ref", NULL); + g_file_set_contents (dead_instance_lock, "", 0, &error); + g_assert_no_error (error); + /* Wait for the child to be ready */ bytes = glnx_fd_readall_bytes (stdout_fd, NULL, &error); g_assert_no_error (error); /* Pretend the locks were created in early 1970, to bypass the workaround * for a race */ - g_assert_no_errno (g_utime (alive_lock, &a_while_ago)); - g_assert_no_errno (g_utime (dead_lock, &a_while_ago)); + g_assert_no_errno (g_utime (alive_instance_lock, &a_while_ago)); + g_assert_no_errno (g_utime (alive_dead_instance_lock, &a_while_ago)); + g_assert_no_errno (g_utime (dead_instance_lock, &a_while_ago)); /* This has the side-effect of GC'ing instances */ instances = flatpak_instance_get_all (); - g_assert_no_errno (stat (alive_dir, &stat_buf)); - g_assert_cmpint (stat (dead_dir, &stat_buf) == 0 ? 0 : errno, ==, ENOENT); + /* We GC exactly those instances that are no longer running */ + g_assert_no_errno (stat (alive_instance_dir, &stat_buf)); + g_assert_cmpint (stat (alive_dead_instance_dir, &stat_buf) == 0 ? 0 : errno, ==, ENOENT); + g_assert_cmpint (stat (dead_instance_dir, &stat_buf) == 0 ? 0 : errno, ==, ENOENT); g_assert_cmpuint (instances->len, ==, 1); instance = g_ptr_array_index (instances, 0);