Skip to contentSkip to conversation

Testing libvirt XML -> libxl_domain_config conversion

PATCH New Jim Fehlig 1 participant 4 messages Sep 11, 2014

lore
Share & tools
lore

Jim Fehlig

author
#0 lore raw

v4 cover Testing libvirt XML -> libxl_domain_config conversion

This is version 4 of the work started by danpb:

https://www.redhat.com/archives/libvir-list/2014-May/msg01102.html

This series tests the conversion of libvirt XML to libxl_domain_config objects by the libvirt libxl driver.

Changed in V4:

- V3 patches 2-4 have been pushed
- Patch 1 is unchanged from V3
- Patch 2 is new and adds tests for virJSONStringCompare
- Cleanup of ignored context paths definition in patch 3 (was #5 in V3)

Daniel P. Berrange (2):
util: Introduce virJSONStringCompare for JSON doc comparisons
libxl: Add a test suite for libxl option generator

Jim Fehlig (1):
tests: add tests for virJSONStringCompare

 configure.ac                           |   2 +
 src/libvirt_private.syms               |   1 +
 src/util/virjson.c                     | 242 +++++++++++++++++++++++++++++++++
 src/util/virjson.h                     |  16 +++
 tests/Makefile.am                      |  25 +++-
 tests/jsontest.c                       |  63 ++++++++-
 tests/libxlxml2jsondata/basic-hvm.json | 217 +++++++++++++++++++++++++++++
 tests/libxlxml2jsondata/basic-hvm.xml  |  36 +++++
 tests/libxlxml2jsondata/basic-pv.json  | 163 ++++++++++++++++++++++
 tests/libxlxml2jsondata/basic-pv.xml   |  28 ++++
 tests/libxlxml2jsontest.c              | 228 +++++++++++++++++++++++++++++++
 tests/virmocklibxl.c                   |  87 ++++++++++++
 12 files changed, 1102 insertions(+), 6 deletions(-)
 create mode 100644 tests/libxlxml2jsondata/basic-hvm.json
 create mode 100644 tests/libxlxml2jsondata/basic-hvm.xml
 create mode 100644 tests/libxlxml2jsondata/basic-pv.json
 create mode 100644 tests/libxlxml2jsondata/basic-pv.xml
 create mode 100644 tests/libxlxml2jsontest.c
 create mode 100644 tests/virmocklibxl.c
Signature · 1 line
1.8.4.5
author
#1 lore raw

PATCH v4 1/3 util: Introduce virJSONStringCompare for JSON doc comparisons

From: "Daniel P. Berrange" <[email protected]>

Comparing JSON docs using strcmp is simple, but is not flexible as it is sensitive to whitespace used in the doc generation. When comparing objects it may also be desirable to treat the existance of keys in the actual object but not expected object as non-fatal. Introduce a virJSONStringCompare function which takes two strings representing expected and actual JSON docs and then does a DOM comparison. Comparison is controled with the ignore_contexts and flags parameters. No comparison is done on context paths specified in ignore_contexts. The VIR_JSON_COMPARE_IGNORE_EXPECTED_NULL flag can be used to ignore actual values that have changed from an expected value of null.

---

Beyond rebasing, unchanged from V3.

 src/libvirt_private.syms |   1 +
 src/util/virjson.c       | 242 +++++++++++++++++++++++++++++++++++++++++++++++
 src/util/virjson.h       |  16 ++++
 3 files changed, 259 insertions(+)
src/libvirt_private.syms +1 -0
index fdf4548..b0c0625 100644
@@ -1446,6 +1446,7 @@ virISCSIScanTargets;
 
 
 # util/virjson.h
+virJSONStringCompare;
 virJSONValueArrayAppend;
 virJSONValueArrayGet;
 virJSONValueArraySize;
src/util/virjson.c +242 -0

270 lines not on this page · raw message

src/util/virjson.h +16 -0
index 9487729..5dc948f 100644
@@ -34,6 +34,8 @@ typedef enum {
     VIR_JSON_TYPE_NUMBER,
     VIR_JSON_TYPE_BOOLEAN,
     VIR_JSON_TYPE_NULL,
+
+    VIR_JSON_TYPE_LAST,
 } virJSONType;
 
 typedef struct _virJSONValue virJSONValue;
@@ -141,4 +143,18 @@ virJSONValuePtr virJSONValueFromString(const char *jsonstring);
 char *virJSONValueToString(virJSONValuePtr object,
                            bool pretty);
 
+typedef enum {
+    /*
+     * when comparing two values, if their types are different,
+     * and the 'expected' value type is 'null', then this will
+     * be considered non-fatal.
+     */
+    VIR_JSON_COMPARE_IGNORE_EXPECTED_NULL = (1 << 0),
+} virJSONCompareFlags;
+
+bool virJSONStringCompare(const char *expect,
+                          const char *actual,
+                          const char **ignore_contexts,
+                          unsigned int flags);
+
 #endif /* __VIR_JSON_H_ */
Signature · 1 line
1.8.4.5
author
#2 lore raw

PATCH v4 2/3 tests: add tests for virJSONStringCompare

In reply to Jim Fehlig

---

This patch is new to V4.

 tests/jsontest.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 58 insertions(+), 5 deletions(-)
tests/jsontest.c +58 -5
index a2a42e3..5d5f619 100644
@@ -12,6 +12,7 @@
 struct testInfo {
     const char *doc;
     const char *expect;
+    const char **ignore_contexts;
     bool pass;
 };
 
@@ -131,22 +132,45 @@ testJSONAddRemove(const void *data)
 
 
 static int
+testJSONCompare(const void *data)
+{
+    const struct testInfo *info = data;
+    bool ret;
+
+    ret = virJSONStringCompare(info->expect,
+                               info->doc,
+                               info->ignore_contexts,
+                               0);
+
+    if (ret == info->pass)
+        return 0;
+    else
+        return -1;
+}
+
+
+static int
 mymain(void)
 {
     int ret = 0;
+    const char *ignore_paths[] = {
+        "/dict1/key3",
+        "/key4",
+        NULL
+    };
 
-#define DO_TEST_FULL(name, cmd, doc, expect, pass)                  \
+#define DO_TEST_FULL(name, cmd, doc, expect, ignore_ctx, pass)      \
     do {                                                            \
-        struct testInfo info = { doc, expect, pass };               \
+        struct testInfo info = { doc, expect, ignore_ctx, pass };   \
         if (virtTestRun(name, testJSON ## cmd, &info) < 0)          \
             ret = -1;                                               \
     } while (0)
 
 #define DO_TEST_PARSE(name, doc)                \
-    DO_TEST_FULL(name, FromString, doc, NULL, true)
+    DO_TEST_FULL(name, FromString, doc, NULL, NULL, true)
 
 #define DO_TEST_PARSE_FAIL(name, doc)           \
-    DO_TEST_FULL(name, FromString, doc, NULL, false)
+    DO_TEST_FULL(name, FromString, doc, NULL, NULL, false)
 
 
     DO_TEST_PARSE("Simple", "{\"return\": {}, \"id\": \"libvirt-1\"}");
@@ -188,9 +212,10 @@ mymain(void)
     DO_TEST_FULL("add and remove", AddRemove,
                  "{\"name\": \"sample\", \"value\": true}",
                  "{\"value\":true,\"newname\":\"foo\"}",
+                 NULL,
                  true);
     DO_TEST_FULL("add and remove", AddRemove,
-                 "[ 1 ]", NULL, false);
+                 "[ 1 ]", NULL, NULL, false);
 
 
     DO_TEST_PARSE("almost nothing", "[]");
@@ -214,6 +239,34 @@ mymain(void)
                        "[ {[\"key1\", \"key2\"]: \"value\"} ]");
     DO_TEST_PARSE_FAIL("object with unterminated key", "{ \"key:7 }");
 
+    DO_TEST_FULL("Compare identical docs", Compare,
+                 "{ \"key1\" : \"value1\", \"key2\" : \"value2\"}",
+                 "{ \"key1\" : \"value1\", \"key2\" : \"value2\"}",
+                 NULL, true);
+
+    DO_TEST_FULL("Compare different docs", Compare,
+                 "{\"dict1\" : { \"key1\" : \"value1\", \"key2\" : \"value2\", "
+                 "\"key3\" : [\"elem1\", \"elemfoo\"]}}",
+                 "{\"dict1\" : { \"key1\" : \"value1\", \"key2\" : \"value2\", "
+                 "\"key3\" : [\"elem1\", \"elem2\"]}}",
+                 NULL, false);
+
+    DO_TEST_FULL("Compare docs and ignore differing contexts", Compare,
+                 "{\"dict1\" : { \"key1\" : \"value1\", \"key2\" : \"value2\", "
+                 "\"key3\" : \"valuefoo\"}, \"key4\" : \"valuebar\"}",
+                 "{\"dict1\" : { \"key1\" : \"value1\", \"key2\" : \"value2\", "
+                 "\"key3\" : \"value3\"}, \"key4\" : \"value4\"}",
+                 ignore_paths, true);
+
+    DO_TEST_FULL("Compare docs with insufficient ignored contexts", Compare,
+                 "{\"dict1\" : { \"key1\" : \"value1\", \"key2\" : \"value2\", "
+                 "\"key3\" : \"valuefoo\"}, \"key4\" : \"valuebar\", "
+                 "\"key5\" : \"valuebaz\"}",
+                 "{\"dict1\" : { \"key1\" : \"value1\", \"key2\" : \"value2\", "
+                 "\"key3\" : \"value3\"}, \"key4\" : \"value4\", "
+                 "\"key5\" : \"value5\"}",
+                 ignore_paths, false);
+
     return (ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
 }
 
Signature · 1 line
1.8.4.5
author
#3 lore raw

PATCH v4 3/3 libxl: Add a test suite for libxl option generator

In reply to Jim Fehlig

From: "Daniel P. Berrange" <[email protected]>

The libxl library allows a libxl_domain_config object to be serialized to a JSON string. Use this to allow testing of the XML -> libxl_domain_config conversion process

---

V4: Cleanup definition of ignored context paths

 configure.ac                           |   2 +
 tests/Makefile.am                      |  25 +++-
 tests/libxlxml2jsondata/basic-hvm.json | 217 +++++++++++++++++++++++++++++++
 tests/libxlxml2jsondata/basic-hvm.xml  |  36 ++++++
 tests/libxlxml2jsondata/basic-pv.json  | 163 +++++++++++++++++++++++
 tests/libxlxml2jsondata/basic-pv.xml   |  28 ++++
 tests/libxlxml2jsontest.c              | 228 +++++++++++++++++++++++++++++++++
 tests/virmocklibxl.c                   |  87 +++++++++++++
 8 files changed, 785 insertions(+), 1 deletion(-)
configure.ac +2 -0
index b4fb99a..80c331f 100644
@@ -875,6 +875,8 @@ if test "$with_libxl" != "no" ; then
     AC_CHECK_LIB([xenlight], [libxl_ctx_alloc], [
         with_libxl=yes
         LIBXL_LIBS="$LIBXL_LIBS -lxenlight -lxenctrl"
+        LIBS="$LIBS -lxenlight -lxenctrl"
+        AC_CHECK_FUNCS([libxl_domain_config_to_json])
     ],[
         if test "$with_libxl" = "yes"; then
             fail=1
tests/Makefile.am +24 -1
index d6c3cfb..a8e4e88 100644
@@ -84,6 +84,7 @@ EXTRA_DIST =		\
 	domainsnapshotxml2xmlout \
 	fchostdata \
 	interfaceschemadata \
+	libxlxml2jsondata \
 	lxcconf2xmldata \
 	lxcxml2xmldata \
 	lxcxml2xmloutdata \
@@ -222,6 +223,9 @@ if WITH_XEN
 test_programs += xml2sexprtest sexpr2xmltest \
 	xmconfigtest xencapstest statstest reconnect
 endif WITH_XEN
+if WITH_LIBXL
+test_programs += libxlxml2jsontest
+endif WITH_LIBXL
 if WITH_QEMU
 test_programs += qemuxml2argvtest qemuxml2xmltest qemuxmlnstest \
 	qemuargv2xmltest qemuhelptest domainsnapshotxml2xmltest \
@@ -393,7 +397,9 @@ test_libraries += libqemumonitortestutils.la \
 		qemuxml2argvmock.la \
 		$(NULL)
 endif WITH_QEMU
-
+if WITH_LIBXL
+test_libraries += virmocklibxl.la
+endif WITH_LIBXL
 if WITH_BHYVE
 test_libraries += bhyvexml2argvmock.la
 endif WITH_BHYVE
@@ -593,6 +599,23 @@ EXTRA_DIST += qemuxml2argvtest.c qemuxml2xmltest.c qemuargv2xmltest.c \
 	$(QEMUMONITORTESTUTILS_SOURCES)
 endif ! WITH_QEMU
 
+if WITH_LIBXL
+libxl_LDADDS = ../src/libvirt_driver_libxl_impl.la
+libxl_LDADDS += $(LDADDS)
+
+libxlxml2jsontest_SOURCES = \
+	libxlxml2jsontest.c testutilsxen.c testutilsxen.h \
+	testutils.c testutils.h
+libxlxml2jsontest_LDADD = $(libxl_LDADDS) $(LIBXML_LIBS)
+
+virmocklibxl_la_SOURCES = \
+	virmocklibxl.c
+virmocklibxl_la_CFLAGS = $(AM_CFLAGS)
+virmocklibxl_la_LDFLAGS = -module -avoid-version \
+        -rpath /evil/libtool/hack/to/force/shared/lib/creation
+
+endif WITH_LIBXL
+
 if WITH_LXC
 
 lxc_LDADDS = ../src/libvirt_driver_lxc_impl.la
tests/libxlxml2jsondata/basic-hvm.json new file +217 -0

217 lines not on this page · raw message

tests/libxlxml2jsondata/basic-hvm.xml new file +36 -0
new file mode 100644
index 0000000..693a715
@@ -0,0 +1,36 @@
+<domain type='xen'>
+  <name>sles-hvm</name>
+  <description>None</description>
+  <uuid>2147d599-9cc6-c0dc-92ab-4064b5446e9b</uuid>
+  <memory>1048576</memory>
+  <currentMemory>1048576</currentMemory>
+  <vcpu>4</vcpu>
+  <on_poweroff>destroy</on_poweroff>
+  <on_reboot>restart</on_reboot>
+  <on_crash>destroy</on_crash>
+  <clock sync='utc'/>
+  <os>
+    <type>hvm</type>
+    <loader>/usr/lib/xen/boot/hvmloader</loader>
+    <boot dev='hd'/>
+  </os>
+  <features>
+    <apic/>
+    <acpi/>
+    <pae/>
+  </features>
+  <devices>
+    <emulator>/usr/lib/xen/bin/qemu-system-i386</emulator>
+    <disk type='file' device='disk'>
+      <driver name='qemu'/>
+      <source file='/var/lib/xen/images/sles-hvm.img'/>
+      <target dev='hda'/>
+    </disk>
+    <interface type='bridge'>
+      <source bridge='br0'/>
+      <mac address='00:16:3e:66:12:b4'/>
+      <script path='/etc/xen/scripts/vif-bridge'/>
+    </interface>
+    <graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0'/>
+  </devices>
+</domain>
tests/libxlxml2jsondata/basic-pv.json new file +163 -0

163 lines not on this page · raw message

tests/libxlxml2jsondata/basic-pv.xml new file +28 -0
new file mode 100644
index 0000000..109e657
@@ -0,0 +1,28 @@
+<domain type='xen'>
+  <name>sles11-pv</name>
+  <uuid>039e9ee6-4a84-3055-4c81-8ba426ae2656</uuid>
+  <memory>524288</memory>
+  <currentMemory>524288</currentMemory>
+  <vcpu>4</vcpu>
+  <bootloader>pygrub</bootloader>
+  <os>
+    <type arch='i686' machine='xenpv'>linux</type>
+  </os>
+  <clock offset='utc'/>
+  <on_poweroff>destroy</on_poweroff>
+  <on_reboot>restart</on_reboot>
+  <on_crash>destroy</on_crash>
+  <devices>
+    <disk type='file' device='disk'>
+      <driver name='qemu'/>
+      <source file='/var/lib/xen/images/sles11-pv.img'/>
+      <target dev='xvda'/>
+    </disk>
+    <interface type='bridge'>
+      <source bridge='br0'/>
+      <mac address='00:16:3e:3e:86:60'/>
+      <script path='/etc/xen/scripts/vif-bridge'/>
+    </interface>
+    <graphics type='vnc' port='-1' autoport='yes' listen='0.0.0.0'/>
+  </devices>
+</domain>
tests/libxlxml2jsontest.c new file +228 -0

228 lines not on this page · raw message

tests/virmocklibxl.c new file +87 -0

87 lines not on this page · raw message

Signature · 1 line
1.8.4.5