Repository: incubator-brooklyn Updated Branches: refs/heads/master ffbd6eff3 -> a6c77f39b
brooklyn-dist: add org.apache package prefix Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/713b6289 Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/713b6289 Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/713b6289 Branch: refs/heads/master Commit: 713b62891dfdff893ef21d6b6b3943b8af969f71 Parents: 85b1fcf Author: Ciprian Ciubotariu <[email protected]> Authored: Thu Jul 16 18:10:16 2015 +0300 Committer: Ciprian Ciubotariu <[email protected]> Committed: Tue Aug 4 17:19:12 2015 +0300 ---------------------------------------------------------------------- .../brooklyn/cli/BaseCliIntegrationTest.java | 190 ------------------- .../java/brooklyn/cli/CliIntegrationTest.java | 129 ------------- .../brooklyn/cli/BaseCliIntegrationTest.java | 190 +++++++++++++++++++ .../apache/brooklyn/cli/CliIntegrationTest.java | 129 +++++++++++++ 4 files changed, 319 insertions(+), 319 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/713b6289/usage/dist/src/test/java/brooklyn/cli/BaseCliIntegrationTest.java ---------------------------------------------------------------------- diff --git a/usage/dist/src/test/java/brooklyn/cli/BaseCliIntegrationTest.java b/usage/dist/src/test/java/brooklyn/cli/BaseCliIntegrationTest.java deleted file mode 100644 index 643ce34..0000000 --- a/usage/dist/src/test/java/brooklyn/cli/BaseCliIntegrationTest.java +++ /dev/null @@ -1,190 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package brooklyn.cli; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertFalse; -import static org.testng.Assert.assertTrue; -import static org.testng.Assert.fail; - -import java.io.InputStream; -import java.io.OutputStream; -import java.util.NoSuchElementException; -import java.util.Scanner; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; - -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; - -import brooklyn.entity.basic.ApplicationBuilder; - -import com.google.common.collect.Lists; - -/** - * Command line interface test support. - */ -public class BaseCliIntegrationTest { - - // TODO does this need to be hard-coded? - private static final String BROOKLYN_BIN_PATH = "./target/brooklyn-dist/bin/brooklyn"; - private static final String BROOKLYN_CLASSPATH = "./target/test-classes/:./target/classes/"; - - // Times in seconds to allow Brooklyn to run and produce output - private static final long DELAY = 10l; - private static final long TIMEOUT = DELAY + 30l; - - private ExecutorService executor; - - @BeforeMethod(alwaysRun = true) - public void setup() { - executor = Executors.newCachedThreadPool(); - } - - @AfterMethod(alwaysRun = true) - public void teardown() { - executor.shutdownNow(); - } - - /** Invoke the brooklyn script with arguments. */ - public Process startBrooklyn(String...argv) throws Throwable { - ProcessBuilder pb = new ProcessBuilder(); - pb.environment().remove("BROOKLYN_HOME"); - pb.environment().put("BROOKLYN_CLASSPATH", BROOKLYN_CLASSPATH); - pb.command(Lists.asList(BROOKLYN_BIN_PATH, argv)); - return pb.start(); - } - - public void testBrooklyn(Process brooklyn, BrooklynCliTest test, int expectedExit) throws Throwable { - testBrooklyn(brooklyn, test, expectedExit, false); - } - - /** Tests the operation of the Brooklyn CLI. */ - public void testBrooklyn(Process brooklyn, BrooklynCliTest test, int expectedExit, boolean stop) throws Throwable { - try { - Future<Integer> future = executor.submit(test); - - // Send CR to stop if required - if (stop) { - OutputStream out = brooklyn.getOutputStream(); - out.write('\n'); - out.flush(); - } - - int exitStatus = future.get(TIMEOUT, TimeUnit.SECONDS); - - // Check error code from process - assertEquals(exitStatus, expectedExit, "Command returned wrong status"); - } catch (TimeoutException te) { - fail("Timed out waiting for process to complete", te); - } catch (ExecutionException ee) { - if (ee.getCause() instanceof AssertionError) { - throw ee.getCause(); - } else throw ee; - } finally { - brooklyn.destroy(); - } - } - - /** A {@link Callable} that encapsulates Brooklyn CLI test logic. */ - public static abstract class BrooklynCliTest implements Callable<Integer> { - - private final Process brooklyn; - - private String consoleOutput; - private String consoleError; - - public BrooklynCliTest(Process brooklyn) { - this.brooklyn = brooklyn; - } - - @Override - public Integer call() throws Exception { - // Wait for initial output - Thread.sleep(TimeUnit.SECONDS.toMillis(DELAY)); - - // Get the console output of running that command - consoleOutput = convertStreamToString(brooklyn.getInputStream()); - consoleError = convertStreamToString(brooklyn.getErrorStream()); - - // Check if the output looks as expected - checkConsole(); - - // Return exit status on completion - return brooklyn.waitFor(); - } - - /** Perform test assertions on console output and error streams. */ - public abstract void checkConsole(); - - private String convertStreamToString(InputStream is) { - try { - return new Scanner(is).useDelimiter("\\A").next(); - } catch (NoSuchElementException e) { - return ""; - } - } - - protected void assertConsoleOutput(String...expected) { - for (String e : expected) { - assertTrue(consoleOutput.contains(e), "Execution output not logged; output=" + consoleOutput); - } - } - - protected void assertNoConsoleOutput(String...expected) { - for (String e : expected) { - assertFalse(consoleOutput.contains(e), "Execution output logged; output=" + consoleOutput); - } - } - - protected void assertConsoleError(String...expected) { - for (String e : expected) { - assertTrue(consoleError.contains(e), "Execution error not logged; error=" + consoleError); - } - } - - protected void assertNoConsoleError(String...expected) { - for (String e : expected) { - assertFalse(consoleError.contains(e), "Execution error logged; error=" + consoleError); - } - } - - protected void assertConsoleOutputEmpty() { - assertTrue(consoleOutput.isEmpty(), "Output present; output=" + consoleOutput); - } - - protected void assertConsoleErrorEmpty() { - assertTrue(consoleError.isEmpty(), "Error present; error=" + consoleError); - } - }; - - /** An empty application for testing. */ - public static class TestApplication extends ApplicationBuilder { - @Override - protected void doBuild() { - // Empty, for testing - } - } - -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/713b6289/usage/dist/src/test/java/brooklyn/cli/CliIntegrationTest.java ---------------------------------------------------------------------- diff --git a/usage/dist/src/test/java/brooklyn/cli/CliIntegrationTest.java b/usage/dist/src/test/java/brooklyn/cli/CliIntegrationTest.java deleted file mode 100644 index be59565..0000000 --- a/usage/dist/src/test/java/brooklyn/cli/CliIntegrationTest.java +++ /dev/null @@ -1,129 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package brooklyn.cli; - -import org.testng.annotations.Test; - -/** - * Test the command line interface operation. - */ -public class CliIntegrationTest extends BaseCliIntegrationTest { - - /** - * Checks if running {@code brooklyn help} produces the expected output. - */ - @Test(groups = "Integration") - public void testLaunchCliHelp() throws Throwable { - final Process brooklyn = startBrooklyn("help"); - - BrooklynCliTest test = new BrooklynCliTest(brooklyn) { - @Override - public void checkConsole() { - assertConsoleOutput("usage: brooklyn"); // Usage info not present - assertConsoleOutput("The most commonly used brooklyn commands are:"); - assertConsoleOutput("help Display help for available commands", - "info Display information about brooklyn", - "launch Starts a brooklyn application"); // List of common commands not present - assertConsoleOutput("See 'brooklyn help <command>' for more information on a specific command."); - assertConsoleErrorEmpty(); - } - }; - - testBrooklyn(brooklyn, test, 0); - } - - /** - * Checks if launching an application using {@code brooklyn launch} produces the expected output. - */ - @Test(groups = "Integration") - public void testLaunchCliApp() throws Throwable { - final Process brooklyn = startBrooklyn("--verbose", "launch", "--stopOnKeyPress", "--app", "brooklyn.cli.BaseCliIntegrationTest$TestApplication", "--location", "localhost", "--noConsole"); - - BrooklynCliTest test = new BrooklynCliTest(brooklyn) { - @Override - public void checkConsole() { - assertConsoleOutput("Launching brooklyn app:"); // Launch message not output - assertNoConsoleOutput("Initiating Jersey application"); // Web console started - assertConsoleOutput("Started application BasicApplicationImpl"); // Application not started - assertConsoleOutput("Server started. Press return to stop."); // Server started message not output - assertConsoleErrorEmpty(); - } - }; - - testBrooklyn(brooklyn, test, 0, true); - } - - /** - * Checks if a correct error and help message is given if using incorrect param. - */ - @Test(groups = "Integration") - public void testLaunchCliAppParamError() throws Throwable { - final Process brooklyn = startBrooklyn("launch", "nothing", "--app"); - - BrooklynCliTest test = new BrooklynCliTest(brooklyn) { - @Override - public void checkConsole() { - assertConsoleError("Parse error: Required values for option 'application class or file' not provided"); - assertConsoleError("NAME", "SYNOPSIS", "OPTIONS", "COMMANDS"); - assertConsoleOutputEmpty(); - } - }; - - testBrooklyn(brooklyn, test, 1); - } - - /** - * Checks if a correct error and help message is given if using incorrect command. - */ - @Test(groups = "Integration") - public void testLaunchCliAppCommandError() throws Throwable { - final Process brooklyn = startBrooklyn("biscuit"); - - BrooklynCliTest test = new BrooklynCliTest(brooklyn) { - @Override - public void checkConsole() { - assertConsoleError("Parse error: No command specified"); - assertConsoleError("NAME", "SYNOPSIS", "OPTIONS", "COMMANDS"); - assertConsoleOutputEmpty(); - } - }; - - testBrooklyn(brooklyn, test, 1); - } - - /** - * Checks if a correct error and help message is given if using incorrect application. - */ - @Test(groups = "Integration") - public void testLaunchCliAppLaunchError() throws Throwable { - final String app = "org.eample.DoesNotExist"; - final Process brooklyn = startBrooklyn("launch", "--app", app, "--location", "nowhere"); - - BrooklynCliTest test = new BrooklynCliTest(brooklyn) { - @Override - public void checkConsole() { - assertConsoleOutput("ERROR Execution error: brooklyn.util.ResourceUtils.getResourceFromUrl"); - assertConsoleError("Execution error: Error getting resource '"+app+"' for LaunchCommand"); - } - }; - - testBrooklyn(brooklyn, test, 2); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/713b6289/usage/dist/src/test/java/org/apache/brooklyn/cli/BaseCliIntegrationTest.java ---------------------------------------------------------------------- diff --git a/usage/dist/src/test/java/org/apache/brooklyn/cli/BaseCliIntegrationTest.java b/usage/dist/src/test/java/org/apache/brooklyn/cli/BaseCliIntegrationTest.java new file mode 100644 index 0000000..49b4d6d --- /dev/null +++ b/usage/dist/src/test/java/org/apache/brooklyn/cli/BaseCliIntegrationTest.java @@ -0,0 +1,190 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.brooklyn.cli; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertTrue; +import static org.testng.Assert.fail; + +import java.io.InputStream; +import java.io.OutputStream; +import java.util.NoSuchElementException; +import java.util.Scanner; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; + +import brooklyn.entity.basic.ApplicationBuilder; + +import com.google.common.collect.Lists; + +/** + * Command line interface test support. + */ +public class BaseCliIntegrationTest { + + // TODO does this need to be hard-coded? + private static final String BROOKLYN_BIN_PATH = "./target/brooklyn-dist/bin/brooklyn"; + private static final String BROOKLYN_CLASSPATH = "./target/test-classes/:./target/classes/"; + + // Times in seconds to allow Brooklyn to run and produce output + private static final long DELAY = 10l; + private static final long TIMEOUT = DELAY + 30l; + + private ExecutorService executor; + + @BeforeMethod(alwaysRun = true) + public void setup() { + executor = Executors.newCachedThreadPool(); + } + + @AfterMethod(alwaysRun = true) + public void teardown() { + executor.shutdownNow(); + } + + /** Invoke the brooklyn script with arguments. */ + public Process startBrooklyn(String...argv) throws Throwable { + ProcessBuilder pb = new ProcessBuilder(); + pb.environment().remove("BROOKLYN_HOME"); + pb.environment().put("BROOKLYN_CLASSPATH", BROOKLYN_CLASSPATH); + pb.command(Lists.asList(BROOKLYN_BIN_PATH, argv)); + return pb.start(); + } + + public void testBrooklyn(Process brooklyn, BrooklynCliTest test, int expectedExit) throws Throwable { + testBrooklyn(brooklyn, test, expectedExit, false); + } + + /** Tests the operation of the Brooklyn CLI. */ + public void testBrooklyn(Process brooklyn, BrooklynCliTest test, int expectedExit, boolean stop) throws Throwable { + try { + Future<Integer> future = executor.submit(test); + + // Send CR to stop if required + if (stop) { + OutputStream out = brooklyn.getOutputStream(); + out.write('\n'); + out.flush(); + } + + int exitStatus = future.get(TIMEOUT, TimeUnit.SECONDS); + + // Check error code from process + assertEquals(exitStatus, expectedExit, "Command returned wrong status"); + } catch (TimeoutException te) { + fail("Timed out waiting for process to complete", te); + } catch (ExecutionException ee) { + if (ee.getCause() instanceof AssertionError) { + throw ee.getCause(); + } else throw ee; + } finally { + brooklyn.destroy(); + } + } + + /** A {@link Callable} that encapsulates Brooklyn CLI test logic. */ + public static abstract class BrooklynCliTest implements Callable<Integer> { + + private final Process brooklyn; + + private String consoleOutput; + private String consoleError; + + public BrooklynCliTest(Process brooklyn) { + this.brooklyn = brooklyn; + } + + @Override + public Integer call() throws Exception { + // Wait for initial output + Thread.sleep(TimeUnit.SECONDS.toMillis(DELAY)); + + // Get the console output of running that command + consoleOutput = convertStreamToString(brooklyn.getInputStream()); + consoleError = convertStreamToString(brooklyn.getErrorStream()); + + // Check if the output looks as expected + checkConsole(); + + // Return exit status on completion + return brooklyn.waitFor(); + } + + /** Perform test assertions on console output and error streams. */ + public abstract void checkConsole(); + + private String convertStreamToString(InputStream is) { + try { + return new Scanner(is).useDelimiter("\\A").next(); + } catch (NoSuchElementException e) { + return ""; + } + } + + protected void assertConsoleOutput(String...expected) { + for (String e : expected) { + assertTrue(consoleOutput.contains(e), "Execution output not logged; output=" + consoleOutput); + } + } + + protected void assertNoConsoleOutput(String...expected) { + for (String e : expected) { + assertFalse(consoleOutput.contains(e), "Execution output logged; output=" + consoleOutput); + } + } + + protected void assertConsoleError(String...expected) { + for (String e : expected) { + assertTrue(consoleError.contains(e), "Execution error not logged; error=" + consoleError); + } + } + + protected void assertNoConsoleError(String...expected) { + for (String e : expected) { + assertFalse(consoleError.contains(e), "Execution error logged; error=" + consoleError); + } + } + + protected void assertConsoleOutputEmpty() { + assertTrue(consoleOutput.isEmpty(), "Output present; output=" + consoleOutput); + } + + protected void assertConsoleErrorEmpty() { + assertTrue(consoleError.isEmpty(), "Error present; error=" + consoleError); + } + }; + + /** An empty application for testing. */ + public static class TestApplication extends ApplicationBuilder { + @Override + protected void doBuild() { + // Empty, for testing + } + } + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/713b6289/usage/dist/src/test/java/org/apache/brooklyn/cli/CliIntegrationTest.java ---------------------------------------------------------------------- diff --git a/usage/dist/src/test/java/org/apache/brooklyn/cli/CliIntegrationTest.java b/usage/dist/src/test/java/org/apache/brooklyn/cli/CliIntegrationTest.java new file mode 100644 index 0000000..895b55a --- /dev/null +++ b/usage/dist/src/test/java/org/apache/brooklyn/cli/CliIntegrationTest.java @@ -0,0 +1,129 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.brooklyn.cli; + +import org.testng.annotations.Test; + +/** + * Test the command line interface operation. + */ +public class CliIntegrationTest extends BaseCliIntegrationTest { + + /** + * Checks if running {@code brooklyn help} produces the expected output. + */ + @Test(groups = "Integration") + public void testLaunchCliHelp() throws Throwable { + final Process brooklyn = startBrooklyn("help"); + + BrooklynCliTest test = new BrooklynCliTest(brooklyn) { + @Override + public void checkConsole() { + assertConsoleOutput("usage: brooklyn"); // Usage info not present + assertConsoleOutput("The most commonly used brooklyn commands are:"); + assertConsoleOutput("help Display help for available commands", + "info Display information about brooklyn", + "launch Starts a brooklyn application"); // List of common commands not present + assertConsoleOutput("See 'brooklyn help <command>' for more information on a specific command."); + assertConsoleErrorEmpty(); + } + }; + + testBrooklyn(brooklyn, test, 0); + } + + /** + * Checks if launching an application using {@code brooklyn launch} produces the expected output. + */ + @Test(groups = "Integration") + public void testLaunchCliApp() throws Throwable { + final Process brooklyn = startBrooklyn("--verbose", "launch", "--stopOnKeyPress", "--app", "brooklyn.cli.BaseCliIntegrationTest$TestApplication", "--location", "localhost", "--noConsole"); + + BrooklynCliTest test = new BrooklynCliTest(brooklyn) { + @Override + public void checkConsole() { + assertConsoleOutput("Launching brooklyn app:"); // Launch message not output + assertNoConsoleOutput("Initiating Jersey application"); // Web console started + assertConsoleOutput("Started application BasicApplicationImpl"); // Application not started + assertConsoleOutput("Server started. Press return to stop."); // Server started message not output + assertConsoleErrorEmpty(); + } + }; + + testBrooklyn(brooklyn, test, 0, true); + } + + /** + * Checks if a correct error and help message is given if using incorrect param. + */ + @Test(groups = "Integration") + public void testLaunchCliAppParamError() throws Throwable { + final Process brooklyn = startBrooklyn("launch", "nothing", "--app"); + + BrooklynCliTest test = new BrooklynCliTest(brooklyn) { + @Override + public void checkConsole() { + assertConsoleError("Parse error: Required values for option 'application class or file' not provided"); + assertConsoleError("NAME", "SYNOPSIS", "OPTIONS", "COMMANDS"); + assertConsoleOutputEmpty(); + } + }; + + testBrooklyn(brooklyn, test, 1); + } + + /** + * Checks if a correct error and help message is given if using incorrect command. + */ + @Test(groups = "Integration") + public void testLaunchCliAppCommandError() throws Throwable { + final Process brooklyn = startBrooklyn("biscuit"); + + BrooklynCliTest test = new BrooklynCliTest(brooklyn) { + @Override + public void checkConsole() { + assertConsoleError("Parse error: No command specified"); + assertConsoleError("NAME", "SYNOPSIS", "OPTIONS", "COMMANDS"); + assertConsoleOutputEmpty(); + } + }; + + testBrooklyn(brooklyn, test, 1); + } + + /** + * Checks if a correct error and help message is given if using incorrect application. + */ + @Test(groups = "Integration") + public void testLaunchCliAppLaunchError() throws Throwable { + final String app = "org.eample.DoesNotExist"; + final Process brooklyn = startBrooklyn("launch", "--app", app, "--location", "nowhere"); + + BrooklynCliTest test = new BrooklynCliTest(brooklyn) { + @Override + public void checkConsole() { + assertConsoleOutput("ERROR Execution error: brooklyn.util.ResourceUtils.getResourceFromUrl"); + assertConsoleError("Execution error: Error getting resource '"+app+"' for LaunchCommand"); + } + }; + + testBrooklyn(brooklyn, test, 2); + } + +}
