From 0d48a4b54de060725e3337af5664e94ba4f1a3ae Mon Sep 17 00:00:00 2001 From: Manuel Bentele Date: Fri, 12 Feb 2021 12:07:24 +0100 Subject: [qemu] Add unit tests for run-virt plugin's help option --- core/modules/qemu/runvirt-plugin-qemu/pom.xml | 6 + .../java/org/openslx/runvirt/plugin/qemu/App.java | 9 +- .../org/openslx/runvirt/plugin/qemu/AppTest.java | 186 ++++++++++++++++++++- 3 files changed, 194 insertions(+), 7 deletions(-) diff --git a/core/modules/qemu/runvirt-plugin-qemu/pom.xml b/core/modules/qemu/runvirt-plugin-qemu/pom.xml index 45b8f1d5..f744c4f3 100644 --- a/core/modules/qemu/runvirt-plugin-qemu/pom.xml +++ b/core/modules/qemu/runvirt-plugin-qemu/pom.xml @@ -47,6 +47,12 @@ 1.7.25 compile + + com.ginsberg + junit5-system-exit + 1.0.0 + test + diff --git a/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/App.java b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/App.java index fb72f18a..8a70be33 100644 --- a/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/App.java +++ b/core/modules/qemu/runvirt-plugin-qemu/src/main/java/org/openslx/runvirt/plugin/qemu/App.java @@ -18,17 +18,17 @@ public class App /** * Stores name of the run-virt QEMU plugin (command line tool). */ - private static final String APP_NAME = "run-virt QEMU plugin"; + public static final String APP_NAME = "run-virt QEMU plugin"; /** * Stores description of the run-virt QEMU plugin (command line tool). */ - private static final String APP_DESC = "Finalize a Libvirt VM (domain XML) configuration and manage the VM."; + public static final String APP_DESC = "Finalize a Libvirt VM (domain XML) configuration and manage the VM."; /** * Stores additional information for the run-virt QEMU plugin (command line tool). */ - private static final String APP_INFO = "The " + APP_NAME + " is part of the bwLehrpool infrastructure."; + public static final String APP_INFO = "The " + APP_NAME + " is part of the bwLehrpool infrastructure."; /** * Instance of a logger to log messages. @@ -64,6 +64,9 @@ public class App // print command line arguments for debugging purposes App.printCmdLnArgs( cmdLn ); + + // return with successful exit code + System.exit( 0 ); } /** diff --git a/core/modules/qemu/runvirt-plugin-qemu/src/test/java/org/openslx/runvirt/plugin/qemu/AppTest.java b/core/modules/qemu/runvirt-plugin-qemu/src/test/java/org/openslx/runvirt/plugin/qemu/AppTest.java index 110fb639..126fd26d 100644 --- a/core/modules/qemu/runvirt-plugin-qemu/src/test/java/org/openslx/runvirt/plugin/qemu/AppTest.java +++ b/core/modules/qemu/runvirt-plugin-qemu/src/test/java/org/openslx/runvirt/plugin/qemu/AppTest.java @@ -1,24 +1,202 @@ package org.openslx.runvirt.plugin.qemu; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + 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.Nested; import org.junit.jupiter.api.Test; +import org.openslx.runvirt.plugin.qemu.cmdln.CommandLineArgs.CmdLnOption; + +import com.ginsberg.junit.exit.ExpectSystemExit; +import com.ginsberg.junit.exit.ExpectSystemExitWithStatus; public class AppTest { @BeforeAll - public static void setUp() + private static void setUp() { // disable logging with log4j LogManager.getRootLogger().setLevel( Level.OFF ); } - @Test - public void shouldAnswerWithTrue() + @Nested + public class CmdLnTest { - assertTrue( true ); + private final ByteArrayOutputStream out = new ByteArrayOutputStream(); + private final ByteArrayOutputStream err = new ByteArrayOutputStream(); + + private void setUp() + { + // redirect output and error stream content + System.setOut( new PrintStream( this.out ) ); + System.setErr( new PrintStream( this.err ) ); + } + + @Test + @DisplayName( "Test ouput of correct 'help' command line option (short version)" ) + @ExpectSystemExit + public void testCmdLnOptionHelpShortCorrect() + { + String[] argsShortHelpOptionCorrect = { "-" + CmdLnOption.HELP.getShortOption() }; + + this.setUp(); + + // test correct usage of the short help option + try { + App.main( argsShortHelpOptionCorrect ); + } catch ( Exception e ) { + // do nothing and check output afterwards + } + + final String shortHelpOptionCorrectOutput = new String( this.out.toString() ); + final String shortHelpOptionCorrectErrOutput = new String( this.err.toString() ); + assertTrue( shortHelpOptionCorrectOutput.contains( "usage" ) ); + assertTrue( shortHelpOptionCorrectOutput.contains( App.APP_NAME ) ); + assertTrue( shortHelpOptionCorrectOutput.contains( App.APP_INFO ) ); + assertTrue( shortHelpOptionCorrectOutput.contains( App.APP_DESC ) ); + + // test that no error was logged and output is available + assertEquals( 1641, shortHelpOptionCorrectOutput.length() ); + assertEquals( 0, shortHelpOptionCorrectErrOutput.length() ); + } + + @Test + @DisplayName( "Test ouput of correct 'help' command line option (long version)" ) + @ExpectSystemExit + public void testCmdLnOptionHelpLongCorrect() + { + String[] argsLongHelpOptionCorrect = { "--" + CmdLnOption.HELP.getLongOption() }; + + this.setUp(); + + // test correct usage of the long help option + try { + App.main( argsLongHelpOptionCorrect ); + } catch ( Exception e ) { + // do nothing and check output afterwards + } + + final String longHelpOptionCorrectOutput = this.out.toString(); + final String longHelpOptionCorrectErrOutput = this.err.toString(); + assertTrue( longHelpOptionCorrectOutput.contains( "usage" ) ); + assertTrue( longHelpOptionCorrectOutput.contains( App.APP_NAME ) ); + assertTrue( longHelpOptionCorrectOutput.contains( App.APP_INFO ) ); + assertTrue( longHelpOptionCorrectOutput.contains( App.APP_DESC ) ); + + // test that no error was logged and output is available + assertEquals( 1641, longHelpOptionCorrectOutput.length() ); + assertEquals( 0, longHelpOptionCorrectErrOutput.length() ); + } + + @Test + @DisplayName( "Test ouput of incorrect 'help' command line option (short version)" ) + @ExpectSystemExit + public void testCmdLnOptionHelpShortIncorrect() + { + String[] argsShortHelpOptionIncorrect = { "---" + CmdLnOption.HELP.getShortOption() }; + + this.setUp(); + + // test incorrect usage of the short help option + try { + App.main( argsShortHelpOptionIncorrect ); + } catch ( Exception e ) { + // do nothing and check output afterwards + } + + final String shortHelpOptionIncorrectOutput = this.out.toString(); + final String shortHelpOptionIncorrectErrOutput = this.err.toString(); + assertTrue( shortHelpOptionIncorrectOutput.contains( "usage" ) ); + assertTrue( shortHelpOptionIncorrectOutput.contains( App.APP_NAME ) ); + assertTrue( shortHelpOptionIncorrectOutput.contains( App.APP_INFO ) ); + assertTrue( shortHelpOptionIncorrectOutput.contains( App.APP_DESC ) ); + + // test that error was logged and output is available + assertEquals( 1641, shortHelpOptionIncorrectOutput.length() ); + assertEquals( 0, shortHelpOptionIncorrectErrOutput.length() ); + } + + @Test + @DisplayName( "Test ouput of incorrect 'help' command line option (long version)" ) + @ExpectSystemExit + public void testCmdLnOptionHelpLongIncorrect() + { + String[] argsLongHelpOptionIncorrect = { "---" + CmdLnOption.HELP.getLongOption() }; + + this.setUp(); + + // test incorrect usage of the long help option + try { + App.main( argsLongHelpOptionIncorrect ); + } catch ( Exception e ) { + // do nothing and check output afterwards + } + + final String longHelpOptionIncorrectOutput = this.out.toString(); + final String longHelpOptionIncorrectErrOutput = this.err.toString(); + assertTrue( longHelpOptionIncorrectOutput.contains( "usage" ) ); + assertTrue( longHelpOptionIncorrectOutput.contains( App.APP_NAME ) ); + assertTrue( longHelpOptionIncorrectOutput.contains( App.APP_INFO ) ); + assertTrue( longHelpOptionIncorrectOutput.contains( App.APP_DESC ) ); + + // test that error was logged and output is available + assertEquals( 1641, longHelpOptionIncorrectOutput.length() ); + assertEquals( 0, longHelpOptionIncorrectErrOutput.length() ); + } + + @Test + @DisplayName( "Test exit status of application invoked with correct 'help' command line option (short version)" ) + @ExpectSystemExitWithStatus( 0 ) + public void testCmdLnOptionHelpShortCorrectExit() + { + String[] argsShortHelpOptionCorrect = { "-" + CmdLnOption.HELP.getShortOption() }; + + this.setUp(); + + App.main( argsShortHelpOptionCorrect ); + } + + @Test + @DisplayName( "Test exit status of application invoked with correct 'help' command line option (long version)" ) + @ExpectSystemExitWithStatus( 0 ) + public void testCmdLnOptionHelpLongCorrectExit() + { + String[] argsLongHelpOptionCorrect = { "--" + CmdLnOption.HELP.getLongOption() }; + + this.setUp(); + + App.main( argsLongHelpOptionCorrect ); + } + + @Test + @DisplayName( "Test exit status of application invoked with incorrect 'help' command line option (short version)" ) + @ExpectSystemExitWithStatus( 1 ) + public void testCmdLnOptionHelpShortIncorrectExit() + { + String[] argsShortHelpOptionCorrect = { "---" + CmdLnOption.HELP.getShortOption() }; + + this.setUp(); + + App.main( argsShortHelpOptionCorrect ); + } + + @Test + @DisplayName( "Test exit status of application invoked with incorrect 'help' command line option (long version)" ) + @ExpectSystemExitWithStatus( 1 ) + public void testCmdLnOptionHelpLongIncorrectExit() + { + String[] argsLongHelpOptionCorrect = { "---" + CmdLnOption.HELP.getLongOption() }; + + this.setUp(); + + App.main( argsLongHelpOptionCorrect ); + } } } -- cgit v1.2.3-55-g7522