From 10567e0145ce651c327267d8f4ea31d82bc7e239 Mon Sep 17 00:00:00 2001 From: Manuel Bentele Date: Fri, 29 Jan 2021 12:22:05 +0100 Subject: Add base classes and utilites to represent Libvirt XML documents --- .../libvirt/xml/LibvirtXmlDocumentTest.java | 262 +++++++++++++++++++++ .../libvirt/xml/LibvirtXmlTestResources.java | 29 +++ 2 files changed, 291 insertions(+) create mode 100644 src/test/java/org/openslx/libvirt/xml/LibvirtXmlDocumentTest.java create mode 100644 src/test/java/org/openslx/libvirt/xml/LibvirtXmlTestResources.java (limited to 'src/test/java/org/openslx/libvirt/xml') diff --git a/src/test/java/org/openslx/libvirt/xml/LibvirtXmlDocumentTest.java b/src/test/java/org/openslx/libvirt/xml/LibvirtXmlDocumentTest.java new file mode 100644 index 0000000..1b6e5a5 --- /dev/null +++ b/src/test/java/org/openslx/libvirt/xml/LibvirtXmlDocumentTest.java @@ -0,0 +1,262 @@ +package org.openslx.libvirt.xml; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +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.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; + +import javax.xml.transform.Source; +import javax.xml.transform.stream.StreamSource; + +import org.apache.commons.io.FileUtils; +import org.apache.log4j.Level; +import org.apache.log4j.LogManager; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.function.Executable; + +class LibvirtXmlDocumentStub extends LibvirtXmlDocument +{ + public LibvirtXmlDocumentStub( File xml ) + throws LibvirtXmlDocumentException, LibvirtXmlSerializationException, LibvirtXmlValidationException + { + super( xml ); + } + + public LibvirtXmlDocumentStub( File xml, Source rngSchema ) + throws LibvirtXmlDocumentException, LibvirtXmlSerializationException, LibvirtXmlValidationException + { + super( xml, rngSchema ); + } +} + +public class LibvirtXmlDocumentTest +{ + private static final String EMPTY = new String(); + + @BeforeAll + public static void setUp() + { + // disable logging with log4j + LogManager.getRootLogger().setLevel( Level.OFF ); + } + + private LibvirtXmlDocument newLibvirtXmlDocumentInstance( String xmlFileName ) + { + LibvirtXmlDocument document = null; + + try { + File xmlFile = LibvirtXmlTestResources.getLibvirtXmlFile( xmlFileName ); + document = new LibvirtXmlDocumentStub( xmlFile ); + } catch ( LibvirtXmlDocumentException | LibvirtXmlSerializationException | LibvirtXmlValidationException e ) { + String errorMsg = new String( "Cannot prepare requested Libvirt XML file from the resources folder" ); + fail( errorMsg ); + } + + return document; + } + + private LibvirtXmlDocument newLibvirtXmlDocumentValidationInstance( String xmlFileName, String rngSchemaFileName ) + throws LibvirtXmlValidationException + { + LibvirtXmlDocument document = null; + + try { + File xmlFile = LibvirtXmlTestResources.getLibvirtXmlFile( xmlFileName ); + Source rngSchemaSource = new StreamSource( LibvirtXmlResources.getLibvirtRng( rngSchemaFileName ) ); + document = new LibvirtXmlDocumentStub( xmlFile, rngSchemaSource ); + } catch ( LibvirtXmlDocumentException | LibvirtXmlSerializationException e ) { + String errorMsg = new String( "Cannot prepare requested Libvirt XML file from the resources folder" ); + fail( errorMsg ); + } + + return document; + } + + @Test + @DisplayName( "Read libvirt XML file to String" ) + public void testReadXmlFileToString() throws LibvirtXmlSerializationException, IOException + { + LibvirtXmlDocument vm = this.newLibvirtXmlDocumentInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + 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; + + assertEquals( lengthOriginalXmlContent, lengthReadXmlContent ); + } + + @Test + @DisplayName( "Read libvirt XML file to file" ) + public void testReadXmlFileToFile() throws LibvirtXmlSerializationException, IOException + { + LibvirtXmlDocument vm = this.newLibvirtXmlDocumentInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + File originalXmlFile = LibvirtXmlTestResources.getLibvirtXmlFile( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + File readXmlFile = LibvirtXmlTestResources.createLibvirtXmlTempFile(); + + vm.toXml( readXmlFile ); + + final String readXmlContent = FileUtils.readFileToString( readXmlFile, StandardCharsets.UTF_8 ); + 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; + + assertEquals( lengthOriginalXmlContent, lengthReadXmlContent ); + } + + @Test + @DisplayName( "Validate correct libvirt XML file" ) + public void testValidateCorrectXmlFile() + { + Executable validateXmlDocument = () -> { + this.newLibvirtXmlDocumentValidationInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml", "domain.rng" ); + }; + + assertDoesNotThrow( validateXmlDocument ); + } + + @Test + @DisplayName( "Validate incorrect libvirt XML file" ) + public void testValidateIncorrectXmlFile() + { + Executable validateXmlDocument = () -> { + this.newLibvirtXmlDocumentValidationInstance( "qemu-kvm_default-ubuntu-20-04-vm-invalid.xml", "domain.rng" ); + }; + + assertThrows( LibvirtXmlValidationException.class, validateXmlDocument ); + } + + @Test + @DisplayName( "Get non-existent node from libvirt XML file" ) + public void testGetNonExistentElement() + { + LibvirtXmlDocument vm = this.newLibvirtXmlDocumentInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertNull( vm.getRootXmlNode().getXmlElement( "info" ) ); + } + + @Test + @DisplayName( "Set non-existent node in libvirt XML file" ) + public void testSetNonExistentElement() + { + LibvirtXmlDocument vm = this.newLibvirtXmlDocumentInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + vm.getRootXmlNode().setXmlElement( "info" ); + assertNotNull( vm.getRootXmlNode().getXmlElement( "info" ) ); + } + + @Test + @DisplayName( "Get non-existent element's value in libvirt XML file" ) + public void testGetNonExistentElementValue() + { + LibvirtXmlDocument vm = this.newLibvirtXmlDocumentInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertNull( vm.getRootXmlNode().getXmlElementValue( "info" ) ); + } + + @Test + @DisplayName( "Set non-existent element's value in libvirt XML file" ) + public void testSetNonExistentElementValue() + { + LibvirtXmlDocument vm = this.newLibvirtXmlDocumentInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + vm.getRootXmlNode().setXmlElementValue( "info", "content" ); + assertEquals( "content", vm.getRootXmlNode().getXmlElementValue( "info" ) ); + } + + @Test + @DisplayName( "Get empty element from libvirt XML file" ) + public void testGetEmptyElement() + { + LibvirtXmlDocument vm = this.newLibvirtXmlDocumentInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertNotNull( vm.getRootXmlNode().getXmlElement( "features/acpi" ) ); + } + + @Test + @DisplayName( "Set empty element in libvirt XML file" ) + public void testSetEmptyElement() + { + LibvirtXmlDocument vm = this.newLibvirtXmlDocumentInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + vm.getRootXmlNode().setXmlElement( "features/acpi" ); + assertNotNull( vm.getRootXmlNode().getXmlElement( "features/acpi" ) ); + } + + @Test + @DisplayName( "Get empty element's value from libvirt XML file" ) + public void testGetEmptyElementValue() + { + LibvirtXmlDocument vm = this.newLibvirtXmlDocumentInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( EMPTY, vm.getRootXmlNode().getXmlElementValue( "features/acpi" ) ); + } + + @Test + @DisplayName( "Set empty element's value in libvirt XML file" ) + public void testSetEmptyElementValue() + { + LibvirtXmlDocument vm = this.newLibvirtXmlDocumentInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + vm.getRootXmlNode().setXmlElementValue( "features/acpi", "content" ); + assertEquals( "content", vm.getRootXmlNode().getXmlElementValue( "features/acpi" ) ); + } + + @Test + @DisplayName( "Get non-existent element's attribute value from libvirt XML file" ) + public void testGetNonExistentElementAttributeValue() + { + LibvirtXmlDocument vm = this.newLibvirtXmlDocumentInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertNull( vm.getRootXmlNode().getXmlElementAttributeValue( "info", "test" ) ); + } + + @Test + @DisplayName( "Set non-existent element's attribute value from libvirt XML file" ) + public void testSetNonExistentElementAttributeValue() + { + LibvirtXmlDocument vm = this.newLibvirtXmlDocumentInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + vm.getRootXmlNode().setXmlElementAttributeValue( "info", "test", "info" ); + assertEquals( "info", vm.getRootXmlNode().getXmlElementAttributeValue( "info", "test" ) ); + } + + @Test + @DisplayName( "Get element's non-existent attribute value from libvirt XML file" ) + public void testGetElementNonExistentAttributeValue() + { + LibvirtXmlDocument vm = this.newLibvirtXmlDocumentInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertNull( vm.getRootXmlNode().getXmlElementAttributeValue( "features/acpi", "test" ) ); + } + + @Test + @DisplayName( "Set element's non-existent attribute value from libvirt XML file" ) + public void testSetElementNonExistentAttributeValue() + { + LibvirtXmlDocument vm = this.newLibvirtXmlDocumentInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + vm.getRootXmlNode().setXmlElementAttributeValue( "features/acpi", "test", "info" ); + assertEquals( "info", vm.getRootXmlNode().getXmlElementAttributeValue( "features/acpi", "test" ) ); + } + + @Test + @DisplayName( "Get element's attribute value from libvirt XML file" ) + public void testGetElementAttributeValue() + { + LibvirtXmlDocument vm = this.newLibvirtXmlDocumentInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( "partial", vm.getRootXmlNode().getXmlElementAttributeValue( "cpu", "check" ) ); + } + + @Test + @DisplayName( "Set element's attribute value from libvirt XML file" ) + public void testSetElementAttributeValue() + { + LibvirtXmlDocument vm = this.newLibvirtXmlDocumentInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + vm.getRootXmlNode().setXmlElementAttributeValue( "cpu", "check", "full" ); + assertEquals( "full", vm.getRootXmlNode().getXmlElementAttributeValue( "cpu", "check" ) ); + } +} diff --git a/src/test/java/org/openslx/libvirt/xml/LibvirtXmlTestResources.java b/src/test/java/org/openslx/libvirt/xml/LibvirtXmlTestResources.java new file mode 100644 index 0000000..6cc0360 --- /dev/null +++ b/src/test/java/org/openslx/libvirt/xml/LibvirtXmlTestResources.java @@ -0,0 +1,29 @@ +package org.openslx.libvirt.xml; + +import java.io.File; +import java.io.IOException; +import java.net.URL; + +public final class LibvirtXmlTestResources +{ + private static final String LIBVIRT_PREFIX_PATH = File.separator + "libvirt"; + private static final String LIBVIRT_PREFIX_PATH_XML = LIBVIRT_PREFIX_PATH + File.separator + "xml"; + + private static final String LIBVIRT_TEMP_PREFIX = "libvirt-"; + private static final String LIBVIRT_TEMP_SUFFIX = ".xml"; + + public static File getLibvirtXmlFile( String libvirtXmlFileName ) + { + String libvirtXmlPath = LibvirtXmlTestResources.LIBVIRT_PREFIX_PATH_XML + File.separator + libvirtXmlFileName; + URL libvirtXml = LibvirtXmlTestResources.class.getResource( libvirtXmlPath ); + return new File( libvirtXml.getFile() ); + } + + public static File createLibvirtXmlTempFile() throws IOException + { + File tempFile = File.createTempFile( LibvirtXmlTestResources.LIBVIRT_TEMP_PREFIX, + LibvirtXmlTestResources.LIBVIRT_TEMP_SUFFIX ); + tempFile.deleteOnExit(); + return tempFile; + } +} -- cgit v1.2.3-55-g7522