virsh: Add 'echo --err' option

Since test:///default resets state on every connection, writing a test
that covers a sequence of commands must be done from a single
session. But if the test wants to exercise particular failure modes as
well as successes, it can be nice to leave witnesses in the stderr
stream immediately before and after the spot where the expected error
should be, to ensure the rest of the script is not causing errors.

Do this by adding an --err option.

Signed-off-by: Eric Blake <eblake@redhat.com>
Acked-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Eric Blake 2019-03-23 19:02:13 -05:00
parent 4e650259f9
commit 2efb42e9ac
2 changed files with 16 additions and 3 deletions

View File

@ -623,12 +623,14 @@ the usable CPU models are only limited by the hypervisor. This command will
print that all CPU models are accepted for these architectures and the actual print that all CPU models are accepted for these architectures and the actual
list of supported CPU models can be checked in the domain capabilities XML. list of supported CPU models can be checked in the domain capabilities XML.
=item B<echo> [I<--shell>] [I<--xml>] [I<arg>...] =item B<echo> [I<--shell>] [I<--xml>] [I<err>...] [I<arg>...]
Echo back each I<arg>, separated by space. If I<--shell> is Echo back each I<arg>, separated by space. If I<--shell> is
specified, then the output will be single-quoted where needed, so that specified, then the output will be single-quoted where needed, so that
it is suitable for reuse in a shell context. If I<--xml> is it is suitable for reuse in a shell context. If I<--xml> is
specified, then the output will be escaped for use in XML. specified, then the output will be escaped for use in XML.
If I<--err> is specified, prefix B<"error: "> and output to stderr
instead of stdout.
=item B<hypervisor-cpu-compare> I<FILE> [I<virttype>] [I<emulator>] [I<arch>] =item B<hypervisor-cpu-compare> I<FILE> [I<virttype>] [I<emulator>] [I<arch>]
[I<machine>] [I<--error>] [I<machine>] [I<--error>]

View File

@ -3296,6 +3296,10 @@ const vshCmdOptDef opts_echo[] = {
.type = VSH_OT_BOOL, .type = VSH_OT_BOOL,
.help = N_("escape for XML use") .help = N_("escape for XML use")
}, },
{.name = "err",
.type = VSH_OT_BOOL,
.help = N_("output to stderr"),
},
{.name = "str", {.name = "str",
.type = VSH_OT_ALIAS, .type = VSH_OT_ALIAS,
.help = "string" .help = "string"
@ -3329,6 +3333,7 @@ cmdEcho(vshControl *ctl, const vshCmd *cmd)
{ {
bool shell = false; bool shell = false;
bool xml = false; bool xml = false;
bool err = false;
int count = 0; int count = 0;
const vshCmdOpt *opt = NULL; const vshCmdOpt *opt = NULL;
char *arg; char *arg;
@ -3338,6 +3343,8 @@ cmdEcho(vshControl *ctl, const vshCmd *cmd)
shell = true; shell = true;
if (vshCommandOptBool(cmd, "xml")) if (vshCommandOptBool(cmd, "xml"))
xml = true; xml = true;
if (vshCommandOptBool(cmd, "err"))
err = true;
while ((opt = vshCommandOptArgv(ctl, cmd, opt))) { while ((opt = vshCommandOptArgv(ctl, cmd, opt))) {
char *str; char *str;
@ -3372,8 +3379,12 @@ cmdEcho(vshControl *ctl, const vshCmd *cmd)
return false; return false;
} }
arg = virBufferContentAndReset(&buf); arg = virBufferContentAndReset(&buf);
if (arg) if (arg) {
if (err)
vshError(ctl, "%s", arg);
else
vshPrint(ctl, "%s", arg); vshPrint(ctl, "%s", arg);
}
VIR_FREE(arg); VIR_FREE(arg);
return true; return true;
} }