]> git.ipfire.org Git - people/stevee/pakfire.git/commitdiff
tests: jail: Add test for memory limit
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 9 Aug 2022 16:22:44 +0000 (16:22 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 9 Aug 2022 16:22:44 +0000 (16:22 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
tests/libpakfire/jail.c

index 6cd15fe1261222de6ce1a064d4d578e4a56ec3d6..b701a6346f8c70126c33cc94a8b89af9eddbe99c 100644 (file)
@@ -159,12 +159,51 @@ FAIL:
        return r;
 }
 
+static int test_memory_limit(const struct test* t) {
+       struct pakfire_cgroup* cgroup = NULL;
+       struct pakfire_jail* jail = NULL;
+       int r = EXIT_FAILURE;
+
+       const char* argv[] = {
+               "/command", "exhaust-memory", NULL,
+       };
+
+       // Create cgroup
+       ASSERT_SUCCESS(pakfire_cgroup_open(&cgroup, t->pakfire, "pakfire-test", 0));
+
+       // Create jail
+       ASSERT_SUCCESS(pakfire_jail_create(&jail, t->pakfire, 0));
+
+       // Connect jail to the cgroup
+       ASSERT_SUCCESS(pakfire_jail_set_cgroup(jail, cgroup));
+
+       // Set a memory limit of 100 MiB
+       ASSERT_SUCCESS(pakfire_cgroup_set_memory_limit(cgroup, 100 * 1024 * 1024));
+
+       // Try to exhaust all memory
+       ASSERT_FAILURE(pakfire_jail_exec(jail, argv, NULL));
+
+       // Success
+       r = EXIT_SUCCESS;
+
+FAIL:
+       if (jail)
+               pakfire_jail_unref(jail);
+       if (cgroup) {
+               pakfire_cgroup_destroy(cgroup);
+               pakfire_cgroup_unref(cgroup);
+       }
+
+       return r;
+}
+
 int main(int argc, const char* argv[]) {
        testsuite_add_test(test_create);
        testsuite_add_test(test_env);
        testsuite_add_test(test_exec);
        testsuite_add_test(test_launch_into_cgroup);
        testsuite_add_test(test_nice);
+       testsuite_add_test(test_memory_limit);
 
        return testsuite_run(argc, argv);
 }