]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
conf: Don't output <cpu> tag if it contains no information.
authorAndrea Bolognani <abologna@redhat.com>
Fri, 10 Apr 2015 13:09:59 +0000 (15:09 +0200)
committerErik Skultety <eskultet@redhat.com>
Mon, 13 Apr 2015 07:27:26 +0000 (09:27 +0200)
The tag is already marked as optional in the schema, so no changes
are needed there.

RHBZ: https://bugzilla.redhat.com/show_bug.cgi?id=1202606

src/conf/cpu_conf.c
tests/qemuxml2argvdata/qemuxml2argv-cpu-empty.xml [new file with mode: 0644]
tests/qemuxml2xmloutdata/qemuxml2xmlout-cpu-empty.xml [new file with mode: 0644]
tests/qemuxml2xmltest.c

index cd3882df2cc6fa6f6b4f0f284b59366c2bef243d..e959ecca0366e1df951e6b1221d74ea9eca410f2 100644 (file)
@@ -435,13 +435,14 @@ virCPUDefFormatBufFull(virBufferPtr buf,
                        bool updateCPU)
 {
     int ret = -1;
+    virBuffer attributeBuf = VIR_BUFFER_INITIALIZER;
     virBuffer childrenBuf = VIR_BUFFER_INITIALIZER;
     int indent = virBufferGetIndent(buf, false);
 
     if (!def)
         return 0;
 
-    virBufferAddLit(buf, "<cpu");
+    /* Format attributes */
     if (def->type == VIR_CPU_TYPE_GUEST) {
         const char *tmp;
 
@@ -451,7 +452,7 @@ virCPUDefFormatBufFull(virBufferPtr buf,
                                _("Unexpected CPU mode %d"), def->mode);
                 goto cleanup;
             }
-            virBufferAsprintf(buf, " mode='%s'", tmp);
+            virBufferAsprintf(&attributeBuf, " mode='%s'", tmp);
         }
 
         if (def->model &&
@@ -463,10 +464,11 @@ virCPUDefFormatBufFull(virBufferPtr buf,
                                def->match);
                 goto cleanup;
             }
-            virBufferAsprintf(buf, " match='%s'", tmp);
+            virBufferAsprintf(&attributeBuf, " match='%s'", tmp);
         }
     }
 
+    /* Format children */
     virBufferAdjustIndent(&childrenBuf, indent + 2);
     if (def->arch)
         virBufferAsprintf(&childrenBuf, "<arch>%s</arch>\n",
@@ -477,16 +479,25 @@ virCPUDefFormatBufFull(virBufferPtr buf,
     if (virDomainNumaDefCPUFormat(&childrenBuf, numa) < 0)
         goto cleanup;
 
-    if (virBufferUse(&childrenBuf)) {
-        virBufferAddLit(buf, ">\n");
-        virBufferAddBuffer(buf, &childrenBuf);
-        virBufferAddLit(buf, "</cpu>\n");
-    } else {
-        virBufferAddLit(buf, "/>\n");
+    /* Put it all together */
+    if (virBufferUse(&attributeBuf) || virBufferUse(&childrenBuf)) {
+        virBufferAddLit(buf, "<cpu");
+
+        if (virBufferUse(&attributeBuf))
+            virBufferAddBuffer(buf, &attributeBuf);
+
+        if (virBufferUse(&childrenBuf)) {
+            virBufferAddLit(buf, ">\n");
+            virBufferAddBuffer(buf, &childrenBuf);
+            virBufferAddLit(buf, "</cpu>\n");
+        } else {
+            virBufferAddLit(buf, "/>\n");
+        }
     }
 
     ret = 0;
  cleanup:
+    virBufferFreeAndReset(&attributeBuf);
     virBufferFreeAndReset(&childrenBuf);
     return ret;
 }
diff --git a/tests/qemuxml2argvdata/qemuxml2argv-cpu-empty.xml b/tests/qemuxml2argvdata/qemuxml2argv-cpu-empty.xml
new file mode 100644 (file)
index 0000000..2a79826
--- /dev/null
@@ -0,0 +1,23 @@
+<domain type='kvm'>
+  <name>cpu-empty</name>
+  <uuid>1aed4c39-ad6e-4a78-9264-4ce996290d17</uuid>
+  <memory unit='KiB'>4000768</memory>
+  <currentMemory unit='KiB'>1048576</currentMemory>
+  <vcpu placement='static'>1</vcpu>
+  <os>
+    <type arch='x86_64' machine='pc'>hvm</type>
+    <boot dev='hd'/>
+  </os>
+  <cpu>
+  </cpu>
+  <clock offset='utc'/>
+  <on_poweroff>destroy</on_poweroff>
+  <on_reboot>restart</on_reboot>
+  <on_crash>destroy</on_crash>
+  <devices>
+    <emulator>/usr/bin/qemu-kvm</emulator>
+    <controller type='usb' index='0'/>
+    <controller type='pci' index='0' model='pci-root'/>
+    <memballoon model='virtio'/>
+  </devices>
+</domain>
diff --git a/tests/qemuxml2xmloutdata/qemuxml2xmlout-cpu-empty.xml b/tests/qemuxml2xmloutdata/qemuxml2xmlout-cpu-empty.xml
new file mode 100644 (file)
index 0000000..e678607
--- /dev/null
@@ -0,0 +1,21 @@
+<domain type='kvm'>
+  <name>cpu-empty</name>
+  <uuid>1aed4c39-ad6e-4a78-9264-4ce996290d17</uuid>
+  <memory unit='KiB'>4000768</memory>
+  <currentMemory unit='KiB'>1048576</currentMemory>
+  <vcpu placement='static'>1</vcpu>
+  <os>
+    <type arch='x86_64' machine='pc'>hvm</type>
+    <boot dev='hd'/>
+  </os>
+  <clock offset='utc'/>
+  <on_poweroff>destroy</on_poweroff>
+  <on_reboot>restart</on_reboot>
+  <on_crash>destroy</on_crash>
+  <devices>
+    <emulator>/usr/bin/qemu-kvm</emulator>
+    <controller type='usb' index='0'/>
+    <controller type='pci' index='0' model='pci-root'/>
+    <memballoon model='virtio'/>
+  </devices>
+</domain>
index 817e4083379d53e80b422d80e1a9bfe972131e77..cd0b280f92f466ddfe499c799f433f5c0cd1e26d 100644 (file)
@@ -361,6 +361,7 @@ mymain(void)
 
     DO_TEST("clock-utc");
     DO_TEST("clock-localtime");
+    DO_TEST_DIFFERENT("cpu-empty");
     DO_TEST("cpu-kvmclock");
     DO_TEST("cpu-host-kvmclock");
     DO_TEST("cpu-host-passthrough-features");