]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
Added tests for the SEXPR<->XML conversion process
authorDaniel P. Berrange <berrange@redhat.com>
Thu, 24 Aug 2006 15:05:19 +0000 (15:05 +0000)
committerDaniel P. Berrange <berrange@redhat.com>
Thu, 24 Aug 2006 15:05:19 +0000 (15:05 +0000)
18 files changed:
ChangeLog
src/xend_internal.c
src/xend_internal.h
src/xs_internal.c
tests/.cvsignore
tests/Makefile.am
tests/sexpr2xml-fv.sexpr [new file with mode: 0644]
tests/sexpr2xml-fv.xml [new file with mode: 0644]
tests/sexpr2xml-pv.sexpr [new file with mode: 0644]
tests/sexpr2xml-pv.xml [new file with mode: 0644]
tests/sexpr2xmltest.c [new file with mode: 0644]
tests/testutils.c
tests/testutils.h
tests/xml2sexpr-fv.sexpr [new file with mode: 0644]
tests/xml2sexpr-fv.xml [new file with mode: 0644]
tests/xml2sexpr-pv.sexpr [new file with mode: 0644]
tests/xml2sexpr-pv.xml [new file with mode: 0644]
tests/xml2sexprtest.c [new file with mode: 0644]

index e1f5884c6e94b5e4b583e5edbafad0e60ec8ccec..48361139797b97bab85a2874539a8b0b7fdb02e4 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+Thu Aug 24 10:04:02 EDT 2006 Daniel Berrange <berrange@redhat.com>
+
+       * tests/xml2sexpr.c, tests/sexpr2xml.c: New tests suites for
+       validating the SEXPR<->XML conversion
+       * tests/*.sexpr, tests/*.xml: Data files for test suite
+       * src/xend_internal.h, src/xend_internal.c: Expose the method
+       for converting SEXPR -> XML to allow use by unit tests
+       * src/xs_internal.c: Check if connection is NULL before
+       deferencing it - test suite passes in NULL.
+
 Mon Aug 21 13:00:03 EDT 2006 Daniel Berrange <berrange@redhat.com>
 
        * src/virsh.c: check return code of virConnectListDomains in case
index 10826d84951290796f0b838ca116b2bc1f70182a..c398797a9cf6c0bf1e774a455da16b41f8e2431d 100644 (file)
@@ -1655,6 +1655,20 @@ xend_parse_sexp_desc(virConnectPtr conn, struct sexpr *root)
     return (NULL);
 }
 
+char *
+xend_parse_domain_sexp(virConnectPtr conn, char *sexpr) {
+  struct sexpr *root = string2sexpr(sexpr);
+  char *data;
+
+  if (!root)
+      return NULL;
+
+  data = xend_parse_sexp_desc(conn, root);
+
+  sexpr_free(root);
+
+  return data;
+}
 
 /**
  * sexpr_to_xend_domain_info:
index 5b7f05df3c81e31c378f35b2fec6fda27a1a485f..eff24628b2f8991e73558e32a6dd5fba7382d767 100644 (file)
@@ -613,6 +613,8 @@ char *xenDaemonDomainDumpXMLByID(virConnectPtr xend,
  */
     int xend_log(virConnectPtr xend, char *buffer, size_t n_buffer);
 
+  char *xend_parse_domain_sexp(virConnectPtr conn,  char *root);
+
 /* refactored ones */
 void xenDaemonRegister(void);
 int xenDaemonOpen(virConnectPtr conn, const char *name, int flags);
index 6a5ea0382928d525104d488d21a170fe84904316..241cb7f7635ed4f7a878575b935fec3da45ff654 100644 (file)
@@ -148,7 +148,7 @@ virDomainDoStoreQuery(virConnectPtr conn, int domid, const char *path)
     char s[256];
     unsigned int len = 0;
 
-    if (conn->xshandle == NULL)
+    if (!conn || conn->xshandle == NULL)
         return (NULL);
 
     snprintf(s, 255, "/local/domain/%d/%s", domid, path);
index e431d7cdd41f3f0029aa34477e47ba5422583aa6..fe457ec66ad57bd8e023a180c26cc309e6ed3a53 100644 (file)
@@ -3,3 +3,5 @@ Makefile.in
 .deps
 .libs
 xmlrpctest
+sexpr2xmltest
+xml2sexprtest
index b98e394f46bde5bddfec61f7791706f227b8b26e..505ac30e2bf9d697ebc2df876c15345b7bea0aa9 100644 (file)
@@ -16,7 +16,9 @@ LDADDS = \
 
 EXTRA_DIST = xmlrpcserver.py
 
-noinst_PROGRAMS = xmlrpctest
+noinst_PROGRAMS = xmlrpctest xml2sexprtest sexpr2xmltest
+
+TESTS = xml2sexprtest sexpr2xmltest
 
 # Note: xmlrpc.[c|h] is not in libvirt yet
 xmlrpctest_SOURCES = \
@@ -28,5 +30,17 @@ xmlrpctest_SOURCES = \
 xmlrpctest_LDFLAGS =
 xmlrpctest_LDADD = $(LDADDS)
 
+xml2sexprtest_SOURCES = \
+       xml2sexprtest.c \
+       testutils.c testutils.h
+xml2sexprtest_LDFLAGS =
+xml2sexprtest_LDADD = $(LDADDS)
+
+sexpr2xmltest_SOURCES = \
+       sexpr2xmltest.c \
+       testutils.c testutils.h
+sexpr2xmltest_LDFLAGS =
+sexpr2xmltest_LDADD = $(LDADDS)
+
 $(LIBVIRT):
        -@(cd $(top_builddir)/src && $(MAKE) MAKEFLAGS+=--silent)
diff --git a/tests/sexpr2xml-fv.sexpr b/tests/sexpr2xml-fv.sexpr
new file mode 100644 (file)
index 0000000..4c5540f
--- /dev/null
@@ -0,0 +1 @@
+(domain (domid 3)(name 'fvtest')(memory 400)(maxmem 400)(vcpus 1)(uuid 'b5d70dd275cdaca517769660b059d8bc')(on_poweroff 'destroy')(on_reboot 'restart')(on_crash 'restart')(image (hvm (kernel '/usr/lib/xen/boot/hvmloader')(device_model '/usr/lib64/xen/bin/qemu-dm')(boot c)(cdrom '/root/boot.iso')(acpi 1)(vnc 1)))(device (vbd (dev 'ioemu:hda')(uname 'file:/root/foo.img')(mode 'w')))(device (vif (mac '00:16:3e:1b:b1:47')(bridge 'xenbr0')(script 'vif-bridge')(type ioemu))))
diff --git a/tests/sexpr2xml-fv.xml b/tests/sexpr2xml-fv.xml
new file mode 100644 (file)
index 0000000..08a5ee1
--- /dev/null
@@ -0,0 +1,35 @@
+<domain type='xen' id='3'>
+  <name>fvtest</name>
+  <uuid>b5d70dd275cdaca517769660b059d8bc</uuid>
+  <os>
+    <type>hvm</type>
+    <loader>/usr/lib/xen/boot/hvmloader</loader>
+    <boot dev='hd'/>
+  </os>
+  <memory>409600</memory>
+  <vcpu>1</vcpu>
+  <on_poweroff>destroy</on_poweroff>
+  <on_reboot>restart</on_reboot>
+  <on_crash>restart</on_crash>
+  <features>
+    <acpi/>
+  </features>
+  <devices>
+    <emulator>/usr/lib64/xen/bin/qemu-dm</emulator>
+    <disk type='file' device='disk'>
+      <source file='/root/foo.img'/>
+      <target dev='hda'/>
+    </disk>
+    <interface type='bridge'>
+      <source bridge='xenbr0'/>
+      <mac address='00:16:3e:1b:b1:47'/>
+      <script path='vif-bridge'/>
+    </interface>
+    <disk type='file' device='cdrom'>
+      <source file='/root/boot.iso'/>
+      <target dev='hdc'/>
+      <readonly/>
+    </disk>
+    <graphics type='vnc' port='5903'/>
+  </devices>
+</domain>
diff --git a/tests/sexpr2xml-pv.sexpr b/tests/sexpr2xml-pv.sexpr
new file mode 100644 (file)
index 0000000..74c9e01
--- /dev/null
@@ -0,0 +1,2 @@
+(domain (domid 6)(name 'pvtest')(memory 420)(maxmem 420)(vcpus 2)(uuid '596a5d2171f48fb2e068e2386a5c413e')(on_poweroff 'destroy')(on_reboot 'destroy')(on_crash 'destroy')(image (linux (kernel '/var/lib/xen/vmlinuz.2Dn2YT')(ramdisk '/var/lib/xen/initrd.img.0u-Vhq')(args ' method=http://download.fedora.devel.redhat.com/pub/fedora/linux/core/test/5.91/x86_64/os  ')))(device (vbd (dev 'xvda')(uname 'file:/root/some.img')(mode 'w'))))
+
diff --git a/tests/sexpr2xml-pv.xml b/tests/sexpr2xml-pv.xml
new file mode 100644 (file)
index 0000000..66d6fe0
--- /dev/null
@@ -0,0 +1,21 @@
+<domain type='xen' id='6'>
+  <name>pvtest</name>
+  <uuid>596a5d2171f48fb2e068e2386a5c413e</uuid>
+  <os>
+    <type>linux</type>
+    <kernel>/var/lib/xen/vmlinuz.2Dn2YT</kernel>
+    <initrd>/var/lib/xen/initrd.img.0u-Vhq</initrd>
+    <cmdline> method=http://download.fedora.devel.redhat.com/pub/fedora/linux/core/test/5.91/x86_64/os  </cmdline>
+  </os>
+  <memory>430080</memory>
+  <vcpu>2</vcpu>
+  <on_poweroff>destroy</on_poweroff>
+  <on_reboot>destroy</on_reboot>
+  <on_crash>destroy</on_crash>
+  <devices>
+    <disk type='file' device='disk'>
+      <source file='/root/some.img'/>
+      <target dev='xvda'/>
+    </disk>
+  </devices>
+</domain>
diff --git a/tests/sexpr2xmltest.c b/tests/sexpr2xmltest.c
new file mode 100644 (file)
index 0000000..17fb4b9
--- /dev/null
@@ -0,0 +1,71 @@
+
+#include <stdio.h>
+#include <string.h>
+
+#include "xml.h"
+#include "xend_internal.h"
+#include "testutils.h"
+#include "internal.h"
+
+static char *progname;
+
+#define MAX_FILE 4096
+
+static int testCompareFiles(const char *xml, const char *sexpr) {
+  char xmlData[MAX_FILE];
+  char sexprData[MAX_FILE];
+  char *gotxml = NULL;
+  char *xmlPtr = &(xmlData[0]);
+  char *sexprPtr = &(sexprData[0]);
+
+  if (virtTestLoadFile(xml, &xmlPtr, MAX_FILE) < 0)
+    return -1;
+
+  if (virtTestLoadFile(sexpr, &sexprPtr, MAX_FILE) < 0)
+    return -1;
+
+  if (!(gotxml = xend_parse_domain_sexp(NULL, sexprData)))
+    return -1;
+
+  if (getenv("DEBUG_TESTS")) {
+      printf("In  %d '%s'\n", strlen(xmlData), xmlData);
+      printf("Out %d '%s'\n", strlen(gotxml), gotxml);
+  }
+  if (strcmp(xmlData, gotxml))
+    return -1;
+
+  return 0;
+}
+
+static int testComparePV(void *data ATTRIBUTE_UNUSED) {
+  return testCompareFiles("sexpr2xml-pv.xml",
+                         "sexpr2xml-pv.sexpr");
+}
+
+static int testCompareFV(void *data ATTRIBUTE_UNUSED) {
+  return testCompareFiles("sexpr2xml-fv.xml",
+                         "sexpr2xml-fv.sexpr");
+}
+
+int
+main(int argc, char **argv)
+{
+    int ret = 0;
+    
+    progname = argv[0];
+    
+    if (argc > 1) {
+        fprintf(stderr, "Usage: %s\n", progname); 
+        exit(EXIT_FAILURE);
+    }
+    
+    if (virtTestRun("SEXPR-2-XML PV config", 
+                   1, testComparePV, NULL) != 0)
+        ret = -1;
+
+    if (virtTestRun("SEXPR-2-XML FV config", 
+                   1, testCompareFV, NULL) != 0)
+        ret = -1;
+
+    exit(ret==0 ? EXIT_SUCCESS : EXIT_FAILURE);
+}
index ee562d71aa9c7726b5fd1563eb0918b64f38f606..4da6c7dc08f1f8c104634e097052702030a2b3b3 100644 (file)
@@ -13,6 +13,9 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/time.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
 
 #include "testutils.h"
 
@@ -72,3 +75,33 @@ virtTestRun(const char *title, int nloops, int (*body)(void *data), void *data)
                free(ts);
        return ret;  
 }
+
+int virtTestLoadFile(const char *name,
+                    char **buf,
+                    int buflen) {
+    FILE *fp = fopen(name, "r");
+    struct stat st;
+    
+    if (!fp)
+        return -1;
+
+    if (fstat(fileno(fp), &st) < 0) {
+        fclose(fp);
+        return -1;
+    }
+
+    if (st.st_size > (buflen-1)) {
+        fclose(fp);
+        return -1;
+    }
+
+    if (fread(*buf, st.st_size, 1, fp) != 1) {
+        fclose(fp);
+        return -1;
+    }
+    (*buf)[st.st_size] = '\0';
+
+    fclose(fp);
+    return st.st_size;
+}
+
index 8976e82220244796309553b1bfc1b3cebc4fc5f5..4cad281cc41e66f5845f828f237c0290d8085119 100644 (file)
@@ -25,6 +25,9 @@ int   virtTestRun             (const char *title,
                                 int nloops, 
                                 int (*body)(void *data), 
                                 void *data);
+int virtTestLoadFile(const char *name,
+                    char **buf,
+                    int buflen);
 
 #ifdef __cplusplus
 }
diff --git a/tests/xml2sexpr-fv.sexpr b/tests/xml2sexpr-fv.sexpr
new file mode 100644 (file)
index 0000000..62513df
--- /dev/null
@@ -0,0 +1 @@
+(vm (name 'fvtest')(memory 400)(maxmem 400)(vcpus 1)(uuid 'b5d70dd275cdaca517769660b059d8bc')(on_poweroff 'destroy')(on_reboot 'restart')(on_crash 'restart')(image (hvm (kernel '/usr/lib/xen/boot/hvmloader')(device_model '/usr/lib64/xen/bin/qemu-dm')(boot c)(cdrom '/root/boot.iso')(acpi 1)(vnc 1)))(device (vbd (dev 'ioemu:hda')(uname 'file:/root/foo.img')(mode 'w')))(device (vif (mac '00:16:3e:1b:b1:47')(bridge 'xenbr0')(script 'vif-bridge')(type ioemu))))
\ No newline at end of file
diff --git a/tests/xml2sexpr-fv.xml b/tests/xml2sexpr-fv.xml
new file mode 100644 (file)
index 0000000..68aef91
--- /dev/null
@@ -0,0 +1,36 @@
+<domain type='xen'>
+  <name>fvtest</name>
+  <uuid>b5d70dd275cdaca517769660b059d8bc</uuid>
+  <os>
+    <type>hvm</type>
+    <loader>/usr/lib/xen/boot/hvmloader</loader>
+    <boot dev='hd'/>
+  </os>
+  <memory>409600</memory>
+  <vcpu>1</vcpu>
+  <on_poweroff>destroy</on_poweroff>
+  <on_reboot>restart</on_reboot>
+  <on_crash>restart</on_crash>
+  <features>
+    <acpi/>
+  </features>
+  <devices>
+    <emulator>/usr/lib64/xen/bin/qemu-dm</emulator>
+    <interface type='bridge'>
+      <source bridge='xenbr0'/>
+      <mac address='00:16:3e:1b:b1:47'/>
+      <script path='vif-bridge'/>
+    </interface>
+    <disk type='file' device='cdrom'>
+      <source file='/root/boot.iso'/>
+      <target dev='hdc'/>
+      <readonly/>
+    </disk>
+    <disk type='file'>
+      <source file='/root/foo.img'/>
+      <target dev='ioemu:hda'/>
+    </disk>
+    <graphics type='vnc' port='5917'/>
+  </devices>
+</domain>
+
diff --git a/tests/xml2sexpr-pv.sexpr b/tests/xml2sexpr-pv.sexpr
new file mode 100644 (file)
index 0000000..3ab1a83
--- /dev/null
@@ -0,0 +1 @@
+(vm (name 'pvtest')(memory 420)(maxmem 420)(vcpus 2)(uuid '596a5d2171f48fb2e068e2386a5c413e')(on_poweroff 'destroy')(on_reboot 'destroy')(on_crash 'destroy')(image (linux (kernel '/var/lib/xen/vmlinuz.2Dn2YT')(ramdisk '/var/lib/xen/initrd.img.0u-Vhq')(args ' method=http://download.fedora.devel.redhat.com/pub/fedora/linux/core/test/5.91/x86_64/os  ')))(device (vbd (dev 'xvda')(uname 'file:/root/some.img')(mode 'w'))))
\ No newline at end of file
diff --git a/tests/xml2sexpr-pv.xml b/tests/xml2sexpr-pv.xml
new file mode 100644 (file)
index 0000000..938682b
--- /dev/null
@@ -0,0 +1,23 @@
+<domain type='xen' id='15'>
+  <name>pvtest</name>
+  <uuid>596a5d2171f48fb2e068e2386a5c413e</uuid>
+  <os>
+    <type>linux</type>
+    <kernel>/var/lib/xen/vmlinuz.2Dn2YT</kernel>
+    <initrd>/var/lib/xen/initrd.img.0u-Vhq</initrd>
+    <cmdline> method=http://download.fedora.devel.redhat.com/pub/fedora/linux/core/test/5.91/x86_64/os  </cmdline>
+  </os>
+  <memory>430080</memory>
+  <vcpu>2</vcpu>
+  <on_poweroff>destroy</on_poweroff>
+  <on_reboot>destroy</on_reboot>
+  <on_crash>destroy</on_crash>
+  <devices>
+    <disk type='file' device='disk'>
+      <source file='/root/some.img'/>
+      <target dev='xvda'/>
+    </disk>
+    <console tty='/dev/pts/4'/>
+  </devices>
+</domain>
+
diff --git a/tests/xml2sexprtest.c b/tests/xml2sexprtest.c
new file mode 100644 (file)
index 0000000..335145c
--- /dev/null
@@ -0,0 +1,76 @@
+
+#include <stdio.h>
+#include <string.h>
+
+#include "xml.h"
+#include "testutils.h"
+#include "internal.h"
+
+static char *progname;
+
+#define MAX_FILE 4096
+
+static int testCompareFiles(const char *xml, const char *sexpr, const char *name) {
+  char xmlData[MAX_FILE];
+  char sexprData[MAX_FILE];
+  char *gotname = NULL;
+  char *gotsexpr = NULL;
+  char *xmlPtr = &(xmlData[0]);
+  char *sexprPtr = &(sexprData[0]);
+
+  if (virtTestLoadFile(xml, &xmlPtr, MAX_FILE) < 0)
+    return -1;
+
+  if (virtTestLoadFile(sexpr, &sexprPtr, MAX_FILE) < 0)
+    return -1;
+
+  if (!(gotsexpr = virDomainParseXMLDesc(xmlData, &gotname))) 
+    return -1;
+
+  if (getenv("DEBUG_TESTS")) {
+      printf("In  %d '%s'\n", strlen(sexprData), sexprData);
+      printf("Out %d '%s'\n", strlen(gotsexpr), gotsexpr);
+  }
+  if (strcmp(sexprData, gotsexpr))
+    return -1;
+
+  if (strcmp(name, gotname))
+    return -1;
+
+  return 0;
+}
+
+static int testComparePV(void *data ATTRIBUTE_UNUSED) {
+  return testCompareFiles("xml2sexpr-pv.xml",
+                         "xml2sexpr-pv.sexpr",
+                         "pvtest");
+}
+
+static int testCompareFV(void *data ATTRIBUTE_UNUSED) {
+  return testCompareFiles("xml2sexpr-fv.xml",
+                         "xml2sexpr-fv.sexpr",
+                         "fvtest");
+}
+
+int
+main(int argc, char **argv)
+{
+    int ret = 0;
+    
+    progname = argv[0];
+    
+    if (argc > 1) {
+        fprintf(stderr, "Usage: %s\n", progname); 
+        exit(EXIT_FAILURE);
+    }
+    
+    if (virtTestRun("XML-2-SEXPR PV config", 
+                   1, testComparePV, NULL) != 0)
+        ret = -1;
+
+    if (virtTestRun("XML-2-SEXPR FV config", 
+                   1, testCompareFV, NULL) != 0)
+        ret = -1;
+
+    exit(ret==0 ? EXIT_SUCCESS : EXIT_FAILURE);
+}