static int testCompareXMLtoXMLFiles(const char *xml) { char *xmlData = NULL; char *actual = NULL; char *pathResult = NULL; int ret = -1; virVBoxSnapshotConfMachinePtr machine = NULL; if (VIR_STRDUP(pathResult, abs_builddir "/vboxsnapshotxmldata/testResult.vbox") < 0) return -1; if (virFileMakePath(abs_builddir "/vboxsnapshotxmldata") < 0) goto cleanup; if (virTestLoadFile(xml, &xmlData) < 0) goto cleanup; if (!(machine = virVBoxSnapshotConfLoadVboxFile(xml, (char*)""))) goto cleanup; if (virVBoxSnapshotConfSaveVboxFile(machine, pathResult) < 0) goto cleanup; if (virTestLoadFile(pathResult, &actual) < 0) goto cleanup; if (!(actual = testFilterXML(actual))) goto cleanup; if (!(xmlData = testFilterXML(xmlData))) goto cleanup; if (STRNEQ(actual, xmlData)) { virTestDifference(stderr, xmlData, actual); goto cleanup; } ret = 0; cleanup: unlink(pathResult); rmdir(abs_builddir "/vboxsnapshotxmldata"); VIR_FREE(xmlData); VIR_FREE(actual); virVBoxSnapshotConfMachineFree(machine); VIR_FREE(pathResult); return ret; }
static int testCompareXMLToXMLFiles(const char *xml) { char *xmlData = NULL; char *actual = NULL; int ret = -1; virInterfaceDefPtr dev = NULL; if (virTestLoadFile(xml, &xmlData) < 0) goto fail; if (!(dev = virInterfaceDefParseString(xmlData))) goto fail; if (!(actual = virInterfaceDefFormat(dev))) goto fail; if (STRNEQ(xmlData, actual)) { virTestDifferenceFull(stderr, xmlData, xml, actual, NULL); goto fail; } ret = 0; fail: VIR_FREE(xmlData); VIR_FREE(actual); virInterfaceDefFree(dev); return ret; }
static int testCompareFiles(const char *vmx, const char *xml) { int ret = -1; char *vmxData = NULL; char *formatted = NULL; virDomainDefPtr def = NULL; if (virTestLoadFile(vmx, &vmxData) < 0) goto cleanup; if (!(def = virVMXParseConfig(&ctx, xmlopt, caps, vmxData))) goto cleanup; if (!virDomainDefCheckABIStability(def, def, xmlopt)) { fprintf(stderr, "ABI stability check failed on %s", vmx); goto cleanup; } if (!(formatted = virDomainDefFormat(def, caps, VIR_DOMAIN_DEF_FORMAT_SECURE))) goto cleanup; if (virTestCompareToFile(formatted, xml) < 0) goto cleanup; ret = 0; cleanup: VIR_FREE(vmxData); VIR_FREE(formatted); virDomainDefFree(def); return ret; }
static int testCompareFiles(const char *xml, const char *sexpr) { char *sexprData = NULL; char *gotxml = NULL; int id; char * tty; int vncport; int ret = -1; virDomainDefPtr def = NULL; virConnectPtr conn; struct _xenUnifiedPrivate priv; conn = virGetConnect(); if (!conn) goto fail; if (virTestLoadFile(sexpr, &sexprData) < 0) goto fail; memset(&priv, 0, sizeof(priv)); /* Many puppies died to bring you this code. */ priv.caps = caps; conn->privateData = &priv; if (virMutexInit(&priv.lock) < 0) goto fail; if (xenGetDomIdFromSxprString(sexprData, &id) < 0) goto fail; xenUnifiedLock(&priv); tty = xenStoreDomainGetConsolePath(conn, id); vncport = xenStoreDomainGetVNCPort(conn, id); xenUnifiedUnlock(&priv); if (!(def = xenParseSxprString(sexprData, tty, vncport, caps, xmlopt))) goto fail; if (!virDomainDefCheckABIStability(def, def)) { fprintf(stderr, "ABI stability check failed on %s", xml); goto fail; } if (!(gotxml = virDomainDefFormat(def, caps, 0))) goto fail; if (virTestCompareToFile(gotxml, xml) < 0) goto fail; ret = 0; fail: VIR_FREE(sexprData); VIR_FREE(gotxml); virDomainDefFree(def); virObjectUnref(conn); return ret; }
static int testJSONDeflatten(const void *data) { const struct testInfo *info = data; virJSONValuePtr injson = NULL; virJSONValuePtr deflattened = NULL; char *infile = NULL; char *indata = NULL; char *outfile = NULL; char *actual = NULL; int ret = -1; if (virAsprintf(&infile, "%s/virjsondata/deflatten-%s-in.json", abs_srcdir, info->name) < 0 || virAsprintf(&outfile, "%s/virjsondata/deflatten-%s-out.json", abs_srcdir, info->name) < 0) goto cleanup; if (virTestLoadFile(infile, &indata) < 0) goto cleanup; if (!(injson = virJSONValueFromString(indata))) goto cleanup; if ((deflattened = virJSONValueObjectDeflatten(injson))) { if (!info->pass) { VIR_TEST_VERBOSE("%s: deflattening should have failed\n", info->name); goto cleanup; } } else { if (!info->pass) ret = 0; goto cleanup; } if (!(actual = virJSONValueToString(deflattened, true))) goto cleanup; if (virTestCompareToFile(actual, outfile) < 0) goto cleanup; ret = 0; cleanup: virJSONValueFree(injson); virJSONValueFree(deflattened); VIR_FREE(infile); VIR_FREE(indata); VIR_FREE(outfile); VIR_FREE(actual); return ret; }
static int testCompareXMLToXMLFiles(const char *xml) { char *xmlData = NULL; char *actual = NULL; int ret = -1; virNodeDeviceDefPtr dev = NULL; virNodeDevCapsDefPtr caps; if (virTestLoadFile(xml, &xmlData) < 0) goto fail; if (!(dev = virNodeDeviceDefParseString(xmlData, EXISTING_DEVICE, NULL))) goto fail; /* Calculate some things that are not read in */ for (caps = dev->caps; caps; caps = caps->next) { virNodeDevCapDataPtr data = &caps->data; if (caps->data.type == VIR_NODE_DEV_CAP_STORAGE) { if (data->storage.flags & VIR_NODE_DEV_CAP_STORAGE_REMOVABLE) { if (data->storage.flags & VIR_NODE_DEV_CAP_STORAGE_REMOVABLE_MEDIA_AVAILABLE) { data->storage.logical_block_size = 2048; data->storage.num_blocks = data->storage.removable_media_size / data->storage.logical_block_size; } } else { data->storage.logical_block_size = 512; data->storage.num_blocks = data->storage.size / data->storage.logical_block_size; } } } if (!(actual = virNodeDeviceDefFormat(dev))) goto fail; if (STRNEQ(xmlData, actual)) { virTestDifferenceFull(stderr, xmlData, xml, actual, NULL); goto fail; } ret = 0; fail: VIR_FREE(xmlData); VIR_FREE(actual); virNodeDeviceDefFree(dev); return ret; }
static int testCompareXMLToXMLFiles(const char *netxml, const char *updatexml, const char *outxml, unsigned int flags, unsigned int command, unsigned int section, int parentIndex, bool expectFailure) { char *updateXmlData = NULL; char *actual = NULL; int ret = -1; virNetworkDefPtr def = NULL; if (virTestLoadFile(updatexml, &updateXmlData) < 0) goto error; if (!(def = virNetworkDefParseFile(netxml))) goto fail; if (virNetworkDefUpdateSection(def, command, section, parentIndex, updateXmlData, 0) < 0) goto fail; if (!(actual = virNetworkDefFormat(def, flags))) goto fail; if (!expectFailure) { if (virTestCompareToFile(actual, outxml) < 0) goto error; } ret = 0; fail: if (expectFailure) { if (ret == 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", "Failed to fail."); ret = -1; } else { virResetLastError(); ret = 0; } } error: VIR_FREE(updateXmlData); VIR_FREE(actual); virNetworkDefFree(def); return ret; }
static int testCompareFormatXML(const char *xmcfg, const char *xml) { char *xmcfgData = NULL; char *gotxml = NULL; virConfPtr conf = NULL; int ret = -1; virConnectPtr conn; struct _xenUnifiedPrivate priv; virDomainDefPtr def = NULL; conn = virGetConnect(); if (!conn) goto fail; if (virTestLoadFile(xmcfg, &xmcfgData) < 0) goto fail; /* Many puppies died to bring you this code. */ priv.caps = caps; conn->privateData = &priv; if (!(conf = virConfReadString(xmcfgData, 0))) goto fail; if (!(def = xenParseXM(conf, caps, xmlopt))) goto fail; if (!(gotxml = virDomainDefFormat(def, caps, VIR_DOMAIN_DEF_FORMAT_SECURE))) goto fail; if (virTestCompareToFile(gotxml, xml) < 0) goto fail; ret = 0; fail: if (conf) virConfFree(conf); VIR_FREE(xmcfgData); VIR_FREE(gotxml); virDomainDefFree(def); virObjectUnref(conn); return ret; }
static int testCompareXMLToConfigFiles(const char *xmlfile, const char *configfile, bool expectError) { int ret = -1; char *config = NULL; char *actualxml = NULL; virDomainDefPtr vmdef = NULL; if (virTestLoadFile(configfile, &config) < 0) goto fail; vmdef = lxcParseConfigString(config, caps, xmlopt); if ((vmdef && expectError) || (!vmdef && !expectError)) goto fail; if (vmdef) { if (testSanitizeDef(vmdef) < 0) goto fail; if (!(actualxml = virDomainDefFormat(vmdef, caps, 0))) goto fail; if (virTestCompareToFile(actualxml, xmlfile) < 0) goto fail; } ret = 0; fail: VIR_FREE(actualxml); VIR_FREE(config); virDomainDefFree(vmdef); return ret; }
static int testVerStrParse(const void *data) { const struct testInfo *info = data; int ret = -1; char *path = NULL; char *databuf = NULL; unsigned long version; int vmware_type; if (virAsprintf(&path, "%s/vmwareverdata/%s.txt", abs_srcdir, info->name) < 0) return -1; if (virTestLoadFile(path, &databuf) < 0) goto cleanup; if ((vmware_type = vmwareDriverTypeFromString(info->vmware_type)) < 0) goto cleanup; if (vmwareParseVersionStr(vmware_type, databuf, &version) < 0) goto cleanup; if (version != info->version) { fprintf(stderr, "%s: parsed versions do not match: got %lu, " "expected %lu\n", info->name, version, info->version); goto cleanup; } ret = 0; cleanup: VIR_FREE(path); VIR_FREE(databuf); return ret; }
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; }
static int testQemuHotplug(const void *data) { int ret = -1; struct qemuHotplugTestData *test = (struct qemuHotplugTestData *) data; char *domain_filename = NULL; char *device_filename = NULL; char *result_filename = NULL; char *domain_xml = NULL; char *device_xml = NULL; char *result_xml = NULL; const char *const *tmp; bool fail = test->fail; bool keep = test->keep; unsigned int device_parse_flags = 0; virDomainObjPtr vm = NULL; virDomainDeviceDefPtr dev = NULL; virCapsPtr caps = NULL; qemuMonitorTestPtr test_mon = NULL; qemuDomainObjPrivatePtr priv = NULL; if (virAsprintf(&domain_filename, "%s/qemuhotplugtestdomains/qemuhotplug-%s.xml", abs_srcdir, test->domain_filename) < 0 || virAsprintf(&device_filename, "%s/qemuhotplugtestdevices/qemuhotplug-%s.xml", abs_srcdir, test->device_filename) < 0 || virAsprintf(&result_filename, "%s/qemuhotplugtestdomains/qemuhotplug-%s+%s.xml", abs_srcdir, test->domain_filename, test->device_filename) < 0) goto cleanup; if (virTestLoadFile(domain_filename, &domain_xml) < 0 || virTestLoadFile(device_filename, &device_xml) < 0) goto cleanup; if (test->action != UPDATE && virTestLoadFile(result_filename, &result_xml) < 0) goto cleanup; if (!(caps = virQEMUDriverGetCapabilities(&driver, false))) goto cleanup; if (test->vm) { vm = test->vm; } else { if (qemuHotplugCreateObjects(driver.xmlopt, &vm, domain_xml, test->deviceDeletedEvent, test->domain_filename) < 0) goto cleanup; } if (test->action == ATTACH) device_parse_flags = VIR_DOMAIN_DEF_PARSE_INACTIVE; if (!(dev = virDomainDeviceDefParse(device_xml, vm->def, caps, driver.xmlopt, device_parse_flags))) goto cleanup; /* Now is the best time to feed the spoofed monitor with predefined * replies. */ if (!(test_mon = qemuMonitorTestNew(true, driver.xmlopt, vm, &driver, NULL))) goto cleanup; tmp = test->mon; while (tmp && *tmp) { const char *command_name; const char *response; if (!(command_name = *tmp++) || !(response = *tmp++)) break; if (qemuMonitorTestAddItem(test_mon, command_name, response) < 0) goto cleanup; } priv = vm->privateData; priv->mon = qemuMonitorTestGetMonitor(test_mon); priv->monJSON = true; /* XXX We need to unlock the monitor here, as * qemuDomainObjEnterMonitorInternal (called from qemuDomainChangeGraphics) * tries to lock it again */ virObjectUnlock(priv->mon); switch (test->action) { case ATTACH: ret = testQemuHotplugAttach(vm, dev); if (ret == 0) { /* vm->def stolen dev->data.* so we just need to free the dev * envelope */ VIR_FREE(dev); } if (ret == 0 || fail) ret = testQemuHotplugCheckResult(vm, result_xml, result_filename, fail); break; case DETACH: ret = testQemuHotplugDetach(vm, dev); if (ret == 0 || fail) ret = testQemuHotplugCheckResult(vm, domain_xml, domain_filename, fail); break; case UPDATE: ret = testQemuHotplugUpdate(vm, dev); } cleanup: VIR_FREE(domain_filename); VIR_FREE(device_filename); VIR_FREE(result_filename); VIR_FREE(domain_xml); VIR_FREE(device_xml); VIR_FREE(result_xml); /* don't dispose test monitor with VM */ if (priv) priv->mon = NULL; if (keep) { test->vm = vm; } else { virObjectUnref(vm); test->vm = NULL; } virDomainDeviceDefFree(dev); virObjectUnref(caps); qemuMonitorTestFree(test_mon); return ((ret < 0 && fail) || (!ret && !fail)) ? 0 : -1; }
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 (virTestLoadFile(cmdfile, &cmd) < 0) goto fail; if (!(vmdef = qemuParseCommandLineString(driver.caps, driver.xmlopt, cmd, NULL, NULL, NULL))) goto fail; if (!virTestOOMActive()) { if ((log = virTestLogContentAndReset()) == 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 (virTestCompareToFile(actualxml, xmlfile) < 0) goto fail; ret = 0; fail: VIR_FREE(actualxml); VIR_FREE(cmd); VIR_FREE(log); virDomainDefFree(vmdef); return ret; }
static int testQemuDiskXMLToProps(const void *opaque) { struct testQemuDiskXMLToJSONData *data = (void *) opaque; virDomainDiskDefPtr disk = NULL; virStorageSourcePtr n; virJSONValuePtr formatProps = NULL; virJSONValuePtr storageProps = NULL; char *xmlpath = NULL; char *xmlstr = NULL; int ret = -1; if (virAsprintf(&xmlpath, "%s%s.xml", testQemuDiskXMLToJSONPath, data->name) < 0) goto cleanup; if (virTestLoadFile(xmlpath, &xmlstr) < 0) goto cleanup; /* qemu stores node names in the status XML portion */ if (!(disk = virDomainDiskDefParse(xmlstr, NULL, data->driver->xmlopt, VIR_DOMAIN_DEF_PARSE_STATUS))) goto cleanup; if (qemuCheckDiskConfig(disk, data->qemuCaps) < 0 || qemuDomainDeviceDefValidateDisk(disk, data->qemuCaps) < 0) { VIR_TEST_VERBOSE("invalid configuration for disk\n"); goto cleanup; } for (n = disk->src; virStorageSourceIsBacking(n); n = n->backingStore) { if (testQemuDiskXMLToJSONFakeSecrets(n) < 0) goto cleanup; if (qemuDomainValidateStorageSource(n, data->qemuCaps) < 0) goto cleanup; if (qemuDomainPrepareDiskSourceData(disk, n, NULL, data->qemuCaps) < 0) goto cleanup; if (!(formatProps = qemuBlockStorageSourceGetBlockdevProps(n)) || !(storageProps = qemuBlockStorageSourceGetBackendProps(n, false))) { if (!data->fail) { VIR_TEST_VERBOSE("failed to generate qemu blockdev props\n"); goto cleanup; } } else if (data->fail) { VIR_TEST_VERBOSE("qemu blockdev props should have failed\n"); goto cleanup; } if (VIR_APPEND_ELEMENT(data->props, data->nprops, formatProps) < 0 || VIR_APPEND_ELEMENT(data->props, data->nprops, storageProps) < 0) goto cleanup; } ret = 0; cleanup: virJSONValueFree(formatProps); virJSONValueFree(storageProps); virDomainDiskDefFree(disk); VIR_FREE(xmlpath); VIR_FREE(xmlstr); return ret; }