test: Use VIR_AUTOFREE for test driver

Let's make use of the auto __cleanup capabilities cleaning up any
now unnecessary goto paths.

Signed-off-by: John Ferlan <jferlan@redhat.com>
Reviewed-by: Erik Skultety <eskultet@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
John Ferlan 2019-02-01 08:54:56 -05:00
parent ba045165d4
commit ceb3255cdf

View File

@ -200,10 +200,10 @@ testDomainDefNamespaceParse(xmlDocPtr xml ATTRIBUTE_UNUSED,
void **data)
{
testDomainNamespaceDefPtr nsdata = NULL;
xmlNodePtr *nodes = NULL;
int tmp, n;
size_t i;
unsigned int tmpuint;
VIR_AUTOFREE(xmlNodePtr *) nodes = NULL;
if (xmlXPathRegisterNs(ctxt, BAD_CAST "test",
BAD_CAST TEST_NAMESPACE_HREF) < 0) {
@ -233,7 +233,6 @@ testDomainDefNamespaceParse(xmlDocPtr xml ATTRIBUTE_UNUSED,
nsdata->snap_nodes[nsdata->num_snap_nodes] = newnode;
nsdata->num_snap_nodes++;
}
VIR_FREE(nodes);
tmp = virXPathBoolean("boolean(./test:transient)", ctxt);
if (tmp == -1) {
@ -280,7 +279,6 @@ testDomainDefNamespaceParse(xmlDocPtr xml ATTRIBUTE_UNUSED,
return 0;
error:
VIR_FREE(nodes);
testDomainDefNamespaceFree(nsdata);
return -1;
}
@ -695,11 +693,10 @@ testParseXMLDocFromFile(xmlNodePtr node, const char *file, const char *type)
xmlNodePtr ret = NULL;
xmlDocPtr doc = NULL;
char *absFile = NULL;
char *relFile = virXMLPropString(node, "file");
VIR_AUTOFREE(char *) relFile = NULL;
if (relFile != NULL) {
if ((relFile = virXMLPropString(node, "file"))) {
absFile = testBuildFilename(file, relFile);
VIR_FREE(relFile);
if (!absFile) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("resolving %s filename"), type);
@ -722,16 +719,15 @@ testParseXMLDocFromFile(xmlNodePtr node, const char *file, const char *type)
error:
xmlFreeDoc(doc);
VIR_FREE(absFile);
return ret;
}
static int
testParseNodeInfo(virNodeInfoPtr nodeInfo, xmlXPathContextPtr ctxt)
{
char *str;
long l;
int ret;
VIR_AUTOFREE(char *) str = NULL;
ret = virXPathLong("string(/node/cpu/nodes[1])", ctxt, &l);
if (ret == 0) {
@ -794,10 +790,8 @@ testParseNodeInfo(virNodeInfoPtr nodeInfo, xmlXPathContextPtr ctxt)
if (virStrcpyStatic(nodeInfo->model, str) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Model %s too big for destination"), str);
VIR_FREE(str);
goto error;
}
VIR_FREE(str);
}
ret = virXPathLong("string(/node/memory[1])", ctxt, &l);
@ -877,12 +871,12 @@ testParseDomains(testDriverPtr privconn,
{
int num, ret = -1;
size_t i;
xmlNodePtr *nodes = NULL;
virDomainObjPtr obj = NULL;
VIR_AUTOFREE(xmlNodePtr *) nodes = NULL;
num = virXPathNodeSet("/node/domain", ctxt, &nodes);
if (num < 0)
goto error;
return -1;
for (i = 0; i < num; i++) {
virDomainDefPtr def;
@ -928,7 +922,6 @@ testParseDomains(testDriverPtr privconn,
ret = 0;
error:
virDomainObjEndAPI(&obj);
VIR_FREE(nodes);
return ret;
}
@ -938,38 +931,35 @@ testParseNetworks(testDriverPtr privconn,
const char *file,
xmlXPathContextPtr ctxt)
{
int num, ret = -1;
int num;
size_t i;
xmlNodePtr *nodes = NULL;
virNetworkObjPtr obj;
VIR_AUTOFREE(xmlNodePtr *) nodes = NULL;
num = virXPathNodeSet("/node/network", ctxt, &nodes);
if (num < 0)
goto error;
return -1;
for (i = 0; i < num; i++) {
virNetworkDefPtr def;
xmlNodePtr node = testParseXMLDocFromFile(nodes[i], file, "network");
if (!node)
goto error;
return -1;
def = virNetworkDefParseNode(ctxt->doc, node);
if (!def)
goto error;
return -1;
if (!(obj = virNetworkObjAssignDef(privconn->networks, def, 0))) {
virNetworkDefFree(def);
goto error;
return -1;
}
virNetworkObjSetActive(obj, true);
virNetworkObjEndAPI(&obj);
}
ret = 0;
error:
VIR_FREE(nodes);
return ret;
return 0;
}
@ -978,39 +968,36 @@ testParseInterfaces(testDriverPtr privconn,
const char *file,
xmlXPathContextPtr ctxt)
{
int num, ret = -1;
int num;
size_t i;
xmlNodePtr *nodes = NULL;
virInterfaceObjPtr obj;
VIR_AUTOFREE(xmlNodePtr *) nodes = NULL;
num = virXPathNodeSet("/node/interface", ctxt, &nodes);
if (num < 0)
goto error;
return -1;
for (i = 0; i < num; i++) {
virInterfaceDefPtr def;
xmlNodePtr node = testParseXMLDocFromFile(nodes[i], file,
"interface");
if (!node)
goto error;
return -1;
def = virInterfaceDefParseNode(ctxt->doc, node);
if (!def)
goto error;
return -1;
if (!(obj = virInterfaceObjListAssignDef(privconn->ifaces, def))) {
virInterfaceDefFree(def);
goto error;
return -1;
}
virInterfaceObjSetActive(obj, true);
virInterfaceObjEndAPI(&obj);
}
ret = 0;
error:
VIR_FREE(nodes);
return ret;
return 0;
}
@ -1021,51 +1008,47 @@ testOpenVolumesForPool(const char *file,
int objidx)
{
virStoragePoolDefPtr def = virStoragePoolObjGetDef(obj);
char *vol_xpath;
size_t i;
int num, ret = -1;
xmlNodePtr *nodes = NULL;
int num;
VIR_AUTOFREE(char *) vol_xpath = NULL;
VIR_AUTOFREE(xmlNodePtr *) nodes = NULL;
VIR_AUTOPTR(virStorageVolDef) volDef = NULL;
/* Find storage volumes */
if (virAsprintf(&vol_xpath, "/node/pool[%d]/volume", objidx) < 0)
goto error;
return -1;
num = virXPathNodeSet(vol_xpath, ctxt, &nodes);
VIR_FREE(vol_xpath);
if (num < 0)
goto error;
return -1;
for (i = 0; i < num; i++) {
xmlNodePtr node = testParseXMLDocFromFile(nodes[i], file,
"volume");
if (!node)
goto error;
return -1;
if (!(volDef = virStorageVolDefParseNode(def, ctxt->doc, node, 0)))
goto error;
return -1;
if (!volDef->target.path) {
if (virAsprintf(&volDef->target.path, "%s/%s",
def->target.path, volDef->name) < 0)
goto error;
return -1;
}
if (!volDef->key && VIR_STRDUP(volDef->key, volDef->target.path) < 0)
goto error;
return -1;
if (virStoragePoolObjAddVol(obj, volDef) < 0)
goto error;
return -1;
def->allocation += volDef->target.allocation;
def->available = (def->capacity - def->allocation);
volDef = NULL;
}
ret = 0;
error:
VIR_FREE(nodes);
return ret;
return 0;
}
@ -1074,50 +1057,47 @@ testParseStorage(testDriverPtr privconn,
const char *file,
xmlXPathContextPtr ctxt)
{
int num, ret = -1;
int num;
size_t i;
xmlNodePtr *nodes = NULL;
virStoragePoolObjPtr obj;
VIR_AUTOFREE(xmlNodePtr *) nodes = NULL;
num = virXPathNodeSet("/node/pool", ctxt, &nodes);
if (num < 0)
goto error;
return -1;
for (i = 0; i < num; i++) {
virStoragePoolDefPtr def;
xmlNodePtr node = testParseXMLDocFromFile(nodes[i], file,
"pool");
if (!node)
goto error;
return -1;
def = virStoragePoolDefParseNode(ctxt->doc, node);
if (!def)
goto error;
return -1;
if (!(obj = virStoragePoolObjAssignDef(privconn->pools, def, false))) {
virStoragePoolDefFree(def);
goto error;
return -1;
}
if (testStoragePoolObjSetDefaults(obj) == -1) {
virStoragePoolObjEndAPI(&obj);
goto error;
return -1;
}
virStoragePoolObjSetActive(obj, true);
/* Find storage volumes */
if (testOpenVolumesForPool(file, ctxt, obj, i+1) < 0) {
virStoragePoolObjEndAPI(&obj);
goto error;
return -1;
}
virStoragePoolObjEndAPI(&obj);
}
ret = 0;
error:
VIR_FREE(nodes);
return ret;
return 0;
}
@ -1126,79 +1106,70 @@ testParseNodedevs(testDriverPtr privconn,
const char *file,
xmlXPathContextPtr ctxt)
{
int num, ret = -1;
int num;
size_t i;
xmlNodePtr *nodes = NULL;
virNodeDeviceObjPtr obj;
VIR_AUTOFREE(xmlNodePtr *) nodes = NULL;
num = virXPathNodeSet("/node/device", ctxt, &nodes);
if (num < 0)
goto error;
return -1;
for (i = 0; i < num; i++) {
virNodeDeviceDefPtr def;
xmlNodePtr node = testParseXMLDocFromFile(nodes[i], file,
"nodedev");
if (!node)
goto error;
return -1;
def = virNodeDeviceDefParseNode(ctxt->doc, node, 0, NULL);
if (!def)
goto error;
return -1;
if (!(obj = virNodeDeviceObjListAssignDef(privconn->devs, def))) {
virNodeDeviceDefFree(def);
goto error;
return -1;
}
virNodeDeviceObjSetSkipUpdateCaps(obj, true);
virNodeDeviceObjEndAPI(&obj);
}
ret = 0;
error:
VIR_FREE(nodes);
return ret;
return 0;
}
static int
testParseAuthUsers(testDriverPtr privconn,
xmlXPathContextPtr ctxt)
{
int num, ret = -1;
int num;
size_t i;
xmlNodePtr *nodes = NULL;
VIR_AUTOFREE(xmlNodePtr *) nodes = NULL;
num = virXPathNodeSet("/node/auth/user", ctxt, &nodes);
if (num < 0)
goto error;
return -1;
privconn->numAuths = num;
if (num && VIR_ALLOC_N(privconn->auths, num) < 0)
goto error;
return -1;
for (i = 0; i < num; i++) {
char *username, *password;
VIR_AUTOFREE(char *) username = NULL;
ctxt->node = nodes[i];
username = virXPathString("string(.)", ctxt);
if (!username || STREQ(username, "")) {
virReportError(VIR_ERR_XML_ERROR, "%s",
_("missing username in /node/auth/user field"));
VIR_FREE(username);
goto error;
return -1;
}
/* This field is optional. */
password = virXMLPropString(nodes[i], "password");
privconn->auths[i].username = username;
privconn->auths[i].password = password;
privconn->auths[i].password = virXMLPropString(nodes[i], "password");
VIR_STEAL_PTR(privconn->auths[i].username, username);
}
ret = 0;
error:
VIR_FREE(nodes);
return ret;
return 0;
}
static int
@ -1348,7 +1319,8 @@ testConnectAuthenticate(virConnectPtr conn,
testDriverPtr privconn = conn->privateData;
int ret = -1;
ssize_t i;
char *username = NULL, *password = NULL;
VIR_AUTOFREE(char *) username = NULL;
VIR_AUTOFREE(char *) password = NULL;
virObjectLock(privconn);
if (privconn->numAuths == 0) {
@ -1388,8 +1360,6 @@ testConnectAuthenticate(virConnectPtr conn,
ret = 0;
cleanup:
virObjectUnlock(privconn);
VIR_FREE(username);
VIR_FREE(password);
return ret;
}
@ -1977,12 +1947,12 @@ testDomainSaveFlags(virDomainPtr domain, const char *path,
const char *dxml, unsigned int flags)
{
testDriverPtr privconn = domain->conn->privateData;
char *xml = NULL;
int fd = -1;
int len;
virDomainObjPtr privdom;
virObjectEventPtr event = NULL;
int ret = -1;
VIR_AUTOFREE(char *) xml = NULL;
virCheckFlags(0, -1);
if (dxml) {
@ -2052,8 +2022,6 @@ testDomainSaveFlags(virDomainPtr domain, const char *path,
ret = 0;
cleanup:
VIR_FREE(xml);
/* Don't report failure in close or unlink, because
* in either case we're already in a failure scenario
* and have reported an earlier error */
@ -2080,7 +2048,6 @@ testDomainRestoreFlags(virConnectPtr conn,
unsigned int flags)
{
testDriverPtr privconn = conn->privateData;
char *xml = NULL;
char magic[15];
int fd = -1;
int len;
@ -2088,6 +2055,7 @@ testDomainRestoreFlags(virConnectPtr conn,
virDomainObjPtr dom = NULL;
virObjectEventPtr event = NULL;
int ret = -1;
VIR_AUTOFREE(char *) xml = NULL;
virCheckFlags(0, -1);
if (dxml) {
@ -2162,7 +2130,6 @@ testDomainRestoreFlags(virConnectPtr conn,
cleanup:
virDomainDefFree(def);
VIR_FREE(xml);
VIR_FORCE_CLOSE(fd);
virDomainObjEndAPI(&dom);
virObjectEventStateQueue(privconn->eventState, event);
@ -2578,7 +2545,7 @@ testDomainRenameCallback(virDomainObjPtr privdom,
virObjectEventPtr event_new = NULL;
virObjectEventPtr event_old = NULL;
int ret = -1;
char *new_dom_name = NULL;
VIR_AUTOFREE(char *) new_dom_name = NULL;
virCheckFlags(0, -1);
@ -2605,7 +2572,6 @@ testDomainRenameCallback(virDomainObjPtr privdom,
ret = 0;
cleanup:
VIR_FREE(new_dom_name);
virObjectEventStateQueue(driver->eventState, event_old);
virObjectEventStateQueue(driver->eventState, event_new);
return ret;
@ -5466,12 +5432,12 @@ testNodeDeviceMockCreateVport(testDriverPtr driver,
const char *wwnn,
const char *wwpn)
{
char *xml = NULL;
virNodeDeviceDefPtr def = NULL;
virNodeDevCapsDefPtr caps;
virNodeDeviceObjPtr obj = NULL, objcopy = NULL;
virNodeDeviceDefPtr objdef;
virObjectEventPtr event = NULL;
VIR_AUTOFREE(char *) xml = NULL;
/* In the real code, we'd call virVHBAManageVport which would take the
* wwnn/wwpn from the input XML in order to call the "vport_create"
@ -5535,7 +5501,6 @@ testNodeDeviceMockCreateVport(testDriverPtr driver,
virObjectEventStateQueue(driver->eventState, event);
cleanup:
VIR_FREE(xml);
virNodeDeviceDefFree(def);
return obj;
}
@ -5548,10 +5513,11 @@ testNodeDeviceCreateXML(virConnectPtr conn,
{
testDriverPtr driver = conn->privateData;
virNodeDeviceDefPtr def = NULL;
char *wwnn = NULL, *wwpn = NULL;
virNodeDevicePtr dev = NULL, ret = NULL;
virNodeDeviceObjPtr obj = NULL;
virNodeDeviceDefPtr objdef;
VIR_AUTOFREE(char *) wwnn = NULL;
VIR_AUTOFREE(char *) wwpn = NULL;
virCheckFlags(0, NULL);
@ -5592,8 +5558,6 @@ testNodeDeviceCreateXML(virConnectPtr conn,
virNodeDeviceObjEndAPI(&obj);
virNodeDeviceDefFree(def);
virObjectUnref(dev);
VIR_FREE(wwnn);
VIR_FREE(wwpn);
return ret;
}
@ -5605,8 +5569,9 @@ testNodeDeviceDestroy(virNodeDevicePtr dev)
virNodeDeviceObjPtr obj = NULL;
virNodeDeviceObjPtr parentobj = NULL;
virNodeDeviceDefPtr def;
char *wwnn = NULL, *wwpn = NULL;
virObjectEventPtr event = NULL;
VIR_AUTOFREE(char *) wwnn = NULL;
VIR_AUTOFREE(char *) wwpn = NULL;
if (!(obj = testNodeDeviceObjFindByName(driver, dev->name)))
return -1;
@ -5644,8 +5609,6 @@ testNodeDeviceDestroy(virNodeDevicePtr dev)
cleanup:
virNodeDeviceObjEndAPI(&obj);
virObjectEventStateQueue(driver->eventState, event);
VIR_FREE(wwnn);
VIR_FREE(wwpn);
return ret;
}