[PATCH v2 11/12] virshtest: add expectError parameter to testCompareOutputLit

Luke Yue lukedyue at gmail.com
Fri Aug 20 12:44:38 UTC 2021


Add expectError so that we can failed some test on purpose and compare
the error message.

There are some problems though, if the command raise an internal error,
the test would fail. And when testing command is failed on purpose, the
error message will be printed even the test passed.

Signed-off-by: Luke Yue <lukedyue at gmail.com>
---
 tests/virshtest.c | 53 ++++++++++++++++++++++++++---------------------
 1 file changed, 29 insertions(+), 24 deletions(-)

diff --git a/tests/virshtest.c b/tests/virshtest.c
index 87da1f5889..0d2ffd960a 100644
--- a/tests/virshtest.c
+++ b/tests/virshtest.c
@@ -109,7 +109,7 @@ static int testFilterLine(char *buffer,
 }
 
 static int
-testCompareOutputLit(const char *expectData,
+testCompareOutputLit(const char *expectData, const char *expectError,
                      const char *filter, const char *const argv[])
 {
     g_autofree char *actualData = NULL;
@@ -125,14 +125,19 @@ testCompareOutputLit(const char *expectData,
     virCommandSetOutputBuffer(cmd, &actualData);
     virCommandSetErrorBuffer(cmd, &errbuf);
 
-    if (virCommandRun(cmd, NULL) < 0)
+    if (virCommandRun(cmd, NULL) < 0 && STREQ(expectError, ""))
         return -1;
 
-    if (STRNEQ(errbuf, "")) {
+    if (STREQ(expectError, "") && STRNEQ(errbuf, "")) {
         fprintf(stderr, "Command reported error: %s", errbuf);
         return -1;
     }
 
+    if (STRNEQ(errbuf, expectError)) {
+        virTestDifference(stderr, errbuf, expectError);
+        return -1;
+    }
+
     if (filter && testFilterLine(actualData, filter) < 0)
         return -1;
 
@@ -162,7 +167,7 @@ static int testCompareListDefault(const void *data G_GNUC_UNUSED)
 ----------------------\n\
  1    test   running\n\
 \n";
-    return testCompareOutputLit(exp, NULL, argv);
+    return testCompareOutputLit(exp, "", NULL, argv);
 }
 
 static int testCompareListCustom(const void *data G_GNUC_UNUSED)
@@ -175,7 +180,7 @@ static int testCompareListCustom(const void *data G_GNUC_UNUSED)
  2    fc4    running\n\
  3    fc5    running\n\
 \n";
-    return testCompareOutputLit(exp, NULL, argv);
+    return testCompareOutputLit(exp, "", NULL, argv);
 }
 
 static int testCompareNodeinfoDefault(const void *data G_GNUC_UNUSED)
@@ -191,7 +196,7 @@ Thread(s) per core:  2\n\
 NUMA cell(s):        2\n\
 Memory size:         3145728 KiB\n\
 \n";
-    return testCompareOutputLit(exp, NULL, argv);
+    return testCompareOutputLit(exp, "", NULL, argv);
 }
 
 static int testCompareNodeinfoCustom(const void *data G_GNUC_UNUSED)
@@ -211,112 +216,112 @@ Thread(s) per core:  2\n\
 NUMA cell(s):        4\n\
 Memory size:         8192000 KiB\n\
 \n";
-    return testCompareOutputLit(exp, NULL, argv);
+    return testCompareOutputLit(exp, "", NULL, argv);
 }
 
 static int testCompareDominfoByID(const void *data G_GNUC_UNUSED)
 {
     const char *const argv[] = { VIRSH_CUSTOM, "dominfo", "2", NULL };
     const char *exp = dominfo_fc4;
-    return testCompareOutputLit(exp, "\nCPU time:", argv);
+    return testCompareOutputLit(exp, "", "\nCPU time:", argv);
 }
 
 static int testCompareDominfoByUUID(const void *data G_GNUC_UNUSED)
 {
     const char *const argv[] = { VIRSH_CUSTOM, "dominfo", DOM_FC4_UUID, NULL };
     const char *exp = dominfo_fc4;
-    return testCompareOutputLit(exp, "\nCPU time:", argv);
+    return testCompareOutputLit(exp, "", "\nCPU time:", argv);
 }
 
 static int testCompareDominfoByName(const void *data G_GNUC_UNUSED)
 {
     const char *const argv[] = { VIRSH_CUSTOM, "dominfo", "fc4", NULL };
     const char *exp = dominfo_fc4;
-    return testCompareOutputLit(exp, "\nCPU time:", argv);
+    return testCompareOutputLit(exp, "", "\nCPU time:", argv);
 }
 
 static int testCompareTaintedDominfoByName(const void *data G_GNUC_UNUSED)
 {
     const char *const argv[] = { VIRSH_CUSTOM, "dominfo", "fc5", NULL };
     const char *exp = dominfo_fc5;
-    return testCompareOutputLit(exp, "\nCPU time:", argv);
+    return testCompareOutputLit(exp, "", "\nCPU time:", argv);
 }
 
 static int testCompareDomuuidByID(const void *data G_GNUC_UNUSED)
 {
     const char *const argv[] = { VIRSH_CUSTOM, "domuuid", "2", NULL };
     const char *exp = domuuid_fc4;
-    return testCompareOutputLit(exp, NULL, argv);
+    return testCompareOutputLit(exp, "", NULL, argv);
 }
 
 static int testCompareDomuuidByName(const void *data G_GNUC_UNUSED)
 {
     const char *const argv[] = { VIRSH_CUSTOM, "domuuid", "fc4", NULL };
     const char *exp = domuuid_fc4;
-    return testCompareOutputLit(exp, NULL, argv);
+    return testCompareOutputLit(exp, "", NULL, argv);
 }
 
 static int testCompareDomidByName(const void *data G_GNUC_UNUSED)
 {
     const char *const argv[] = { VIRSH_CUSTOM, "domid", "fc4", NULL };
     const char *exp = domid_fc4;
-    return testCompareOutputLit(exp, NULL, argv);
+    return testCompareOutputLit(exp, "", NULL, argv);
 }
 
 static int testCompareDomidByUUID(const void *data G_GNUC_UNUSED)
 {
     const char *const argv[] = { VIRSH_CUSTOM, "domid", DOM_FC4_UUID, NULL };
     const char *exp = domid_fc4;
-    return testCompareOutputLit(exp, NULL, argv);
+    return testCompareOutputLit(exp, "", NULL, argv);
 }
 
 static int testCompareDomnameByID(const void *data G_GNUC_UNUSED)
 {
     const char *const argv[] = { VIRSH_CUSTOM, "domname", "2", NULL };
     const char *exp = domname_fc4;
-    return testCompareOutputLit(exp, NULL, argv);
+    return testCompareOutputLit(exp, "", NULL, argv);
 }
 
 static int testCompareDomnameByUUID(const void *data G_GNUC_UNUSED)
 {
     const char *const argv[] = { VIRSH_CUSTOM, "domname", DOM_FC4_UUID, NULL };
     const char *exp = domname_fc4;
-    return testCompareOutputLit(exp, NULL, argv);
+    return testCompareOutputLit(exp, "", NULL, argv);
 }
 
 static int testCompareDomstateByID(const void *data G_GNUC_UNUSED)
 {
     const char *const argv[] = { VIRSH_CUSTOM, "domstate", "2", NULL };
     const char *exp = domstate_fc4;
-    return testCompareOutputLit(exp, NULL, argv);
+    return testCompareOutputLit(exp, "", NULL, argv);
 }
 
 static int testCompareDomstateByUUID(const void *data G_GNUC_UNUSED)
 {
     const char *const argv[] = { VIRSH_CUSTOM, "domstate", DOM_FC4_UUID, NULL };
     const char *exp = domstate_fc4;
-    return testCompareOutputLit(exp, NULL, argv);
+    return testCompareOutputLit(exp, "", NULL, argv);
 }
 
 static int testCompareDomstateByName(const void *data G_GNUC_UNUSED)
 {
     const char *const argv[] = { VIRSH_CUSTOM, "domstate", "fc4", NULL };
     const char *exp = domstate_fc4;
-    return testCompareOutputLit(exp, NULL, argv);
+    return testCompareOutputLit(exp, "", NULL, argv);
 }
 
 static int testCompareDomControlInfoByName(const void *data G_GNUC_UNUSED)
 {
     const char *const argv[] = { VIRSH_CUSTOM, "domcontrol", "fc4", NULL };
     const char *exp = "ok\n\n";
-    return testCompareOutputLit(exp, NULL, argv);
+    return testCompareOutputLit(exp, "", NULL, argv);
 }
 
 static int testCompareGetBlkioParameters(const void *data G_GNUC_UNUSED)
 {
     const char *const argv[] = { VIRSH_CUSTOM, "blkiotune", "fv0", NULL };
     const char *exp = get_blkio_parameters;
-    return testCompareOutputLit(exp, NULL, argv);
+    return testCompareOutputLit(exp, "", NULL, argv);
 }
 
 static int testCompareSetBlkioParameters(const void *data G_GNUC_UNUSED)
@@ -335,7 +340,7 @@ static int testCompareSetBlkioParameters(const void *data G_GNUC_UNUSED)
                                  " SET_BLKIO_PARAMETER ";\
                                  blkiotune fv0", NULL };
     const char *exp = set_blkio_parameters;
-    return testCompareOutputLit(exp, NULL, argv);
+    return testCompareOutputLit(exp, "", NULL, argv);
 }
 
 struct testInfo {
@@ -346,7 +351,7 @@ struct testInfo {
 static int testCompareEcho(const void *data)
 {
     const struct testInfo *info = data;
-    return testCompareOutputLit(info->result, NULL, info->argv);
+    return testCompareOutputLit(info->result, "", NULL, info->argv);
 }
 
 
-- 
2.33.0




More information about the libvir-list mailing list