]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
test: Extract common parts of test driver data allocation
authorPeter Krempa <pkrempa@redhat.com>
Wed, 24 Jun 2015 09:06:24 +0000 (11:06 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Thu, 25 Jun 2015 12:09:48 +0000 (14:09 +0200)
src/test/test_driver.c

index 732dde93c8cdaf8735be85e674c98ac954ab6967..c68b3d64a378249f731f4d9bbc8976c1eba9f459 100644 (file)
@@ -309,24 +309,6 @@ testDomainDefNamespaceParse(xmlDocPtr xml ATTRIBUTE_UNUSED,
     return -1;
 }
 
-static virDomainXMLOptionPtr
-testBuildXMLConfig(void)
-{
-    virDomainXMLPrivateDataCallbacks priv = {
-        .alloc = testDomainObjPrivateAlloc,
-        .free = testDomainObjPrivateFree
-    };
-
-    /* All our XML extensions are input only, so we only need to parse */
-    virDomainXMLNamespace ns = {
-        .parse = testDomainDefNamespaceParse,
-        .free = testDomainDefNamespaceFree,
-    };
-
-    return virDomainXMLOptionNew(NULL, &priv, &ns);
-}
-
-
 static virCapsPtr
 testBuildCapabilities(virConnectPtr conn)
 {
@@ -402,6 +384,45 @@ testBuildCapabilities(virConnectPtr conn)
 }
 
 
+static testDriverPtr
+testDriverNew(void)
+{
+    virDomainXMLPrivateDataCallbacks priv = {
+        .alloc = testDomainObjPrivateAlloc,
+        .free = testDomainObjPrivateFree
+    };
+
+    virDomainXMLNamespace ns = {
+        .parse = testDomainDefNamespaceParse,
+        .free = testDomainDefNamespaceFree,
+    };
+    testDriverPtr ret;
+
+    if (VIR_ALLOC(ret) < 0)
+        return NULL;
+
+    if (virMutexInit(&ret->lock) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       "%s", _("cannot initialize mutex"));
+        goto error;
+    }
+
+    if (!(ret->xmlopt = virDomainXMLOptionNew(NULL, &priv, &ns)) ||
+        !(ret->eventState = virObjectEventStateNew()) ||
+        !(ret->domains = virDomainObjListNew()) ||
+        !(ret->networks = virNetworkObjListNew()))
+        goto error;
+
+    ret->nextDomID = 1;
+
+    return ret;
+
+ error:
+    testDriverFree(ret);
+    return NULL;
+}
+
+
 static const char *defaultDomainXML =
 "<domain type='test'>"
 "  <name>test</name>"
@@ -730,24 +751,11 @@ testOpenDefault(virConnectPtr conn)
         return VIR_DRV_OPEN_SUCCESS;
     }
 
-    if (VIR_ALLOC(privconn) < 0)
-        goto error;
-
-    if (virMutexInit(&privconn->lock) < 0) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       "%s", _("cannot initialize mutex"));
+    if (!(privconn = testDriverNew()))
         goto error;
-    }
 
     conn->privateData = privconn;
 
-    if (!(privconn->eventState = virObjectEventStateNew()))
-        goto error;
-
-    if (!(privconn->domains = virDomainObjListNew()) ||
-        !(privconn->networks = virNetworkObjListNew()))
-        goto error;
-
     memmove(&privconn->nodeInfo, &defaultNodeInfo, sizeof(defaultNodeInfo));
 
     /* Numa setup */
@@ -770,11 +778,6 @@ testOpenDefault(virConnectPtr conn)
     if (!(privconn->caps = testBuildCapabilities(conn)))
         goto error;
 
-    if (!(privconn->xmlopt = testBuildXMLConfig()))
-        goto error;
-
-    privconn->nextDomID = 1;
-
     if (!(domdef = virDomainDefParseString(defaultDomainXML,
                                            privconn->caps,
                                            privconn->xmlopt,
@@ -1412,31 +1415,15 @@ testOpenFromFile(virConnectPtr conn, const char *file)
     xmlXPathContextPtr ctxt = NULL;
     testDriverPtr privconn;
 
-    if (VIR_ALLOC(privconn) < 0)
-        return VIR_DRV_OPEN_ERROR;
-    if (virMutexInit(&privconn->lock) < 0) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       "%s", _("cannot initialize mutex"));
-        VIR_FREE(privconn);
+    if (!(privconn = testDriverNew()))
         return VIR_DRV_OPEN_ERROR;
-    }
 
     testDriverLock(privconn);
     conn->privateData = privconn;
 
-    if (!(privconn->domains = virDomainObjListNew()) ||
-        !(privconn->networks = virNetworkObjListNew()))
-        goto error;
-
     if (!(privconn->caps = testBuildCapabilities(conn)))
         goto error;
 
-    if (!(privconn->xmlopt = testBuildXMLConfig()))
-        goto error;
-
-    if (!(privconn->eventState = virObjectEventStateNew()))
-        goto error;
-
     if (!(doc = virXMLParseFileCtxt(file, &ctxt)))
         goto error;
 
@@ -1446,7 +1433,6 @@ testOpenFromFile(virConnectPtr conn, const char *file)
         goto error;
     }
 
-    privconn->nextDomID = 1;
     privconn->numCells = 0;
     if (VIR_STRDUP(privconn->path, file) < 0)
         goto error;