diff options
author | Stephan Schwär | 2021-02-19 00:09:54 +0100 |
---|---|---|
committer | Stephan Schwär | 2021-02-19 00:09:54 +0100 |
commit | 62dce8c2b4e519f689e89038ff2afe4496f60628 (patch) | |
tree | 7c6802a436e2beb943065b5a377b61eb77d93170 /src/test/java/org/openslx | |
parent | [docker] Check recieved content. (diff) | |
parent | Add automatic RelaxNG schema validation for Libvirt domain XML documents (diff) | |
download | master-sync-shared-62dce8c2b4e519f689e89038ff2afe4496f60628.tar.gz master-sync-shared-62dce8c2b4e519f689e89038ff2afe4496f60628.tar.xz master-sync-shared-62dce8c2b4e519f689e89038ff2afe4496f60628.zip |
Merge remote-tracking branch 'origin/feature/qemu-integration'
Diffstat (limited to 'src/test/java/org/openslx')
6 files changed, 1325 insertions, 0 deletions
diff --git a/src/test/java/org/openslx/libvirt/domain/DomainTest.java b/src/test/java/org/openslx/libvirt/domain/DomainTest.java new file mode 100644 index 0000000..a604b21 --- /dev/null +++ b/src/test/java/org/openslx/libvirt/domain/DomainTest.java @@ -0,0 +1,294 @@ +package org.openslx.libvirt.domain; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.fail; + +import java.math.BigInteger; + +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.openslx.libvirt.domain.Domain.CpuCheck; +import org.openslx.libvirt.domain.Domain.CpuMode; +import org.openslx.libvirt.xml.LibvirtXmlDocumentException; +import org.openslx.libvirt.xml.LibvirtXmlSerializationException; +import org.openslx.libvirt.xml.LibvirtXmlTestResources; +import org.openslx.libvirt.xml.LibvirtXmlValidationException; + +public class DomainTest +{ + @BeforeAll + public static void setUp() + { + // disable logging with log4j + LogManager.getRootLogger().setLevel( Level.OFF ); + } + + private Domain newDomainInstance( String xmlFileName ) + { + Domain domain = null; + + try { + domain = new Domain( LibvirtXmlTestResources.getLibvirtXmlFile( xmlFileName ) ); + } catch ( LibvirtXmlDocumentException | LibvirtXmlSerializationException | LibvirtXmlValidationException e ) { + String errorMsg = new String( "Cannot prepare requested Libvirt domain XML file from the resources folder" ); + fail( errorMsg ); + } + + return domain; + } + + @Test + @DisplayName( "Get VM type from libvirt XML file" ) + public void testGetType() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( Domain.Type.KVM.toString(), vm.getType().toString() ); + } + + @Test + @DisplayName( "Set VM type from libvirt XML file" ) + public void testSetType() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + vm.setType( Domain.Type.QEMU ); + assertEquals( Domain.Type.QEMU.toString(), vm.getType().toString() ); + } + + @Test + @DisplayName( "Get VM name from libvirt XML file" ) + public void testGetName() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( "ubuntu-20-04", vm.getName() ); + } + + @Test + @DisplayName( "Set VM name in libvirt XML file" ) + public void testSetName() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + vm.setName( "ubuntu-18-04" ); + assertEquals( "ubuntu-18-04", vm.getName() ); + } + + @Test + @DisplayName( "Get VM title from libvirt XML file" ) + public void testGetTitle() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( "Ubuntu 20.04", vm.getTitle() ); + } + + @Test + @DisplayName( "Set VM title in libvirt XML file" ) + public void testSetTitle() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + vm.setTitle( "Ubuntu 18.04" ); + assertEquals( "Ubuntu 18.04", vm.getTitle() ); + } + + @Test + @DisplayName( "Get VM description from libvirt XML file" ) + public void testGetDescription() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( "Ubuntu 20.04 desktop installation", vm.getDescription() ); + } + + @Test + @DisplayName( "Set VM description in libvirt XML file" ) + public void testSetDescription() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + vm.setDescription( "Ubuntu 18.04 server installation" ); + assertEquals( "Ubuntu 18.04 server installation", vm.getDescription() ); + } + + @Test + @DisplayName( "Get VM UUID from libvirt XML file" ) + public void testGetUuid() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( "8dc5433c-0228-49e4-b019-fa2b606aa544", vm.getUuid() ); + } + + @Test + @DisplayName( "Set VM UUID in libvirt XML file" ) + public void testSetUuid() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + vm.setUuid( "5ab08167-3d95-400e-ac83-e6af8d150971" ); + assertEquals( "5ab08167-3d95-400e-ac83-e6af8d150971", vm.getUuid() ); + } + + @Test + @DisplayName( "Get VM memory from libvirt XML file" ) + public void testGetMemory() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( new BigInteger( "4294967296" ).toString(), vm.getMemory().toString() ); + } + + @Test + @DisplayName( "Set VM memory in libvirt XML file" ) + public void testSetMemory() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + vm.setMemory( new BigInteger( "12073740288" ) ); + assertEquals( new BigInteger( "12073740288" ).toString(), vm.getMemory().toString() ); + } + + @Test + @DisplayName( "Get current VM memory from libvirt XML file" ) + public void testGetCurrentMemory() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( new BigInteger( "4294967296" ).toString(), vm.getCurrentMemory().toString() ); + } + + @Test + @DisplayName( "Set current VM memory in libvirt XML file" ) + public void testSetCurrentMemory() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + vm.setCurrentMemory( new BigInteger( "8087237632" ) ); + assertEquals( new BigInteger( "8087237632" ).toString(), vm.getCurrentMemory().toString() ); + } + + @Test + @DisplayName( "Get VM number of vCpus from libvirt XML file" ) + public void testGetVCpu() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( 2, vm.getVCpu() ); + } + + @Test + @DisplayName( "Set VM number of vCpus in libvirt XML file" ) + public void testSetVCpu() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + vm.setVCpu( 4 ); + assertEquals( 4, vm.getVCpu() ); + } + + @Test + @DisplayName( "Get VM CPU model from libvirt XML file" ) + public void testGetCpuModel() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertNull( vm.getCpuModel() ); + } + + @Test + @DisplayName( "Set VM CPU model in libvirt XML file" ) + public void testSetCpuModel() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + vm.setCpuModel( "core2duo" ); + assertEquals( "core2duo", vm.getCpuModel() ); + } + + @Test + @DisplayName( "Get VM CPU mode from libvirt XML file" ) + public void testGetCpuModelMode() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( CpuMode.HOST_MODEL.toString(), vm.getCpuMode().toString() ); + } + + @Test + @DisplayName( "Set VM CPU mode in libvirt XML file" ) + public void testSetCpuModelMode() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + vm.setCpuMode( CpuMode.HOST_PASSTHROUGH ); + assertEquals( CpuMode.HOST_PASSTHROUGH.toString(), vm.getCpuMode().toString() ); + } + + @Test + @DisplayName( "Get VM CPU check from libvirt XML file" ) + public void testGetCpuCheck() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( CpuCheck.PARTIAL.toString(), vm.getCpuCheck().toString() ); + } + + @Test + @DisplayName( "Set VM CPU check in libvirt XML file" ) + public void testSetCpuCheck() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + vm.setCpuCheck( CpuCheck.NONE ); + assertEquals( CpuCheck.NONE.toString(), vm.getCpuCheck().toString() ); + } + + @Test + @DisplayName( "Get all VM devices from libvirt XML file" ) + public void testGetDevices() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( 21, vm.getDevices().size() ); + } + + @Test + @DisplayName( "Get all VM controller devices from libvirt XML file" ) + public void testGetControllerDevices() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( 14, vm.getControllerDevices().size() ); + } + + @Test + @DisplayName( "Get all VM disk devices from libvirt XML file" ) + public void testGetDiskDevices() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( 3, vm.getDiskDevices().size() ); + } + + @Test + @DisplayName( "Get all VM hostdev devices from libvirt XML file" ) + public void testGetHostdevDevices() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( 0, vm.getHostdevDevices().size() ); + } + + @Test + @DisplayName( "Get all VM interface devices from libvirt XML file" ) + public void testGetInterfaceDevices() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( 1, vm.getInterfaceDevices().size() ); + } + + @Test + @DisplayName( "Get all VM graphic devices from libvirt XML file" ) + public void testGetGraphicDevices() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( 1, vm.getGraphicDevices().size() ); + } + + @Test + @DisplayName( "Get all VM sound devices from libvirt XML file" ) + public void testGetSoundDevices() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( 1, vm.getSoundDevices().size() ); + } + + @Test + @DisplayName( "Get all VM video devices from libvirt XML file" ) + public void testGetVideoDevices() + { + Domain vm = this.newDomainInstance( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( 1, vm.getVideoDevices().size() ); + } +} 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..75b934e --- /dev/null +++ b/src/test/java/org/openslx/libvirt/xml/LibvirtXmlDocumentTest.java @@ -0,0 +1,260 @@ +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.io.InputStream; +import java.nio.charset.StandardCharsets; + +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, InputStream 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 ); + InputStream rngSchema = LibvirtXmlResources.getLibvirtRng( rngSchemaFileName ); + document = new LibvirtXmlDocumentStub( xmlFile, rngSchema ); + } 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; + } +} diff --git a/src/test/java/org/openslx/util/vm/DiskImageTest.java b/src/test/java/org/openslx/util/vm/DiskImageTest.java new file mode 100644 index 0000000..e1105d8 --- /dev/null +++ b/src/test/java/org/openslx/util/vm/DiskImageTest.java @@ -0,0 +1,260 @@ +package org.openslx.util.vm; + +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 java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; + +import org.apache.log4j.Level; +import org.apache.log4j.LogManager; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.openslx.util.vm.DiskImage.ImageFormat; +import org.openslx.util.vm.DiskImage.UnknownImageFormatException; + +public class DiskImageTest +{ + @BeforeAll + public static void setUp() + { + // disable logging with log4j + LogManager.getRootLogger().setLevel( Level.OFF ); + } + + @Test + @DisplayName( "Test detection of VMDK disk image" ) + public void testVmdkDiskImage() throws FileNotFoundException, IOException, UnknownImageFormatException + { + File file = DiskImageTestResources.getDiskFile( "image-default.vmdk" ); + DiskImage image = new DiskImage( file ); + + assertEquals( ImageFormat.VMDK.toString(), image.getImageFormat().toString() ); + assertEquals( true, image.isStandalone ); + assertEquals( false, image.isSnapshot ); + assertEquals( false, image.isCompressed ); + assertEquals( 18, image.hwVersion ); + assertNull( image.diskDescription ); + } + + @Test + @DisplayName( "Test detection of VDI disk image" ) + public void testVdiDiskImage() throws FileNotFoundException, IOException, UnknownImageFormatException + { + DiskImage image = new DiskImage( DiskImageTestResources.getDiskFile( "image-default.vdi" ) ); + + assertEquals( ImageFormat.VDI.toString(), image.getImageFormat().toString() ); + assertEquals( true, image.isStandalone ); + assertEquals( false, image.isSnapshot ); + assertEquals( false, image.isCompressed ); + assertEquals( 0, image.hwVersion ); + assertNotNull( image.diskDescription ); + } + + @Test + @DisplayName( "Test detection of default QCOW2 disk image" ) + public void testQcow2DiskImage() throws FileNotFoundException, IOException, UnknownImageFormatException + { + DiskImage image = new DiskImage( DiskImageTestResources.getDiskFile( "image-default.qcow2" ) ); + + assertEquals( ImageFormat.QCOW2.toString(), image.getImageFormat().toString() ); + assertEquals( true, image.isStandalone ); + assertEquals( false, image.isSnapshot ); + assertEquals( false, image.isCompressed ); + assertEquals( 3, image.hwVersion ); + assertNull( image.diskDescription ); + } + + @Test + @DisplayName( "Test detection of compressed, 16384 byte cluster QCOW2 disk image with extended L2 tables" ) + public void testQcow2DetectionL2Compressed16384DiskImage() + throws FileNotFoundException, IOException, UnknownImageFormatException + { + DiskImage image = new DiskImage( DiskImageTestResources.getDiskFile( "image_cs-16384_cp-on_l2-on.qcow2" ) ); + + assertEquals( ImageFormat.QCOW2.toString(), image.getImageFormat().toString() ); + assertEquals( true, image.isStandalone ); + assertEquals( false, image.isSnapshot ); + assertEquals( true, image.isCompressed ); + assertEquals( 3, image.hwVersion ); + assertNull( image.diskDescription ); + } + + @Test + @DisplayName( "Test detection of compressed, 16384 byte cluster QCOW2 disk image without extended L2 tables" ) + public void testQcow2DetectionNonL2Compressed16384DiskImage() + throws FileNotFoundException, IOException, UnknownImageFormatException + { + DiskImage image = new DiskImage( DiskImageTestResources.getDiskFile( "image_cs-16384_cp-on_l2-off.qcow2" ) ); + + assertEquals( ImageFormat.QCOW2.toString(), image.getImageFormat().toString() ); + assertEquals( true, image.isStandalone ); + assertEquals( false, image.isSnapshot ); + assertEquals( true, image.isCompressed ); + assertEquals( 3, image.hwVersion ); + assertNull( image.diskDescription ); + } + + @Test + @DisplayName( "Test detection of non-compressed, 16384 byte cluster QCOW2 disk image with extended L2 tables" ) + public void testQcow2DetectionL2NonCompressed16384DiskImage() + throws FileNotFoundException, IOException, UnknownImageFormatException + { + DiskImage image = new DiskImage( DiskImageTestResources.getDiskFile( "image_cs-16384_cp-off_l2-on.qcow2" ) ); + + assertEquals( ImageFormat.QCOW2.toString(), image.getImageFormat().toString() ); + assertEquals( true, image.isStandalone ); + assertEquals( false, image.isSnapshot ); + assertEquals( false, image.isCompressed ); + assertEquals( 3, image.hwVersion ); + assertNull( image.diskDescription ); + } + + @Test + @DisplayName( "Test detection of non-compressed, 16384 byte cluster QCOW2 disk image without extended L2 tables" ) + public void testQcow2DetectionNonL2NonCompressed16384DiskImage() + throws FileNotFoundException, IOException, UnknownImageFormatException + { + DiskImage image = new DiskImage( DiskImageTestResources.getDiskFile( "image_cs-16384_cp-off_l2-off.qcow2" ) ); + + assertEquals( ImageFormat.QCOW2.toString(), image.getImageFormat().toString() ); + assertEquals( true, image.isStandalone ); + assertEquals( false, image.isSnapshot ); + assertEquals( false, image.isCompressed ); + assertEquals( 3, image.hwVersion ); + assertNull( image.diskDescription ); + } + + @Test + @DisplayName( "Test detection of compressed, 65536 byte cluster QCOW2 disk image with extended L2 tables" ) + public void testQcow2DetectionL2Compressed65536DiskImage() + throws FileNotFoundException, IOException, UnknownImageFormatException + { + DiskImage image = new DiskImage( DiskImageTestResources.getDiskFile( "image_cs-65536_cp-on_l2-on.qcow2" ) ); + + assertEquals( ImageFormat.QCOW2.toString(), image.getImageFormat().toString() ); + assertEquals( true, image.isStandalone ); + assertEquals( false, image.isSnapshot ); + assertEquals( true, image.isCompressed ); + assertEquals( 3, image.hwVersion ); + assertNull( image.diskDescription ); + } + + @Test + @DisplayName( "Test detection of compressed, 65536 byte cluster QCOW2 disk image without extended L2 tables" ) + public void testQcow2DetectionNonL2Compressed65536DiskImage() + throws FileNotFoundException, IOException, UnknownImageFormatException + { + DiskImage image = new DiskImage( DiskImageTestResources.getDiskFile( "image_cs-65536_cp-on_l2-off.qcow2" ) ); + + assertEquals( ImageFormat.QCOW2.toString(), image.getImageFormat().toString() ); + assertEquals( true, image.isStandalone ); + assertEquals( false, image.isSnapshot ); + assertEquals( true, image.isCompressed ); + assertEquals( 3, image.hwVersion ); + assertNull( image.diskDescription ); + } + + @Test + @DisplayName( "Test detection of non-compressed, 65536 byte cluster QCOW2 disk image with extended L2 tables" ) + public void testQcow2DetectionL2NonCompressed65536DiskImage() + throws FileNotFoundException, IOException, UnknownImageFormatException + { + DiskImage image = new DiskImage( DiskImageTestResources.getDiskFile( "image_cs-65536_cp-off_l2-on.qcow2" ) ); + + assertEquals( ImageFormat.QCOW2.toString(), image.getImageFormat().toString() ); + assertEquals( true, image.isStandalone ); + assertEquals( false, image.isSnapshot ); + assertEquals( false, image.isCompressed ); + assertEquals( 3, image.hwVersion ); + assertNull( image.diskDescription ); + } + + @Test + @DisplayName( "Test detection of non-compressed, 65536 byte cluster QCOW2 disk image without extended L2 tables" ) + public void testQcow2DetectionNonL2NonCompressed65536DiskImage() + throws FileNotFoundException, IOException, UnknownImageFormatException + { + DiskImage image = new DiskImage( DiskImageTestResources.getDiskFile( "image_cs-65536_cp-off_l2-off.qcow2" ) ); + + assertEquals( ImageFormat.QCOW2.toString(), image.getImageFormat().toString() ); + assertEquals( true, image.isStandalone ); + assertEquals( false, image.isSnapshot ); + assertEquals( false, image.isCompressed ); + assertEquals( 3, image.hwVersion ); + assertNull( image.diskDescription ); + } + + @Test + @DisplayName( "Test detection of compressed, 2097152 byte cluster QCOW2 disk image with extended L2 tables" ) + public void testQcow2DetectionL2Compressed2097152DiskImage() + throws FileNotFoundException, IOException, UnknownImageFormatException + { + DiskImage image = new DiskImage( DiskImageTestResources.getDiskFile( "image_cs-2097152_cp-on_l2-on.qcow2" ) ); + + assertEquals( ImageFormat.QCOW2.toString(), image.getImageFormat().toString() ); + assertEquals( true, image.isStandalone ); + assertEquals( false, image.isSnapshot ); + assertEquals( true, image.isCompressed ); + assertEquals( 3, image.hwVersion ); + assertNull( image.diskDescription ); + } + + @Test + @DisplayName( "Test detection of compressed, 2097152 byte cluster QCOW2 disk image without extended L2 tables" ) + public void testQcow2DetectionNonL2Compressed2097152DiskImage() + throws FileNotFoundException, IOException, UnknownImageFormatException + { + DiskImage image = new DiskImage( DiskImageTestResources.getDiskFile( "image_cs-2097152_cp-on_l2-off.qcow2" ) ); + + assertEquals( ImageFormat.QCOW2.toString(), image.getImageFormat().toString() ); + assertEquals( true, image.isStandalone ); + assertEquals( false, image.isSnapshot ); + assertEquals( true, image.isCompressed ); + assertEquals( 3, image.hwVersion ); + assertNull( image.diskDescription ); + } + + @Test + @DisplayName( "Test detection of non-compressed, 2097152 byte cluster QCOW2 disk image with extended L2 tables" ) + public void testQcow2DetectionL2NonCompressed2097152DiskImage() + throws FileNotFoundException, IOException, UnknownImageFormatException + { + DiskImage image = new DiskImage( DiskImageTestResources.getDiskFile( "image_cs-2097152_cp-off_l2-on.qcow2" ) ); + + assertEquals( ImageFormat.QCOW2.toString(), image.getImageFormat().toString() ); + assertEquals( true, image.isStandalone ); + assertEquals( false, image.isSnapshot ); + assertEquals( false, image.isCompressed ); + assertEquals( 3, image.hwVersion ); + assertNull( image.diskDescription ); + } + + @Test + @DisplayName( "Test detection of non-compressed, 2097152 byte cluster QCOW2 disk image without extended L2 tables" ) + public void testQcow2DetectionNonL2NonCompressed2097152DiskImage() + throws FileNotFoundException, IOException, UnknownImageFormatException + { + DiskImage image = new DiskImage( DiskImageTestResources.getDiskFile( "image_cs-2097152_cp-off_l2-off.qcow2" ) ); + + assertEquals( ImageFormat.QCOW2.toString(), image.getImageFormat().toString() ); + assertEquals( true, image.isStandalone ); + assertEquals( false, image.isSnapshot ); + assertEquals( false, image.isCompressed ); + assertEquals( 3, image.hwVersion ); + assertNull( image.diskDescription ); + } + + @Test + @DisplayName( "Test of invalid disk image" ) + public void testInvalidDiskImage() throws FileNotFoundException, IOException, UnknownImageFormatException + { + Assertions.assertThrows( UnknownImageFormatException.class, () -> { + new DiskImage( DiskImageTestResources.getDiskFile( "image-default.invalid" ) ); + } ); + } +} diff --git a/src/test/java/org/openslx/util/vm/DiskImageTestResources.java b/src/test/java/org/openslx/util/vm/DiskImageTestResources.java new file mode 100644 index 0000000..1f164bd --- /dev/null +++ b/src/test/java/org/openslx/util/vm/DiskImageTestResources.java @@ -0,0 +1,16 @@ +package org.openslx.util.vm; + +import java.io.File; +import java.net.URL; + +public class DiskImageTestResources +{ + private static final String DISK_PREFIX_PATH = File.separator + "disk"; + + public static File getDiskFile( String diskFileName ) + { + String diskPath = DiskImageTestResources.DISK_PREFIX_PATH + File.separator + diskFileName; + URL disk = DiskImageTestResources.class.getResource( diskPath ); + return new File( disk.getFile() ); + } +} diff --git a/src/test/java/org/openslx/util/vm/QemuMetaDataTest.java b/src/test/java/org/openslx/util/vm/QemuMetaDataTest.java new file mode 100644 index 0000000..f201a77 --- /dev/null +++ b/src/test/java/org/openslx/util/vm/QemuMetaDataTest.java @@ -0,0 +1,466 @@ +package org.openslx.util.vm; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.params.provider.Arguments.arguments; + +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Field; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Stream; + +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.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; +import org.openslx.libvirt.domain.Domain; +import org.openslx.libvirt.domain.device.ControllerUsb; +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.domain.device.Sound; +import org.openslx.libvirt.xml.LibvirtXmlTestResources; +import org.openslx.util.vm.DiskImage.ImageFormat; +import org.openslx.util.vm.VmMetaData.EtherType; +import org.openslx.util.vm.VmMetaData.EthernetDevType; +import org.openslx.util.vm.VmMetaData.SoundCardType; +import org.openslx.util.vm.VmMetaData.UsbSpeed; + +public class QemuMetaDataTest +{ + private static Domain getPrivateDomainFromQemuMetaData( QemuMetaData qemuMetadata ) + throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException + { + Field privateDomainField = QemuMetaData.class.getDeclaredField( "vmConfig" ); + privateDomainField.setAccessible( true ); + return Domain.class.cast( privateDomainField.get( qemuMetadata ) ); + } + + @BeforeAll + public static void setUp() + { + // disable logging with log4j + LogManager.getRootLogger().setLevel( Level.OFF ); + } + + @Test + @DisplayName( "Test display name from VM configuration" ) + public void testQemuMetaDataGetDisplayName() throws UnsupportedVirtualizerFormatException, IOException + { + File file = LibvirtXmlTestResources.getLibvirtXmlFile( "qemu-kvm_default-archlinux-vm.xml" ); + QemuMetaData vmConfig = new QemuMetaData( null, file ); + + final String displayName = vmConfig.getDisplayName(); + + assertEquals( "archlinux", displayName ); + } + + @Test + @DisplayName( "Test machine snapshot state from VM configuration" ) + public void testQemuMetaDataIsMachineSnapshot() throws UnsupportedVirtualizerFormatException, IOException + { + File file = LibvirtXmlTestResources.getLibvirtXmlFile( "qemu-kvm_default-archlinux-vm.xml" ); + QemuMetaData vmConfig = new QemuMetaData( null, file ); + + final boolean isVmSnapshot = vmConfig.isMachineSnapshot(); + + assertEquals( false, isVmSnapshot ); + } + + @Test + @DisplayName( "Test supported image formats from VM configuration" ) + public void testQemuMetaDataGetSupportedImageFormats() throws UnsupportedVirtualizerFormatException, IOException + { + File file = LibvirtXmlTestResources.getLibvirtXmlFile( "qemu-kvm_default-archlinux-vm.xml" ); + QemuMetaData vmConfig = new QemuMetaData( null, file ); + + final List<DiskImage.ImageFormat> supportedImageFormats = vmConfig.getSupportedImageFormats(); + + assertNotNull( supportedImageFormats ); + assertEquals( 3, supportedImageFormats.size() ); + assertEquals( true, supportedImageFormats + .containsAll( Arrays.asList( ImageFormat.QCOW2, ImageFormat.VMDK, ImageFormat.VDI ) ) ); + } + + @Test + @DisplayName( "Test output of HDDs from VM configuration" ) + public void testQemuMetaDataGetHdds() throws UnsupportedVirtualizerFormatException, IOException + { + File file = LibvirtXmlTestResources.getLibvirtXmlFile( "qemu-kvm_default-archlinux-vm.xml" ); + QemuMetaData vmConfig = new QemuMetaData( null, file ); + + final List<VmMetaData.HardDisk> hdds = vmConfig.getHdds(); + + assertNotNull( hdds ); + assertEquals( 1, hdds.size() ); + assertEquals( "/var/lib/libvirt/images/archlinux.qcow2", hdds.get( 0 ).diskImage ); + } + + @Test + @DisplayName( "Test output of unfiltered VM configuration" ) + public void testQemuMetaDataGetDefinitionArray() throws UnsupportedVirtualizerFormatException, IOException + { + File file = LibvirtXmlTestResources.getLibvirtXmlFile( "qemu-kvm_default-archlinux-vm.xml" ); + QemuMetaData vmConfig = new QemuMetaData( null, file ); + + final String unfilteredXmlConfig = new String( vmConfig.getDefinitionArray(), StandardCharsets.UTF_8 ); + final String originalXmlConfig = FileUtils.readFileToString( file, StandardCharsets.UTF_8 ); + + assertNotNull( unfilteredXmlConfig ); + + final int lengthUnfilteredXmlConfig = unfilteredXmlConfig.split( System.lineSeparator() ).length; + final int lengthOriginalXmlConfig = originalXmlConfig.split( System.lineSeparator() ).length; + + assertEquals( lengthOriginalXmlConfig, lengthUnfilteredXmlConfig ); + } + + @Test + @DisplayName( "Test output of filtered VM configuration" ) + public void testQemuMetaDataGetFilteredDefinitionArray() throws UnsupportedVirtualizerFormatException, IOException + { + File file = LibvirtXmlTestResources.getLibvirtXmlFile( "qemu-kvm_default-archlinux-vm.xml" ); + QemuMetaData vmConfig = new QemuMetaData( null, file ); + + final int numberOfDeletedElements = 4; + + final String filteredXmlConfig = new String( vmConfig.getFilteredDefinitionArray(), StandardCharsets.UTF_8 ); + final String originalXmlConfig = FileUtils.readFileToString( file, StandardCharsets.UTF_8 ); + + assertNotNull( filteredXmlConfig ); + + final int lengthFilteredXmlConfig = filteredXmlConfig.split( System.lineSeparator() ).length; + final int lengthOriginalXmlConfig = originalXmlConfig.split( System.lineSeparator() ).length; + + assertEquals( lengthOriginalXmlConfig, lengthFilteredXmlConfig + numberOfDeletedElements ); + } + + @ParameterizedTest + @DisplayName( "Test add HDD to VM configuration" ) + @ValueSource( strings = { "qemu-kvm_default-archlinux-vm.xml", "qemu-kvm_default-archlinux-vm-no-hdd.xml" } ) + public void testQemuMetaDataAddHdd( String xmlFileName ) + throws UnsupportedVirtualizerFormatException, NoSuchFieldException, SecurityException, + IllegalArgumentException, IllegalAccessException + { + File diskFile = DiskImageTestResources.getDiskFile( "image-default.qcow2" ); + File file = LibvirtXmlTestResources.getLibvirtXmlFile( xmlFileName ); + QemuMetaData vmConfig = new QemuMetaData( null, file ); + + Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); + + final int numHddsLibvirtDomainXmlBeforeAdd = vmLibvirtDomainConfig.getDiskStorageDevices().size(); + final int numHddsQemuMetaDataBeforeAdd = vmConfig.hdds.size(); + + vmConfig.addHddTemplate( diskFile, null, null ); + + final int numHddsLibvirtDomainXmlAfterAdd = vmLibvirtDomainConfig.getDiskStorageDevices().size(); + final int numHddsQemuMetaDataAfterAdd = vmConfig.hdds.size(); + + assertTrue( numHddsLibvirtDomainXmlBeforeAdd == numHddsQemuMetaDataBeforeAdd ); + assertTrue( numHddsLibvirtDomainXmlAfterAdd == numHddsQemuMetaDataAfterAdd ); + assertTrue( numHddsQemuMetaDataBeforeAdd >= 0 ); + assertTrue( numHddsQemuMetaDataAfterAdd > 0 ); + + if ( numHddsQemuMetaDataBeforeAdd >= 1 ) { + // update existing HDD in the Libvirt XML config, but do not add a new HDD + assertEquals( numHddsQemuMetaDataBeforeAdd, numHddsQemuMetaDataAfterAdd ); + } else { + // numHddsQemuMetaDataBeforeAdd == 0 + // add a HDD to the Libvirt XML config, since there was no HDD available + assertEquals( numHddsQemuMetaDataBeforeAdd + 1, numHddsQemuMetaDataAfterAdd ); + } + + DiskStorage addedStorageDevice = vmLibvirtDomainConfig.getDiskStorageDevices().get( 0 ); + assertEquals( diskFile.getAbsolutePath(), addedStorageDevice.getStorageSource() ); + } + + @ParameterizedTest + @DisplayName( "Test add CDROM to VM configuration" ) + @ValueSource( strings = { "qemu-kvm_default-archlinux-vm.xml", "qemu-kvm_default-archlinux-vm-cdrom.xml" } ) + public void testQemuMetaDataAddCdrom( String xmlFileName ) + throws UnsupportedVirtualizerFormatException, NoSuchFieldException, SecurityException, + IllegalArgumentException, IllegalAccessException + { + File diskFile = DiskImageTestResources.getDiskFile( "image-default.qcow2" ); + File file = LibvirtXmlTestResources.getLibvirtXmlFile( xmlFileName ); + QemuMetaData vmConfig = new QemuMetaData( null, file ); + + Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); + + final int numCdromsLibvirtDomainXmlBeforeAdd = vmLibvirtDomainConfig.getDiskCdromDevices().size(); + + vmConfig.addCdrom( 0, diskFile.getAbsolutePath() ); + + final int numCdromsLibvirtDomainXmlAfterAdd = vmLibvirtDomainConfig.getDiskCdromDevices().size(); + + assertTrue( numCdromsLibvirtDomainXmlBeforeAdd >= 0 ); + assertTrue( numCdromsLibvirtDomainXmlAfterAdd > 0 ); + + DiskCdrom addedCdromDevice = vmLibvirtDomainConfig.getDiskCdromDevices().get( 0 ); + assertEquals( diskFile.getAbsolutePath(), addedCdromDevice.getStorageSource() ); + } + + @ParameterizedTest + @DisplayName( "Test add physical CDROM drive to VM configuration" ) + @ValueSource( strings = { "qemu-kvm_default-archlinux-vm.xml", "qemu-kvm_default-archlinux-vm-cdrom.xml" } ) + public void testQemuMetaDataAddPhysicalCdromDrive( String xmlFileName ) + throws UnsupportedVirtualizerFormatException, NoSuchFieldException, SecurityException, + IllegalArgumentException, IllegalAccessException + { + File file = LibvirtXmlTestResources.getLibvirtXmlFile( xmlFileName ); + QemuMetaData vmConfig = new QemuMetaData( null, file ); + + Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); + + final int numCdromsLibvirtDomainXmlBeforeAdd = vmLibvirtDomainConfig.getDiskCdromDevices().size(); + + vmConfig.addCdrom( 0, null ); + + final int numCdromsLibvirtDomainXmlAfterAdd = vmLibvirtDomainConfig.getDiskCdromDevices().size(); + + assertTrue( numCdromsLibvirtDomainXmlBeforeAdd >= 0 ); + assertTrue( numCdromsLibvirtDomainXmlAfterAdd > 0 ); + + DiskCdrom addedCdromDevice = vmLibvirtDomainConfig.getDiskCdromDevices().get( 0 ); + assertEquals( QemuMetaData.CDROM_DEFAULT_PHYSICAL_DRIVE, addedCdromDevice.getStorageSource() ); + } + + @ParameterizedTest + @DisplayName( "Test add floppy to VM configuration" ) + @ValueSource( strings = { "qemu-kvm_default-archlinux-vm.xml", "qemu-kvm_default-archlinux-vm-floppy.xml" } ) + public void testQemuMetaDataAddFloppy( String xmlFileName ) + throws UnsupportedVirtualizerFormatException, NoSuchFieldException, SecurityException, + IllegalArgumentException, IllegalAccessException + { + File diskFile = DiskImageTestResources.getDiskFile( "image-default.qcow2" ); + File file = LibvirtXmlTestResources.getLibvirtXmlFile( xmlFileName ); + QemuMetaData vmConfig = new QemuMetaData( null, file ); + + Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); + + final int numFloppiesLibvirtDomainXmlBeforeAdd = vmLibvirtDomainConfig.getDiskFloppyDevices().size(); + + vmConfig.addFloppy( 0, diskFile.getAbsolutePath(), true ); + + final int numFloppiesLibvirtDomainXmlAfterAdd = vmLibvirtDomainConfig.getDiskFloppyDevices().size(); + + assertTrue( numFloppiesLibvirtDomainXmlBeforeAdd >= 0 ); + assertTrue( numFloppiesLibvirtDomainXmlAfterAdd > 0 ); + + DiskFloppy addedFloppyDevice = vmLibvirtDomainConfig.getDiskFloppyDevices().get( 0 ); + assertTrue( addedFloppyDevice.isReadOnly() ); + assertEquals( diskFile.getAbsolutePath(), addedFloppyDevice.getStorageSource() ); + } + + @ParameterizedTest + @DisplayName( "Test add CPU core count to VM configuration" ) + @ValueSource( ints = { 2, 4, 6, 8 } ) + public void testQemuMetaDataAddCpuCoreCount( int coreCount ) + throws UnsupportedVirtualizerFormatException, NoSuchFieldException, SecurityException, + IllegalArgumentException, IllegalAccessException + { + File file = LibvirtXmlTestResources.getLibvirtXmlFile( "qemu-kvm_default-archlinux-vm.xml" ); + QemuMetaData vmConfig = new QemuMetaData( null, file ); + + Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); + + vmConfig.addCpuCoreCount( coreCount ); + + assertEquals( coreCount, vmLibvirtDomainConfig.getVCpu() ); + } + + @ParameterizedTest + @DisplayName( "Test get sound card from VM configuration" ) + @ValueSource( strings = { "qemu-kvm_default-archlinux-vm.xml", "qemu-kvm_default-archlinux-vm-no-sound.xml" } ) + public void testQemuMetaDataGetSoundCardType( String xmlFileName ) + throws UnsupportedVirtualizerFormatException, NoSuchFieldException, SecurityException, + IllegalArgumentException, IllegalAccessException + { + File file = LibvirtXmlTestResources.getLibvirtXmlFile( xmlFileName ); + QemuMetaData vmConfig = new QemuMetaData( null, file ); + + Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); + + SoundCardType soundCardType = vmConfig.getSoundCard(); + + if ( vmLibvirtDomainConfig.getSoundDevices().isEmpty() ) { + assertEquals( SoundCardType.NONE, soundCardType ); + } else { + assertEquals( SoundCardType.HD_AUDIO, soundCardType ); + } + } + + @ParameterizedTest + @DisplayName( "Test set sound card in VM configuration" ) + @ValueSource( strings = { "qemu-kvm_default-archlinux-vm.xml", "qemu-kvm_default-archlinux-vm-no-sound.xml" } ) + public void testQemuMetaDataSetSoundCardType( String xmlFileName ) + throws UnsupportedVirtualizerFormatException, NoSuchFieldException, SecurityException, + IllegalArgumentException, IllegalAccessException + { + File file = LibvirtXmlTestResources.getLibvirtXmlFile( xmlFileName ); + QemuMetaData vmConfig = new QemuMetaData( null, file ); + + Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); + + final int numSoundDevsLibvirtDomainXmlBeforeAdd = vmLibvirtDomainConfig.getSoundDevices().size(); + + vmConfig.setSoundCard( SoundCardType.SOUND_BLASTER ); + + final int numSoundDevsLibvirtDomainXmlAfterAdd = vmLibvirtDomainConfig.getSoundDevices().size(); + + assertTrue( numSoundDevsLibvirtDomainXmlBeforeAdd >= 0 ); + assertTrue( numSoundDevsLibvirtDomainXmlAfterAdd > 0 ); + + Sound addedSoundDevice = vmLibvirtDomainConfig.getSoundDevices().get( 0 ); + assertEquals( Sound.Model.SB16, addedSoundDevice.getModel() ); + } + + @ParameterizedTest + @DisplayName( "Test get ethernet device type from VM configuration" ) + @ValueSource( strings = { "qemu-kvm_default-archlinux-vm.xml", "qemu-kvm_default-archlinux-vm-no-nic.xml" } ) + public void testQemuMetaDataGetEthernetDevType( String xmlFileName ) + throws UnsupportedVirtualizerFormatException, NoSuchFieldException, SecurityException, + IllegalArgumentException, IllegalAccessException + { + File file = LibvirtXmlTestResources.getLibvirtXmlFile( xmlFileName ); + QemuMetaData vmConfig = new QemuMetaData( null, file ); + + Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); + + EthernetDevType ethernetDeviceType = vmConfig.getEthernetDevType( 0 ); + + if ( vmLibvirtDomainConfig.getInterfaceDevices().isEmpty() ) { + assertEquals( EthernetDevType.NONE, ethernetDeviceType ); + } else { + assertEquals( EthernetDevType.PARAVIRT, ethernetDeviceType ); + } + } + + @ParameterizedTest + @DisplayName( "Test set ethernet device type in VM configuration" ) + @ValueSource( strings = { "qemu-kvm_default-archlinux-vm.xml", "qemu-kvm_default-archlinux-vm-no-nic.xml" } ) + public void testQemuMetaDataSetEthernetDevType( String xmlFileName ) + throws UnsupportedVirtualizerFormatException, NoSuchFieldException, SecurityException, + IllegalArgumentException, IllegalAccessException + { + File file = LibvirtXmlTestResources.getLibvirtXmlFile( xmlFileName ); + QemuMetaData vmConfig = new QemuMetaData( null, file ); + + Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); + + vmConfig.setEthernetDevType( 0, EthernetDevType.E1000E ); + + if ( !vmLibvirtDomainConfig.getInterfaceDevices().isEmpty() ) { + Interface addedEthernetDevice = vmLibvirtDomainConfig.getInterfaceDevices().get( 0 ); + assertEquals( Interface.Model.E1000E, addedEthernetDevice.getModel() ); + } + } + + @ParameterizedTest + @DisplayName( "Test get maximal USB speed from VM configuration" ) + @ValueSource( strings = { "qemu-kvm_default-archlinux-vm.xml", "qemu-kvm_default-archlinux-vm-no-usb.xml" } ) + public void testQemuMetaDataGetMaxUsbSpeed( String xmlFileName ) + throws UnsupportedVirtualizerFormatException, NoSuchFieldException, SecurityException, + IllegalArgumentException, IllegalAccessException + { + File file = LibvirtXmlTestResources.getLibvirtXmlFile( xmlFileName ); + QemuMetaData vmConfig = new QemuMetaData( null, file ); + + Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); + + UsbSpeed maxUsbSpeed = vmConfig.getMaxUsbSpeed(); + + if ( vmLibvirtDomainConfig.getUsbControllerDevices().isEmpty() ) { + assertEquals( UsbSpeed.NONE, maxUsbSpeed ); + } else { + assertEquals( UsbSpeed.USB3_0, maxUsbSpeed ); + } + } + + @ParameterizedTest + @DisplayName( "Test set maximal USB speed in VM configuration" ) + @ValueSource( strings = { "qemu-kvm_default-archlinux-vm.xml", "qemu-kvm_default-archlinux-vm-no-usb.xml" } ) + public void testQemuMetaDataSetMaxUsbSpeed( String xmlFileName ) + throws UnsupportedVirtualizerFormatException, NoSuchFieldException, SecurityException, + IllegalArgumentException, IllegalAccessException + { + File file = LibvirtXmlTestResources.getLibvirtXmlFile( xmlFileName ); + QemuMetaData vmConfig = new QemuMetaData( null, file ); + + Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); + + final int numUsbControllersLibvirtDomainXmlBeforeAdd = vmLibvirtDomainConfig.getUsbControllerDevices().size(); + + vmConfig.setMaxUsbSpeed( UsbSpeed.USB2_0 ); + + final int numUsbControllersLibvirtDomainXmlAfterAdd = vmLibvirtDomainConfig.getUsbControllerDevices().size(); + + assertTrue( numUsbControllersLibvirtDomainXmlBeforeAdd >= 0 ); + assertTrue( numUsbControllersLibvirtDomainXmlAfterAdd > 0 ); + + ControllerUsb addedUsbControllerDevice = vmLibvirtDomainConfig.getUsbControllerDevices().get( 0 ); + assertEquals( ControllerUsb.Model.ICH9_EHCI1, addedUsbControllerDevice.getModel() ); + } + + static Stream<Arguments> configAndEthernetTypeProvider() + { + return Stream.of( + arguments( "qemu-kvm_default-archlinux-vm.xml", EtherType.BRIDGED ), + arguments( "qemu-kvm_default-archlinux-vm.xml", EtherType.HOST_ONLY ), + arguments( "qemu-kvm_default-archlinux-vm.xml", EtherType.NAT ), + arguments( "qemu-kvm_default-archlinux-vm-no-usb.xml", EtherType.BRIDGED ), + arguments( "qemu-kvm_default-archlinux-vm-no-usb.xml", EtherType.HOST_ONLY ), + arguments( "qemu-kvm_default-archlinux-vm-no-usb.xml", EtherType.NAT ) ); + } + + @ParameterizedTest + @DisplayName( "Test add ethernet device to VM configuration" ) + @MethodSource( "configAndEthernetTypeProvider" ) + public void testQemuMetaDataAddEthernet( String xmlFileName, EtherType ethernetType ) + throws UnsupportedVirtualizerFormatException, NoSuchFieldException, SecurityException, + IllegalArgumentException, IllegalAccessException + { + File file = LibvirtXmlTestResources.getLibvirtXmlFile( xmlFileName ); + QemuMetaData vmConfig = new QemuMetaData( null, file ); + + Domain vmLibvirtDomainConfig = QemuMetaDataTest.getPrivateDomainFromQemuMetaData( vmConfig ); + + final int numEthernetDevsLibvirtDomainXmlBeforeAdd = vmLibvirtDomainConfig.getInterfaceDevices().size(); + + vmConfig.addEthernet( ethernetType ); + + final int numEthernetDevsLibvirtDomainXmlAfterAdd = vmLibvirtDomainConfig.getInterfaceDevices().size(); + + assertTrue( numEthernetDevsLibvirtDomainXmlBeforeAdd >= 0 ); + assertTrue( numEthernetDevsLibvirtDomainXmlAfterAdd > 0 ); + + Interface addedEthernetDevice = vmLibvirtDomainConfig.getInterfaceDevices().get( 0 ); + switch ( ethernetType ) { + case BRIDGED: + assertEquals( Interface.Type.BRIDGE, addedEthernetDevice.getType() ); + assertEquals( Interface.Model.VIRTIO, addedEthernetDevice.getModel() ); + assertEquals( QemuMetaData.NETWORK_DEFAULT_BRIDGE, addedEthernetDevice.getSource() ); + break; + case HOST_ONLY: + assertEquals( Interface.Type.NETWORK, addedEthernetDevice.getType() ); + assertEquals( Interface.Model.VIRTIO, addedEthernetDevice.getModel() ); + assertEquals( QemuMetaData.NETWORK_DEFAULT_HOST_ONLY, addedEthernetDevice.getSource() ); + break; + case NAT: + assertEquals( Interface.Type.NETWORK, addedEthernetDevice.getType() ); + assertEquals( Interface.Model.VIRTIO, addedEthernetDevice.getModel() ); + assertEquals( QemuMetaData.NETWORK_DEFAULT_NAT, addedEthernetDevice.getSource() ); + break; + } + } +} |