]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virshtest: Add support for testing commands read from input file and adapt alias...
authorPeter Krempa <pkrempa@redhat.com>
Wed, 20 Mar 2024 08:48:32 +0000 (09:48 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Tue, 2 Apr 2024 12:24:30 +0000 (14:24 +0200)
Add support for reading a file and passing it to virsh in 'batch' mode
so that multiple commands can be easily tested with one invocation of
virsh.

To show how it's used adapt the alias handling tests to be invoked all
at once.

As in batch mode the arguments are read from a string and separated
inside virsh, one test is kept separate to be parsed in argv mode.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
tests/virshtest.c
tests/virshtestdata/echo-alias.in [new file with mode: 0644]
tests/virshtestdata/echo-alias.out [new file with mode: 0644]

index e0b5cd31501497684d3faed84b91bae2c6650e7d..376fb861ed8572e2a63100bde37c8ce3a4cc144e 100644 (file)
@@ -561,6 +561,26 @@ mymain(void)
                    testIOThreadPin, NULL) != 0)
         ret = -1;
 
+# define DO_TEST_SCRIPT(testname_, testfilter, ...) \
+    { \
+        const char *testname = testname_; \
+        g_autofree char *infile = g_strdup_printf("%s/virshtestdata/%s.in", \
+                                                  abs_srcdir, testname); \
+        const char *myargv[] = { __VA_ARGS__, NULL, NULL }; \
+        const char **tmp = myargv; \
+        const struct testInfo info = { testname, testfilter, myargv, NULL }; \
+        g_autofree char *scriptarg = NULL; \
+        if (virFileReadAll(infile, 256 * 1024, &scriptarg) < 0) { \
+            fprintf(stderr, "\nfailed to load '%s'\n", infile); \
+            ret = -1; \
+        } \
+        while (*tmp) \
+            tmp++; \
+        *tmp = scriptarg; \
+        if (virTestRun(testname, testCompare, &info) < 0) \
+            ret = -1; \
+    } while (0);
+
     /* It's a bit awkward listing result before argument, but that's a
      * limitation of C99 vararg macros.  */
 # define DO_TEST(i, result, ...) \
@@ -644,11 +664,8 @@ mymain(void)
             "echo \t '-'\"-\" \t --shell \t a");
 
     /* Tests of alias handling.  */
-    DO_TEST(31, "hello\n", "echo", "--string", "hello");
-    DO_TEST(32, "hello\n", "echo --string hello");
+    DO_TEST_SCRIPT("echo-alias", NULL, VIRSH_DEFAULT);
     DO_TEST(33, "hello\n", "echo", "--str", "hello");
-    DO_TEST(34, "hello\n", "echo --str hello");
-    DO_TEST(35, "hello\n", "echo --hi");
 
     /* Tests of multiple commands.  */
     DO_TEST(36, "a\nb\n", " echo a; echo b;");
diff --git a/tests/virshtestdata/echo-alias.in b/tests/virshtestdata/echo-alias.in
new file mode 100644 (file)
index 0000000..f123437
--- /dev/null
@@ -0,0 +1,3 @@
+echo --string hello
+echo --str hello
+echo --hi;
diff --git a/tests/virshtestdata/echo-alias.out b/tests/virshtestdata/echo-alias.out
new file mode 100644 (file)
index 0000000..640bb73
--- /dev/null
@@ -0,0 +1,3 @@
+hello
+hello
+hello