static int testCompareFiles(const char *xml, const char *sexpr) { char *gotsexpr = NULL; int ret = -1; virDomainDefPtr def = NULL; if (!(def = virDomainDefParseFile(xml, caps, xmlopt, NULL, VIR_DOMAIN_DEF_PARSE_INACTIVE))) goto fail; if (!virDomainDefCheckABIStability(def, def, xmlopt)) { fprintf(stderr, "ABI stability check failed on %s", xml); goto fail; } if (!(gotsexpr = xenFormatSxpr(NULL, def))) goto fail; if (virTestCompareToFile(gotsexpr, sexpr) < 0) goto fail; ret = 0; fail: VIR_FREE(gotsexpr); virDomainDefFree(def); return ret; }
static int testSysinfo(const void *data) { int result = -1; const char *sysfsActualData; virSysinfoDefPtr ret = NULL; virBuffer buf = VIR_BUFFER_INITIALIZER; const struct testSysinfoData *testdata = data; virSysinfoSetup(testdata->decoder, testdata->sysinfo, testdata->cpuinfo); if (!testdata->expected || !(ret = testdata->func())) goto cleanup; if (virSysinfoFormat(&buf, ret) < 0) goto cleanup; if (!(sysfsActualData = virBufferCurrentContent(&buf))) goto cleanup; if (virTestCompareToFile(sysfsActualData, testdata->expected) < 0) goto cleanup; result = 0; cleanup: virSysinfoDefFree(ret); virBufferFreeAndReset(&buf); return result; }
static int test_virCapabilitiesFormat(const void *opaque) { struct virCapabilitiesFormatData *data = (struct virCapabilitiesFormatData *) opaque; virCapsPtr caps = NULL; char *capsXML = NULL; char *path = NULL; int ret = -1; if (!(caps = buildVirCapabilities(data->max_cells, data->max_cpus_in_cell, data->max_mem_in_cell))) goto cleanup; if (!(capsXML = virCapabilitiesFormatXML(caps))) goto cleanup; if (virAsprintf(&path, "%s/vircaps2xmldata/vircaps-%s.xml", abs_srcdir, data->filename) < 0) goto cleanup; if (virTestCompareToFile(capsXML, path) < 0) goto cleanup; ret = 0; cleanup: VIR_FREE(path); VIR_FREE(capsXML); virObjectUnref(caps); 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 testCompareXMLToXMLFiles(const char *xml) { char *actual = NULL; int ret = -1; virNWFilterBindingDefPtr dev = NULL; virResetLastError(); if (!(dev = virNWFilterBindingDefParseFile(xml))) goto fail; if (!(actual = virNWFilterBindingDefFormat(dev))) goto fail; if (virTestCompareToFile(actual, xml) < 0) goto fail; ret = 0; fail: VIR_FREE(actual); virNWFilterBindingDefFree(dev); return ret; }
static int testCompareXMLToXMLFiles(const char *poolxml, const char *inxml, const char *outxml, unsigned int flags) { char *actual = NULL; int ret = -1; virStoragePoolDefPtr pool = NULL; virStorageVolDefPtr dev = NULL; if (!(pool = virStoragePoolDefParseFile(poolxml))) goto fail; if (!(dev = virStorageVolDefParseFile(pool, inxml, flags))) goto fail; if (!(actual = virStorageVolDefFormat(pool, dev))) goto fail; if (virTestCompareToFile(actual, outxml) < 0) goto fail; ret = 0; fail: VIR_FREE(actual); virStoragePoolDefFree(pool); virStorageVolDefFree(dev); 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 test_virCapabilities(const void *opaque) { struct virCapabilitiesData *data = (struct virCapabilitiesData *) opaque; const char *archStr = virArchToString(data->arch); virCapsPtr caps = NULL; char *capsXML = NULL; char *path = NULL; char *dir = NULL; char *resctrl = NULL; int ret = -1; /* * We want to keep our directory structure clean, so if there's both resctrl * and system used, we need to use slightly different path; a subdir. */ if (virAsprintf(&dir, "%s/vircaps2xmldata/linux-%s%s", abs_srcdir, data->filename, data->resctrl ? "/system" : "") < 0) goto cleanup; if (virAsprintf(&resctrl, "%s/vircaps2xmldata/linux-%s/resctrl", abs_srcdir, data->filename) < 0) goto cleanup; virFileWrapperAddPrefix("/sys/devices/system", dir); virFileWrapperAddPrefix("/sys/fs/resctrl", resctrl); caps = virCapabilitiesNew(data->arch, data->offlineMigrate, data->liveMigrate); if (!caps) goto cleanup; if (virCapabilitiesInitNUMA(caps) < 0 || virCapabilitiesInitCaches(caps) < 0) goto cleanup; virFileWrapperClearPrefixes(); if (!(capsXML = virCapabilitiesFormatXML(caps))) goto cleanup; if (virAsprintf(&path, "%s/vircaps2xmldata/vircaps-%s-%s.xml", abs_srcdir, archStr, data->filename) < 0) goto cleanup; if (virTestCompareToFile(capsXML, path) < 0) goto cleanup; ret = 0; cleanup: VIR_FREE(dir); VIR_FREE(resctrl); VIR_FREE(path); VIR_FREE(capsXML); virObjectUnref(caps); return ret; }
static int test_virDomainCapsFormat(const void *opaque) { const struct testData *data = opaque; virDomainCapsPtr domCaps = NULL; char *path = NULL; char *domCapsXML = NULL; int ret = -1; if (virAsprintf(&path, "%s/domaincapsschemadata/%s.xml", abs_srcdir, data->name) < 0) goto cleanup; if (!(domCaps = virDomainCapsNew(data->emulator, data->machine, virArchFromString(data->arch), data->type))) goto cleanup; switch (data->capsType) { case CAPS_NONE: break; case CAPS_ALL: if (fillAllCaps(domCaps) < 0) goto cleanup; break; case CAPS_QEMU: #if WITH_QEMU if (fillQemuCaps(domCaps, data->capsName, data->arch, data->machine, data->capsOpaque) < 0) goto cleanup; #endif break; case CAPS_LIBXL: #if WITH_LIBXL if (fillXenCaps(domCaps) < 0) goto cleanup; #endif break; } if (!(domCapsXML = virDomainCapsFormat(domCaps))) goto cleanup; if (virTestCompareToFile(domCapsXML, path) < 0) goto cleanup; ret = 0; cleanup: VIR_FREE(domCapsXML); VIR_FREE(path); virObjectUnref(domCaps); 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 testCompareParseXML(const char *xmcfg, const char *xml) { char *gotxmcfgData = NULL; virConfPtr conf = NULL; int ret = -1; virConnectPtr conn = NULL; int wrote = 4096; struct _xenUnifiedPrivate priv; virDomainDefPtr def = NULL; if (VIR_ALLOC_N(gotxmcfgData, wrote) < 0) goto fail; conn = virGetConnect(); if (!conn) goto fail; /* Many puppies died to bring you this code. */ priv.caps = caps; conn->privateData = &priv; if (!(def = virDomainDefParseFile(xml, caps, xmlopt, NULL, VIR_DOMAIN_DEF_PARSE_INACTIVE))) goto fail; if (!virDomainDefCheckABIStability(def, def, xmlopt)) { fprintf(stderr, "ABI stability check failed on %s", xml); goto fail; } if (!(conf = xenFormatXM(conn, def))) goto fail; if (virConfWriteMem(gotxmcfgData, &wrote, conf) < 0) goto fail; gotxmcfgData[wrote] = '\0'; if (virTestCompareToFile(gotxmcfgData, xmcfg) < 0) goto fail; ret = 0; fail: VIR_FREE(gotxmcfgData); if (conf) virConfFree(conf); virDomainDefFree(def); virObjectUnref(conn); return ret; }
static int testQemuCaps(const void *opaque) { int ret = -1; const testQemuData *data = opaque; char *repliesFile = NULL; char *capsFile = NULL; qemuMonitorTestPtr mon = NULL; virQEMUCapsPtr capsActual = NULL; char *actual = NULL; if (virAsprintf(&repliesFile, "%s/qemucapabilitiesdata/%s.%s.replies", abs_srcdir, data->base, data->archName) < 0 || virAsprintf(&capsFile, "%s/qemucapabilitiesdata/%s.%s.xml", abs_srcdir, data->base, data->archName) < 0) goto cleanup; if (!(mon = qemuMonitorTestNewFromFile(repliesFile, data->xmlopt, false))) goto cleanup; if (!(capsActual = virQEMUCapsNew()) || virQEMUCapsInitQMPMonitor(capsActual, qemuMonitorTestGetMonitor(mon)) < 0) goto cleanup; if (virQEMUCapsGet(capsActual, QEMU_CAPS_KVM)) { if (virQEMUCapsInitQMPMonitorTCG(capsActual, qemuMonitorTestGetMonitor(mon)) < 0) goto cleanup; /* Fill microcodeVersion with a "random" value which is the file * length to provide a reproducible number for testing. */ virQEMUCapsSetMicrocodeVersion(capsActual, virFileLength(repliesFile, -1)); } if (!(actual = virQEMUCapsFormatCache(capsActual))) goto cleanup; if (virTestCompareToFile(actual, capsFile) < 0) goto cleanup; ret = 0; cleanup: VIR_FREE(repliesFile); VIR_FREE(capsFile); VIR_FREE(actual); qemuMonitorTestFree(mon); virObjectUnref(capsActual); 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 testCompareXMLToArgvFiles(const char *xml, const char *cmdline) { char *actualargv = NULL; virBuffer buf = VIR_BUFFER_INITIALIZER; virNWFilterHashTablePtr vars = virNWFilterHashTableCreate(0); virNWFilterInst inst; int ret = -1; memset(&inst, 0, sizeof(inst)); virCommandSetDryRun(&buf, NULL, NULL); if (!vars) goto cleanup; if (testSetDefaultParameters(vars) < 0) goto cleanup; if (virNWFilterDefToInst(xml, vars, &inst) < 0) goto cleanup; if (ebiptables_driver.applyNewRules("vnet0", inst.rules, inst.nrules) < 0) goto cleanup; if (virBufferError(&buf)) goto cleanup; actualargv = virBufferContentAndReset(&buf); virTestClearCommandPath(actualargv); virCommandSetDryRun(NULL, NULL, NULL); testRemoveCommonRules(actualargv); if (virTestCompareToFile(actualargv, cmdline) < 0) goto cleanup; ret = 0; cleanup: virBufferFreeAndReset(&buf); VIR_FREE(actualargv); virNWFilterInstReset(&inst); virNWFilterHashTableFree(vars); 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 testQemuCapsCopy(const void *opaque) { int ret = -1; const testQemuData *data = opaque; char *capsFile = NULL; virCapsPtr caps = NULL; virQEMUCapsPtr orig = NULL; virQEMUCapsPtr copy = NULL; char *actual = NULL; if (virAsprintf(&capsFile, "%s/qemucapabilitiesdata/%s.%s.xml", abs_srcdir, data->base, data->archName) < 0) goto cleanup; if (!(caps = virCapabilitiesNew(virArchFromString(data->archName), false, false))) goto cleanup; if (!(orig = qemuTestParseCapabilities(caps, capsFile))) goto cleanup; if (!(copy = virQEMUCapsNewCopy(orig))) goto cleanup; if (!(actual = virQEMUCapsFormatCache(copy))) goto cleanup; if (virTestCompareToFile(actual, capsFile) < 0) goto cleanup; ret = 0; cleanup: VIR_FREE(capsFile); virObjectUnref(caps); virObjectUnref(orig); virObjectUnref(copy); VIR_FREE(actual); return ret; }
static int testCompareXMLToConfFiles(const char *inxml, const char *outconf, dnsmasqCapsPtr caps) { char *actual = NULL; int ret = -1; virNetworkDefPtr dev = NULL; virNetworkObjPtr obj = NULL; virCommandPtr cmd = NULL; char *pidfile = NULL; dnsmasqContext *dctx = NULL; if (!(dev = virNetworkDefParseFile(inxml))) goto fail; if (!(obj = virNetworkObjNew())) goto fail; obj->def = dev; dctx = dnsmasqContextNew(dev->name, "/var/lib/libvirt/dnsmasq"); if (dctx == NULL) goto fail; if (networkDnsmasqConfContents(obj, pidfile, &actual, dctx, caps) < 0) goto fail; if (virTestCompareToFile(actual, outconf) < 0) goto fail; ret = 0; fail: VIR_FREE(actual); VIR_FREE(pidfile); virCommandFree(cmd); virObjectUnref(obj); dnsmasqContextFree(dctx); return ret; }
static int testQemuCaps(const void *opaque) { int ret = -1; const testQemuData *data = opaque; char *repliesFile = NULL; char *capsFile = NULL; qemuMonitorTestPtr mon = NULL; virQEMUCapsPtr capsActual = NULL; char *actual = NULL; if (virAsprintf(&repliesFile, "%s/qemucapabilitiesdata/%s.%s.replies", abs_srcdir, data->base, data->archName) < 0 || virAsprintf(&capsFile, "%s/qemucapabilitiesdata/%s.%s.xml", abs_srcdir, data->base, data->archName) < 0) goto cleanup; if (!(mon = qemuMonitorTestNewFromFile(repliesFile, data->xmlopt, false))) goto cleanup; if (!(capsActual = virQEMUCapsNew()) || virQEMUCapsInitQMPMonitor(capsActual, qemuMonitorTestGetMonitor(mon)) < 0) goto cleanup; if (!(actual = virQEMUCapsFormatCache(capsActual, 0, 0))) goto cleanup; if (virTestCompareToFile(actual, capsFile) < 0) goto cleanup; ret = 0; cleanup: VIR_FREE(repliesFile); VIR_FREE(capsFile); VIR_FREE(actual); qemuMonitorTestFree(mon); virObjectUnref(capsActual); return ret; }
static int testQemuDiskXMLToPropsValidateFile(const void *opaque) { struct testQemuDiskXMLToJSONData *data = (void *) opaque; virBuffer buf = VIR_BUFFER_INITIALIZER; char *jsonpath = NULL; char *actual = NULL; int ret = -1; size_t i; if (data->fail) return EXIT_AM_SKIP; if (virAsprintf(&jsonpath, "%s%s.json", testQemuDiskXMLToJSONPath, data->name) < 0) goto cleanup; for (i = 0; i < data->nprops; i++) { char *jsonstr; if (!(jsonstr = virJSONValueToString(data->props[i], true))) goto cleanup; virBufferAdd(&buf, jsonstr, -1); VIR_FREE(jsonstr); } if (virBufferCheckError(&buf) < 0) goto cleanup; actual = virBufferContentAndReset(&buf); ret = virTestCompareToFile(actual, jsonpath); cleanup: VIR_FREE(jsonpath); VIR_FREE(actual); return ret; }
static int testCompareXMLToXMLFiles(const char *inxml, const char *outxml) { char *actual = NULL; int ret = -1; virSecretDefPtr secret = NULL; if (!(secret = virSecretDefParseFile(inxml))) goto fail; if (!(actual = virSecretDefFormat(secret))) goto fail; if (virTestCompareToFile(actual, outxml) < 0) goto fail; ret = 0; fail: VIR_FREE(actual); virSecretDefFree(secret); return ret; }
static int testCompareXMLToArgvFiles(const char *xml, const char *cmdline) { char *expectargv = NULL; char *actualargv = NULL; virBuffer buf = VIR_BUFFER_INITIALIZER; virNetworkDefPtr def = NULL; int ret = -1; virCommandSetDryRun(&buf, NULL, NULL); if (!(def = virNetworkDefParseFile(xml))) goto cleanup; if (networkAddFirewallRules(def) < 0) goto cleanup; if (virBufferError(&buf)) goto cleanup; actualargv = virBufferContentAndReset(&buf); virTestClearCommandPath(actualargv); virCommandSetDryRun(NULL, NULL, NULL); if (virTestCompareToFile(actualargv, cmdline) < 0) goto cleanup; ret = 0; cleanup: virBufferFreeAndReset(&buf); VIR_FREE(expectargv); VIR_FREE(actualargv); virNetworkDefFree(def); 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 testMACFlush(const void *opaque) { const struct testData *data = opaque; char *file = NULL; char *str = NULL; int ret = -1; if (virAsprintf(&file, "%s/virmacmaptestdata/%s.json", abs_srcdir, data->file) < 0) goto cleanup; if (virMacMapDumpStr(data->mgr, &str) < 0) goto cleanup; if (virTestCompareToFile(str, file) < 0) goto cleanup; ret = 0; cleanup: VIR_FREE(file); VIR_FREE(str); return ret; }
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 testCompareXMLToArgvFiles(bool shouldFail, const char *poolxml, const char *volxml, const char *inputpoolxml, const char *inputvolxml, const char *cmdline, unsigned int flags, unsigned long parse_flags) { char *actualCmdline = NULL; virStorageVolEncryptConvertStep convertStep = VIR_STORAGE_VOL_ENCRYPT_NONE; int ret = -1; virCommandPtr cmd = NULL; virStorageVolDefPtr vol = NULL, inputvol = NULL; virStoragePoolDefPtr def = NULL; virStoragePoolDefPtr inputpool = NULL; virStoragePoolObjPtr obj = NULL; if (!(def = virStoragePoolDefParseFile(poolxml))) goto cleanup; if (!(obj = virStoragePoolObjNew())) { virStoragePoolDefFree(def); goto cleanup; } virStoragePoolObjSetDef(obj, def); if (inputpoolxml) { if (!(inputpool = virStoragePoolDefParseFile(inputpoolxml))) goto cleanup; } if (inputvolxml) parse_flags |= VIR_VOL_XML_PARSE_NO_CAPACITY; if (!(vol = virStorageVolDefParseFile(def, volxml, parse_flags))) goto cleanup; if (inputvolxml && !(inputvol = virStorageVolDefParseFile(inputpool, inputvolxml, 0))) goto cleanup; testSetVolumeType(vol, def); testSetVolumeType(inputvol, inputpool); /* Using an input file for encryption requires a multi-step process * to create an image of the same size as the inputvol and then to * convert the inputvol afterwards. Since we only care about the * command line we have to copy code from storageBackendCreateQemuImg * and adjust it for the test needs. */ if (inputvol && (vol->target.encryption || inputvol->target.encryption)) convertStep = VIR_STORAGE_VOL_ENCRYPT_CREATE; do { cmd = virStorageBackendCreateQemuImgCmdFromVol(obj, vol, inputvol, flags, create_tool, "/path/to/secretFile", "/path/to/inputSecretFile", convertStep); if (!cmd) { if (shouldFail) { virResetLastError(); ret = 0; } goto cleanup; } if (convertStep != VIR_STORAGE_VOL_ENCRYPT_CONVERT) { if (!(actualCmdline = virCommandToString(cmd))) goto cleanup; } else { char *createCmdline = actualCmdline; char *cvtCmdline; int rc; if (!(cvtCmdline = virCommandToString(cmd))) goto cleanup; rc = virAsprintf(&actualCmdline, "%s\n%s", createCmdline, cvtCmdline); VIR_FREE(createCmdline); VIR_FREE(cvtCmdline); if (rc < 0) goto cleanup; } if (convertStep == VIR_STORAGE_VOL_ENCRYPT_NONE) convertStep = VIR_STORAGE_VOL_ENCRYPT_DONE; else if (convertStep == VIR_STORAGE_VOL_ENCRYPT_CREATE) convertStep = VIR_STORAGE_VOL_ENCRYPT_CONVERT; else if (convertStep == VIR_STORAGE_VOL_ENCRYPT_CONVERT) convertStep = VIR_STORAGE_VOL_ENCRYPT_DONE; } while (convertStep != VIR_STORAGE_VOL_ENCRYPT_DONE); if (virTestCompareToFile(actualCmdline, cmdline) < 0) goto cleanup; ret = 0; cleanup: virStoragePoolDefFree(inputpool); virStorageVolDefFree(vol); virStorageVolDefFree(inputvol); virCommandFree(cmd); VIR_FREE(actualCmdline); virStoragePoolObjEndAPI(&obj); return ret; }
static int fillAllCaps(virDomainCapsPtr domCaps) { virDomainCapsOSPtr os = &domCaps->os; virDomainCapsLoaderPtr loader = &os->loader; virDomainCapsCPUPtr cpu = &domCaps->cpu; virDomainCapsDeviceDiskPtr disk = &domCaps->disk; virDomainCapsDeviceGraphicsPtr graphics = &domCaps->graphics; virDomainCapsDeviceVideoPtr video = &domCaps->video; virDomainCapsDeviceHostdevPtr hostdev = &domCaps->hostdev; virCPUDef host = { .type = VIR_CPU_TYPE_HOST, .arch = VIR_ARCH_X86_64, .model = (char *) "host", .vendor = (char *) "CPU Vendorrr", }; domCaps->maxvcpus = 255; os->supported = true; loader->supported = true; SET_ALL_BITS(loader->type); SET_ALL_BITS(loader->readonly); if (fillStringValues(&loader->values, "/foo/bar", "/tmp/my_path", NULL) < 0) return -1; cpu->hostPassthrough = true; cpu->hostModel = virCPUDefCopy(&host); if (!(cpu->custom = virDomainCapsCPUModelsNew(3)) || virDomainCapsCPUModelsAdd(cpu->custom, "Model1", -1, VIR_DOMCAPS_CPU_USABLE_UNKNOWN) < 0 || virDomainCapsCPUModelsAdd(cpu->custom, "Model2", -1, VIR_DOMCAPS_CPU_USABLE_NO) < 0 || virDomainCapsCPUModelsAdd(cpu->custom, "Model3", -1, VIR_DOMCAPS_CPU_USABLE_YES) < 0) return -1; disk->supported = true; SET_ALL_BITS(disk->diskDevice); SET_ALL_BITS(disk->bus); graphics->supported = true; SET_ALL_BITS(graphics->type); video->supported = true; SET_ALL_BITS(video->modelType); hostdev->supported = true; SET_ALL_BITS(hostdev->mode); SET_ALL_BITS(hostdev->startupPolicy); SET_ALL_BITS(hostdev->subsysType); SET_ALL_BITS(hostdev->capsType); SET_ALL_BITS(hostdev->pciBackend); return 0; } #if WITH_QEMU # include "testutilsqemu.h" static virCPUDef aarch64Cpu = { .sockets = 1, .cores = 1, .threads = 1, }; static virCPUDef ppc64leCpu = { .type = VIR_CPU_TYPE_HOST, .arch = VIR_ARCH_PPC64LE, .model = (char *) "POWER8", .sockets = 1, .cores = 1, .threads = 1, }; static virCPUDef x86Cpu = { .type = VIR_CPU_TYPE_HOST, .arch = VIR_ARCH_X86_64, .model = (char *) "Broadwell", .sockets = 1, .cores = 1, .threads = 1, }; static virCPUDef s390Cpu = { .type = VIR_CPU_TYPE_HOST, .arch = VIR_ARCH_S390X, .sockets = 1, .cores = 1, .threads = 1, }; static int fakeHostCPU(virCapsPtr caps, virArch arch) { virCPUDefPtr cpu; switch (arch) { case VIR_ARCH_AARCH64: cpu = &aarch64Cpu; break; case VIR_ARCH_PPC64LE: cpu = &ppc64leCpu; break; case VIR_ARCH_X86_64: cpu = &x86Cpu; break; case VIR_ARCH_S390X: cpu = &s390Cpu; break; default: virReportError(VIR_ERR_INTERNAL_ERROR, "cannot fake host CPU for arch %s", virArchToString(arch)); return -1; } if (!(caps->host.cpu = virCPUDefCopy(cpu))) return -1; return 0; } static int fillQemuCaps(virDomainCapsPtr domCaps, const char *name, const char *arch, const char *machine, virQEMUDriverConfigPtr cfg) { int ret = -1; char *path = NULL; virCapsPtr caps = NULL; virQEMUCapsPtr qemuCaps = NULL; virDomainCapsLoaderPtr loader = &domCaps->os.loader; if (!(caps = virCapabilitiesNew(domCaps->arch, false, false)) || fakeHostCPU(caps, domCaps->arch) < 0) goto cleanup; if (virAsprintf(&path, "%s/qemucapabilitiesdata/%s.%s.xml", abs_srcdir, name, arch) < 0 || !(qemuCaps = qemuTestParseCapabilities(caps, path))) goto cleanup; if (machine && VIR_STRDUP(domCaps->machine, virQEMUCapsGetCanonicalMachine(qemuCaps, machine)) < 0) goto cleanup; if (!domCaps->machine && VIR_STRDUP(domCaps->machine, virQEMUCapsGetDefaultMachine(qemuCaps)) < 0) goto cleanup; if (virQEMUCapsFillDomainCaps(caps, domCaps, qemuCaps, cfg->firmwares, cfg->nfirmwares) < 0) goto cleanup; /* The function above tries to query host's KVM & VFIO capabilities by * calling qemuHostdevHostSupportsPassthroughLegacy() and * qemuHostdevHostSupportsPassthroughVFIO() which, however, can't be * successfully mocked as they are not exposed as internal APIs. Therefore, * instead of mocking set the expected values here by hand. */ VIR_DOMAIN_CAPS_ENUM_SET(domCaps->hostdev.pciBackend, VIR_DOMAIN_HOSTDEV_PCI_BACKEND_DEFAULT, VIR_DOMAIN_HOSTDEV_PCI_BACKEND_KVM, VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO); /* As of f05b6a918e28 we are expecting to see OVMF_CODE.fd file which * may not exists everywhere. */ while (loader->values.nvalues) VIR_FREE(loader->values.values[--loader->values.nvalues]); if (fillStringValues(&loader->values, "/usr/share/AAVMF/AAVMF_CODE.fd", "/usr/share/OVMF/OVMF_CODE.fd", NULL) < 0) goto cleanup; ret = 0; cleanup: virObjectUnref(caps); virObjectUnref(qemuCaps); VIR_FREE(path); return ret; } #endif /* WITH_QEMU */ #ifdef WITH_LIBXL # include "testutilsxen.h" static int fillXenCaps(virDomainCapsPtr domCaps) { virFirmwarePtr *firmwares; int ret = -1; if (VIR_ALLOC_N(firmwares, 2) < 0) return ret; if (VIR_ALLOC(firmwares[0]) < 0 || VIR_ALLOC(firmwares[1]) < 0) goto cleanup; if (VIR_STRDUP(firmwares[0]->name, "/usr/lib/xen/boot/hvmloader") < 0 || VIR_STRDUP(firmwares[1]->name, "/usr/lib/xen/boot/ovmf.bin") < 0) goto cleanup; if (libxlMakeDomainCapabilities(domCaps, firmwares, 2) < 0) goto cleanup; ret = 0; cleanup: virFirmwareFreeList(firmwares, 2); return ret; } #endif /* WITH_LIBXL */ #ifdef WITH_BHYVE # include "bhyve/bhyve_capabilities.h" static int fillBhyveCaps(virDomainCapsPtr domCaps, unsigned int *bhyve_caps) { virDomainCapsStringValuesPtr firmwares = NULL; int ret = -1; if (VIR_ALLOC(firmwares) < 0) return -1; if (fillStringValues(firmwares, "/foo/bar", "/foo/baz", NULL) < 0) goto cleanup; if (virBhyveDomainCapsFill(domCaps, *bhyve_caps, firmwares) < 0) goto cleanup; ret = 0; cleanup: VIR_FREE(firmwares); return ret; } #endif /* WITH_BHYVE */ enum testCapsType { CAPS_NONE, CAPS_ALL, CAPS_QEMU, CAPS_LIBXL, CAPS_BHYVE, }; struct testData { const char *name; const char *emulator; const char *machine; const char *arch; virDomainVirtType type; enum testCapsType capsType; const char *capsName; void *capsOpaque; }; static int test_virDomainCapsFormat(const void *opaque) { const struct testData *data = opaque; virDomainCapsPtr domCaps = NULL; char *path = NULL; char *domCapsXML = NULL; int ret = -1; if (virAsprintf(&path, "%s/domaincapsschemadata/%s.xml", abs_srcdir, data->name) < 0) goto cleanup; if (!(domCaps = virDomainCapsNew(data->emulator, data->machine, virArchFromString(data->arch), data->type))) goto cleanup; switch (data->capsType) { case CAPS_NONE: break; case CAPS_ALL: if (fillAllCaps(domCaps) < 0) goto cleanup; break; case CAPS_QEMU: #if WITH_QEMU if (fillQemuCaps(domCaps, data->capsName, data->arch, data->machine, data->capsOpaque) < 0) goto cleanup; #endif break; case CAPS_LIBXL: #if WITH_LIBXL if (fillXenCaps(domCaps) < 0) goto cleanup; #endif break; case CAPS_BHYVE: #if WITH_BHYVE if (fillBhyveCaps(domCaps, data->capsOpaque) < 0) goto cleanup; #endif break; } if (!(domCapsXML = virDomainCapsFormat(domCaps))) goto cleanup; if (virTestCompareToFile(domCapsXML, path) < 0) goto cleanup; ret = 0; cleanup: VIR_FREE(domCapsXML); VIR_FREE(path); virObjectUnref(domCaps); return ret; } static int mymain(void) { int ret = 0; #if WITH_BHYVE unsigned int bhyve_caps = 0; #endif #if WITH_QEMU virQEMUDriverConfigPtr cfg = virQEMUDriverConfigNew(false); if (!cfg) return EXIT_FAILURE; #endif #define DO_TEST(Name, Emulator, Machine, Arch, Type, CapsType) \ do { \ struct testData data = { \ .name = Name, \ .emulator = Emulator, \ .machine = Machine, \ .arch = Arch, \ .type = Type, \ .capsType = CapsType, \ }; \ if (virTestRun(Name, test_virDomainCapsFormat, &data) < 0) \ ret = -1; \ } while (0) #define DO_TEST_QEMU(Name, CapsName, Emulator, Machine, Arch, Type) \ do { \ char *name = NULL; \ if (virAsprintf(&name, "qemu_%s%s%s.%s", \ Name, \ Machine ? "-" : "", Machine ? Machine : "", \ Arch) < 0) { \ ret = -1; \ break; \ } \ struct testData data = { \ .name = name, \ .emulator = Emulator, \ .machine = Machine, \ .arch = Arch, \ .type = Type, \ .capsType = CAPS_QEMU, \ .capsName = CapsName, \ .capsOpaque = cfg, \ }; \ if (virTestRun(name, test_virDomainCapsFormat, &data) < 0) \ ret = -1; \ VIR_FREE(name); \ } while (0) #define DO_TEST_LIBXL(Name, Emulator, Machine, Arch, Type) \ do { \ struct testData data = { \ .name = Name, \ .emulator = Emulator, \ .machine = Machine, \ .arch = Arch, \ .type = Type, \ .capsType = CAPS_LIBXL, \ }; \ if (virTestRun(Name, test_virDomainCapsFormat, &data) < 0) \ ret = -1; \ } while (0) DO_TEST("basic", "/bin/emulatorbin", "my-machine-type", "x86_64", VIR_DOMAIN_VIRT_UML, CAPS_NONE); DO_TEST("full", "/bin/emulatorbin", "my-machine-type", "x86_64", VIR_DOMAIN_VIRT_KVM, CAPS_ALL); #define DO_TEST_BHYVE(Name, Emulator, BhyveCaps, Type) \ do { \ char *name = NULL; \ if (virAsprintf(&name, "bhyve_%s.x86_64", Name) < 0) { \ ret = -1; \ break; \ } \ struct testData data = { \ .name = name, \ .emulator = Emulator, \ .arch = "x86_64", \ .type = Type, \ .capsType = CAPS_BHYVE, \ .capsOpaque = BhyveCaps, \ }; \ if (virTestRun(name, test_virDomainCapsFormat, &data) < 0) \ ret = -1; \ VIR_FREE(name); \ } while (0) #if WITH_QEMU DO_TEST_QEMU("1.7.0", "caps_1.7.0", "/usr/bin/qemu-system-x86_64", NULL, "x86_64", VIR_DOMAIN_VIRT_KVM); DO_TEST_QEMU("2.6.0", "caps_2.6.0", "/usr/bin/qemu-system-x86_64", NULL, "x86_64", VIR_DOMAIN_VIRT_KVM); DO_TEST_QEMU("2.6.0", "caps_2.6.0-gicv2", "/usr/bin/qemu-system-aarch64", NULL, "aarch64", VIR_DOMAIN_VIRT_KVM); DO_TEST_QEMU("2.6.0-gicv2", "caps_2.6.0-gicv2", "/usr/bin/qemu-system-aarch64", "virt", "aarch64", VIR_DOMAIN_VIRT_KVM); DO_TEST_QEMU("2.6.0-gicv3", "caps_2.6.0-gicv3", "/usr/bin/qemu-system-aarch64", "virt", "aarch64", VIR_DOMAIN_VIRT_KVM); DO_TEST_QEMU("2.6.0", "caps_2.6.0", "/usr/bin/qemu-system-ppc64", NULL, "ppc64le", VIR_DOMAIN_VIRT_KVM); DO_TEST_QEMU("2.8.0", "caps_2.8.0", "/usr/bin/qemu-system-x86_64", NULL, "x86_64", VIR_DOMAIN_VIRT_KVM); DO_TEST_QEMU("2.8.0-tcg", "caps_2.8.0", "/usr/bin/qemu-system-x86_64", NULL, "x86_64", VIR_DOMAIN_VIRT_QEMU); DO_TEST_QEMU("2.9.0", "caps_2.9.0", "/usr/bin/qemu-system-x86_64", NULL, "x86_64", VIR_DOMAIN_VIRT_KVM); DO_TEST_QEMU("2.9.0", "caps_2.9.0", "/usr/bin/qemu-system-x86_64", "q35", "x86_64", VIR_DOMAIN_VIRT_KVM); DO_TEST_QEMU("2.9.0-tcg", "caps_2.9.0", "/usr/bin/qemu-system-x86_64", NULL, "x86_64", VIR_DOMAIN_VIRT_QEMU); DO_TEST_QEMU("2.7.0", "caps_2.7.0", "/usr/bin/qemu-system-s390x", NULL, "s390x", VIR_DOMAIN_VIRT_KVM); DO_TEST_QEMU("2.8.0", "caps_2.8.0", "/usr/bin/qemu-system-s390x", NULL, "s390x", VIR_DOMAIN_VIRT_KVM); virObjectUnref(cfg); #endif /* WITH_QEMU */ #if WITH_LIBXL # ifdef LIBXL_HAVE_PVUSB # define LIBXL_XENPV_CAPS "libxl-xenpv-usb" # define LIBXL_XENFV_CAPS "libxl-xenfv-usb" # else # define LIBXL_XENPV_CAPS "libxl-xenpv" # define LIBXL_XENFV_CAPS "libxl-xenfv" # endif DO_TEST_LIBXL(LIBXL_XENPV_CAPS, "/usr/bin/qemu-system-x86_64", "xenpv", "x86_64", VIR_DOMAIN_VIRT_XEN); DO_TEST_LIBXL(LIBXL_XENFV_CAPS, "/usr/bin/qemu-system-x86_64", "xenfv", "x86_64", VIR_DOMAIN_VIRT_XEN); #endif /* WITH_LIBXL */ #if WITH_BHYVE DO_TEST_BHYVE("basic", "/usr/sbin/bhyve", &bhyve_caps, VIR_DOMAIN_VIRT_BHYVE); bhyve_caps |= BHYVE_CAP_LPC_BOOTROM; DO_TEST_BHYVE("uefi", "/usr/sbin/bhyve", &bhyve_caps, VIR_DOMAIN_VIRT_BHYVE); bhyve_caps |= BHYVE_CAP_FBUF; DO_TEST_BHYVE("fbuf", "/usr/sbin/bhyve", &bhyve_caps, VIR_DOMAIN_VIRT_BHYVE); #endif /* WITH_BHYVE */ return ret; }
static int testCompareXMLToArgvFiles(bool shouldFail, const char *poolxml, const char *volxml, const char *inputpoolxml, const char *inputvolxml, const char *cmdline, unsigned int flags, int imgformat, unsigned long parse_flags) { char *actualCmdline = NULL; int ret = -1; virCommandPtr cmd = NULL; virConnectPtr conn; virStorageVolDefPtr vol = NULL, inputvol = NULL; virStoragePoolDefPtr pool = NULL; virStoragePoolDefPtr inputpool = NULL; virStoragePoolObj poolobj = {.def = NULL }; if (!(conn = virGetConnect())) goto cleanup; if (!(pool = virStoragePoolDefParseFile(poolxml))) goto cleanup; poolobj.def = pool; if (inputpoolxml) { if (!(inputpool = virStoragePoolDefParseFile(inputpoolxml))) goto cleanup; } if (inputvolxml) parse_flags |= VIR_VOL_XML_PARSE_NO_CAPACITY; if (!(vol = virStorageVolDefParseFile(pool, volxml, parse_flags))) goto cleanup; if (inputvolxml && !(inputvol = virStorageVolDefParseFile(inputpool, inputvolxml, 0))) goto cleanup; testSetVolumeType(vol, pool); testSetVolumeType(inputvol, inputpool); cmd = virStorageBackendCreateQemuImgCmdFromVol(conn, &poolobj, vol, inputvol, flags, create_tool, imgformat); if (!cmd) { if (shouldFail) { virResetLastError(); ret = 0; } goto cleanup; } if (!(actualCmdline = virCommandToString(cmd))) goto cleanup; if (virTestCompareToFile(actualCmdline, cmdline) < 0) goto cleanup; ret = 0; cleanup: virStoragePoolDefFree(pool); virStoragePoolDefFree(inputpool); virStorageVolDefFree(vol); virStorageVolDefFree(inputvol); virCommandFree(cmd); VIR_FREE(actualCmdline); virObjectUnref(conn); return ret; }