]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: allow specifying test threads/iterations/timeouts via cmdline
authorLennart Poettering <lennart@poettering.net>
Wed, 6 Apr 2022 09:49:08 +0000 (11:49 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 7 Apr 2022 16:55:59 +0000 (18:55 +0200)
That's useful when running tests manually.

src/test/test-loop-block.c

index 54f5d28da7a5e2f3153671519803d3cf095f160f..35a981bcce383662cd5b0298cea4b03244f6b083 100644 (file)
 #include "fileio.h"
 #include "fs-util.h"
 #include "gpt.h"
+#include "main-func.h"
 #include "missing_loop.h"
 #include "mkfs-util.h"
 #include "mount-util.h"
 #include "namespace-util.h"
+#include "parse-util.h"
 #include "string-util.h"
 #include "strv.h"
 #include "tests.h"
@@ -21,8 +23,9 @@
 #include "user-util.h"
 #include "virt.h"
 
-#define N_THREADS 5
-#define N_ITERATIONS 3
+static unsigned arg_n_threads = 5;
+static unsigned arg_n_iterations = 3;
+static usec_t arg_timeout = 0;
 
 static usec_t end = 0;
 
@@ -30,7 +33,7 @@ static void* thread_func(void *ptr) {
         int fd = PTR_TO_FD(ptr);
         int r;
 
-        for (unsigned i = 0; i < N_ITERATIONS; i++) {
+        for (unsigned i = 0; i < arg_n_iterations; i++) {
                 _cleanup_(loop_device_unrefp) LoopDevice *loop = NULL;
                 _cleanup_(umount_and_rmdir_and_freep) char *mounted = NULL;
                 _cleanup_(dissected_image_unrefp) DissectedImage *dissected = NULL;
@@ -106,20 +109,47 @@ static bool have_root_gpt_type(void) {
 #endif
 }
 
-int main(int argc, char *argv[]) {
+static int run(int argc, char *argv[]) {
         _cleanup_free_ char *p = NULL, *cmd = NULL;
         _cleanup_(pclosep) FILE *sfdisk = NULL;
         _cleanup_(loop_device_unrefp) LoopDevice *loop = NULL;
         _cleanup_close_ int fd = -1;
         _cleanup_(dissected_image_unrefp) DissectedImage *dissected = NULL;
         _cleanup_(umount_and_rmdir_and_freep) char *mounted = NULL;
-        pthread_t threads[N_THREADS];
         sd_id128_t id;
         int r;
 
         test_setup_logging(LOG_DEBUG);
         log_show_tid(true);
         log_show_time(true);
+        log_show_color(true);
+
+        if (argc >= 2) {
+                r = safe_atou(argv[1], &arg_n_threads);
+                if (r < 0)
+                        return log_error_errno(r, "Failed to parse first argument (number of threads): %s", argv[1]);
+                if (arg_n_threads <= 0)
+                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Number of threads must be at least 1, refusing.");
+        }
+
+        if (argc >= 3) {
+                r = safe_atou(argv[2], &arg_n_iterations);
+                if (r < 0)
+                        return log_error_errno(r, "Failed to parse second argument (number of iterations): %s", argv[2]);
+                if (arg_n_iterations <= 0)
+                        return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Number of iterations must be at least 1, refusing.");
+        }
+
+        if (argc >= 4) {
+                r = parse_sec(argv[3], &arg_timeout);
+                if (r < 0)
+                        return log_error_errno(r, "Failed to parse third argument (timeout): %s", argv[3]);
+        }
+
+        if (argc >= 5)
+                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Too many arguments (expected 3 at max).");
+
+        pthread_t threads[arg_n_threads];
 
         if (!have_root_gpt_type()) {
                 log_tests_skipped("No root partition GPT defined for this architecture, exiting.");
@@ -223,23 +253,22 @@ int main(int argc, char *argv[]) {
 
         log_notice("Threads are being started now");
 
-        /* Let's make sure we run for 10s on slow systems at max */
-        end = usec_add(now(CLOCK_MONOTONIC),
-                       slow_tests_enabled() ? 5 * USEC_PER_SEC :
-                       1 * USEC_PER_SEC);
+        /* zero timeout means pick default: let's make sure we run for 10s on slow systems at max */
+        if (arg_timeout == 0)
+                arg_timeout = slow_tests_enabled() ? 5 * USEC_PER_SEC : 1 * USEC_PER_SEC;
 
-        assert_cc(N_THREADS > 0);
+        end = usec_add(now(CLOCK_MONOTONIC), arg_timeout);
 
-        if (N_THREADS > 1)
-                for (unsigned i = 0; i < N_THREADS; i++)
+        if (arg_n_threads > 1)
+                for (unsigned i = 0; i < arg_n_threads; i++)
                         assert_se(pthread_create(threads + i, NULL, thread_func, FD_TO_PTR(fd)) == 0);
 
         log_notice("All threads started now.");
 
-        if (N_THREADS == 1)
+        if (arg_n_threads == 1)
                 assert_se(thread_func(FD_TO_PTR(fd)) == NULL);
         else
-                for (unsigned i = 0; i < N_THREADS; i++) {
+                for (unsigned i = 0; i < arg_n_threads; i++) {
                         log_notice("Joining thread #%u.", i);
 
                         void *k;
@@ -253,3 +282,5 @@ int main(int argc, char *argv[]) {
 
         return 0;
 }
+
+DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);