Added tests for virsh domid, domname, domstat, domuuid, dominfo, list & nodeinfo commands

This commit is contained in:
Daniel P. Berrange 2006-08-24 21:46:28 +00:00
parent 3c75dc33e4
commit b1ca7467f4
18 changed files with 519 additions and 5 deletions

View File

@ -1,3 +1,13 @@
Thu Aug 24 16:43:47 EDT 2006 Daniel Berrange <berrange@redhat.com>
* tests/virshtest.c: Test suite for validating output / operation
of various virsh commands.
* tests/virshdata/*.txt: Known good output for validating results
during testing
* tests/testutils.h, tests/testutils.c: Added convenience method
for forking a child process & capturing its output.
* tests/Makefile.am: Enable POSIX / XOpen standards
Thu Aug 24 11:03:42 EDT 2006 Daniel Berrange <berrange@redhat.com>
* tests/Makefile.am: Added a 'valgrind' target which simply

View File

@ -256,4 +256,5 @@ AC_OUTPUT(Makefile src/Makefile include/Makefile docs/Makefile \
libvirt.pc libvirt.spec \
include/libvirt/Makefile include/libvirt/libvirt.h \
python/Makefile python/tests/Makefile \
tests/Makefile proxy/Makefile)
tests/Makefile proxy/Makefile \
tests/virshdata/Makefile)

View File

@ -5,3 +5,4 @@ Makefile.in
xmlrpctest
sexpr2xmltest
xml2sexprtest
virshtest

View File

@ -1,5 +1,7 @@
## Process this file with automake to produce Makefile.in
SUBDIRS = virshdata
LIBVIRT = $(top_builddir)/src/.libs/libvirt.a
INCLUDES = \
@ -7,7 +9,8 @@ INCLUDES = \
-I$(top_builddir)/src \
-I$(top_srcdir)/include \
-I$(top_srcdir)/src \
@LIBXML_CFLAGS@
@LIBXML_CFLAGS@ \
-D_XOPEN_SOURCE=600 -D_POSIX_C_SOURCE=199506L
LDADDS = \
@STATIC_BINARIES@ \
@ -16,9 +19,9 @@ LDADDS = \
EXTRA_DIST = xmlrpcserver.py
noinst_PROGRAMS = xmlrpctest xml2sexprtest sexpr2xmltest
noinst_PROGRAMS = xmlrpctest xml2sexprtest sexpr2xmltest virshtest
TESTS = xml2sexprtest sexpr2xmltest
TESTS = xml2sexprtest sexpr2xmltest virshtest
valgrind:
$(MAKE) check TESTS_ENVIRONMENT="valgrind --quiet"
@ -45,5 +48,11 @@ sexpr2xmltest_SOURCES = \
sexpr2xmltest_LDFLAGS =
sexpr2xmltest_LDADD = $(LDADDS)
virshtest_SOURCES = \
virshtest.c \
testutils.c testutils.h
virshtest_LDFLAGS =
virshtest_LDADD = $(LDADDS)
$(LIBVIRT):
-@(cd $(top_builddir)/src && $(MAKE) MAKEFLAGS+=--silent)

View File

@ -15,8 +15,11 @@
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#include <limits.h>
#include <paths.h>
#include "testutils.h"
#define GETTIMEOFDAY(T) gettimeofday(T, NULL)
@ -105,3 +108,91 @@ int virtTestLoadFile(const char *name,
return st.st_size;
}
static
void virtTestCaptureProgramExecChild(const char *const argv[],
int pipefd) {
int i;
int open_max;
int stdinfd = -1;
int stderrfd = -1;
const char *const env[] = {
"LANG=C",
NULL
};
if ((stdinfd = open(_PATH_DEVNULL, O_RDONLY)) < 0)
goto cleanup;
if ((stderrfd = open(_PATH_DEVNULL, O_WRONLY)) < 0)
goto cleanup;
open_max = sysconf (_SC_OPEN_MAX);
for (i = 0; i < open_max; i++) {
if (i != stdinfd &&
i != stderrfd &&
i != pipefd)
close(i);
}
if (dup2(stdinfd, STDIN_FILENO) != STDIN_FILENO)
goto cleanup;
if (dup2(pipefd, STDOUT_FILENO) != STDOUT_FILENO)
goto cleanup;
if (dup2(stderrfd, STDERR_FILENO) != STDERR_FILENO)
goto cleanup;
/* SUS is crazy here, hence the cast */
execve(argv[0], (char *const*)argv, (char *const*)env);
cleanup:
if (stdinfd != -1)
close(stdinfd);
if (stderrfd != -1)
close(stderrfd);
}
int virtTestCaptureProgramOutput(const char *const argv[],
char **buf,
int buflen) {
int pipefd[2];
if (pipe(pipefd) < 0)
return -1;
int pid = fork();
switch (pid) {
case 0:
close(pipefd[0]);
virtTestCaptureProgramExecChild(argv, pipefd[1]);
close(pipefd[1]);
_exit(1);
case -1:
return -1;
default:
{
int got = 0;
int ret = -1;
int want = buflen-1;
close(pipefd[1]);
while (want) {
if ((ret = read(pipefd[0], (*buf)+got, want)) <= 0)
break;
got += ret;
want -= ret;
}
close(pipefd[0]);
if (!ret)
(*buf)[got] = '\0';
waitpid(pid, NULL, 0);
return ret;
}
}
}

View File

@ -28,6 +28,9 @@ int virtTestRun (const char *title,
int virtTestLoadFile(const char *name,
char **buf,
int buflen);
int virtTestCaptureProgramOutput(const char *const argv[],
char **buf,
int buflen);
#ifdef __cplusplus
}

View File

@ -0,0 +1,2 @@
Makefile
Makefile.in

View File

@ -0,0 +1,2 @@
EXTRA_DIST = $(wildcard *.txt)

View File

@ -0,0 +1,2 @@
1

View File

@ -0,0 +1,9 @@
Id: 1
Name: fc4
UUID: ef861801-45b9-11cb-88e3-afbfe5370493
OS Type: linux
State: running
CPU(s): 1
Max memory: 131072 kB
Used memory: 131072 kB

View File

@ -0,0 +1,2 @@
fc4

View File

@ -0,0 +1,2 @@
running

View File

@ -0,0 +1,2 @@
ef861801-45b9-11cb-88e3-afbfe5370493

View File

@ -0,0 +1,5 @@
Id Name State
----------------------------------
0 fv0 running
1 fc4 running

View File

@ -0,0 +1,4 @@
Id Name State
----------------------------------
0 Domain-0 running

View File

@ -0,0 +1,9 @@
CPU model: i986
CPU(s): 50
CPU frequency: 6000 MHz
CPU socket(s): 4
Core(s) per socket: 4
Thread(s) per core: 2
NUMA cell(s): 4
Memory size: 137438953472 kB

View File

@ -0,0 +1,9 @@
CPU model: i686
CPU(s): 16
CPU frequency: 1400 MHz
CPU socket(s): 2
Core(s) per socket: 2
Thread(s) per core: 2
NUMA cell(s): 2
Memory size: 3145728 kB

351
tests/virshtest.c Normal file
View File

@ -0,0 +1,351 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "xml.h"
#include "testutils.h"
#include "internal.h"
static char *progname;
#define MAX_FILE 4096
static int testFilterLine(char *buffer,
const char *toRemove) {
char *start;
char *end;
if (!(start = strstr(buffer, toRemove)))
return -1;
if (!(end = strstr(start+1, "\n"))) {
*start = '\0';
} else {
memmove(start, end, strlen(end)+1);
}
return 0;
}
static int testCompareOutput(const char *expect, const char *filter, const char *const argv[]) {
char expectData[MAX_FILE];
char actualData[MAX_FILE];
char *expectPtr = &(expectData[0]);
char *actualPtr = &(actualData[0]);
if (virtTestLoadFile(expect, &expectPtr, MAX_FILE) < 0)
return -1;
if (virtTestCaptureProgramOutput(argv, &actualPtr, MAX_FILE) < 0)
return -1;
if (filter)
if (testFilterLine(actualData, filter) < 0)
return -1;
if (getenv("DEBUG_TESTS")) {
printf("Expect %d '%s'\n", strlen(expectData), expectData);
printf("Actual %d '%s'\n", strlen(actualData), actualData);
}
if (strcmp(expectData, actualData))
return -1;
return 0;
}
#define VIRSH_DEFAULT "../src/virsh", \
"--connect", \
"test:///default"
static char *custom_uri;
#define VIRSH_CUSTOM "../src/virsh", \
"--connect", \
custom_uri
static int testCompareListDefault(void *data ATTRIBUTE_UNUSED) {
const char *const argv[] = {
VIRSH_DEFAULT,
"list",
NULL
};
return testCompareOutput("virshdata/list-default.txt",
NULL,
argv);
}
static int testCompareListCustom(void *data ATTRIBUTE_UNUSED) {
const char *const argv[] = {
VIRSH_CUSTOM,
"list",
NULL
};
return testCompareOutput("virshdata/list-custom.txt",
NULL,
argv);
}
static int testCompareNodeinfoDefault(void *data ATTRIBUTE_UNUSED) {
const char *const argv[] = {
VIRSH_DEFAULT,
"nodeinfo",
NULL
};
return testCompareOutput("virshdata/nodeinfo-default.txt",
NULL,
argv);
}
static int testCompareNodeinfoCustom(void *data ATTRIBUTE_UNUSED) {
const char *const argv[] = {
VIRSH_CUSTOM,
"nodeinfo",
NULL
};
return testCompareOutput("virshdata/nodeinfo-custom.txt",
NULL,
argv);
}
static int testCompareDominfoByID(void *data ATTRIBUTE_UNUSED) {
const char *const argv[] = {
VIRSH_CUSTOM,
"dominfo",
"1",
NULL
};
return testCompareOutput("virshdata/dominfo-fc4.txt",
"\nCPU time:",
argv);
}
static int testCompareDominfoByUUID(void *data ATTRIBUTE_UNUSED) {
const char *const argv[] = {
VIRSH_CUSTOM,
"dominfo",
"ef861801-45b9-11cb-88e3-afbfe5370493",
NULL
};
return testCompareOutput("virshdata/dominfo-fc4.txt",
"\nCPU time:",
argv);
}
static int testCompareDominfoByName(void *data ATTRIBUTE_UNUSED) {
const char *const argv[] = {
VIRSH_CUSTOM,
"dominfo",
"fc4",
NULL
};
return testCompareOutput("virshdata/dominfo-fc4.txt",
"\nCPU time:",
argv);
}
static int testCompareDomuuidByID(void *data ATTRIBUTE_UNUSED) {
const char *const argv[] = {
VIRSH_CUSTOM,
"domuuid",
"1",
NULL
};
return testCompareOutput("virshdata/domuuid-fc4.txt",
NULL,
argv);
}
static int testCompareDomuuidByName(void *data ATTRIBUTE_UNUSED) {
const char *const argv[] = {
VIRSH_CUSTOM,
"domuuid",
"fc4",
NULL
};
return testCompareOutput("virshdata/domuuid-fc4.txt",
NULL,
argv);
}
static int testCompareDomidByName(void *data ATTRIBUTE_UNUSED) {
const char *const argv[] = {
VIRSH_CUSTOM,
"domid",
"fc4",
NULL
};
return testCompareOutput("virshdata/domid-fc4.txt",
NULL,
argv);
}
static int testCompareDomidByUUID(void *data ATTRIBUTE_UNUSED) {
const char *const argv[] = {
VIRSH_CUSTOM,
"domid",
"ef861801-45b9-11cb-88e3-afbfe5370493",
NULL
};
return testCompareOutput("virshdata/domid-fc4.txt",
NULL,
argv);
}
static int testCompareDomnameByID(void *data ATTRIBUTE_UNUSED) {
const char *const argv[] = {
VIRSH_CUSTOM,
"domname",
"1",
NULL
};
return testCompareOutput("virshdata/domname-fc4.txt",
NULL,
argv);
}
static int testCompareDomnameByUUID(void *data ATTRIBUTE_UNUSED) {
const char *const argv[] = {
VIRSH_CUSTOM,
"domname",
"ef861801-45b9-11cb-88e3-afbfe5370493",
NULL
};
return testCompareOutput("virshdata/domname-fc4.txt",
NULL,
argv);
}
static int testCompareDomstateByID(void *data ATTRIBUTE_UNUSED) {
const char *const argv[] = {
VIRSH_CUSTOM,
"domstate",
"1",
NULL
};
return testCompareOutput("virshdata/domstate-fc4.txt",
NULL,
argv);
}
static int testCompareDomstateByUUID(void *data ATTRIBUTE_UNUSED) {
const char *const argv[] = {
VIRSH_CUSTOM,
"domstate",
"ef861801-45b9-11cb-88e3-afbfe5370493",
NULL
};
return testCompareOutput("virshdata/domstate-fc4.txt",
NULL,
argv);
}
static int testCompareDomstateByName(void *data ATTRIBUTE_UNUSED) {
const char *const argv[] = {
VIRSH_CUSTOM,
"domstate",
"fc4",
NULL
};
return testCompareOutput("virshdata/domstate-fc4.txt",
NULL,
argv);
}
int
main(int argc, char **argv)
{
int ret = 0;
char cwd[PATH_MAX];
char buffer[PATH_MAX];
if (!getcwd(cwd, PATH_MAX-1))
return 1;
snprintf(buffer, PATH_MAX-1, "test://%s/../docs/testnode.xml", cwd);
buffer[PATH_MAX-1] = '\0';
progname = argv[0];
custom_uri = buffer;
if (argc > 1) {
fprintf(stderr, "Usage: %s\n", progname);
exit(EXIT_FAILURE);
}
if (virtTestRun("virsh list (default)",
1, testCompareListDefault, NULL) != 0)
ret = -1;
if (virtTestRun("virsh list (custom)",
1, testCompareListCustom, NULL) != 0)
ret = -1;
if (virtTestRun("virsh nodeinfo (default)",
1, testCompareNodeinfoDefault, NULL) != 0)
ret = -1;
if (virtTestRun("virsh nodeinfo (custom)",
1, testCompareNodeinfoCustom, NULL) != 0)
ret = -1;
if (virtTestRun("virsh dominfo (by id)",
1, testCompareDominfoByID, NULL) != 0)
ret = -1;
if (virtTestRun("virsh dominfo (by uuid)",
1, testCompareDominfoByUUID, NULL) != 0)
ret = -1;
if (virtTestRun("virsh dominfo (by name)",
1, testCompareDominfoByName, NULL) != 0)
ret = -1;
if (virtTestRun("virsh domid (by name)",
1, testCompareDomidByName, NULL) != 0)
ret = -1;
if (virtTestRun("virsh domid (by uuid)",
1, testCompareDomidByUUID, NULL) != 0)
ret = -1;
if (virtTestRun("virsh domuuid (by id)",
1, testCompareDomuuidByID, NULL) != 0)
ret = -1;
if (virtTestRun("virsh domuuid (by name)",
1, testCompareDomuuidByName, NULL) != 0)
ret = -1;
if (virtTestRun("virsh domname (by id)",
1, testCompareDomnameByID, NULL) != 0)
ret = -1;
if (virtTestRun("virsh domname (by uuid)",
1, testCompareDomnameByUUID, NULL) != 0)
ret = -1;
if (virtTestRun("virsh domstate (by id)",
1, testCompareDomstateByID, NULL) != 0)
ret = -1;
if (virtTestRun("virsh domstate (by uuid)",
1, testCompareDomstateByUUID, NULL) != 0)
ret = -1;
if (virtTestRun("virsh domstate (by name)",
1, testCompareDomstateByName, NULL) != 0)
ret = -1;
exit(ret==0 ? EXIT_SUCCESS : EXIT_FAILURE);
}