2010-05-25 11:14:06 +00:00
|
|
|
/*
|
|
|
|
* commandtest.c: Test the libCommand API
|
|
|
|
*
|
2011-01-28 21:22:39 +00:00
|
|
|
* Copyright (C) 2010-2011 Red Hat, Inc.
|
2010-05-25 11:14:06 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include "testutils.h"
|
|
|
|
#include "internal.h"
|
|
|
|
#include "nodeinfo.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "command.h"
|
2011-07-19 18:32:58 +00:00
|
|
|
#include "virfile.h"
|
2011-08-12 21:54:49 +00:00
|
|
|
#include "virpidfile.h"
|
2010-05-25 11:14:06 +00:00
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
|
2011-07-28 15:48:12 +00:00
|
|
|
int
|
|
|
|
main(void)
|
2010-05-25 11:14:06 +00:00
|
|
|
{
|
2011-07-28 15:48:12 +00:00
|
|
|
return EXIT_AM_SKIP;
|
2010-05-25 11:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2010-12-06 10:51:41 +00:00
|
|
|
static int checkoutput(const char *testname)
|
|
|
|
{
|
2010-05-25 11:14:06 +00:00
|
|
|
int ret = -1;
|
|
|
|
char *expectname = NULL;
|
|
|
|
char *expectlog = NULL;
|
|
|
|
char *actualname = NULL;
|
|
|
|
char *actuallog = NULL;
|
|
|
|
|
|
|
|
if (virAsprintf(&expectname, "%s/commanddata/%s.log", abs_srcdir,
|
|
|
|
testname) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
if (virAsprintf(&actualname, "%s/commandhelper.log", abs_builddir) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (virFileReadAll(expectname, 1024*64, &expectlog) < 0) {
|
|
|
|
fprintf(stderr, "cannot read %s\n", expectname);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virFileReadAll(actualname, 1024*64, &actuallog) < 0) {
|
|
|
|
fprintf(stderr, "cannot read %s\n", actualname);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (STRNEQ(expectlog, actuallog)) {
|
|
|
|
virtTestDifference(stderr, expectlog, actuallog);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
2011-05-03 16:37:13 +00:00
|
|
|
if (actualname)
|
|
|
|
unlink(actualname);
|
2010-05-25 11:14:06 +00:00
|
|
|
VIR_FREE(actuallog);
|
|
|
|
VIR_FREE(actualname);
|
|
|
|
VIR_FREE(expectlog);
|
|
|
|
VIR_FREE(expectname);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Run program, no args, inherit all ENV, keep CWD.
|
|
|
|
* Only stdin/out/err open
|
|
|
|
* No slot for return status must log error.
|
|
|
|
*/
|
2010-12-06 10:51:41 +00:00
|
|
|
static int test0(const void *unused ATTRIBUTE_UNUSED)
|
|
|
|
{
|
2010-05-25 11:14:06 +00:00
|
|
|
virCommandPtr cmd;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
cmd = virCommandNew(abs_builddir "/commandhelper-doesnotexist");
|
|
|
|
if (virCommandRun(cmd, NULL) == 0)
|
|
|
|
goto cleanup;
|
2010-12-06 11:58:56 +00:00
|
|
|
|
|
|
|
if (virGetLastError() == NULL)
|
2010-05-25 11:14:06 +00:00
|
|
|
goto cleanup;
|
2010-12-06 11:58:56 +00:00
|
|
|
|
2010-05-25 11:14:06 +00:00
|
|
|
virResetLastError();
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
virCommandFree(cmd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Run program, no args, inherit all ENV, keep CWD.
|
|
|
|
* Only stdin/out/err open
|
|
|
|
* Capturing return status must not log error.
|
|
|
|
*/
|
2010-12-06 10:51:41 +00:00
|
|
|
static int test1(const void *unused ATTRIBUTE_UNUSED)
|
|
|
|
{
|
2010-05-25 11:14:06 +00:00
|
|
|
virCommandPtr cmd;
|
|
|
|
int ret = -1;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
cmd = virCommandNew(abs_builddir "/commandhelper-doesnotexist");
|
|
|
|
if (virCommandRun(cmd, &status) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
if (status == 0)
|
|
|
|
goto cleanup;
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
virCommandFree(cmd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Run program (twice), no args, inherit all ENV, keep CWD.
|
|
|
|
* Only stdin/out/err open
|
|
|
|
*/
|
2010-12-06 10:51:41 +00:00
|
|
|
static int test2(const void *unused ATTRIBUTE_UNUSED)
|
|
|
|
{
|
2010-05-25 11:14:06 +00:00
|
|
|
virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper");
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (virCommandRun(cmd, NULL) < 0) {
|
|
|
|
virErrorPtr err = virGetLastError();
|
|
|
|
printf("Cannot run child %s\n", err->message);
|
2010-12-06 21:48:11 +00:00
|
|
|
virCommandFree(cmd);
|
2010-05-25 11:14:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ret = checkoutput("test2")) != 0) {
|
|
|
|
virCommandFree(cmd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virCommandRun(cmd, NULL) < 0) {
|
|
|
|
virErrorPtr err = virGetLastError();
|
|
|
|
printf("Cannot run child %s\n", err->message);
|
2010-12-06 21:48:11 +00:00
|
|
|
virCommandFree(cmd);
|
2010-05-25 11:14:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
virCommandFree(cmd);
|
|
|
|
|
|
|
|
return checkoutput("test2");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Run program, no args, inherit all ENV, keep CWD.
|
|
|
|
* stdin/out/err + two extra FD open
|
|
|
|
*/
|
2010-12-06 10:51:41 +00:00
|
|
|
static int test3(const void *unused ATTRIBUTE_UNUSED)
|
|
|
|
{
|
2010-05-25 11:14:06 +00:00
|
|
|
virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper");
|
|
|
|
int newfd1 = dup(STDERR_FILENO);
|
|
|
|
int newfd2 = dup(STDERR_FILENO);
|
|
|
|
int newfd3 = dup(STDERR_FILENO);
|
2010-12-06 21:48:11 +00:00
|
|
|
int ret = -1;
|
2010-05-25 11:14:06 +00:00
|
|
|
|
|
|
|
virCommandPreserveFD(cmd, newfd1);
|
|
|
|
virCommandTransferFD(cmd, newfd3);
|
|
|
|
|
|
|
|
if (virCommandRun(cmd, NULL) < 0) {
|
|
|
|
virErrorPtr err = virGetLastError();
|
|
|
|
printf("Cannot run child %s\n", err->message);
|
2010-12-06 21:48:11 +00:00
|
|
|
goto cleanup;
|
2010-05-25 11:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fcntl(newfd1, F_GETFL) < 0 ||
|
|
|
|
fcntl(newfd2, F_GETFL) < 0 ||
|
|
|
|
fcntl(newfd3, F_GETFL) >= 0) {
|
|
|
|
puts("fds in wrong state");
|
2010-12-06 21:48:11 +00:00
|
|
|
goto cleanup;
|
2010-05-25 11:14:06 +00:00
|
|
|
}
|
|
|
|
|
2010-12-06 21:48:11 +00:00
|
|
|
ret = checkoutput("test3");
|
|
|
|
|
|
|
|
cleanup:
|
2010-05-25 11:14:06 +00:00
|
|
|
virCommandFree(cmd);
|
|
|
|
VIR_FORCE_CLOSE(newfd1);
|
|
|
|
VIR_FORCE_CLOSE(newfd2);
|
2010-12-06 21:48:11 +00:00
|
|
|
return ret;
|
2010-05-25 11:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Run program, no args, inherit all ENV, CWD is /
|
|
|
|
* Only stdin/out/err open.
|
|
|
|
* Daemonized
|
|
|
|
*/
|
2010-12-06 10:51:41 +00:00
|
|
|
static int test4(const void *unused ATTRIBUTE_UNUSED)
|
|
|
|
{
|
2010-05-25 11:14:06 +00:00
|
|
|
virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper");
|
2011-08-12 21:54:49 +00:00
|
|
|
char *pidfile = virPidFileBuildPath(abs_builddir, "commandhelper");
|
2010-12-06 21:48:11 +00:00
|
|
|
pid_t pid;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (!pidfile)
|
|
|
|
goto cleanup;
|
2010-05-25 11:14:06 +00:00
|
|
|
|
|
|
|
virCommandSetPidFile(cmd, pidfile);
|
|
|
|
virCommandDaemonize(cmd);
|
|
|
|
|
|
|
|
if (virCommandRun(cmd, NULL) < 0) {
|
|
|
|
virErrorPtr err = virGetLastError();
|
|
|
|
printf("Cannot run child %s\n", err->message);
|
2010-12-06 21:48:11 +00:00
|
|
|
goto cleanup;
|
2010-05-25 11:14:06 +00:00
|
|
|
}
|
|
|
|
|
2011-08-12 21:54:49 +00:00
|
|
|
if (virPidFileRead(abs_builddir, "commandhelper", &pid) < 0) {
|
2010-05-25 11:14:06 +00:00
|
|
|
printf("cannot read pidfile\n");
|
2010-12-06 21:48:11 +00:00
|
|
|
goto cleanup;
|
2010-05-25 11:14:06 +00:00
|
|
|
}
|
|
|
|
while (kill(pid, 0) != -1)
|
|
|
|
usleep(100*1000);
|
|
|
|
|
2010-12-06 21:48:11 +00:00
|
|
|
ret = checkoutput("test4");
|
2010-05-25 11:14:06 +00:00
|
|
|
|
2010-12-06 21:48:11 +00:00
|
|
|
cleanup:
|
|
|
|
virCommandFree(cmd);
|
2011-05-03 16:37:13 +00:00
|
|
|
if (pidfile)
|
|
|
|
unlink(pidfile);
|
2010-05-25 11:14:06 +00:00
|
|
|
VIR_FREE(pidfile);
|
2010-12-06 21:48:11 +00:00
|
|
|
return ret;
|
2010-05-25 11:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Run program, no args, inherit filtered ENV, keep CWD.
|
|
|
|
* Only stdin/out/err open
|
|
|
|
*/
|
2010-12-06 10:51:41 +00:00
|
|
|
static int test5(const void *unused ATTRIBUTE_UNUSED)
|
|
|
|
{
|
2010-05-25 11:14:06 +00:00
|
|
|
virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper");
|
|
|
|
|
|
|
|
virCommandAddEnvPassCommon(cmd);
|
|
|
|
|
|
|
|
if (virCommandRun(cmd, NULL) < 0) {
|
|
|
|
virErrorPtr err = virGetLastError();
|
|
|
|
printf("Cannot run child %s\n", err->message);
|
2010-12-06 21:48:11 +00:00
|
|
|
virCommandFree(cmd);
|
2010-05-25 11:14:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
virCommandFree(cmd);
|
|
|
|
|
|
|
|
return checkoutput("test5");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Run program, no args, inherit filtered ENV, keep CWD.
|
|
|
|
* Only stdin/out/err open
|
|
|
|
*/
|
2010-12-06 10:51:41 +00:00
|
|
|
static int test6(const void *unused ATTRIBUTE_UNUSED)
|
|
|
|
{
|
2010-05-25 11:14:06 +00:00
|
|
|
virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper");
|
|
|
|
|
|
|
|
virCommandAddEnvPass(cmd, "DISPLAY");
|
|
|
|
virCommandAddEnvPass(cmd, "DOESNOTEXIST");
|
|
|
|
|
|
|
|
if (virCommandRun(cmd, NULL) < 0) {
|
|
|
|
virErrorPtr err = virGetLastError();
|
|
|
|
printf("Cannot run child %s\n", err->message);
|
2010-12-06 21:48:11 +00:00
|
|
|
virCommandFree(cmd);
|
2010-05-25 11:14:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
virCommandFree(cmd);
|
|
|
|
|
|
|
|
return checkoutput("test6");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Run program, no args, inherit filtered ENV, keep CWD.
|
|
|
|
* Only stdin/out/err open
|
|
|
|
*/
|
2010-12-06 10:51:41 +00:00
|
|
|
static int test7(const void *unused ATTRIBUTE_UNUSED)
|
|
|
|
{
|
2010-05-25 11:14:06 +00:00
|
|
|
virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper");
|
|
|
|
|
|
|
|
virCommandAddEnvPassCommon(cmd);
|
|
|
|
virCommandAddEnvPass(cmd, "DISPLAY");
|
|
|
|
virCommandAddEnvPass(cmd, "DOESNOTEXIST");
|
|
|
|
|
|
|
|
if (virCommandRun(cmd, NULL) < 0) {
|
|
|
|
virErrorPtr err = virGetLastError();
|
|
|
|
printf("Cannot run child %s\n", err->message);
|
2010-12-06 21:48:11 +00:00
|
|
|
virCommandFree(cmd);
|
2010-05-25 11:14:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
virCommandFree(cmd);
|
|
|
|
|
|
|
|
return checkoutput("test7");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Run program, no args, inherit filtered ENV, keep CWD.
|
|
|
|
* Only stdin/out/err open
|
|
|
|
*/
|
2010-12-06 10:51:41 +00:00
|
|
|
static int test8(const void *unused ATTRIBUTE_UNUSED)
|
|
|
|
{
|
2010-05-25 11:14:06 +00:00
|
|
|
virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper");
|
|
|
|
|
|
|
|
virCommandAddEnvString(cmd, "LANG=C");
|
|
|
|
virCommandAddEnvPair(cmd, "USER", "test");
|
|
|
|
|
|
|
|
if (virCommandRun(cmd, NULL) < 0) {
|
|
|
|
virErrorPtr err = virGetLastError();
|
|
|
|
printf("Cannot run child %s\n", err->message);
|
2010-12-06 21:48:11 +00:00
|
|
|
virCommandFree(cmd);
|
2010-05-25 11:14:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
virCommandFree(cmd);
|
|
|
|
|
|
|
|
return checkoutput("test8");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Run program, some args, inherit all ENV, keep CWD.
|
|
|
|
* Only stdin/out/err open
|
|
|
|
*/
|
2010-12-06 10:51:41 +00:00
|
|
|
static int test9(const void *unused ATTRIBUTE_UNUSED)
|
|
|
|
{
|
2010-05-25 11:14:06 +00:00
|
|
|
virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper");
|
|
|
|
const char* const args[] = { "arg1", "arg2", NULL };
|
2011-11-10 00:19:33 +00:00
|
|
|
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
2010-05-25 11:14:06 +00:00
|
|
|
|
|
|
|
virCommandAddArg(cmd, "-version");
|
|
|
|
virCommandAddArgPair(cmd, "-log", "bar.log");
|
|
|
|
virCommandAddArgSet(cmd, args);
|
2011-11-10 00:19:33 +00:00
|
|
|
virCommandAddArgBuffer(cmd, &buf);
|
|
|
|
virBufferAddLit(&buf, "arg4");
|
|
|
|
virCommandAddArgBuffer(cmd, &buf);
|
|
|
|
virCommandAddArgList(cmd, "arg5", "arg6", NULL);
|
|
|
|
|
|
|
|
if (virBufferUse(&buf)) {
|
|
|
|
printf("Buffer not transferred\n");
|
|
|
|
virBufferFreeAndReset(&buf);
|
|
|
|
virCommandFree(cmd);
|
|
|
|
return -1;
|
|
|
|
}
|
2010-05-25 11:14:06 +00:00
|
|
|
|
|
|
|
if (virCommandRun(cmd, NULL) < 0) {
|
|
|
|
virErrorPtr err = virGetLastError();
|
|
|
|
printf("Cannot run child %s\n", err->message);
|
2010-12-06 21:48:11 +00:00
|
|
|
virCommandFree(cmd);
|
2010-05-25 11:14:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
virCommandFree(cmd);
|
|
|
|
|
|
|
|
return checkoutput("test9");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Run program, some args, inherit all ENV, keep CWD.
|
|
|
|
* Only stdin/out/err open
|
|
|
|
*/
|
2010-12-06 10:51:41 +00:00
|
|
|
static int test10(const void *unused ATTRIBUTE_UNUSED)
|
|
|
|
{
|
2010-05-25 11:14:06 +00:00
|
|
|
virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper");
|
|
|
|
const char *const args[] = {
|
|
|
|
"-version", "-log=bar.log", NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
virCommandAddArgSet(cmd, args);
|
|
|
|
|
|
|
|
if (virCommandRun(cmd, NULL) < 0) {
|
|
|
|
virErrorPtr err = virGetLastError();
|
|
|
|
printf("Cannot run child %s\n", err->message);
|
2010-12-06 21:48:11 +00:00
|
|
|
virCommandFree(cmd);
|
2010-05-25 11:14:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
virCommandFree(cmd);
|
|
|
|
|
|
|
|
return checkoutput("test10");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Run program, some args, inherit all ENV, keep CWD.
|
|
|
|
* Only stdin/out/err open
|
|
|
|
*/
|
2010-12-06 10:51:41 +00:00
|
|
|
static int test11(const void *unused ATTRIBUTE_UNUSED)
|
|
|
|
{
|
2010-05-25 11:14:06 +00:00
|
|
|
const char *args[] = {
|
|
|
|
abs_builddir "/commandhelper",
|
|
|
|
"-version", "-log=bar.log", NULL,
|
|
|
|
};
|
|
|
|
virCommandPtr cmd = virCommandNewArgs(args);
|
|
|
|
|
|
|
|
if (virCommandRun(cmd, NULL) < 0) {
|
|
|
|
virErrorPtr err = virGetLastError();
|
|
|
|
printf("Cannot run child %s\n", err->message);
|
2010-12-06 21:48:11 +00:00
|
|
|
virCommandFree(cmd);
|
2010-05-25 11:14:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
virCommandFree(cmd);
|
|
|
|
|
|
|
|
return checkoutput("test11");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Run program, no args, inherit all ENV, keep CWD.
|
|
|
|
* Only stdin/out/err open. Set stdin data
|
|
|
|
*/
|
2010-12-06 10:51:41 +00:00
|
|
|
static int test12(const void *unused ATTRIBUTE_UNUSED)
|
|
|
|
{
|
2010-05-25 11:14:06 +00:00
|
|
|
virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper");
|
|
|
|
|
|
|
|
virCommandSetInputBuffer(cmd, "Hello World\n");
|
|
|
|
|
|
|
|
if (virCommandRun(cmd, NULL) < 0) {
|
|
|
|
virErrorPtr err = virGetLastError();
|
|
|
|
printf("Cannot run child %s\n", err->message);
|
2010-12-06 21:48:11 +00:00
|
|
|
virCommandFree(cmd);
|
2010-05-25 11:14:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
virCommandFree(cmd);
|
|
|
|
|
|
|
|
return checkoutput("test12");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Run program, no args, inherit all ENV, keep CWD.
|
|
|
|
* Only stdin/out/err open. Set stdin data
|
|
|
|
*/
|
2010-12-06 10:51:41 +00:00
|
|
|
static int test13(const void *unused ATTRIBUTE_UNUSED)
|
|
|
|
{
|
2010-05-25 11:14:06 +00:00
|
|
|
virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper");
|
|
|
|
char *outactual = NULL;
|
|
|
|
const char *outexpect = "BEGIN STDOUT\n"
|
|
|
|
"Hello World\n"
|
|
|
|
"END STDOUT\n";
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
virCommandSetInputBuffer(cmd, "Hello World\n");
|
|
|
|
virCommandSetOutputBuffer(cmd, &outactual);
|
|
|
|
|
|
|
|
if (virCommandRun(cmd, NULL) < 0) {
|
|
|
|
virErrorPtr err = virGetLastError();
|
|
|
|
printf("Cannot run child %s\n", err->message);
|
2010-12-06 21:48:11 +00:00
|
|
|
goto cleanup;
|
2010-05-25 11:14:06 +00:00
|
|
|
}
|
2010-12-06 21:48:11 +00:00
|
|
|
if (!outactual)
|
|
|
|
goto cleanup;
|
2010-05-25 11:14:06 +00:00
|
|
|
|
|
|
|
virCommandFree(cmd);
|
2010-12-06 21:48:11 +00:00
|
|
|
cmd = NULL;
|
2010-05-25 11:14:06 +00:00
|
|
|
|
|
|
|
if (!STREQ(outactual, outexpect)) {
|
|
|
|
virtTestDifference(stderr, outactual, outexpect);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2010-12-06 21:48:11 +00:00
|
|
|
ret = checkoutput("test13");
|
2010-05-25 11:14:06 +00:00
|
|
|
|
|
|
|
cleanup:
|
2010-12-06 21:48:11 +00:00
|
|
|
virCommandFree(cmd);
|
2010-05-25 11:14:06 +00:00
|
|
|
VIR_FREE(outactual);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Run program, no args, inherit all ENV, keep CWD.
|
|
|
|
* Only stdin/out/err open. Set stdin data
|
|
|
|
*/
|
2010-12-06 10:51:41 +00:00
|
|
|
static int test14(const void *unused ATTRIBUTE_UNUSED)
|
|
|
|
{
|
2010-05-25 11:14:06 +00:00
|
|
|
virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper");
|
|
|
|
char *outactual = NULL;
|
|
|
|
const char *outexpect = "BEGIN STDOUT\n"
|
|
|
|
"Hello World\n"
|
|
|
|
"END STDOUT\n";
|
|
|
|
char *erractual = NULL;
|
|
|
|
const char *errexpect = "BEGIN STDERR\n"
|
|
|
|
"Hello World\n"
|
|
|
|
"END STDERR\n";
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
virCommandSetInputBuffer(cmd, "Hello World\n");
|
|
|
|
virCommandSetOutputBuffer(cmd, &outactual);
|
|
|
|
virCommandSetErrorBuffer(cmd, &erractual);
|
|
|
|
|
|
|
|
if (virCommandRun(cmd, NULL) < 0) {
|
|
|
|
virErrorPtr err = virGetLastError();
|
|
|
|
printf("Cannot run child %s\n", err->message);
|
2010-12-06 21:48:11 +00:00
|
|
|
goto cleanup;
|
2010-05-25 11:14:06 +00:00
|
|
|
}
|
2010-12-06 21:48:11 +00:00
|
|
|
if (!outactual || !erractual)
|
|
|
|
goto cleanup;
|
2010-05-25 11:14:06 +00:00
|
|
|
|
|
|
|
virCommandFree(cmd);
|
2010-12-06 21:48:11 +00:00
|
|
|
cmd = NULL;
|
2010-05-25 11:14:06 +00:00
|
|
|
|
|
|
|
if (!STREQ(outactual, outexpect)) {
|
|
|
|
virtTestDifference(stderr, outactual, outexpect);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (!STREQ(erractual, errexpect)) {
|
|
|
|
virtTestDifference(stderr, erractual, errexpect);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2010-12-06 21:48:11 +00:00
|
|
|
ret = checkoutput("test14");
|
2010-05-25 11:14:06 +00:00
|
|
|
|
|
|
|
cleanup:
|
2010-12-06 21:48:11 +00:00
|
|
|
virCommandFree(cmd);
|
2010-05-25 11:14:06 +00:00
|
|
|
VIR_FREE(outactual);
|
|
|
|
VIR_FREE(erractual);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Run program, no args, inherit all ENV, change CWD.
|
|
|
|
* Only stdin/out/err open
|
|
|
|
*/
|
2010-12-06 10:51:41 +00:00
|
|
|
static int test15(const void *unused ATTRIBUTE_UNUSED)
|
|
|
|
{
|
2010-05-25 11:14:06 +00:00
|
|
|
virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper");
|
2010-12-06 12:03:26 +00:00
|
|
|
char *cwd = NULL;
|
|
|
|
int ret = -1;
|
2010-05-25 11:14:06 +00:00
|
|
|
|
2010-12-06 12:03:26 +00:00
|
|
|
if (virAsprintf(&cwd, "%s/commanddata", abs_srcdir) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
virCommandSetWorkingDirectory(cmd, cwd);
|
2010-05-25 11:14:06 +00:00
|
|
|
|
|
|
|
if (virCommandRun(cmd, NULL) < 0) {
|
|
|
|
virErrorPtr err = virGetLastError();
|
|
|
|
printf("Cannot run child %s\n", err->message);
|
2010-12-06 12:03:26 +00:00
|
|
|
goto cleanup;
|
2010-05-25 11:14:06 +00:00
|
|
|
}
|
|
|
|
|
2010-12-06 12:03:26 +00:00
|
|
|
ret = checkoutput("test15");
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
VIR_FREE(cwd);
|
2010-05-25 11:14:06 +00:00
|
|
|
virCommandFree(cmd);
|
|
|
|
|
2010-12-06 12:03:26 +00:00
|
|
|
return ret;
|
2010-05-25 11:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't run program; rather, log what would be run.
|
|
|
|
*/
|
2010-12-06 10:51:41 +00:00
|
|
|
static int test16(const void *unused ATTRIBUTE_UNUSED)
|
|
|
|
{
|
2011-07-28 15:51:26 +00:00
|
|
|
virCommandPtr cmd = virCommandNew("true");
|
2010-05-25 11:14:06 +00:00
|
|
|
char *outactual = NULL;
|
2011-07-28 15:51:26 +00:00
|
|
|
const char *outexpect = "A=B true C";
|
2010-05-25 11:14:06 +00:00
|
|
|
int ret = -1;
|
|
|
|
int fd = -1;
|
|
|
|
|
|
|
|
virCommandAddEnvPair(cmd, "A", "B");
|
|
|
|
virCommandAddArg(cmd, "C");
|
|
|
|
|
|
|
|
if ((outactual = virCommandToString(cmd)) == NULL) {
|
|
|
|
virErrorPtr err = virGetLastError();
|
|
|
|
printf("Cannot convert to string: %s\n", err->message);
|
2010-12-06 21:48:11 +00:00
|
|
|
goto cleanup;
|
2010-05-25 11:14:06 +00:00
|
|
|
}
|
|
|
|
if ((fd = open(abs_builddir "/commandhelper.log",
|
|
|
|
O_CREAT | O_TRUNC | O_WRONLY, 0600)) < 0) {
|
|
|
|
printf("Cannot open log file: %s\n", strerror (errno));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
virCommandWriteArgLog(cmd, fd);
|
|
|
|
if (VIR_CLOSE(fd) < 0) {
|
|
|
|
printf("Cannot close log file: %s\n", strerror (errno));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!STREQ(outactual, outexpect)) {
|
|
|
|
virtTestDifference(stderr, outactual, outexpect);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2010-12-06 21:48:11 +00:00
|
|
|
|
|
|
|
ret = checkoutput("test16");
|
2010-05-25 11:14:06 +00:00
|
|
|
|
|
|
|
cleanup:
|
2010-12-06 21:48:11 +00:00
|
|
|
virCommandFree(cmd);
|
2010-05-25 11:14:06 +00:00
|
|
|
VIR_FORCE_CLOSE(fd);
|
|
|
|
VIR_FREE(outactual);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-12-03 21:14:16 +00:00
|
|
|
/*
|
|
|
|
* Test string handling when no output is present.
|
|
|
|
*/
|
|
|
|
static int test17(const void *unused ATTRIBUTE_UNUSED)
|
|
|
|
{
|
2011-07-28 15:51:26 +00:00
|
|
|
virCommandPtr cmd = virCommandNew("true");
|
2010-12-03 21:14:16 +00:00
|
|
|
int ret = -1;
|
|
|
|
char *outbuf;
|
|
|
|
char *errbuf;
|
|
|
|
|
|
|
|
virCommandSetOutputBuffer(cmd, &outbuf);
|
|
|
|
if (outbuf != NULL) {
|
|
|
|
puts("buffer not sanitized at registration");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virCommandRun(cmd, NULL) < 0) {
|
|
|
|
virErrorPtr err = virGetLastError();
|
|
|
|
printf("Cannot run child %s\n", err->message);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!outbuf || *outbuf) {
|
|
|
|
puts("output buffer is not an allocated empty string");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
VIR_FREE(outbuf);
|
|
|
|
if ((outbuf = strdup("should not be leaked")) == NULL) {
|
|
|
|
puts("test framework failure");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
virCommandSetErrorBuffer(cmd, &errbuf);
|
|
|
|
if (errbuf != NULL) {
|
|
|
|
puts("buffer not sanitized at registration");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virCommandRun(cmd, NULL) < 0) {
|
|
|
|
virErrorPtr err = virGetLastError();
|
|
|
|
printf("Cannot run child %s\n", err->message);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!outbuf || *outbuf || !errbuf || *errbuf) {
|
|
|
|
puts("output buffers are not allocated empty strings");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
virCommandFree(cmd);
|
|
|
|
VIR_FREE(outbuf);
|
|
|
|
VIR_FREE(errbuf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-12-21 18:49:49 +00:00
|
|
|
/*
|
|
|
|
* Run long-running daemon, to ensure no hang.
|
|
|
|
*/
|
|
|
|
static int test18(const void *unused ATTRIBUTE_UNUSED)
|
|
|
|
{
|
|
|
|
virCommandPtr cmd = virCommandNewArgList("sleep", "100", NULL);
|
2011-08-12 21:54:49 +00:00
|
|
|
char *pidfile = virPidFileBuildPath(abs_builddir, "commandhelper");
|
2010-12-21 18:49:49 +00:00
|
|
|
pid_t pid;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (!pidfile)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
virCommandSetPidFile(cmd, pidfile);
|
|
|
|
virCommandDaemonize(cmd);
|
|
|
|
|
|
|
|
alarm(5);
|
|
|
|
if (virCommandRun(cmd, NULL) < 0) {
|
|
|
|
virErrorPtr err = virGetLastError();
|
|
|
|
printf("Cannot run child %s\n", err->message);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
alarm(0);
|
|
|
|
|
2011-08-12 21:54:49 +00:00
|
|
|
if (virPidFileRead(abs_builddir, "commandhelper", &pid) < 0) {
|
2010-12-21 18:49:49 +00:00
|
|
|
printf("cannot read pidfile\n");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2011-03-22 22:22:37 +00:00
|
|
|
|
|
|
|
virCommandFree(cmd);
|
|
|
|
cmd = NULL;
|
|
|
|
if (kill(pid, 0) != 0) {
|
|
|
|
printf("daemon should still be running\n");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2010-12-21 18:49:49 +00:00
|
|
|
while (kill(pid, SIGINT) != -1)
|
|
|
|
usleep(100*1000);
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
virCommandFree(cmd);
|
2011-05-03 16:37:13 +00:00
|
|
|
if (pidfile)
|
|
|
|
unlink(pidfile);
|
2010-12-21 18:49:49 +00:00
|
|
|
VIR_FREE(pidfile);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-03-22 22:22:37 +00:00
|
|
|
/*
|
|
|
|
* Asynchronously run long-running daemon, to ensure no hang.
|
|
|
|
*/
|
|
|
|
static int test19(const void *unused ATTRIBUTE_UNUSED)
|
|
|
|
{
|
|
|
|
virCommandPtr cmd = virCommandNewArgList("sleep", "100", NULL);
|
|
|
|
pid_t pid;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
alarm(5);
|
|
|
|
if (virCommandRunAsync(cmd, &pid) < 0) {
|
|
|
|
virErrorPtr err = virGetLastError();
|
|
|
|
printf("Cannot run child %s\n", err->message);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kill(pid, 0) != 0) {
|
|
|
|
printf("Child should still be running");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
virCommandAbort(cmd);
|
|
|
|
|
|
|
|
if (kill(pid, 0) == 0) {
|
|
|
|
printf("Child should be aborted");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
alarm(0);
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
virCommandFree(cmd);
|
|
|
|
return ret;
|
|
|
|
}
|
2010-12-21 18:49:49 +00:00
|
|
|
|
2011-05-11 15:51:30 +00:00
|
|
|
static const char *const newenv[] = {
|
|
|
|
"PATH=/usr/bin:/bin",
|
|
|
|
"HOSTNAME=test",
|
|
|
|
"LANG=C",
|
|
|
|
"HOME=/home/test",
|
|
|
|
"USER=test",
|
|
|
|
"LOGNAME=test"
|
|
|
|
"TMPDIR=/tmp",
|
|
|
|
"DISPLAY=:0.0",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2010-05-25 11:14:06 +00:00
|
|
|
static int
|
2011-04-29 16:21:20 +00:00
|
|
|
mymain(void)
|
2010-05-25 11:14:06 +00:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2011-01-28 21:22:39 +00:00
|
|
|
int fd;
|
2010-05-25 11:14:06 +00:00
|
|
|
|
|
|
|
if (chdir("/tmp") < 0)
|
|
|
|
return(EXIT_FAILURE);
|
|
|
|
|
2011-02-24 12:12:27 +00:00
|
|
|
setpgid(0, 0);
|
|
|
|
setsid();
|
|
|
|
|
2011-08-25 11:05:54 +00:00
|
|
|
|
|
|
|
/* Prime the debug/verbose settings from the env vars,
|
|
|
|
* since we're about to reset 'environ' */
|
|
|
|
virTestGetDebug();
|
|
|
|
virTestGetVerbose();
|
|
|
|
|
|
|
|
virInitialize();
|
2010-12-10 22:30:56 +00:00
|
|
|
/* Kill off any inherited fds that might interfere with our
|
|
|
|
* testing. */
|
2011-01-28 21:22:39 +00:00
|
|
|
fd = 3;
|
|
|
|
VIR_FORCE_CLOSE(fd);
|
|
|
|
fd = 4;
|
|
|
|
VIR_FORCE_CLOSE(fd);
|
|
|
|
fd = 5;
|
|
|
|
VIR_FORCE_CLOSE(fd);
|
2010-12-10 22:30:56 +00:00
|
|
|
|
2010-05-25 11:14:06 +00:00
|
|
|
environ = (char **)newenv;
|
|
|
|
|
|
|
|
# define DO_TEST(NAME) \
|
|
|
|
if (virtTestRun("Command Exec " #NAME " test", \
|
|
|
|
1, NAME, NULL) < 0) \
|
|
|
|
ret = -1
|
|
|
|
|
|
|
|
DO_TEST(test0);
|
|
|
|
DO_TEST(test1);
|
|
|
|
DO_TEST(test2);
|
|
|
|
DO_TEST(test3);
|
|
|
|
DO_TEST(test4);
|
|
|
|
DO_TEST(test5);
|
|
|
|
DO_TEST(test6);
|
|
|
|
DO_TEST(test7);
|
|
|
|
DO_TEST(test8);
|
|
|
|
DO_TEST(test9);
|
|
|
|
DO_TEST(test10);
|
|
|
|
DO_TEST(test11);
|
|
|
|
DO_TEST(test12);
|
|
|
|
DO_TEST(test13);
|
|
|
|
DO_TEST(test14);
|
|
|
|
DO_TEST(test15);
|
|
|
|
DO_TEST(test16);
|
2010-12-03 21:14:16 +00:00
|
|
|
DO_TEST(test17);
|
2010-12-21 18:49:49 +00:00
|
|
|
DO_TEST(test18);
|
2011-03-22 22:22:37 +00:00
|
|
|
DO_TEST(test19);
|
2010-05-25 11:14:06 +00:00
|
|
|
|
|
|
|
return(ret==0 ? EXIT_SUCCESS : EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
VIRT_TEST_MAIN(mymain)
|
2011-07-28 15:48:12 +00:00
|
|
|
|
|
|
|
#endif /* !WIN32 */
|