Add expectError so that we can make some test fail on purpose and compare
the error message.
The problem is that the test would pass, but the failed test would raise
an internal error, with VIR_TEST_DEBUG=1, the error message would still
be printed.
Signed-off-by: Luke Yue <lukedyue(a)gmail.com>
---
tests/virshtest.c | 61 +++++++++++++++++++++++++----------------------
1 file changed, 33 insertions(+), 28 deletions(-)
diff --git a/tests/virshtest.c b/tests/virshtest.c
index c2c892c60d..af2a70f5fb 100644
--- a/tests/virshtest.c
+++ b/tests/virshtest.c
@@ -110,7 +110,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;
@@ -126,14 +126,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;
@@ -163,7 +168,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)
@@ -176,7 +181,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)
@@ -192,7 +197,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)
@@ -212,112 +217,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)
@@ -336,7 +341,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);
}
static int testIOThreadAdd(const void *data G_GNUC_UNUSED)
@@ -357,7 +362,7 @@ static int testIOThreadAdd(const void *data G_GNUC_UNUSED)
4 0\n\
6 0\n\
\n";
- return testCompareOutputLit(exp, NULL, argv);
+ return testCompareOutputLit(exp, "", NULL, argv);
}
static int testIOThreadDel(const void *data G_GNUC_UNUSED)
@@ -376,7 +381,7 @@ static int testIOThreadDel(const void *data G_GNUC_UNUSED)
-----------------------------\n\
4 0\n\
\n";
- return testCompareOutputLit(exp, NULL, argv);
+ return testCompareOutputLit(exp, "", NULL, argv);
}
static int testIOThreadSet(const void *data G_GNUC_UNUSED)
@@ -408,7 +413,7 @@ Domain: 'fc4'\n\
iothread.4.poll-max-ns" EQUAL "32768\n\
iothread.4.poll-grow" EQUAL "0\n\
iothread.4.poll-shrink" EQUAL "0\n\n";
- return testCompareOutputLit(exp, NULL, argv);
+ return testCompareOutputLit(exp, "", NULL, argv);
}
static int testIOThreadPin(const void *data G_GNUC_UNUSED)
@@ -429,7 +434,7 @@ static int testIOThreadPin(const void *data G_GNUC_UNUSED)
-----------------------------\n\
2 0\n\
\n";
- return testCompareOutputLit(exp, NULL, argv);
+ return testCompareOutputLit(exp, "", NULL, argv);
}
struct testInfo {
@@ -440,7 +445,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.1