From 4f9e5f2dafeb8a379b38873d18203e3d1b77bc74 Mon Sep 17 00:00:00 2001 From: Manuel Bentele Date: Tue, 16 Feb 2021 09:15:46 +0100 Subject: [qemu] Add unit tests for run-virt plugin's command line argument parser --- .../plugin/qemu/cmdln/CommandLineArgsTest.java | 548 +++++++++++++++++++++ 1 file changed, 548 insertions(+) create mode 100644 core/modules/qemu/runvirt-plugin-qemu/src/test/java/org/openslx/runvirt/plugin/qemu/cmdln/CommandLineArgsTest.java diff --git a/core/modules/qemu/runvirt-plugin-qemu/src/test/java/org/openslx/runvirt/plugin/qemu/cmdln/CommandLineArgsTest.java b/core/modules/qemu/runvirt-plugin-qemu/src/test/java/org/openslx/runvirt/plugin/qemu/cmdln/CommandLineArgsTest.java new file mode 100644 index 00000000..1399f9a4 --- /dev/null +++ b/core/modules/qemu/runvirt-plugin-qemu/src/test/java/org/openslx/runvirt/plugin/qemu/cmdln/CommandLineArgsTest.java @@ -0,0 +1,548 @@ +package org.openslx.runvirt.plugin.qemu.cmdln; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.File; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.openslx.runvirt.plugin.qemu.cmdln.CommandLineArgs.CmdLnOption; + +public class CommandLineArgsTest +{ + // @formatter:off + private static final String CMDLN_PREFIX_OPTION_SHORT = "-"; + private static final String CMDLN_PREFIX_OPTION_LONG = "--"; + + private static final String CMDLN_TEST_NAME = "test"; + private static final String CMDLN_TEST_FILENAME = System.getProperty( "user.dir" ) + File.separator + CMDLN_TEST_NAME; + private static final String CMDLN_TEST_UUID = "c9570672-cbae-4cbd-801a-881b281b8d79"; + private static final String CMDLN_TEST_OS = "Windows 10 (x64)"; + private static final int CMDLN_TEST_NCPUS = 4; + private static final String CMDLN_TEST_MEM = "1024"; + private static final String CMDLN_TEST_PARPORT = "/dev/parport0"; + private static final String CMDLN_TEST_SERPORT = "/dev/ttyS0"; + private static final String CMDLN_TEST_MAC = "02:42:8e:77:1b:e6"; + private static final String CMDLN_TEST_AUDIO_MODEL = "sb16"; + // @formatter:on + + @Test + @DisplayName( "Test the parsing of wrong command line options" ) + public void testCmdlnOptionsIncorrect() + { + final String[] args = { + CMDLN_PREFIX_OPTION_SHORT + "hello", + "world argument", + CMDLN_PREFIX_OPTION_LONG + "info", + "description" + }; + + CommandLineArgs cmdLn = new CommandLineArgs(); + + assertThrows( CommandLineArgsException.class, () -> cmdLn.parseCmdLnArgs( args ) ); + } + + @Test + @DisplayName( "Test the parsing of the help command line option (short version)" ) + public void testCmdlnOptionHelpShort() throws CommandLineArgsException + { + final String[] args = { + CMDLN_PREFIX_OPTION_SHORT + CmdLnOption.HELP.getShortOption() + }; + + CommandLineArgs cmdLn = new CommandLineArgs( args ); + + assertTrue( cmdLn.isHelpAquired() ); + } + + @Test + @DisplayName( "Test the parsing of the help command line option (long version)" ) + public void testCmdlnOptionHelpLong() throws CommandLineArgsException + { + final String[] args = { + CMDLN_PREFIX_OPTION_LONG + CmdLnOption.HELP.getLongOption() + }; + + CommandLineArgs cmdLn = new CommandLineArgs( args ); + + assertTrue( cmdLn.isHelpAquired() ); + } + + @Test + @DisplayName( "Test the parsing of the VM configuration input command line option (short version)" ) + public void testCmdlnOptionVmCfgInpShort() throws CommandLineArgsException + { + final String[] args = { + CMDLN_PREFIX_OPTION_SHORT + CmdLnOption.VM_CFGINP.getShortOption(), + CMDLN_TEST_FILENAME + }; + + CommandLineArgs cmdLn = new CommandLineArgs( args ); + + assertEquals( CMDLN_TEST_FILENAME, cmdLn.getVmCfgInpFileName() ); + } + + @Test + @DisplayName( "Test the parsing of the VM configuration input command line option (long version)" ) + public void testCmdlnOptionVmCfgInpLong() throws CommandLineArgsException + { + final String[] args = { + CMDLN_PREFIX_OPTION_LONG + CmdLnOption.VM_CFGINP.getLongOption(), + CMDLN_TEST_FILENAME + }; + + CommandLineArgs cmdLn = new CommandLineArgs( args ); + + assertEquals( CMDLN_TEST_FILENAME, cmdLn.getVmCfgInpFileName() ); + } + + @Test + @DisplayName( "Test the parsing of the VM configuration output command line option (short version)" ) + public void testCmdlnOptionVmCfgOutShort() throws CommandLineArgsException + { + final String[] args = { + CMDLN_PREFIX_OPTION_SHORT + CmdLnOption.VM_CFGOUT.getShortOption(), + CMDLN_TEST_FILENAME + }; + + CommandLineArgs cmdLn = new CommandLineArgs( args ); + + assertEquals( CMDLN_TEST_FILENAME, cmdLn.getVmCfgOutFileName() ); + } + + @Test + @DisplayName( "Test the parsing of the VM configuration output command line option (long version)" ) + public void testCmdlnOptionVmCfgOutLong() throws CommandLineArgsException + { + final String[] args = { + CMDLN_PREFIX_OPTION_LONG + CmdLnOption.VM_CFGOUT.getLongOption(), + CMDLN_TEST_FILENAME + }; + + CommandLineArgs cmdLn = new CommandLineArgs( args ); + + assertEquals( CMDLN_TEST_FILENAME, cmdLn.getVmCfgOutFileName() ); + } + + @Test + @DisplayName( "Test the parsing of the VM name command line option (short version)" ) + public void testCmdlnOptionVmNameShort() throws CommandLineArgsException + { + final String[] args = { + CMDLN_PREFIX_OPTION_SHORT + CmdLnOption.VM_NAME.getShortOption(), + CMDLN_TEST_NAME + }; + + CommandLineArgs cmdLn = new CommandLineArgs( args ); + + assertEquals( CMDLN_TEST_NAME, cmdLn.getVmName() ); + } + + @Test + @DisplayName( "Test the parsing of the VM name command line option (long version)" ) + public void testCmdlnOptionVmNameLong() throws CommandLineArgsException + { + final String[] args = { + CMDLN_PREFIX_OPTION_LONG + CmdLnOption.VM_NAME.getLongOption(), + CMDLN_TEST_NAME + }; + + CommandLineArgs cmdLn = new CommandLineArgs( args ); + + assertEquals( CMDLN_TEST_NAME, cmdLn.getVmName() ); + } + + @Test + @DisplayName( "Test the parsing of the VM UUID command line option (short version)" ) + public void testCmdlnOptionVmUuidShort() throws CommandLineArgsException + { + final String[] args = { + CMDLN_PREFIX_OPTION_SHORT + CmdLnOption.VM_UUID.getShortOption(), + CMDLN_TEST_UUID + }; + + CommandLineArgs cmdLn = new CommandLineArgs( args ); + + assertEquals( CMDLN_TEST_UUID, cmdLn.getVmUuid() ); + } + + @Test + @DisplayName( "Test the parsing of the VM UUID command line option (long version)" ) + public void testCmdlnOptionVmUuidLong() throws CommandLineArgsException + { + final String[] args = { + CMDLN_PREFIX_OPTION_LONG + CmdLnOption.VM_UUID.getLongOption(), + CMDLN_TEST_UUID + }; + + CommandLineArgs cmdLn = new CommandLineArgs( args ); + + assertEquals( CMDLN_TEST_UUID, cmdLn.getVmUuid() ); + } + + @Test + @DisplayName( "Test the parsing of the VM display name command line option (short version)" ) + public void testCmdlnOptionVmDsplNameShort() throws CommandLineArgsException + { + final String[] args = { + CMDLN_PREFIX_OPTION_SHORT + CmdLnOption.VM_DSPLNAME.getShortOption(), + CMDLN_TEST_NAME + }; + + CommandLineArgs cmdLn = new CommandLineArgs( args ); + + assertEquals( CMDLN_TEST_NAME, cmdLn.getVmDisplayName() ); + } + + @Test + @DisplayName( "Test the parsing of the VM display name command line option (long version)" ) + public void testCmdlnOptionVmDsplNameLong() throws CommandLineArgsException + { + final String[] args = { + CMDLN_PREFIX_OPTION_LONG + CmdLnOption.VM_DSPLNAME.getLongOption(), + CMDLN_TEST_NAME + }; + + CommandLineArgs cmdLn = new CommandLineArgs( args ); + + assertEquals( CMDLN_TEST_NAME, cmdLn.getVmDisplayName() ); + } + + @Test + @DisplayName( "Test the parsing of the VM OS command line option (short version)" ) + public void testCmdlnOptionVmOsShort() throws CommandLineArgsException + { + final String[] args = { + CMDLN_PREFIX_OPTION_SHORT + CmdLnOption.VM_OS.getShortOption(), + CMDLN_TEST_OS + }; + + CommandLineArgs cmdLn = new CommandLineArgs( args ); + + assertEquals( CMDLN_TEST_OS, cmdLn.getVmOperatingSystem() ); + } + + @Test + @DisplayName( "Test the parsing of the VM OS command line option (long version)" ) + public void testCmdlnOptionVmOsLong() throws CommandLineArgsException + { + final String[] args = { + CMDLN_PREFIX_OPTION_LONG + CmdLnOption.VM_OS.getLongOption(), + CMDLN_TEST_OS + }; + + CommandLineArgs cmdLn = new CommandLineArgs( args ); + + assertEquals( CMDLN_TEST_OS, cmdLn.getVmOperatingSystem() ); + } + + @Test + @DisplayName( "Test the parsing of the VM CPU number command line option (short version)" ) + public void testCmdlnOptionVmNCpusShort() throws CommandLineArgsException + { + final String[] args = { + CMDLN_PREFIX_OPTION_SHORT + CmdLnOption.VM_NCPUS.getShortOption(), + Integer.toString( CMDLN_TEST_NCPUS ) + }; + + CommandLineArgs cmdLn = new CommandLineArgs( args ); + + assertEquals( CMDLN_TEST_NCPUS, cmdLn.getVmNumCpus() ); + } + + @Test + @DisplayName( "Test the parsing of the VM CPU number command line option (long version)" ) + public void testCmdlnOptionVmNCpusLong() throws CommandLineArgsException + { + final String[] args = { + CMDLN_PREFIX_OPTION_LONG + CmdLnOption.VM_NCPUS.getLongOption(), + Integer.toString( CMDLN_TEST_NCPUS ) + }; + + CommandLineArgs cmdLn = new CommandLineArgs( args ); + + assertEquals( CMDLN_TEST_NCPUS, cmdLn.getVmNumCpus() ); + } + + @Test + @DisplayName( "Test the parsing of the VM memory command line option (short version)" ) + public void testCmdlnOptionVmMemShort() throws CommandLineArgsException + { + final String[] args = { + CMDLN_PREFIX_OPTION_SHORT + CmdLnOption.VM_MEM.getShortOption(), + CMDLN_TEST_MEM + }; + + CommandLineArgs cmdLn = new CommandLineArgs( args ); + + assertEquals( CMDLN_TEST_MEM, cmdLn.getVmMemory() ); + } + + @Test + @DisplayName( "Test the parsing of the VM memory command line option (long version)" ) + public void testCmdlnOptionVmMemLong() throws CommandLineArgsException + { + final String[] args = { + CMDLN_PREFIX_OPTION_LONG + CmdLnOption.VM_MEM.getLongOption(), + CMDLN_TEST_MEM + }; + + CommandLineArgs cmdLn = new CommandLineArgs( args ); + + assertEquals( CMDLN_TEST_MEM, cmdLn.getVmMemory() ); + } + + @Test + @DisplayName( "Test the parsing of the VM first HDD disk image command line option (short version)" ) + public void testCmdlnOptionVmHdd0Short() throws CommandLineArgsException + { + final String[] args = { + CMDLN_PREFIX_OPTION_SHORT + CmdLnOption.VM_HDD0.getShortOption(), + CMDLN_TEST_FILENAME + }; + + CommandLineArgs cmdLn = new CommandLineArgs( args ); + + assertEquals( CMDLN_TEST_FILENAME, cmdLn.getVmDiskFileNameHDD0() ); + } + + @Test + @DisplayName( "Test the parsing of the VM first HDD disk image command line option (long version)" ) + public void testCmdlnOptionVmHdd0Long() throws CommandLineArgsException + { + final String[] args = { + CMDLN_PREFIX_OPTION_LONG + CmdLnOption.VM_HDD0.getLongOption(), + CMDLN_TEST_FILENAME + }; + + CommandLineArgs cmdLn = new CommandLineArgs( args ); + + assertEquals( CMDLN_TEST_FILENAME, cmdLn.getVmDiskFileNameHDD0() ); + } + + @Test + @DisplayName( "Test the parsing of the VM first floppy disk image command line option (short version)" ) + public void testCmdlnOptionVmFloppy0Short() throws CommandLineArgsException + { + final String[] args = { + CMDLN_PREFIX_OPTION_SHORT + CmdLnOption.VM_FLOPPY0.getShortOption(), + CMDLN_TEST_FILENAME + }; + + CommandLineArgs cmdLn = new CommandLineArgs( args ); + + assertEquals( CMDLN_TEST_FILENAME, cmdLn.getVmDiskFileNameFloppy0() ); + } + + @Test + @DisplayName( "Test the parsing of the VM first floppy disk image command line option (long version)" ) + public void testCmdlnOptionVmFloppy0Long() throws CommandLineArgsException + { + final String[] args = { + CMDLN_PREFIX_OPTION_LONG + CmdLnOption.VM_FLOPPY0.getLongOption(), + CMDLN_TEST_FILENAME + }; + + CommandLineArgs cmdLn = new CommandLineArgs( args ); + + assertEquals( CMDLN_TEST_FILENAME, cmdLn.getVmDiskFileNameFloppy0() ); + } + + @Test + @DisplayName( "Test the parsing of the VM second floppy disk image command line option (short version)" ) + public void testCmdlnOptionVmFloppy1Short() throws CommandLineArgsException + { + final String[] args = { + CMDLN_PREFIX_OPTION_SHORT + CmdLnOption.VM_FLOPPY1.getShortOption(), + CMDLN_TEST_FILENAME + }; + + CommandLineArgs cmdLn = new CommandLineArgs( args ); + + assertEquals( CMDLN_TEST_FILENAME, cmdLn.getVmDiskFileNameFloppy1() ); + } + + @Test + @DisplayName( "Test the parsing of the VM second floppy disk image command line option (long version)" ) + public void testCmdlnOptionVmFloppy1Long() throws CommandLineArgsException + { + final String[] args = { + CMDLN_PREFIX_OPTION_LONG + CmdLnOption.VM_FLOPPY1.getLongOption(), + CMDLN_TEST_FILENAME + }; + + CommandLineArgs cmdLn = new CommandLineArgs( args ); + + assertEquals( CMDLN_TEST_FILENAME, cmdLn.getVmDiskFileNameFloppy1() ); + } + + @Test + @DisplayName( "Test the parsing of the VM first CDROM disk image command line option (short version)" ) + public void testCmdlnOptionVmCdrom0Short() throws CommandLineArgsException + { + final String[] args = { + CMDLN_PREFIX_OPTION_SHORT + CmdLnOption.VM_CDROM0.getShortOption(), + CMDLN_TEST_FILENAME + }; + + CommandLineArgs cmdLn = new CommandLineArgs( args ); + + assertEquals( CMDLN_TEST_FILENAME, cmdLn.getVmDiskFileNameCdrom0() ); + } + + @Test + @DisplayName( "Test the parsing of the VM first CDROM disk image command line option (long version)" ) + public void testCmdlnOptionVmCdrom0Long() throws CommandLineArgsException + { + final String[] args = { + CMDLN_PREFIX_OPTION_LONG + CmdLnOption.VM_CDROM0.getLongOption(), + CMDLN_TEST_FILENAME + }; + + CommandLineArgs cmdLn = new CommandLineArgs( args ); + + assertEquals( CMDLN_TEST_FILENAME, cmdLn.getVmDiskFileNameCdrom0() ); + } + + @Test + @DisplayName( "Test the parsing of the VM second CDROM disk image command line option (short version)" ) + public void testCmdlnOptionVmCdrom1Short() throws CommandLineArgsException + { + final String[] args = { + CMDLN_PREFIX_OPTION_SHORT + CmdLnOption.VM_CDROM1.getShortOption(), + CMDLN_TEST_FILENAME + }; + + CommandLineArgs cmdLn = new CommandLineArgs( args ); + + assertEquals( CMDLN_TEST_FILENAME, cmdLn.getVmDiskFileNameCdrom1() ); + } + + @Test + @DisplayName( "Test the parsing of the VM second CDROM disk image command line option (long version)" ) + public void testCmdlnOptionVmCdrom1Long() throws CommandLineArgsException + { + final String[] args = { + CMDLN_PREFIX_OPTION_LONG + CmdLnOption.VM_CDROM1.getLongOption(), + CMDLN_TEST_FILENAME + }; + + CommandLineArgs cmdLn = new CommandLineArgs( args ); + + assertEquals( CMDLN_TEST_FILENAME, cmdLn.getVmDiskFileNameCdrom1() ); + } + + @Test + @DisplayName( "Test the parsing of the VM first parallel interface command line option (short version)" ) + public void testCmdlnOptionVmParallel0Short() throws CommandLineArgsException + { + final String[] args = { + CMDLN_PREFIX_OPTION_SHORT + CmdLnOption.VM_PARALLEL0.getShortOption(), + CMDLN_TEST_PARPORT + }; + + CommandLineArgs cmdLn = new CommandLineArgs( args ); + + assertEquals( CMDLN_TEST_PARPORT, cmdLn.getVmDeviceParallel0() ); + } + + @Test + @DisplayName( "Test the parsing of the VM first parallel interface command line option (long version)" ) + public void testCmdlnOptionVmParallel0Long() throws CommandLineArgsException + { + final String[] args = { + CMDLN_PREFIX_OPTION_LONG + CmdLnOption.VM_PARALLEL0.getLongOption(), + CMDLN_TEST_PARPORT + }; + + CommandLineArgs cmdLn = new CommandLineArgs( args ); + + assertEquals( CMDLN_TEST_PARPORT, cmdLn.getVmDeviceParallel0() ); + } + + @Test + @DisplayName( "Test the parsing of the VM first serial interface command line option (short version)" ) + public void testCmdlnOptionVmSerial0Short() throws CommandLineArgsException + { + final String[] args = { + CMDLN_PREFIX_OPTION_SHORT + CmdLnOption.VM_SERIAL0.getShortOption(), + CMDLN_TEST_SERPORT + }; + + CommandLineArgs cmdLn = new CommandLineArgs( args ); + + assertEquals( CMDLN_TEST_SERPORT, cmdLn.getVmDeviceSerial0() ); + } + + @Test + @DisplayName( "Test the parsing of the VM first serial interface command line option (long version)" ) + public void testCmdlnOptionVmSerial0Long() throws CommandLineArgsException + { + final String[] args = { + CMDLN_PREFIX_OPTION_LONG + CmdLnOption.VM_SERIAL0.getLongOption(), + CMDLN_TEST_SERPORT + }; + + CommandLineArgs cmdLn = new CommandLineArgs( args ); + + assertEquals( CMDLN_TEST_SERPORT, cmdLn.getVmDeviceSerial0() ); + } + + @Test + @DisplayName( "Test the parsing of the VM first network interface MAC command line option (short version)" ) + public void testCmdlnOptionVmMac0Short() throws CommandLineArgsException + { + final String[] args = { + CMDLN_PREFIX_OPTION_SHORT + CmdLnOption.VM_MAC0.getShortOption(), + CMDLN_TEST_MAC + }; + + CommandLineArgs cmdLn = new CommandLineArgs( args ); + + assertEquals( CMDLN_TEST_MAC, cmdLn.getVmMacAddress0() ); + } + + @Test + @DisplayName( "Test the parsing of the VM first network interface MAC command line option (long version)" ) + public void testCmdlnOptionVmMac0Long() throws CommandLineArgsException + { + final String[] args = { + CMDLN_PREFIX_OPTION_LONG + CmdLnOption.VM_MAC0.getLongOption(), + CMDLN_TEST_MAC + }; + + CommandLineArgs cmdLn = new CommandLineArgs( args ); + + assertEquals( CMDLN_TEST_MAC, cmdLn.getVmMacAddress0() ); + } + + @Test + @DisplayName( "Test the parsing of the VM first sound card type command line option (short version)" ) + public void testCmdlnOptionVmAudio0Short() throws CommandLineArgsException + { + final String[] args = { + CMDLN_PREFIX_OPTION_SHORT + CmdLnOption.VM_AUDIO0.getShortOption(), + CMDLN_TEST_AUDIO_MODEL + }; + + CommandLineArgs cmdLn = new CommandLineArgs( args ); + + assertEquals( CMDLN_TEST_AUDIO_MODEL, cmdLn.getVmModelSoundCard0() ); + } + + @Test + @DisplayName( "Test the parsing of the VM first sound card type command line option (long version)" ) + public void testCmdlnOptionVmAudio0Long() throws CommandLineArgsException + { + final String[] args = { + CMDLN_PREFIX_OPTION_LONG + CmdLnOption.VM_AUDIO0.getLongOption(), + CMDLN_TEST_AUDIO_MODEL + }; + + CommandLineArgs cmdLn = new CommandLineArgs( args ); + + assertEquals( CMDLN_TEST_AUDIO_MODEL, cmdLn.getVmModelSoundCard0() ); + } +} -- cgit v1.2.3-55-g7522