[libvirt] [PATCH 1/2] test: enable testing for expected parse errors in network XML

Laine Stump laine at laine.org
Mon Apr 18 19:12:22 UTC 2016


This is patterned after similar functionality for domain XML tests,
but tries harder to avoid reading non-existent networkxml2xmlout data
file when parse fails.
---
 tests/networkxml2xmltest.c | 75 +++++++++++++++++++++++++++++++++++-----------
 1 file changed, 57 insertions(+), 18 deletions(-)

diff --git a/tests/networkxml2xmltest.c b/tests/networkxml2xmltest.c
index 8d60aa8..b83396b 100644
--- a/tests/networkxml2xmltest.c
+++ b/tests/networkxml2xmltest.c
@@ -16,26 +16,58 @@
 
 #define VIR_FROM_THIS VIR_FROM_NONE
 
+typedef enum {
+    TEST_COMPARE_NET_XML2XML_RESULT_SUCCESS,
+    TEST_COMPARE_NET_XML2XML_RESULT_FAIL_PARSE,
+    TEST_COMPARE_NET_XML2XML_RESULT_FAIL_FORMAT,
+    TEST_COMPARE_NET_XML2XML_RESULT_FAIL_COMPARE,
+} testCompareNetXML2XMLResult;
+
 static int
 testCompareXMLToXMLFiles(const char *inxml, const char *outxml,
-                         unsigned int flags)
+                         unsigned int flags,
+                         testCompareNetXML2XMLResult expectResult)
 {
     char *actual = NULL;
-    int ret = -1;
+    int ret;
+    testCompareNetXML2XMLResult result = TEST_COMPARE_DOM_XML2XML_RESULT_SUCCESS;
     virNetworkDefPtr dev = NULL;
 
-    if (!(dev = virNetworkDefParseFile(inxml)))
-        goto fail;
-
-    if (!(actual = virNetworkDefFormat(dev, flags)))
-        goto fail;
+    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 (virtTestCompareToFile(actual, outxml) < 0)
-        goto fail;
+    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 (virtTestCompareToFile(actual, outxml) < 0) {
+        result = TEST_COMPARE_NET_XML2XML_RESULT_FAIL_COMPARE;
+        goto cleanup;
+    }
+    if (expectResult == TEST_COMPARE_NET_XML2XML_RESULT_FAIL_COMPARE)
+        goto cleanup;
 
-    ret = 0;
+ 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();
 
- fail:
     VIR_FREE(actual);
     virNetworkDefFree(dev);
     return ret;
@@ -44,6 +76,7 @@ testCompareXMLToXMLFiles(const char *inxml, const char *outxml,
 struct testInfo {
     const char *name;
     unsigned int flags;
+    testCompareNetXML2XMLResult expectResult;
 };
 
 static int
@@ -61,7 +94,8 @@ testCompareXMLToXMLHelper(const void *data)
         goto cleanup;
     }
 
-    result = testCompareXMLToXMLFiles(inxml, outxml, info->flags);
+    result = testCompareXMLToXMLFiles(inxml, outxml, info->flags,
+                                      info->expectResult);
 
  cleanup:
     VIR_FREE(inxml);
@@ -75,14 +109,19 @@ mymain(void)
 {
     int ret = 0;
 
-#define DO_TEST_FULL(name, flags)                                       \
+#define DO_TEST_FULL(name, flags, expectResult)                         \
     do {                                                                \
-        const struct testInfo info = {name, flags};                     \
+        const struct testInfo info = {name, flags, expectResult};       \
         if (virtTestRun("Network XML-2-XML " name,                      \
-                        testCompareXMLToXMLHelper, &info) < 0)          \
+                        testCompareXMLToXMLHelper, &info) < 0) \
             ret = -1;                                                   \
     } while (0)
-#define DO_TEST(name) DO_TEST_FULL(name, 0)
+#define DO_TEST(name) \
+    DO_TEST_FULL(name, 0, TEST_COMPARE_NET_XML2XML_RESULT_SUCCESS)
+#define DO_TEST_FLAGS(name, flags) \
+    DO_TEST_FULL(name, flags, TEST_COMPARE_NET_XML2XML_RESULT_SUCCESS)    
+#define DO_TEST_PARSE_ERROR(name) \
+    DO_TEST_FULL(name, 0, TEST_COMPARE_NET_XML2XML_RESULT_FAIL_PARSE)
 
     DO_TEST("dhcp6host-routed-network");
     DO_TEST("empty-allow-ipv6");
@@ -106,9 +145,9 @@ mymain(void)
     DO_TEST("vepa-net");
     DO_TEST("bandwidth-network");
     DO_TEST("openvswitch-net");
-    DO_TEST_FULL("passthrough-pf", VIR_NETWORK_XML_INACTIVE);
+    DO_TEST_FLAGS("passthrough-pf", VIR_NETWORK_XML_INACTIVE);
     DO_TEST("hostdev");
-    DO_TEST_FULL("hostdev-pf", VIR_NETWORK_XML_INACTIVE);
+    DO_TEST_FLAGS("hostdev-pf", VIR_NETWORK_XML_INACTIVE);
     DO_TEST("passthrough-address-crash");
     DO_TEST("nat-network-explicit-flood");
     DO_TEST("host-bridge-no-flood");
-- 
2.5.5




More information about the libvir-list mailing list