libvirt/tests/statstest.c

212 lines
5.3 KiB
C
Raw Normal View History

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/utsname.h>
#include "virstatslinux.h"
#include "internal.h"
#include "xen/block_stats.h"
2008-04-18 15:28:33 +00:00
#include "testutils.h"
#include "vircommand.h"
2008-04-18 15:28:33 +00:00
static int testDevice(const char *path, int expect)
{
int actual = xenLinuxDomainDeviceID(1, path);
if (actual == expect) {
return 0;
} else {
if (virTestGetDebug())
2008-04-18 15:28:33 +00:00
fprintf(stderr, "Expect %-6d Actual %-6d\n", expect, actual);
return -1;
}
}
2008-04-18 15:28:33 +00:00
struct testInfo
{
const char *dev;
int num;
};
static int testDeviceHelper(const void *data)
{
const struct testInfo *info = data;
return testDevice(info->dev, info->num);
}
static int
tests: simplify common setup A few of the tests were missing basic sanity checks, while most of them were doing copy-and-paste initialization (in fact, some of them pasted the argc > 1 check more than once!). It's much nicer to do things in one common place, and minimizes the size of the next patch that fixes getcwd usage. * tests/testutils.h (EXIT_AM_HARDFAIL): New define. (progname, abs_srcdir): Define for all tests. (VIRT_TEST_MAIN): Change callback signature. * tests/testutils.c (virtTestMain): Do more common init. * tests/commandtest.c (mymain): Simplify. * tests/cputest.c (mymain): Likewise. * tests/esxutilstest.c (mymain): Likewise. * tests/eventtest.c (mymain): Likewise. * tests/hashtest.c (mymain): Likewise. * tests/networkxml2xmltest.c (mymain): Likewise. * tests/nodedevxml2xmltest.c (myname): Likewise. * tests/nodeinfotest.c (mymain): Likewise. * tests/nwfilterxml2xmltest.c (mymain): Likewise. * tests/qemuargv2xmltest.c (mymain): Likewise. * tests/qemuhelptest.c (mymain): Likewise. * tests/qemuxml2argvtest.c (mymain): Likewise. * tests/qemuxml2xmltest.c (mymain): Likewise. * tests/qparamtest.c (mymain): Likewise. * tests/sexpr2xmltest.c (mymain): Likewise. * tests/sockettest.c (mymain): Likewise. * tests/statstest.c (mymain): Likewise. * tests/storagepoolxml2xmltest.c (mymain): Likewise. * tests/storagevolxml2xmltest.c (mymain): Likewise. * tests/virbuftest.c (mymain): Likewise. * tests/virshtest.c (mymain): Likewise. * tests/vmx2xmltest.c (mymain): Likewise. * tests/xencapstest.c (mymain): Likewise. * tests/xmconfigtest.c (mymain): Likewise. * tests/xml2sexprtest.c (mymain): Likewise. * tests/xml2vmxtest.c (mymain): Likewise.
2011-04-29 16:21:20 +00:00
mymain(void)
{
int ret = 0;
virCommandPtr cmd;
struct utsname ut;
/* Skip test if xend is not running. Calling xend on a non-xen
kernel causes some versions of xend to issue a crash report, so
we first probe uname results. */
uname(&ut);
if (strstr(ut.release, "xen") == NULL)
return EXIT_AM_SKIP;
cmd = virCommandNewArgList("/usr/sbin/xend", "status", NULL);
util: make it easier to grab only regular command exit Auditing all callers of virCommandRun and virCommandWait that passed a non-NULL pointer for exit status turned up some interesting observations. Many callers were merely passing a pointer to avoid the overall command dying, but without caring what the exit status was - but these callers would be better off treating a child death by signal as an abnormal exit. Other callers were actually acting on the status, but not all of them remembered to filter by WIFEXITED and convert with WEXITSTATUS; depending on the platform, this can result in a status being reported as 256 times too big. And among those that correctly parse the output, it gets rather verbose. Finally, there were the callers that explicitly checked that the status was 0, and gave their own message, but with fewer details than what virCommand gives for free. So the best idea is to move the complexity out of callers and into virCommand - by default, we return the actual exit status already cleaned through WEXITSTATUS and treat signals as a failed command; but the few callers that care can ask for raw status and act on it themselves. * src/util/vircommand.h (virCommandRawStatus): New prototype. * src/libvirt_private.syms (util/command.h): Export it. * docs/internals/command.html.in: Document it. * src/util/vircommand.c (virCommandRawStatus): New function. (virCommandWait): Adjust semantics. * tests/commandtest.c (test1): Test it. * daemon/remote.c (remoteDispatchAuthPolkit): Adjust callers. * src/access/viraccessdriverpolkit.c (virAccessDriverPolkitCheck): Likewise. * src/fdstream.c (virFDStreamCloseInt): Likewise. * src/lxc/lxc_process.c (virLXCProcessStart): Likewise. * src/qemu/qemu_command.c (qemuCreateInBridgePortWithHelper): Likewise. * src/xen/xen_driver.c (xenUnifiedXendProbe): Simplify. * tests/reconnect.c (mymain): Likewise. * tests/statstest.c (mymain): Likewise. * src/bhyve/bhyve_process.c (virBhyveProcessStart) (virBhyveProcessStop): Don't overwrite virCommand error. * src/libvirt.c (virConnectAuthGainPolkit): Likewise. * src/openvz/openvz_driver.c (openvzDomainGetBarrierLimit) (openvzDomainSetBarrierLimit): Likewise. * src/util/virebtables.c (virEbTablesOnceInit): Likewise. * src/util/viriptables.c (virIpTablesOnceInit): Likewise. * src/util/virnetdevveth.c (virNetDevVethCreate): Fix debug message. * src/qemu/qemu_capabilities.c (virQEMUCapsInitQMP): Add comment. * src/storage/storage_backend_iscsi.c (virStorageBackendISCSINodeUpdate): Likewise. Signed-off-by: Eric Blake <eblake@redhat.com>
2014-02-20 00:32:19 +00:00
if (virCommandRun(cmd, NULL) < 0) {
virCommandFree(cmd);
return EXIT_AM_SKIP;
}
virCommandFree(cmd);
/* Some of our tests deliberately test failure cases, so
* register a handler to stop error messages cluttering
* up display
*/
virtTestQuiesceLibvirtErrors(false);
#define DO_TEST(dev, num) \
2008-04-18 15:28:33 +00:00
do { \
struct testInfo info = { dev, num }; \
if (virtTestRun("Device " dev " -> " # num, \
testDeviceHelper, &info) < 0) \
2008-04-18 15:28:33 +00:00
ret = -1; \
} while (0)
/********************************
* Xen paravirt disks
********************************/
DO_TEST("xvd", -1);
/* first valid disk */
2008-04-18 15:28:33 +00:00
DO_TEST("xvda", 51712);
DO_TEST("xvda1", 51713);
DO_TEST("xvda15", 51727);
/* Last non-extended disk */
2008-04-18 15:28:33 +00:00
DO_TEST("xvdp", 51952);
DO_TEST("xvdp1", 51953);
DO_TEST("xvdp15", 51967);
/* First extended disk */
DO_TEST("xvdq", 268439552);
DO_TEST("xvdq1", 268439553);
DO_TEST("xvdq15", 268439567);
/* Last extended disk */
DO_TEST("xvdiz", 268501760);
DO_TEST("xvdiz1", 268501761);
DO_TEST("xvdiz15", 268501775);
/* Disk letter too large */
DO_TEST("xvdja", -1);
/* missing disk letter */
2008-04-18 15:28:33 +00:00
DO_TEST("xvd1", -1);
/* partition too large */
2008-04-18 15:28:33 +00:00
DO_TEST("xvda16", -1);
/* partition too small */
2008-04-18 15:28:33 +00:00
DO_TEST("xvda0", -1);
/* leading zeros */
2008-04-18 15:28:33 +00:00
DO_TEST("xvda01", -1);
/* leading + */
2008-04-18 15:28:33 +00:00
DO_TEST("xvda+1", -1);
/* leading - */
2008-04-18 15:28:33 +00:00
DO_TEST("xvda-1", -1);
/********************************
* IDE disks
********************************/
DO_TEST("hd", -1);
/* first numbered disk */
2008-04-18 15:28:33 +00:00
DO_TEST("hda", 768);
DO_TEST("hda1", 769);
DO_TEST("hda63", 831);
/* second numbered disk */
DO_TEST("hdb", 832);
DO_TEST("hdb1", 833);
DO_TEST("hdb63", 895);
/* third numbered disk */
DO_TEST("hdc", 5632);
DO_TEST("hdc1", 5633);
DO_TEST("hdc63", 5695);
/* fourth numbered disk */
DO_TEST("hdd", 5696);
DO_TEST("hdd1", 5697);
DO_TEST("hdd63", 5759);
/* last valid disk */
DO_TEST("hdt", 23360);
DO_TEST("hdt1", 23361);
DO_TEST("hdt63", 23423);
/* Disk letter to large */
2008-04-18 15:28:33 +00:00
DO_TEST("hdu", -1);
/* missing disk letter */
2008-04-18 15:28:33 +00:00
DO_TEST("hd1", -1);
/* partition too large */
2008-04-18 15:28:33 +00:00
DO_TEST("hda64", -1);
/* partition too small */
2008-04-18 15:28:33 +00:00
DO_TEST("hda0", -1);
/********************************
* SCSI disks
********************************/
DO_TEST("sd", -1);
/* first valid disk */
2008-04-18 15:28:33 +00:00
DO_TEST("sda", 2048);
DO_TEST("sda1", 2049);
DO_TEST("sda15", 2063);
/* last valid disk of first SCSI major number */
2008-04-18 15:28:33 +00:00
DO_TEST("sdp", 2288);
DO_TEST("sdp1", 2289);
DO_TEST("sdp15", 2303);
/* first valid disk of second SCSI major number */
2008-04-18 15:28:33 +00:00
DO_TEST("sdq", 16640);
DO_TEST("sdq1", 16641);
DO_TEST("sdq15", 16655);
/* last valid single letter disk */
2008-04-18 15:28:33 +00:00
DO_TEST("sdz", 16784);
DO_TEST("sdz1", 16785);
DO_TEST("sdz15", 16799);
/* first valid dual letter disk */
2008-04-18 15:28:33 +00:00
DO_TEST("sdaa", 16800);
DO_TEST("sdaa1", 16801);
DO_TEST("sdaa15", 16815);
/* second valid dual letter disk */
2008-04-18 15:28:33 +00:00
DO_TEST("sdab", 16816);
DO_TEST("sdab1", 16817);
DO_TEST("sdab15", 16831);
/* first letter of second sequence of dual letter disk */
2008-04-18 15:28:33 +00:00
DO_TEST("sdba", 17216);
DO_TEST("sdba1", 17217);
DO_TEST("sdba15", 17231);
/* last valid dual letter disk */
2008-04-18 15:28:33 +00:00
DO_TEST("sdiv", 34800);
DO_TEST("sdiv1", 34801);
DO_TEST("sdiv15", 34815);
/* Disk letter too large */
2008-04-18 15:28:33 +00:00
DO_TEST("sdix", -1);
/* missing disk letter */
2008-04-18 15:28:33 +00:00
DO_TEST("sd1", -1);
/* partition too large */
2008-04-18 15:28:33 +00:00
DO_TEST("sda16", -1);
/* partition too small */
2008-04-18 15:28:33 +00:00
DO_TEST("sda0", -1);
/* Path stripping */
2008-04-18 15:28:33 +00:00
DO_TEST("/dev", -1);
DO_TEST("/dev/", -1);
DO_TEST("/dev/xvd", -1);
DO_TEST("/dev/xvda", 51712);
DO_TEST("/dev/xvda1", 51713);
DO_TEST("/dev/xvda15", 51727);
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}
VIRT_TEST_MAIN(mymain)