diff options
12 files changed, 113 insertions, 55 deletions
@@ -165,6 +165,18 @@ <scope>test</scope> </dependency> <dependency> + <groupId>org.xmlunit</groupId> + <artifactId>xmlunit-core</artifactId> + <version>[2.0,3.0)</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.xmlunit</groupId> + <artifactId>xmlunit-assertj</artifactId> + <version>[2.0,3.0)</version> + <scope>test</scope> + </dependency> + <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>[2.17,3.0)</version> diff --git a/src/main/java/org/openslx/libvirt/xml/LibvirtXmlDocument.java b/src/main/java/org/openslx/libvirt/xml/LibvirtXmlDocument.java index f24ba4f..97180e3 100644 --- a/src/main/java/org/openslx/libvirt/xml/LibvirtXmlDocument.java +++ b/src/main/java/org/openslx/libvirt/xml/LibvirtXmlDocument.java @@ -312,7 +312,7 @@ public abstract class LibvirtXmlDocument implements LibvirtXmlSerializable, Libv DOMSource source = new DOMSource( this.xmlDocument ); StreamResult xmlString = new StreamResult( xmlWriter ); this.xmlTransformer.transform( source, xmlString ); - xml = xmlWriter.toString() + System.lineSeparator(); + xml = xmlWriter.toString(); xmlWriter.close(); } catch ( TransformerException | IOException e ) { throw new LibvirtXmlSerializationException( e.getLocalizedMessage() ); @@ -337,7 +337,6 @@ public abstract class LibvirtXmlDocument implements LibvirtXmlSerializable, Libv DOMSource source = new DOMSource( this.xmlDocument ); StreamResult xmlStream = new StreamResult( xmlWriter ); this.xmlTransformer.transform( source, xmlStream ); - xmlWriter.append( System.lineSeparator() ); xmlWriter.close(); } catch ( TransformerException | IOException e ) { throw new LibvirtXmlSerializationException( e.getLocalizedMessage() ); diff --git a/src/main/java/org/openslx/virtualization/configuration/VirtualizationConfigurationQemu.java b/src/main/java/org/openslx/virtualization/configuration/VirtualizationConfigurationQemu.java index ced9160..7082e27 100644 --- a/src/main/java/org/openslx/virtualization/configuration/VirtualizationConfigurationQemu.java +++ b/src/main/java/org/openslx/virtualization/configuration/VirtualizationConfigurationQemu.java @@ -746,7 +746,7 @@ public class VirtualizationConfigurationQemu extends VirtualizationConfiguration return null; } else { // append newline at the end of the XML content to match the structure of an original Libvirt XML file - configuration += System.lineSeparator(); + //configuration += LibvirtXmlDocument.LINE_SEPARATOR; return configuration.getBytes( StandardCharsets.UTF_8 ); } } diff --git a/src/main/resources/libvirt/xsl/xml-output-transformation.xsl b/src/main/resources/libvirt/xsl/xml-output-transformation.xsl index febed54..8cd615d 100644 --- a/src/main/resources/libvirt/xsl/xml-output-transformation.xsl +++ b/src/main/resources/libvirt/xsl/xml-output-transformation.xsl @@ -2,11 +2,17 @@ xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xalan="http://xml.apache.org/xalan"> <xsl:output method="xml" omit-xml-declaration="yes" - encoding="UTF-8" indent="yes" xalan:indent-amount="2" /> + encoding="UTF-8" indent="yes" xalan:indent-amount="2" + xalan:line-separator=" " /> <xsl:strip-space elements="*" /> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()" /> </xsl:copy> </xsl:template> + <xsl:template match="/"> + <xsl:apply-templates /> + <xsl:text> </xsl:text> + <xsl:text> </xsl:text> + </xsl:template> </xsl:stylesheet> diff --git a/src/test/java/org/openslx/libvirt/xml/LibvirtXmlDocumentTest.java b/src/test/java/org/openslx/libvirt/xml/LibvirtXmlDocumentTest.java index 02c8b19..15d203c 100644 --- a/src/test/java/org/openslx/libvirt/xml/LibvirtXmlDocumentTest.java +++ b/src/test/java/org/openslx/libvirt/xml/LibvirtXmlDocumentTest.java @@ -7,9 +7,13 @@ import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.fail; +import java.io.BufferedReader; import java.io.File; +import java.io.FileReader; import java.io.IOException; import java.io.InputStream; +import java.io.Reader; +import java.io.StringReader; import java.nio.charset.StandardCharsets; import org.apache.commons.io.FileUtils; @@ -78,6 +82,22 @@ public class LibvirtXmlDocumentTest return document; } + private static long countLines( Reader input ) throws IOException + { + final BufferedReader bfrContent = new BufferedReader( input ); + return bfrContent.lines().count(); + } + + public static long countLinesFromString( String input ) throws IOException + { + return LibvirtXmlDocumentTest.countLines( new StringReader( input ) ); + } + + public static long countLinesFromFile( File input ) throws IOException + { + return LibvirtXmlDocumentTest.countLines( new FileReader( input ) ); + } + @Test @DisplayName( "Read libvirt XML file to String" ) public void testReadXmlFileToString() throws LibvirtXmlSerializationException, IOException @@ -86,12 +106,11 @@ public class LibvirtXmlDocumentTest File originalXmlFile = LibvirtXmlTestResources.getLibvirtXmlFile( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); final String readXmlContent = vm.toXml(); - final String originalXmlContent = FileUtils.readFileToString( originalXmlFile, StandardCharsets.UTF_8 ); assertNotNull( readXmlContent ); - final int lengthReadXmlContent = readXmlContent.split( System.lineSeparator() ).length; - final int lengthOriginalXmlContent = originalXmlContent.split( System.lineSeparator() ).length; + final long lengthReadXmlContent = LibvirtXmlDocumentTest.countLinesFromString( readXmlContent ); + final long lengthOriginalXmlContent = LibvirtXmlDocumentTest.countLinesFromFile( originalXmlFile ); assertEquals( lengthOriginalXmlContent, lengthReadXmlContent ); } @@ -111,8 +130,8 @@ public class LibvirtXmlDocumentTest assertNotNull( readXmlContent ); - final int lengthReadXmlContent = readXmlContent.split( System.lineSeparator() ).length; - final int lengthOriginalXmlContent = originalXmlContent.split( System.lineSeparator() ).length; + final long lengthReadXmlContent = LibvirtXmlDocumentTest.countLinesFromString( readXmlContent ); + final long lengthOriginalXmlContent = LibvirtXmlDocumentTest.countLinesFromString( originalXmlContent ); assertEquals( lengthOriginalXmlContent, lengthReadXmlContent ); } diff --git a/src/test/java/org/openslx/virtualization/configuration/VirtualizationConfigurationQemuTest.java b/src/test/java/org/openslx/virtualization/configuration/VirtualizationConfigurationQemuTest.java index 4b07adc..f8347f4 100644 --- a/src/test/java/org/openslx/virtualization/configuration/VirtualizationConfigurationQemuTest.java +++ b/src/test/java/org/openslx/virtualization/configuration/VirtualizationConfigurationQemuTest.java @@ -31,6 +31,7 @@ import org.openslx.libvirt.domain.device.DiskCdrom; import org.openslx.libvirt.domain.device.DiskFloppy; import org.openslx.libvirt.domain.device.DiskStorage; import org.openslx.libvirt.domain.device.Interface; +import org.openslx.libvirt.xml.LibvirtXmlDocumentTest; import org.openslx.libvirt.xml.LibvirtXmlTestResources; import org.openslx.virtualization.Version; import org.openslx.virtualization.configuration.VirtualizationConfiguration.ConfigurableOptionGroup; @@ -179,8 +180,8 @@ public class VirtualizationConfigurationQemuTest assertNotNull( unfilteredXmlConfig ); - final int lengthUnfilteredXmlConfig = unfilteredXmlConfig.split( System.lineSeparator() ).length; - final int lengthOriginalXmlConfig = originalXmlConfig.split( System.lineSeparator() ).length; + final long lengthUnfilteredXmlConfig = LibvirtXmlDocumentTest.countLinesFromString( unfilteredXmlConfig ); + final long lengthOriginalXmlConfig = LibvirtXmlDocumentTest.countLinesFromString( originalXmlConfig ); assertEquals( lengthOriginalXmlConfig, lengthUnfilteredXmlConfig ); diff --git a/src/test/java/org/openslx/virtualization/configuration/logic/ConfigurationLogicDozModClientToDozModServerTest.java b/src/test/java/org/openslx/virtualization/configuration/logic/ConfigurationLogicDozModClientToDozModServerTest.java index 0c164c6..09de9f5 100644 --- a/src/test/java/org/openslx/virtualization/configuration/logic/ConfigurationLogicDozModClientToDozModServerTest.java +++ b/src/test/java/org/openslx/virtualization/configuration/logic/ConfigurationLogicDozModClientToDozModServerTest.java @@ -41,7 +41,7 @@ public class ConfigurationLogicDozModClientToDozModServerTest final String transformedConfig = config.getConfigurationAsString(); final String expectedTransformedConfig = ConfigurationLogicTestUtils.readFileToString( expectedConfig ); - assertTrue( ConfigurationLogicTestUtils.isContentEqual( expectedTransformedConfig, transformedConfig ) ); + ConfigurationLogicTestUtils.assertXmlEqual( expectedTransformedConfig, transformedConfig ); assertDoesNotThrow( () -> config.validate() ); } @@ -62,7 +62,7 @@ public class ConfigurationLogicDozModClientToDozModServerTest final String transformedConfig = config.getConfigurationAsString(); final String expectedTransformedConfig = ConfigurationLogicTestUtils.readFileToString( expectedConfig ); - assertTrue( ConfigurationLogicTestUtils.isContentEqual( expectedTransformedConfig, transformedConfig ) ); + ConfigurationLogicTestUtils.assertXmlEqual( expectedTransformedConfig, transformedConfig ); // do not validate the VirtualBox configuration afterwards, since the inserted // place holders do not match valid primitive values from the XML schema @@ -87,7 +87,7 @@ public class ConfigurationLogicDozModClientToDozModServerTest final String transformedConfig = config.getConfigurationAsString(); final String expectedTransformedConfig = ConfigurationLogicTestUtils.readFileToString( expectedConfig ); - assertTrue( ConfigurationLogicTestUtils.isContentEqual( expectedTransformedConfig, transformedConfig ) ); + ConfigurationLogicTestUtils.assertVmxVmwareEqual( expectedTransformedConfig, transformedConfig ); assertDoesNotThrow( () -> config.validate() ); } } diff --git a/src/test/java/org/openslx/virtualization/configuration/logic/ConfigurationLogicDozModServerToDozModClientTest.java b/src/test/java/org/openslx/virtualization/configuration/logic/ConfigurationLogicDozModServerToDozModClientTest.java index 4e49eab..772f2f4 100644 --- a/src/test/java/org/openslx/virtualization/configuration/logic/ConfigurationLogicDozModServerToDozModClientTest.java +++ b/src/test/java/org/openslx/virtualization/configuration/logic/ConfigurationLogicDozModServerToDozModClientTest.java @@ -56,7 +56,7 @@ public class ConfigurationLogicDozModServerToDozModClientTest final String transformedConfig = config.getConfigurationAsString(); final String expectedTransformedConfig = ConfigurationLogicTestUtils.readFileToString( expectedConfig ); - assertTrue( ConfigurationLogicTestUtils.isLibvirtContentEqual( expectedTransformedConfig, transformedConfig ) ); + ConfigurationLogicTestUtils.assertXmlLibvirtEqual( expectedTransformedConfig, transformedConfig ); assertDoesNotThrow( () -> config.validate() ); } @@ -77,8 +77,7 @@ public class ConfigurationLogicDozModServerToDozModClientTest final String transformedConfig = config.getConfigurationAsString(); final String expectedTransformedConfig = ConfigurationLogicTestUtils.readFileToString( expectedConfig ); - assertTrue( - ConfigurationLogicTestUtils.isVirtualBoxContentEqual( expectedTransformedConfig, transformedConfig ) ); + ConfigurationLogicTestUtils.assertXmlVirtualBoxEqual( expectedTransformedConfig, transformedConfig ); // do not validate the VirtualBox configuration afterwards, since the inserted network configuration // leads to an invalid DOM although the created output after the transformation is as expected @@ -103,7 +102,7 @@ public class ConfigurationLogicDozModServerToDozModClientTest final String transformedConfig = config.getConfigurationAsString(); final String expectedTransformedConfig = ConfigurationLogicTestUtils.readFileToString( expectedConfig ); - assertTrue( ConfigurationLogicTestUtils.isContentEqual( expectedTransformedConfig, transformedConfig ) ); + ConfigurationLogicTestUtils.assertVmxVmwareEqual( expectedTransformedConfig, transformedConfig ); assertDoesNotThrow( () -> config.validate() ); } } diff --git a/src/test/java/org/openslx/virtualization/configuration/logic/ConfigurationLogicDozModServerToStatelessClientTest.java b/src/test/java/org/openslx/virtualization/configuration/logic/ConfigurationLogicDozModServerToStatelessClientTest.java index 7380277..2271e9c 100644 --- a/src/test/java/org/openslx/virtualization/configuration/logic/ConfigurationLogicDozModServerToStatelessClientTest.java +++ b/src/test/java/org/openslx/virtualization/configuration/logic/ConfigurationLogicDozModServerToStatelessClientTest.java @@ -50,7 +50,7 @@ public class ConfigurationLogicDozModServerToStatelessClientTest final String transformedConfig = config.getConfigurationAsString(); final String expectedTransformedConfig = ConfigurationLogicTestUtils.readFileToString( expectedConfig ); - assertTrue( ConfigurationLogicTestUtils.isContentEqual( expectedTransformedConfig, transformedConfig ) ); + ConfigurationLogicTestUtils.assertXmlEqual( expectedTransformedConfig, transformedConfig ); assertDoesNotThrow( () -> config.validate() ); } @@ -71,8 +71,7 @@ public class ConfigurationLogicDozModServerToStatelessClientTest final String transformedConfig = config.getConfigurationAsString(); final String expectedTransformedConfig = ConfigurationLogicTestUtils.readFileToString( expectedConfig ); - assertTrue( - ConfigurationLogicTestUtils.isVirtualBoxContentEqual( expectedTransformedConfig, transformedConfig ) ); + ConfigurationLogicTestUtils.assertXmlVirtualBoxEqual( expectedTransformedConfig, transformedConfig ); // do not validate the VirtualBox configuration afterwards, since the inserted // place holders do not match valid primitive values from the XML schema @@ -97,7 +96,7 @@ public class ConfigurationLogicDozModServerToStatelessClientTest final String transformedConfig = config.getConfigurationAsString(); final String expectedTransformedConfig = ConfigurationLogicTestUtils.readFileToString( expectedConfig ); - assertTrue( ConfigurationLogicTestUtils.isContentEqual( expectedTransformedConfig, transformedConfig ) ); + ConfigurationLogicTestUtils.assertVmxVmwareEqual( expectedTransformedConfig, transformedConfig ); assertDoesNotThrow( () -> config.validate() ); } } diff --git a/src/test/java/org/openslx/virtualization/configuration/logic/ConfigurationLogicTestUtils.java b/src/test/java/org/openslx/virtualization/configuration/logic/ConfigurationLogicTestUtils.java index 1159d74..4f82097 100644 --- a/src/test/java/org/openslx/virtualization/configuration/logic/ConfigurationLogicTestUtils.java +++ b/src/test/java/org/openslx/virtualization/configuration/logic/ConfigurationLogicTestUtils.java @@ -1,5 +1,6 @@ package org.openslx.virtualization.configuration.logic; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.fail; import java.io.BufferedReader; @@ -7,6 +8,7 @@ import java.io.File; import java.io.IOException; import java.io.StringReader; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -17,6 +19,7 @@ import java.util.stream.Collectors; import org.apache.commons.io.FileUtils; import org.openslx.bwlp.thrift.iface.OperatingSystem; import org.openslx.virtualization.configuration.VirtualizationConfiguration; +import org.xmlunit.assertj.XmlAssert; public class ConfigurationLogicTestUtils { @@ -68,23 +71,9 @@ public class ConfigurationLogicTestUtils return content; } - public static boolean isContentEqual( String expectedContent, String actualContent ) + public static void assertXmlEqual( String expectedXml, String actualXml ) throws AssertionError { - final BufferedReader bfrContent1 = new BufferedReader( new StringReader( expectedContent ) ); - final BufferedReader bfrContent2 = new BufferedReader( new StringReader( actualContent ) ); - final List<String> linesContent1 = bfrContent1.lines().collect( Collectors.toList() ); - final List<String> linesContent2 = bfrContent2.lines().collect( Collectors.toList() ); - - Collections.sort( linesContent1 ); - Collections.sort( linesContent2 ); - - boolean ret = linesContent1.equals( linesContent2 ); - if ( !ret ) { - System.out.println( "----- UNEXPECTED OUTPUT: -------------\n" ); - System.out.println( actualContent ); - System.out.println( "--------------------------------------\n" ); - } - return ret; + XmlAssert.assertThat( expectedXml ).and( actualXml ).ignoreComments().areIdentical(); } public static String removeSourceFilePaths( String content ) @@ -105,21 +94,55 @@ public class ConfigurationLogicTestUtils return matcherUuidContent.replaceAll( "" ); } - public static boolean isLibvirtContentEqual( String content1, String content2 ) + public static void assertXmlLibvirtEqual( String expectedXml, String actualXml ) throws AssertionError { // replace all source file paths with the empty String - final String filteredContent1 = ConfigurationLogicTestUtils.removeSourceFilePaths( content1 ); - final String filteredContent2 = ConfigurationLogicTestUtils.removeSourceFilePaths( content2 ); + final String filteredXml1 = ConfigurationLogicTestUtils.removeSourceFilePaths( expectedXml ); + final String filteredXml2 = ConfigurationLogicTestUtils.removeSourceFilePaths( actualXml ); - return ConfigurationLogicTestUtils.isContentEqual( filteredContent1, filteredContent2 ); + ConfigurationLogicTestUtils.assertXmlEqual( filteredXml1, filteredXml2 ); } - public static boolean isVirtualBoxContentEqual( String content1, String content2 ) + public static void assertXmlVirtualBoxEqual( String expectedXml, String actualXml ) throws AssertionError { // replace all UUIDs with the empty String - final String filteredContent1 = ConfigurationLogicTestUtils.removeUuid( content1 ); - final String filteredContent2 = ConfigurationLogicTestUtils.removeUuid( content2 ); + final String filteredXml1 = ConfigurationLogicTestUtils.removeUuid( expectedXml ); + final String filteredXml2 = ConfigurationLogicTestUtils.removeUuid( actualXml ); + + ConfigurationLogicTestUtils.assertXmlEqual( filteredXml1, filteredXml2 ); + } + + public static void assertVmxVmwareEqual( String expectedVmx, String actualVmx ) throws AssertionError + { + final BufferedReader bfrVmx1 = new BufferedReader( new StringReader( expectedVmx ) ); + final BufferedReader bfrVmx2 = new BufferedReader( new StringReader( actualVmx ) ); + final List<String> linesVmx1 = bfrVmx1.lines().collect( Collectors.toList() ); + final List<String> linesVmx2 = bfrVmx2.lines().collect( Collectors.toList() ); + + // check output size first + if ( linesVmx1.size() != linesVmx2.size() ) { + // create list of items that are expected but missing in the actual output + final List<String> missingItems; + final String missingItemsDesc; + + if ( linesVmx1.size() > linesVmx2.size() ) { + missingItemsDesc = "The following items are expected but missing in the actual output"; + missingItems = new ArrayList<String>( linesVmx1 ); + missingItems.removeAll( linesVmx2 ); + } else { + missingItemsDesc = "The following items are not expected but occuring in the actual output"; + missingItems = new ArrayList<String>( linesVmx2 ); + missingItems.removeAll( linesVmx1 ); + } + + throw new AssertionError( String.format( + "VMX output size is not satisfied: Expected %d lines, but output has %d lines!\n" + + "%s:\n" + + "%s", + linesVmx1.size(), linesVmx2.size(), missingItemsDesc, missingItems ) ); + } - return ConfigurationLogicTestUtils.isContentEqual( filteredContent1, filteredContent2 ); + // check the content of the output line by line + assertEquals( linesVmx1, linesVmx2 ); } } diff --git a/src/test/resources/libvirt/xml/qemu-kvm_default-ubuntu-20-04-vm_transform-editable.xml b/src/test/resources/libvirt/xml/qemu-kvm_default-ubuntu-20-04-vm_transform-editable.xml index bf4de75..ed2210b 100644 --- a/src/test/resources/libvirt/xml/qemu-kvm_default-ubuntu-20-04-vm_transform-editable.xml +++ b/src/test/resources/libvirt/xml/qemu-kvm_default-ubuntu-20-04-vm_transform-editable.xml @@ -54,18 +54,18 @@ <address bus="0" controller="0" target="0" type="drive" unit="0"/> <readonly/> </disk> - <controller index="0" model="ich9-uhci1" type="usb"> + <controller index="0" model="ich9-ehci1" type="usb"> <address bus="0x00" domain="0x0000" function="0x7" slot="0x1d" type="pci"/> </controller> - <controller index="0" model="ich9-uhci2" type="usb"> + <controller index="0" model="ich9-uhci1" type="usb"> <master startport="0"/> <address bus="0x00" domain="0x0000" function="0x0" multifunction="on" slot="0x1d" type="pci"/> </controller> - <controller index="0" model="ich9-uhci3" type="usb"> + <controller index="0" model="ich9-uhci2" type="usb"> <master startport="2"/> <address bus="0x00" domain="0x0000" function="0x1" slot="0x1d" type="pci"/> </controller> - <controller index="0" model="ich9-ehci1" type="usb"> + <controller index="0" model="ich9-uhci3" type="usb"> <master startport="4"/> <address bus="0x00" domain="0x0000" function="0x2" slot="0x1d" type="pci"/> </controller> diff --git a/src/test/resources/virtualbox/xml/virtualbox_default-ubuntu_transform-editable.vbox b/src/test/resources/virtualbox/xml/virtualbox_default-ubuntu_transform-editable.vbox index 9bfffcd..9c89f4b 100644 --- a/src/test/resources/virtualbox/xml/virtualbox_default-ubuntu_transform-editable.vbox +++ b/src/test/resources/virtualbox/xml/virtualbox_default-ubuntu_transform-editable.vbox @@ -18,6 +18,11 @@ <IOAPIC enabled="true"/> <SmbiosUuidLittleEndian enabled="true"/> </BIOS> + <USB> + <Controllers> + <Controller name="OHCI" type="OHCI"/> + </Controllers> + </USB> <Network> <Adapter MACAddress="080027B86D12" enabled="true" slot="0" type="82540EM"> <NAT/> @@ -26,11 +31,6 @@ <AudioAdapter codec="AD1980" driver="Pulse" enabled="true" enabledIn="false"/> <RTC localOrUTC="UTC"/> <Clipboard/> - <USB> - <Controllers> - <Controller name="OHCI" type="OHCI"/> - </Controllers> - </USB> </Hardware> <StorageControllers> <StorageController Bootable="true" PortCount="2" name="IDE" type="PIIX4" useHostIOCache="true"/> |