diff options
Diffstat (limited to 'src/test/java/org/openslx/libvirt')
8 files changed, 1396 insertions, 0 deletions
diff --git a/src/test/java/org/openslx/libvirt/capabilities/CapabilitiesTest.java b/src/test/java/org/openslx/libvirt/capabilities/CapabilitiesTest.java new file mode 100644 index 0000000..fd90698 --- /dev/null +++ b/src/test/java/org/openslx/libvirt/capabilities/CapabilitiesTest.java @@ -0,0 +1,305 @@ +package org.openslx.libvirt.capabilities; + +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.fail; + +import java.math.BigInteger; +import java.util.List; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.core.config.Configurator; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.openslx.libvirt.capabilities.cpu.Cpu; +import org.openslx.libvirt.capabilities.cpu.Feature; +import org.openslx.libvirt.capabilities.cpu.Pages; +import org.openslx.libvirt.capabilities.guest.Domain; +import org.openslx.libvirt.capabilities.guest.Guest; +import org.openslx.libvirt.capabilities.guest.Machine; +import org.openslx.libvirt.domain.Domain.OsType; +import org.openslx.libvirt.domain.Domain.Type; +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 CapabilitiesTest +{ + @BeforeAll + public static void setUp() + { + // disable logging with log4j + Configurator.setRootLevel( Level.OFF ); + } + + private Capabilities newCapabilitiesInstance( String xmlFileName ) + { + Capabilities caps = null; + + try { + caps = new Capabilities( LibvirtXmlTestResources.getLibvirtXmlFile( xmlFileName ) ); + } catch ( LibvirtXmlDocumentException | LibvirtXmlSerializationException | LibvirtXmlValidationException e ) { + final String errorMsg = new String( + "Cannot prepare requested Libvirt capabilities XML file from the resources folder" ); + fail( errorMsg ); + } + + return caps; + } + + @Test + @DisplayName( "Get host UUID from libvirt XML capabilities file" ) + public void testGetHostUuid() + { + final Capabilities caps = this.newCapabilitiesInstance( "qemu-kvm_capabilities_default.xml" ); + + assertEquals( "9b2f12af-1fba-444c-b72b-9cbc43fb3ca5", caps.getHostUuid() ); + } + + @Test + @DisplayName( "Get host CPU from libvirt XML capabilities file" ) + public void testGetHostCpu() + { + final Capabilities caps = this.newCapabilitiesInstance( "qemu-kvm_capabilities_default.xml" ); + final Cpu hostCpu = caps.getHostCpu(); + + assertNotNull( hostCpu ); + assertEquals( "x86_64", hostCpu.getArch() ); + assertEquals( "Skylake-Client-IBRS", hostCpu.getModel() ); + assertEquals( "Intel", hostCpu.getVendor() ); + assertEquals( 1, hostCpu.getTopologySockets() ); + assertEquals( 1, hostCpu.getTopologyDies() ); + assertEquals( 4, hostCpu.getTopologyCores() ); + assertEquals( 1, hostCpu.getTopologyThreads() ); + } + + @Test + @DisplayName( "Get non-existent host CPU from libvirt XML capabilities file" ) + public void testGetHostCpuNonExistent() + { + final Capabilities caps = this.newCapabilitiesInstance( "qemu-kvm_capabilities_no-cpu.xml" ); + final Cpu hostCpu = caps.getHostCpu(); + + assertNull( hostCpu ); + } + + @Test + @DisplayName( "Get host CPU features from libvirt XML capabilities file" ) + public void testGetHostCpuFeatures() + { + final Capabilities caps = this.newCapabilitiesInstance( "qemu-kvm_capabilities_default.xml" ); + final Cpu hostCpu = caps.getHostCpu(); + + assertNotNull( hostCpu ); + + final List<Feature> hostCpuFeatures = hostCpu.getFeatures(); + assertNotNull( hostCpuFeatures ); + assertEquals( 25, hostCpuFeatures.size() ); + + final Feature hostCpuFeature = hostCpuFeatures.get( 9 ); + assertNotNull( hostCpuFeature ); + assertEquals( "vmx", hostCpuFeature.getName() ); + } + + @Test + @DisplayName( "Get empty host CPU features from libvirt XML capabilities file" ) + public void testGetHostCpuFeaturesEmpty() + { + final Capabilities caps = this.newCapabilitiesInstance( "qemu-kvm_capabilities_no-cpu-features.xml" ); + final Cpu hostCpu = caps.getHostCpu(); + + assertNotNull( hostCpu ); + + final List<Feature> hostCpuFeatures = hostCpu.getFeatures(); + assertNotNull( hostCpuFeatures ); + assertEquals( 0, hostCpuFeatures.size() ); + } + + @Test + @DisplayName( "Get host CPU pages from libvirt XML capabilities file" ) + public void testGetHostCpuPages() + { + final Capabilities caps = this.newCapabilitiesInstance( "qemu-kvm_capabilities_default.xml" ); + final Cpu hostCpu = caps.getHostCpu(); + + assertNotNull( hostCpu ); + + final List<Pages> hostCpuPages = hostCpu.getPages(); + assertNotNull( hostCpuPages ); + assertEquals( 3, hostCpuPages.size() ); + + final Pages hostCpuPage = hostCpuPages.get( 2 ); + assertNotNull( hostCpuPage ); + assertEquals( new BigInteger( "1073741824" ).toString(), hostCpuPage.getSize().toString() ); + } + + @Test + @DisplayName( "Get empty host CPU pages from libvirt XML capabilities file" ) + public void testGetHostCpuPagesEmpty() + { + final Capabilities caps = this.newCapabilitiesInstance( "qemu-kvm_capabilities_no-cpu-pages.xml" ); + final Cpu hostCpu = caps.getHostCpu(); + + assertNotNull( hostCpu ); + + final List<Pages> hostCpuPages = hostCpu.getPages(); + assertNotNull( hostCpuPages ); + assertEquals( 0, hostCpuPages.size() ); + } + + @Test + @DisplayName( "Get host IOMMU support from libvirt XML capabilities file" ) + public void testGetHostIommuSupport() + { + final Capabilities caps = this.newCapabilitiesInstance( "qemu-kvm_capabilities_default.xml" ); + + assertEquals( true, caps.hasHostIommuSupport() ); + } + + @Test + @DisplayName( "Get non-existent host IOMMU support from libvirt XML capabilities file" ) + public void testGetHostIommuSupportNonExistent() + { + final Capabilities caps = this.newCapabilitiesInstance( "qemu-kvm_capabilities_no-iommu.xml" ); + + assertEquals( false, caps.hasHostIommuSupport() ); + } + + @Test + @DisplayName( "Get guests from libvirt XML capabilities file" ) + public void testGetGuests() + { + final Capabilities caps = this.newCapabilitiesInstance( "qemu-kvm_capabilities_default.xml" ); + + final List<Guest> guests = caps.getGuests(); + assertNotNull( guests ); + assertEquals( 26, guests.size() ); + + final Guest guest = guests.get( 3 ); + assertNotNull( guest ); + assertEquals( OsType.HVM.toString(), guest.getOsType().toString() ); + assertEquals( "aarch64", guest.getArchName() ); + assertEquals( 64, guest.getArchWordSize() ); + assertEquals( "/usr/bin/qemu-system-aarch64", guest.getArchEmulator() ); + } + + @Test + @DisplayName( "Get empty guests from libvirt XML capabilities file" ) + public void testGetGuestsEmpty() + { + final Capabilities caps = this.newCapabilitiesInstance( "qemu-kvm_capabilities_no-guests.xml" ); + + final List<Guest> guests = caps.getGuests(); + assertNotNull( guests ); + assertEquals( 0, guests.size() ); + } + + @Test + @DisplayName( "Get guest machines from libvirt XML capabilities file" ) + public void testGetGuestMachines() + { + final Capabilities caps = this.newCapabilitiesInstance( "qemu-kvm_capabilities_default.xml" ); + + final List<Guest> guests = caps.getGuests(); + assertNotNull( guests ); + assertEquals( 26, guests.size() ); + + final Guest guest = guests.get( 3 ); + assertNotNull( guest ); + + final List<Machine> guestMachines = guest.getArchMachines(); + assertNotNull( guestMachines ); + assertEquals( 89, guestMachines.size() ); + + final Machine guestMachine = guestMachines.get( 5 ); + assertNotNull( guestMachine ); + assertNull( guestMachine.getCanonicalMachine() ); + assertEquals( 2, guestMachine.getMaxCpus() ); + assertEquals( "nuri", guestMachine.getName() ); + } + + @Test + @DisplayName( "Get empty guest machines from libvirt XML capabilities file" ) + public void testGetGuestMachinesEmpty() + { + final Capabilities caps = this.newCapabilitiesInstance( "qemu-kvm_capabilities_no-guest-machines.xml" ); + + final List<Guest> guests = caps.getGuests(); + assertNotNull( guests ); + assertEquals( 26, guests.size() ); + + final Guest guest = guests.get( 3 ); + assertNotNull( guest ); + + final List<Machine> guestMachines = guest.getArchMachines(); + assertNotNull( guestMachines ); + assertEquals( 0, guestMachines.size() ); + } + + @Test + @DisplayName( "Get canonical guest machine from libvirt XML capabilities file" ) + public void testGetGuestMachineCanonical() + { + final Capabilities caps = this.newCapabilitiesInstance( "qemu-kvm_capabilities_default.xml" ); + + final List<Guest> guests = caps.getGuests(); + assertNotNull( guests ); + assertEquals( 26, guests.size() ); + + final Guest guest = guests.get( 3 ); + assertNotNull( guest ); + + final List<Machine> guestMachines = guest.getArchMachines(); + assertNotNull( guestMachines ); + assertEquals( 89, guestMachines.size() ); + + final Machine guestMachine = guestMachines.get( 29 ); + assertNotNull( guestMachine ); + assertEquals( "virt-5.2", guestMachine.getCanonicalMachine() ); + assertEquals( 512, guestMachine.getMaxCpus() ); + assertEquals( "virt", guestMachine.getName() ); + } + + @Test + @DisplayName( "Get guest machine domains from libvirt XML capabilities file" ) + public void testGetGuestMachineDomains() + { + final Capabilities caps = this.newCapabilitiesInstance( "qemu-kvm_capabilities_default.xml" ); + + final List<Guest> guests = caps.getGuests(); + assertNotNull( guests ); + assertEquals( 26, guests.size() ); + + final Guest guest = guests.get( 5 ); + assertNotNull( guest ); + + final List<Domain> guestDomains = guest.getArchDomains(); + assertNotNull( guestDomains ); + assertEquals( 2, guestDomains.size() ); + + final Domain guestDomain = guestDomains.get( 1 ); + assertNotNull( guestDomain ); + assertEquals( Type.KVM, guestDomain.getType() ); + } + + @Test + @DisplayName( "Get empty guest machine domains from libvirt XML capabilities file" ) + public void testGetGuestMachineDomainsEmpty() + { + final Capabilities caps = this.newCapabilitiesInstance( "qemu-kvm_capabilities_no-guest-machines.xml" ); + + final List<Guest> guests = caps.getGuests(); + assertNotNull( guests ); + assertEquals( 26, guests.size() ); + + final Guest guest = guests.get( 3 ); + assertNotNull( guest ); + + final List<Domain> guestDomains = guest.getArchDomains(); + assertNotNull( guestDomains ); + assertEquals( 0, guestDomains.size() ); + } +} 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..f55c511 --- /dev/null +++ b/src/test/java/org/openslx/libvirt/domain/DomainTest.java @@ -0,0 +1,518 @@ +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.logging.log4j.Level; +import org.apache.logging.log4j.core.config.Configurator; +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.domain.Domain.OsType; +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 + Configurator.setRootLevel( Level.OFF ); + } + + public static Domain getDomain( String xmlFileName ) + { + Domain domain = null; + + try { + domain = new Domain( LibvirtXmlTestResources.getLibvirtXmlStream( 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 = DomainTest.getDomain( "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 = DomainTest.getDomain( "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 = DomainTest.getDomain( "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 = DomainTest.getDomain( "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 = DomainTest.getDomain( "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 = DomainTest.getDomain( "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 = DomainTest.getDomain( "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 = DomainTest.getDomain( "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 libosinfo operating system identifier in libvirt XML file" ) + public void testGetLibOsInfoOsId() + { + Domain vm = DomainTest.getDomain( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( "http://ubuntu.com/ubuntu/20.04", vm.getLibOsInfoOsId() ); + } + + @Test + @DisplayName( "Get VM UUID from libvirt XML file" ) + public void testGetUuid() + { + Domain vm = DomainTest.getDomain( "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 = DomainTest.getDomain( "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 = DomainTest.getDomain( "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 = DomainTest.getDomain( "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 = DomainTest.getDomain( "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 = DomainTest.getDomain( "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 = DomainTest.getDomain( "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 = DomainTest.getDomain( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + vm.setVCpu( 4 ); + assertEquals( 4, vm.getVCpu() ); + } + + @Test + @DisplayName( "Get VM's OS type from libvirt XML file" ) + public void testGetOsType() + { + Domain vm = DomainTest.getDomain( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( OsType.HVM.toString(), vm.getOsType().toString() ); + } + + @Test + @DisplayName( "Set VM's OS type in libvirt XML file" ) + public void testSetOsType() + { + Domain vm = DomainTest.getDomain( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + vm.setOsType( OsType.XEN ); + assertEquals( OsType.XEN.toString(), vm.getOsType().toString() ); + } + + @Test + @DisplayName( "Get VM's OS architecture from libvirt XML file" ) + public void testGetOsArch() + { + Domain vm = DomainTest.getDomain( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( "x86_64", vm.getOsArch() ); + } + + @Test + @DisplayName( "Set VM's OS architecture in libvirt XML file" ) + public void testSetOsArch() + { + Domain vm = DomainTest.getDomain( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + vm.setOsArch( "aarch" ); + assertEquals( "aarch", vm.getOsArch() ); + } + + @Test + @DisplayName( "Get VM's OS machine from libvirt XML file" ) + public void testGetOsMachine() + { + Domain vm = DomainTest.getDomain( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( "pc-q35-5.1", vm.getOsMachine() ); + } + + @Test + @DisplayName( "Set VM's OS machine in libvirt XML file" ) + public void testSetOsMachine() + { + Domain vm = DomainTest.getDomain( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + vm.setOsMachine( "pc" ); + assertEquals( "pc", vm.getOsMachine() ); + } + + @Test + @DisplayName( "Get VM's OS loader from libvirt XML file" ) + public void testGetOsLoader() + { + Domain vm = DomainTest.getDomain( "qemu-kvm_default-ubuntu-20-04-vm_uefi.xml" ); + assertEquals( "/usr/share/edk2-ovmf/x64/OVMF_CODE.fd", vm.getOsLoader() ); + } + + @Test + @DisplayName( "Set VM's OS loader in libvirt XML file" ) + public void testSetOsLoader() + { + Domain vm = DomainTest.getDomain( "qemu-kvm_default-ubuntu-20-04-vm_uefi.xml" ); + vm.setOsLoader( "/usr/share/qemu/edk2-x86_64-code.fd" ); + assertEquals( "/usr/share/qemu/edk2-x86_64-code.fd", vm.getOsLoader() ); + } + + @Test + @DisplayName( "Get VM's OS Nvram from libvirt XML file" ) + public void testGetOsNvram() + { + Domain vm = DomainTest.getDomain( "qemu-kvm_default-ubuntu-20-04-vm_uefi.xml" ); + assertEquals( "/var/lib/libvirt/nvram/guest_VARS.fd", vm.getOsNvram() ); + } + + @Test + @DisplayName( "Set VM's OS Nvram in libvirt XML file" ) + public void testSetOsNvram() + { + Domain vm = DomainTest.getDomain( "qemu-kvm_default-ubuntu-20-04-vm_uefi.xml" ); + vm.setOsNvram( "/tmp/nvram-tmp/tmp_VARS.fd" ); + assertEquals( "/tmp/nvram-tmp/tmp_VARS.fd", vm.getOsNvram() ); + } + + @Test + @DisplayName( "Get VM CPU model from libvirt XML file" ) + public void testGetCpuModel() + { + Domain vm = DomainTest.getDomain( "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 = DomainTest.getDomain( "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 = DomainTest.getDomain( "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 = DomainTest.getDomain( "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 = DomainTest.getDomain( "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 = DomainTest.getDomain( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + vm.setCpuCheck( CpuCheck.NONE ); + assertEquals( CpuCheck.NONE.toString(), vm.getCpuCheck().toString() ); + } + + @Test + @DisplayName( "Get VM CPU dies from libvirt XML file" ) + public void testGetCpuDies() + { + Domain vm = DomainTest.getDomain( "qemu-kvm_default-ubuntu-20-04-vm_cpu-topology.xml" ); + assertEquals( 2, vm.getCpuDies() ); + } + + @Test + @DisplayName( "Set VM CPU dies in libvirt XML file" ) + public void testSetCpuDies() + { + Domain vm = DomainTest.getDomain( "qemu-kvm_default-ubuntu-20-04-vm_cpu-topology.xml" ); + vm.setCpuDies( 3 ); + assertEquals( 3, vm.getCpuDies() ); + } + + @Test + @DisplayName( "Get VM CPU sockets from libvirt XML file" ) + public void testGetCpuSockets() + { + Domain vm = DomainTest.getDomain( "qemu-kvm_default-ubuntu-20-04-vm_cpu-topology.xml" ); + assertEquals( 3, vm.getCpuSockets() ); + } + + @Test + @DisplayName( "Set VM CPU sockets in libvirt XML file" ) + public void testSetCpuSockets() + { + Domain vm = DomainTest.getDomain( "qemu-kvm_default-ubuntu-20-04-vm_cpu-topology.xml" ); + vm.setCpuSockets( 2 ); + assertEquals( 2, vm.getCpuSockets() ); + } + + @Test + @DisplayName( "Get VM CPU cores from libvirt XML file" ) + public void testGetCpuCores() + { + Domain vm = DomainTest.getDomain( "qemu-kvm_default-ubuntu-20-04-vm_cpu-topology.xml" ); + assertEquals( 4, vm.getCpuCores() ); + } + + @Test + @DisplayName( "Set VM CPU cores in libvirt XML file" ) + public void testSetCpuCores() + { + Domain vm = DomainTest.getDomain( "qemu-kvm_default-ubuntu-20-04-vm_cpu-topology.xml" ); + vm.setCpuCores( 8 ); + assertEquals( 8, vm.getCpuCores() ); + } + + @Test + @DisplayName( "Get VM CPU threads from libvirt XML file" ) + public void testGetCpuThreads() + { + Domain vm = DomainTest.getDomain( "qemu-kvm_default-ubuntu-20-04-vm_cpu-topology.xml" ); + assertEquals( 1, vm.getCpuThreads() ); + } + + @Test + @DisplayName( "Set VM CPU threads in libvirt XML file" ) + public void testSetCpuThreads() + { + Domain vm = DomainTest.getDomain( "qemu-kvm_default-ubuntu-20-04-vm_cpu-topology.xml" ); + vm.setCpuThreads( 2 ); + assertEquals( 2, vm.getCpuThreads() ); + } + + @Test + @DisplayName( "Get VM emulator binary from libvirt XML file" ) + public void testGetDevicesEmulator() + { + Domain vm = DomainTest.getDomain( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( "/usr/bin/qemu-system-x86_64", vm.getDevicesEmulator() ); + } + + @Test + @DisplayName( "Set VM emulator binary in libvirt XML file" ) + public void testSetDevicesEmulator() + { + Domain vm = DomainTest.getDomain( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + vm.setDevicesEmulator( "/usr/bin/qemu-system-i386" ); + assertEquals( "/usr/bin/qemu-system-i386", vm.getDevicesEmulator() ); + } + + @Test + @DisplayName( "Get all VM devices from libvirt XML file" ) + public void testGetDevices() + { + Domain vm = DomainTest.getDomain( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( 22, vm.getDevices().size() ); + } + + @Test + @DisplayName( "Get all VM controller devices from libvirt XML file" ) + public void testGetControllerDevices() + { + Domain vm = DomainTest.getDomain( "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 = DomainTest.getDomain( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( 3, vm.getDiskDevices().size() ); + } + + @Test + @DisplayName( "Get all VM file system devices from libvirt XML file" ) + public void testGetFileSystemDevices() + { + Domain vm = DomainTest.getDomain( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( 0, vm.getFileSystemDevices().size() ); + } + + @Test + @DisplayName( "Get all VM hostdev devices from libvirt XML file" ) + public void testGetHostdevDevices() + { + Domain vm = DomainTest.getDomain( "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 = DomainTest.getDomain( "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 = DomainTest.getDomain( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( 1, vm.getGraphicDevices().size() ); + } + + @Test + @DisplayName( "Get all VM parallel port devices from libvirt XML file" ) + public void testGetParallelDevices() + { + Domain vm = DomainTest.getDomain( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( 0, vm.getParallelDevices().size() ); + } + + @Test + @DisplayName( "Get all VM serial port devices from libvirt XML file" ) + public void testGetSerialDevices() + { + Domain vm = DomainTest.getDomain( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( 1, vm.getSerialDevices().size() ); + } + + @Test + @DisplayName( "Get all VM sound devices from libvirt XML file" ) + public void testGetSoundDevices() + { + Domain vm = DomainTest.getDomain( "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 = DomainTest.getDomain( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( 1, vm.getVideoDevices().size() ); + } + + @Test + @DisplayName( "Get all QEMU command line arguments from libvirt XML file" ) + public void testGetQemuCmdlnArguments() + { + Domain vm = DomainTest.getDomain( "qemu-kvm_default-ubuntu-20-04-vm_qemu-cmdln.xml" ); + assertEquals( 2, vm.getQemuCmdlnArguments().size() ); + } + + @Test + @DisplayName( "Set QEMU command line arguments in libvirt XML file" ) + public void testAddQemuCmdlnArguments() + { + Domain vm = DomainTest.getDomain( "qemu-kvm_default-ubuntu-20-04-vm.xml" ); + assertEquals( 0, vm.getQemuCmdlnArguments().size() ); + + vm.addQemuCmdlnArgument( "-set" ); + vm.addQemuCmdlnArgument( "device.hostdev0.x-igd-opregion=on" ); + + assertEquals( 2, vm.getQemuCmdlnArguments().size() ); + } +} diff --git a/src/test/java/org/openslx/libvirt/domain/device/HostdevMdevDeviceAddressTest.java b/src/test/java/org/openslx/libvirt/domain/device/HostdevMdevDeviceAddressTest.java new file mode 100644 index 0000000..15e419e --- /dev/null +++ b/src/test/java/org/openslx/libvirt/domain/device/HostdevMdevDeviceAddressTest.java @@ -0,0 +1,71 @@ +package org.openslx.libvirt.domain.device; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.UUID; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +public class HostdevMdevDeviceAddressTest +{ + @Test + @DisplayName( "Test that a mediated device address instance is parsed from a valid String" ) + public void testHostdevMdevDeviceAddressValueOfValid() + { + final UUID deviceAddress = UUID.randomUUID(); + final HostdevMdevDeviceAddress mdevDeviceAddr = HostdevMdevDeviceAddress + .valueOf( deviceAddress.toString() ); + + assertNotNull( mdevDeviceAddr ); + assertEquals( deviceAddress, mdevDeviceAddr.getDeviceAddress() ); + assertEquals( deviceAddress.toString(), mdevDeviceAddr.getDeviceAddressAsString() ); + } + + @Test + @DisplayName( "Test that no mediated device address instance is parsed from an invalid String" ) + public void testHostdevMdevDeviceAddressValueOfInvalid() + { + final HostdevMdevDeviceAddress mdevDeviceAddr = HostdevMdevDeviceAddress.valueOf( "0xaffe" ); + + assertNull( mdevDeviceAddr ); + } + + @Test + @DisplayName( "Test that two mediated device address instances are equal" ) + public void testHostdevMdevDeviceAddressEquals() + { + final HostdevMdevDeviceAddress mdevDeviceAddr1 = new HostdevMdevDeviceAddress( + new UUID( 0xdeadaffe, 0xaffedead ) ); + final HostdevMdevDeviceAddress mdevDeviceAddr2 = new HostdevMdevDeviceAddress( + new UUID( 0xdeadaffe, 0xaffedead ) ); + + assertTrue( mdevDeviceAddr1.equals( mdevDeviceAddr2 ) ); + } + + @Test + @DisplayName( "Test that two mediated device address instances are not equal" ) + public void testHostdevMdevDeviceAddressNotEquals() + { + final HostdevMdevDeviceAddress mdevDeviceAddr1 = new HostdevMdevDeviceAddress( + new UUID( 0xdeadaffe, 0xaffedead ) ); + final HostdevMdevDeviceAddress mdevDeviceAddr2 = new HostdevMdevDeviceAddress( + new UUID( 0xaffedead, 0xdeadaffe ) ); + + assertFalse( mdevDeviceAddr1.equals( mdevDeviceAddr2 ) ); + } + + @Test + @DisplayName( "Test that a mediated device address can be dumped to a String" ) + public void testHostdevMdevDeviceAddressToString() + { + final UUID deviceAddr = UUID.randomUUID(); + final HostdevMdevDeviceAddress mdevDeviceAddr = new HostdevMdevDeviceAddress( deviceAddr ); + + assertEquals( deviceAddr.toString(), mdevDeviceAddr.toString() ); + } +} diff --git a/src/test/java/org/openslx/libvirt/domain/device/HostdevPciDeviceAddressTest.java b/src/test/java/org/openslx/libvirt/domain/device/HostdevPciDeviceAddressTest.java new file mode 100644 index 0000000..377e126 --- /dev/null +++ b/src/test/java/org/openslx/libvirt/domain/device/HostdevPciDeviceAddressTest.java @@ -0,0 +1,79 @@ +package org.openslx.libvirt.domain.device; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +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.assertTrue; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +public class HostdevPciDeviceAddressTest +{ + @Test + @DisplayName( "Test that a PCI device address instance is not created if invalid values are specified" ) + public void testHostdevPciDeviceAddressInstanceInvalid() + { + assertThrows( IllegalArgumentException.class, + () -> new HostdevPciDeviceAddress( Integer.MIN_VALUE, 0x03, 0x0a, 0x7 ) ); + assertThrows( IllegalArgumentException.class, + () -> new HostdevPciDeviceAddress( 0x72ab, 0x0c, 0x1a, Integer.MAX_VALUE ) ); + } + + @Test + @DisplayName( "Test that a PCI device address instance is parsed from a valid String" ) + public void testHostdevPciDeviceAddressValueOfValid() + { + final HostdevPciDeviceAddress pciDeviceAddr = HostdevPciDeviceAddress.valueOf( "002b:2a:1f.1" ); + + assertNotNull( pciDeviceAddr ); + assertEquals( 0x002b, pciDeviceAddr.getPciDomain() ); + assertEquals( "002b", pciDeviceAddr.getPciDomainAsString() ); + assertEquals( 0x2a, pciDeviceAddr.getPciBus() ); + assertEquals( "2a", pciDeviceAddr.getPciBusAsString() ); + assertEquals( 0x1f, pciDeviceAddr.getPciDevice() ); + assertEquals( "1f", pciDeviceAddr.getPciDeviceAsString() ); + assertEquals( 0x1, pciDeviceAddr.getPciFunction() ); + assertEquals( "1", pciDeviceAddr.getPciFunctionAsString() ); + } + + @Test + @DisplayName( "Test that no PCI device address instance is parsed from an invalid String" ) + public void testHostdevPciDeviceAddressValueOfInvalid() + { + final HostdevPciDeviceAddress pciDeviceAddr = HostdevPciDeviceAddress.valueOf( "0000b2ac1f31" ); + + assertNull( pciDeviceAddr ); + } + + @Test + @DisplayName( "Test that two PCI device address instances are equal" ) + public void testHostdevPciDeviceAddressEquals() + { + final HostdevPciDeviceAddress pciDeviceAddr1 = new HostdevPciDeviceAddress( 0x0000, 0x2a, 0x1f, 0x1 ); + final HostdevPciDeviceAddress pciDeviceAddr2 = new HostdevPciDeviceAddress( 0x0000, 0x2a, 0x1f, 0x1 ); + + assertTrue( pciDeviceAddr1.equals( pciDeviceAddr2 ) ); + } + + @Test + @DisplayName( "Test that two PCI device address instances are not equal" ) + public void testHostdevPciDeviceAddressNotEquals() + { + final HostdevPciDeviceAddress pciDeviceAddr1 = new HostdevPciDeviceAddress( 0x0000, 0x2a, 0x1f, 0x1 ); + final HostdevPciDeviceAddress pciDeviceAddr2 = new HostdevPciDeviceAddress( 0x0000, 0x2a, 0x1f, 0x2 ); + + assertFalse( pciDeviceAddr1.equals( pciDeviceAddr2 ) ); + } + + @Test + @DisplayName( "Test that a PCI device address can be dumped to a String" ) + public void testHostdevPciDeviceAddressToString() + { + final HostdevPciDeviceAddress pciDeviceAddr = new HostdevPciDeviceAddress( 0x0000, 0x2a, 0x1f, 0x1 ); + + assertEquals( "0000:2a:1f.1", pciDeviceAddr.toString() ); + } +} diff --git a/src/test/java/org/openslx/libvirt/domain/device/HostdevPciDeviceDescriptionTest.java b/src/test/java/org/openslx/libvirt/domain/device/HostdevPciDeviceDescriptionTest.java new file mode 100644 index 0000000..7a740ac --- /dev/null +++ b/src/test/java/org/openslx/libvirt/domain/device/HostdevPciDeviceDescriptionTest.java @@ -0,0 +1,75 @@ +package org.openslx.libvirt.domain.device; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +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.assertTrue; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +public class HostdevPciDeviceDescriptionTest +{ + @Test + @DisplayName( "Test that a PCI device description instance is not created if invalid values are specified" ) + public void testHostdevPciDeviceDescriptionInstanceInvalid() + { + assertThrows( IllegalArgumentException.class, + () -> new HostdevPciDeviceDescription( Integer.MIN_VALUE, 0x293a ) ); + assertThrows( IllegalArgumentException.class, + () -> new HostdevPciDeviceDescription( 0x8086, Integer.MAX_VALUE ) ); + } + + @Test + @DisplayName( "Test that a PCI device description instance is parsed from a valid String" ) + public void testHostdevPciDeviceDescriptionValueOfValid() + { + final HostdevPciDeviceDescription pciDeviceDesc = HostdevPciDeviceDescription.valueOf( "8086:293a" ); + + assertNotNull( pciDeviceDesc ); + assertEquals( 0x8086, pciDeviceDesc.getVendorId() ); + assertEquals( "8086", pciDeviceDesc.getVendorIdAsString() ); + assertEquals( 0x293a, pciDeviceDesc.getDeviceId() ); + assertEquals( "293a", pciDeviceDesc.getDeviceIdAsString() ); + } + + @Test + @DisplayName( "Test that no PCI device description instance is parsed from an invalid String" ) + public void testHostdevPciDeviceDescriptionValueOfInvalid() + { + final HostdevPciDeviceDescription pciDeviceDesc = HostdevPciDeviceDescription.valueOf( "bba93e215" ); + + assertNull( pciDeviceDesc ); + } + + @Test + @DisplayName( "Test that two PCI device description instances are equal" ) + public void testHostdevPciDeviceDescriptionEquals() + { + final HostdevPciDeviceDescription pciDeviceDesc1 = new HostdevPciDeviceDescription( 0x8086, 0x293a ); + final HostdevPciDeviceDescription pciDeviceDesc2 = new HostdevPciDeviceDescription( 0x8086, 0x293a ); + + assertTrue( pciDeviceDesc1.equals( pciDeviceDesc2 ) ); + } + + @Test + @DisplayName( "Test that two PCI device description instances are not equal" ) + public void testHostdevPciDeviceDescriptionNotEquals() + { + final HostdevPciDeviceDescription pciDeviceDesc1 = new HostdevPciDeviceDescription( 0x8086, 0x293a ); + final HostdevPciDeviceDescription pciDeviceDesc2 = new HostdevPciDeviceDescription( 0x293a, 0x8086 ); + + assertFalse( pciDeviceDesc1.equals( pciDeviceDesc2 ) ); + } + + @Test + @DisplayName( "Test that a PCI device description can be dumped to a String" ) + public void testHostdevPciDeviceDescriptionToString() + { + final HostdevPciDeviceDescription pciDeviceDesc = new HostdevPciDeviceDescription( 0x00b1, 0x293a ); + + assertEquals( "00b1:293a", pciDeviceDesc.toString() ); + } +} diff --git a/src/test/java/org/openslx/libvirt/libosinfo/LibOsInfoTest.java b/src/test/java/org/openslx/libvirt/libosinfo/LibOsInfoTest.java new file mode 100644 index 0000000..af1c611 --- /dev/null +++ b/src/test/java/org/openslx/libvirt/libosinfo/LibOsInfoTest.java @@ -0,0 +1,28 @@ +package org.openslx.libvirt.libosinfo; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.openslx.libvirt.libosinfo.os.Os; +import org.openslx.virtualization.Version; + +public class LibOsInfoTest +{ + @Test + @DisplayName( "Test the lookup of an operating system" ) + public void testOsLookup() + { + final String osId = "http://ubuntu.com/ubuntu/20.04"; + final Os os = LibOsInfo.lookupOs( osId ); + + assertNotNull( os ); + + assertEquals( osId, os.getId() ); + assertEquals( "Ubuntu 20.04", os.getName() ); + assertEquals( "linux", os.getFamily() ); + assertEquals( "ubuntu", os.getDistro() ); + assertEquals( new Version( Short.valueOf( "20" ), Short.valueOf( "04" ) ), os.getVersion() ); + } +} 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..56ceeed --- /dev/null +++ b/src/test/java/org/openslx/libvirt/xml/LibvirtXmlDocumentTest.java @@ -0,0 +1,280 @@ +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.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; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.core.config.Configurator; +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 + Configurator.setRootLevel( 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; + } + + 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 + { + 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(); + + assertNotNull( readXmlContent ); + + final long lengthReadXmlContent = LibvirtXmlDocumentTest.countLinesFromString( readXmlContent ); + final long lengthOriginalXmlContent = LibvirtXmlDocumentTest.countLinesFromFile( originalXmlFile ); + + 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 long lengthReadXmlContent = LibvirtXmlDocumentTest.countLinesFromString( readXmlContent ); + final long lengthOriginalXmlContent = LibvirtXmlDocumentTest.countLinesFromString( originalXmlContent ); + + 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 = () -> { + LibvirtXmlDocument doc = this.newLibvirtXmlDocumentValidationInstance( "qemu-kvm_default-ubuntu-20-04-vm-invalid.xml", "domain.rng" ); + doc.validateXml(); + }; + + 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..20d2376 --- /dev/null +++ b/src/test/java/org/openslx/libvirt/xml/LibvirtXmlTestResources.java @@ -0,0 +1,40 @@ +package org.openslx.libvirt.xml; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; + +import org.openslx.util.Resources; + +public final class LibvirtXmlTestResources +{ + private static final String LIBVIRT_PREFIX_PATH = Resources.PATH_SEPARATOR + "libvirt"; + private static final String LIBVIRT_PREFIX_PATH_XML = LIBVIRT_PREFIX_PATH + Resources.PATH_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 + Resources.PATH_SEPARATOR + + libvirtXmlFileName; + URL libvirtXml = LibvirtXmlTestResources.class.getResource( libvirtXmlPath ); + return new File( libvirtXml.getFile() ); + } + + public static InputStream getLibvirtXmlStream( String libvirtXmlFileName ) + { + String libvirtXmlPath = LibvirtXmlTestResources.LIBVIRT_PREFIX_PATH_XML + Resources.PATH_SEPARATOR + + libvirtXmlFileName; + return LibvirtXmlTestResources.class.getResourceAsStream( libvirtXmlPath ); + } + + public static File createLibvirtXmlTempFile() throws IOException + { + File tempFile = File.createTempFile( LibvirtXmlTestResources.LIBVIRT_TEMP_PREFIX, + LibvirtXmlTestResources.LIBVIRT_TEMP_SUFFIX ); + tempFile.deleteOnExit(); + return tempFile; + } +} |