static int testLogParseFilters(const void *opaque) { int ret = -1; int nfilters; const struct testLogData *data = opaque; nfilters = virLogParseFilters(data->str); if (nfilters < 0) { if (!data->pass) { VIR_TEST_DEBUG("Got expected error: %s\n", virGetLastErrorMessage()); virResetLastError(); ret = 0; goto cleanup; } } else if (nfilters != data->count) { VIR_TEST_DEBUG("Expected number of parsed outputs is %d, " "but got %d\n", data->count, nfilters); goto cleanup; } else if (!data->pass) { VIR_TEST_DEBUG("Test should have failed\n"); goto cleanup; } ret = 0; cleanup: virLogReset(); return ret; }
static int testJSONFromString(const void *data) { const struct testInfo *info = data; virJSONValuePtr json; int ret = -1; json = virJSONValueFromString(info->doc); if (info->pass) { if (!json) { VIR_TEST_VERBOSE("Fail to parse %s\n", info->doc); ret = -1; goto cleanup; } else { VIR_TEST_DEBUG("Parsed %s\n", info->doc); } } else { if (json) { VIR_TEST_VERBOSE("Should not have parsed %s\n", info->doc); ret = -1; goto cleanup; } else { VIR_TEST_DEBUG("Fail to parse %s\n", info->doc); } } ret = 0; cleanup: virJSONValueFree(json); return ret; }
static int testCompareXMLToXMLFiles(const char *inxml, const char *outxml, unsigned int flags, testCompareNetXML2XMLResult expectResult) { char *actual = NULL; int ret; testCompareNetXML2XMLResult result = TEST_COMPARE_NET_XML2XML_RESULT_SUCCESS; virNetworkDefPtr dev = NULL; if (!(dev = virNetworkDefParseFile(inxml))) { result = TEST_COMPARE_NET_XML2XML_RESULT_FAIL_PARSE; goto cleanup; } if (expectResult == TEST_COMPARE_NET_XML2XML_RESULT_FAIL_PARSE) goto cleanup; if (!(actual = virNetworkDefFormat(dev, flags))) { result = TEST_COMPARE_NET_XML2XML_RESULT_FAIL_FORMAT; goto cleanup; } if (expectResult == TEST_COMPARE_NET_XML2XML_RESULT_FAIL_FORMAT) goto cleanup; if (virTestCompareToFile(actual, outxml) < 0) { result = TEST_COMPARE_NET_XML2XML_RESULT_FAIL_COMPARE; goto cleanup; } if (expectResult == TEST_COMPARE_NET_XML2XML_RESULT_FAIL_COMPARE) goto cleanup; cleanup: if (result == expectResult) { ret = 0; if (expectResult != TEST_COMPARE_NET_XML2XML_RESULT_SUCCESS) { VIR_TEST_DEBUG("Got expected failure code=%d msg=%s", result, virGetLastErrorMessage()); } } else { ret = -1; VIR_TEST_DEBUG("Expected result code=%d but received code=%d", expectResult, result); } virResetLastError(); VIR_FREE(actual); virNetworkDefFree(dev); return ret; }
static int cpuTestRun(const char *name, const struct data *data) { char *label = NULL; char *tmp; if (virAsprintf(&label, "CPU %s(%s): %s", apis[data->api], data->arch, name) < 0) return -1; tmp = virtTestLogContentAndReset(); VIR_FREE(tmp); if (virtTestRun(label, cpuTest[data->api], data) < 0) { if (virTestGetDebug()) { char *log; if ((log = virtTestLogContentAndReset()) && strlen(log) > 0) VIR_TEST_DEBUG("\n%s\n", log); VIR_FREE(log); } VIR_FREE(label); return -1; } VIR_FREE(label); return 0; }
static int testQemuDiskXMLToPropsValidateSchema(const void *opaque) { struct testQemuDiskXMLToJSONData *data = (void *) opaque; virBuffer debug = VIR_BUFFER_INITIALIZER; char *propsstr = NULL; char *debugmsg = NULL; int ret = 0; size_t i; if (data->fail) return EXIT_AM_SKIP; for (i = 0; i < data->nprops; i++) { if (testQEMUSchemaValidate(data->props[i], data->schemaroot, data->schema, &debug) < 0) { debugmsg = virBufferContentAndReset(&debug); propsstr = virJSONValueToString(data->props[i], true); VIR_TEST_VERBOSE("json does not conform to QAPI schema"); VIR_TEST_DEBUG("json:\n%s\ndoes not match schema. Debug output:\n %s", propsstr, NULLSTR(debugmsg)); VIR_FREE(debugmsg); VIR_FREE(propsstr); ret = -1; } virBufferFreeAndReset(&debug); } return ret; }
static int testJSONFromString(const void *data) { const struct testInfo *info = data; virJSONValuePtr json; const char *expectstr = info->expect ? info->expect : info->doc; char *formatted = NULL; int ret = -1; json = virJSONValueFromString(info->doc); if (!json) { if (info->pass) { VIR_TEST_VERBOSE("Fail to parse %s\n", info->doc); goto cleanup; } else { VIR_TEST_DEBUG("Fail to parse %s\n", info->doc); ret = 0; goto cleanup; } } if (!info->pass) { VIR_TEST_VERBOSE("Should not have parsed %s\n", info->doc); goto cleanup; } VIR_TEST_DEBUG("Parsed %s\n", info->doc); if (!(formatted = virJSONValueToString(json, false))) { VIR_TEST_VERBOSE("Failed to format json data\n"); goto cleanup; } if (STRNEQ(expectstr, formatted)) { virTestDifference(stderr, expectstr, formatted); goto cleanup; } ret = 0; cleanup: VIR_FREE(formatted); virJSONValueFree(json); return ret; }
static int testDevice(const char *path, int expect) { int actual = xenLinuxDomainDeviceID(1, path); if (actual == expect) { return 0; } else { VIR_TEST_DEBUG("Expect %-6d Actual %-6d\n", expect, actual); return -1; } }
static int testLogMatch(const void *opaque) { const struct testLogData *data = opaque; bool got = virLogProbablyLogMessage(data->str); if (got != data->pass) { VIR_TEST_DEBUG("Expected '%d' but got '%d' for '%s'\n", data->pass, got, data->str); return -1; } return 0; }
static virHashTablePtr testHashInit(int size) { virHashTablePtr hash; ssize_t i; if (!(hash = virHashCreate(size, NULL))) return NULL; /* entires are added in reverse order so that they will be linked in * collision list in the same order as in the uuids array */ for (i = ARRAY_CARDINALITY(uuids) - 1; i >= 0; i--) { ssize_t oldsize = virHashTableSize(hash); if (virHashAddEntry(hash, uuids[i], (void *) uuids[i]) < 0) { virHashFree(hash); return NULL; } if (virHashTableSize(hash) != oldsize) { VIR_TEST_DEBUG("hash grown from %zd to %zd", (size_t)oldsize, (size_t)virHashTableSize(hash)); } } for (i = 0; i < ARRAY_CARDINALITY(uuids); i++) { if (!virHashLookup(hash, uuids[i])) { VIR_TEST_VERBOSE("\nentry \"%s\" could not be found\n", uuids[i]); virHashFree(hash); return NULL; } } if (size && size != virHashTableSize(hash)) VIR_TEST_DEBUG("\n"); return hash; }
static int linuxTestCompareFiles(char *sysfs_prefix, const char *cpuinfofile, virArch arch, const char *outputfile) { int ret = -1; char *actualData = NULL; virNodeInfo nodeinfo; FILE *cpuinfo; cpuinfo = fopen(cpuinfofile, "r"); if (!cpuinfo) { fprintf(stderr, "unable to open: %s : %s\n", cpuinfofile, strerror(errno)); goto fail; } memset(&nodeinfo, 0, sizeof(nodeinfo)); if (linuxNodeInfoCPUPopulate(sysfs_prefix, cpuinfo, arch, &nodeinfo) < 0) { if (virTestGetDebug()) { virErrorPtr error = virSaveLastError(); if (error && error->code != VIR_ERR_OK) VIR_TEST_DEBUG("\n%s\n", error->message); virFreeError(error); } VIR_FORCE_FCLOSE(cpuinfo); goto fail; } VIR_FORCE_FCLOSE(cpuinfo); if (virAsprintf(&actualData, "CPUs: %u/%u, MHz: %u, Nodes: %u, Sockets: %u, " "Cores: %u, Threads: %u\n", nodeinfo.cpus, VIR_NODEINFO_MAXCPUS(nodeinfo), nodeinfo.mhz, nodeinfo.nodes, nodeinfo.sockets, nodeinfo.cores, nodeinfo.threads) < 0) goto fail; if (virtTestCompareToFile(actualData, outputfile) < 0) goto fail; ret = 0; fail: VIR_FREE(actualData); return ret; }
static void init_syms(void) { int fd; if (realsocket) return; realsocket = dlsym(RTLD_NEXT, "socket"); if (!realsocket) { VIR_TEST_DEBUG("Unable to find 'socket' symbol\n"); abort(); } fd = realsocket(AF_INET6, SOCK_STREAM, 0); if (fd < 0) return; host_has_ipv6 = true; VIR_FORCE_CLOSE(fd); }
static int testHelpStrParsing(const void *data) { const struct testInfo *info = data; char *path = NULL; char *help = NULL; unsigned int version, kvm_version; bool is_kvm; virQEMUCapsPtr flags = NULL; int ret = -1; char *got = NULL; char *expected = NULL; if (virAsprintf(&path, "%s/qemuhelpdata/%s", abs_srcdir, info->name) < 0) return -1; if (virTestLoadFile(path, &help) < 0) goto cleanup; if (!(flags = virQEMUCapsNew())) goto cleanup; if (virQEMUCapsParseHelpStr("QEMU", help, flags, &version, &is_kvm, &kvm_version, false, NULL) == -1) { if (info->error && virGetLastError()->code == info->error) ret = 0; goto cleanup; } # ifndef WITH_YAJL if (virQEMUCapsGet(info->flags, QEMU_CAPS_MONITOR_JSON)) virQEMUCapsSet(flags, QEMU_CAPS_MONITOR_JSON); # endif VIR_FREE(path); VIR_FREE(help); if (virAsprintf(&path, "%s/qemuhelpdata/%s-device", abs_srcdir, info->name) < 0) goto cleanup; if (virTestLoadFile(path, &help) < 0) goto cleanup; if (virQEMUCapsParseDeviceStr(flags, help) < 0) goto cleanup; got = virQEMUCapsFlagsString(flags); expected = virQEMUCapsFlagsString(info->flags); if (!got || !expected) goto cleanup; if (STRNEQ(got, expected)) { VIR_TEST_DEBUG("%s: computed flags do not match: got %s, expected %s\n", info->name, got, expected); if (virTestGetDebug()) printMismatchedFlags(flags, info->flags); goto cleanup; } if (version != info->version) { fprintf(stderr, "%s: parsed versions do not match: got %u, expected %u\n", info->name, version, info->version); goto cleanup; } if (is_kvm != info->is_kvm) { fprintf(stderr, "%s: parsed is_kvm flag does not match: got %u, expected %u\n", info->name, is_kvm, info->is_kvm); goto cleanup; } if (kvm_version != info->kvm_version) { fprintf(stderr, "%s: parsed KVM versions do not match: got %u, expected %u\n", info->name, kvm_version, info->kvm_version); goto cleanup; } ret = 0; cleanup: VIR_FREE(path); VIR_FREE(help); virObjectUnref(flags); VIR_FREE(got); VIR_FREE(expected); return ret; }
virCapsPtr testQemuCapsInit(void) { virCapsPtr caps; virCapsGuestPtr guest; virCapsGuestMachinePtr *machines = NULL; int nmachines = 0; if (!(caps = virCapabilitiesNew(VIR_ARCH_X86_64, false, false))) return NULL; /* Add dummy 'none' security_driver. This is equal to setting * security_driver = "none" in qemu.conf. */ if (VIR_ALLOC_N(caps->host.secModels, 1) < 0) goto cleanup; caps->host.nsecModels = 1; if (VIR_STRDUP(caps->host.secModels[0].model, "none") < 0 || VIR_STRDUP(caps->host.secModels[0].doi, "0") < 0) goto cleanup; if (!(cpuDefault = virCPUDefCopy(&cpuDefaultData)) || !(cpuHaswell = virCPUDefCopy(&cpuHaswellData))) goto cleanup; caps->host.cpu = cpuDefault; caps->host.nnumaCell_max = 4; if ((machines = testQemuAllocMachines(&nmachines)) == NULL) goto cleanup; if ((guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM, VIR_ARCH_I686, "/usr/bin/qemu", NULL, nmachines, machines)) == NULL || !virCapabilitiesAddGuestFeature(guest, "cpuselection", true, false)) goto cleanup; machines = NULL; if (virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_QEMU, NULL, NULL, 0, NULL) == NULL) goto cleanup; if ((machines = testQemuAllocMachines(&nmachines)) == NULL) goto cleanup; if (virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_KVM, "/usr/bin/qemu-kvm", NULL, nmachines, machines) == NULL) goto cleanup; machines = NULL; if ((machines = testQemuAllocNewerMachines(&nmachines)) == NULL) goto cleanup; if ((guest = virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM, VIR_ARCH_X86_64, "/usr/bin/qemu-system-x86_64", NULL, nmachines, machines)) == NULL || !virCapabilitiesAddGuestFeature(guest, "cpuselection", true, false)) goto cleanup; machines = NULL; if (virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_QEMU, NULL, NULL, 0, NULL) == NULL) goto cleanup; if ((machines = testQemuAllocMachines(&nmachines)) == NULL) goto cleanup; if (virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_KVM, "/usr/bin/kvm", NULL, nmachines, machines) == NULL) goto cleanup; machines = NULL; if (virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_KVM, "/usr/bin/kvm", NULL, 0, NULL) == NULL) goto cleanup; if (testQemuAddPPC64Guest(caps)) goto cleanup; if (testQemuAddPPC64LEGuest(caps)) goto cleanup; if (testQemuAddPPCGuest(caps)) goto cleanup; if (testQemuAddS390Guest(caps)) goto cleanup; if (testQemuAddArmGuest(caps)) goto cleanup; if (testQemuAddAARCH64Guest(caps)) goto cleanup; if (virTestGetDebug()) { char *caps_str; caps_str = virCapabilitiesFormatXML(caps); if (!caps_str) goto cleanup; VIR_TEST_DEBUG("QEMU driver capabilities:\n%s", caps_str); VIR_FREE(caps_str); } return caps; cleanup: virCapabilitiesFreeMachines(machines, nmachines); if (caps->host.cpu != cpuDefault) virCPUDefFree(cpuDefault); if (caps->host.cpu != cpuHaswell) virCPUDefFree(cpuHaswell); virObjectUnref(caps); return NULL; }
static int testCompareXMLToArgvFiles(const char *xmlfile, const char *cmdfile, virQemuXML2ArgvTestFlags flags) { char *actualxml = NULL; char *cmd = NULL; char *log = NULL; int ret = -1; virDomainDefPtr vmdef = NULL; if (virtTestLoadFile(cmdfile, &cmd) < 0) goto fail; if (!(vmdef = qemuParseCommandLineString(driver.caps, driver.xmlopt, cmd, NULL, NULL, NULL))) goto fail; if (!virtTestOOMActive()) { if ((log = virtTestLogContentAndReset()) == NULL) goto fail; if (flags & FLAG_EXPECT_WARNING) { if (*log) { VIR_TEST_DEBUG("Got expected warning from " "qemuParseCommandLineString:\n%s", log); } else { VIR_TEST_DEBUG("qemuParseCommandLineString " "should have logged a warning\n"); goto fail; } } else { /* didn't expect a warning */ if (*log) { VIR_TEST_DEBUG("Got unexpected warning from " "qemuParseCommandLineString:\n%s", log); goto fail; } } } if (testSanitizeDef(vmdef) < 0) goto fail; if (!virDomainDefCheckABIStability(vmdef, vmdef)) { VIR_TEST_DEBUG("ABI stability check failed on %s", xmlfile); goto fail; } if (!(actualxml = virDomainDefFormat(vmdef, driver.caps, 0))) goto fail; if (virtTestCompareToFile(actualxml, xmlfile) < 0) goto fail; ret = 0; fail: VIR_FREE(actualxml); VIR_FREE(cmd); VIR_FREE(log); virDomainDefFree(vmdef); return ret; }
virCapsPtr testQemuCapsInit(void) { virCapsPtr caps; if (!(caps = virCapabilitiesNew(VIR_ARCH_X86_64, false, false))) return NULL; /* Add dummy 'none' security_driver. This is equal to setting * security_driver = "none" in qemu.conf. */ if (VIR_ALLOC_N(caps->host.secModels, 1) < 0) goto cleanup; caps->host.nsecModels = 1; if (VIR_STRDUP(caps->host.secModels[0].model, "none") < 0 || VIR_STRDUP(caps->host.secModels[0].doi, "0") < 0) goto cleanup; if (!(cpuDefault = virCPUDefCopy(&cpuDefaultData)) || !(cpuHaswell = virCPUDefCopy(&cpuHaswellData)) || !(cpuPower8 = virCPUDefCopy(&cpuPower8Data)) || !(cpuPower9 = virCPUDefCopy(&cpuPower9Data))) goto cleanup; qemuTestSetHostCPU(caps, NULL); caps->host.nnumaCell_max = 4; if (testQemuAddI686Guest(caps) < 0) goto cleanup; if (testQemuAddX86_64Guest(caps) < 0) goto cleanup; if (testQemuAddPPC64Guest(caps)) goto cleanup; if (testQemuAddPPC64LEGuest(caps)) goto cleanup; if (testQemuAddPPCGuest(caps)) goto cleanup; if (testQemuAddS390Guest(caps)) goto cleanup; if (testQemuAddArmGuest(caps)) goto cleanup; if (testQemuAddAARCH64Guest(caps)) goto cleanup; if (virTestGetDebug()) { char *caps_str; caps_str = virCapabilitiesFormatXML(caps); if (!caps_str) goto cleanup; VIR_TEST_DEBUG("QEMU driver capabilities:\n%s", caps_str); VIR_FREE(caps_str); } return caps; cleanup: caps->host.cpu = NULL; virCPUDefFree(cpuDefault); virCPUDefFree(cpuHaswell); virCPUDefFree(cpuPower8); virObjectUnref(caps); return NULL; }
static int testCompareStatusXMLToXMLFiles(const void *opaque) { const struct testInfo *data = opaque; virBuffer buf = VIR_BUFFER_INITIALIZER; xmlDocPtr xml = NULL; virDomainObjPtr obj = NULL; char *expect = NULL; char *actual = NULL; char *source = NULL; char *inFile = NULL, *outActiveFile = NULL; int ret = -1; int keepBlanksDefault = xmlKeepBlanksDefault(0); if (virtTestLoadFile(data->inName, &inFile) < 0) goto cleanup; if (virtTestLoadFile(data->outActiveName, &outActiveFile) < 0) goto cleanup; /* construct faked source status XML */ virBufferAdd(&buf, testStatusXMLPrefix, -1); virBufferAdjustIndent(&buf, 2); virBufferAddStr(&buf, inFile); virBufferAdjustIndent(&buf, -2); virBufferAdd(&buf, testStatusXMLSuffix, -1); if (!(source = virBufferContentAndReset(&buf))) { VIR_TEST_DEBUG("Failed to create the source XML"); goto cleanup; } /* construct the expect string */ virBufferAdd(&buf, testStatusXMLPrefix, -1); virBufferAdjustIndent(&buf, 2); virBufferAddStr(&buf, outActiveFile); virBufferAdjustIndent(&buf, -2); virBufferAdd(&buf, testStatusXMLSuffix, -1); if (!(expect = virBufferContentAndReset(&buf))) { VIR_TEST_DEBUG("Failed to create the expect XML"); goto cleanup; } /* parse the fake source status XML */ if (!(xml = virXMLParseString(source, "(domain_status_test_XML)")) || !(obj = virDomainObjParseNode(xml, xmlDocGetRootElement(xml), driver.caps, driver.xmlopt, VIR_DOMAIN_DEF_PARSE_STATUS | VIR_DOMAIN_DEF_PARSE_ACTUAL_NET | VIR_DOMAIN_DEF_PARSE_PCI_ORIG_STATES))) { VIR_TEST_DEBUG("Failed to parse domain status XML:\n%s", source); goto cleanup; } /* format it back */ if (!(actual = virDomainObjFormat(driver.xmlopt, obj, VIR_DOMAIN_DEF_FORMAT_SECURE))) { VIR_TEST_DEBUG("Failed to format domain status XML"); goto cleanup; } if (STRNEQ(actual, expect)) { /* For status test we don't want to regenerate output to not * add the status data.*/ virtTestDifferenceFullNoRegenerate(stderr, expect, data->outActiveName, actual, data->inName); goto cleanup; } ret = 0; cleanup: xmlKeepBlanksDefault(keepBlanksDefault); xmlFreeDoc(xml); virObjectUnref(obj); VIR_FREE(expect); VIR_FREE(actual); VIR_FREE(source); VIR_FREE(inFile); VIR_FREE(outActiveFile); return ret; }