diff options
author | Steve Loughran <stevel@apache.org> | 2014-06-30 16:56:31 +0100 |
---|---|---|
committer | Roman Shaposhnik <rvs@apache.org> | 2014-07-01 11:22:42 -0700 |
commit | 6d37e274f1e6d00b1fa2b5eb506a55d8429a075d (patch) | |
tree | 78ce888a6645fae9101390af2217967e7b77b1a1 /bigtop-test-framework | |
parent | 7e987077bf4caf0fbf4da38d4c747568bc7ef77c (diff) |
BIGTOP-1316 enhanced shell
Diffstat (limited to 'bigtop-test-framework')
-rw-r--r-- | bigtop-test-framework/src/main/groovy/org/apache/bigtop/itest/shell/JUnitShell.groovy | 86 | ||||
-rw-r--r-- | bigtop-test-framework/src/main/groovy/org/apache/bigtop/itest/shell/Shell.groovy | 33 |
2 files changed, 119 insertions, 0 deletions
diff --git a/bigtop-test-framework/src/main/groovy/org/apache/bigtop/itest/shell/JUnitShell.groovy b/bigtop-test-framework/src/main/groovy/org/apache/bigtop/itest/shell/JUnitShell.groovy new file mode 100644 index 00000000..5fd6da6f --- /dev/null +++ b/bigtop-test-framework/src/main/groovy/org/apache/bigtop/itest/shell/JUnitShell.groovy @@ -0,0 +1,86 @@ +/** + * 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 + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * 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.bigtop.itest.shell + +import org.junit.Assert; + +/** + * Extension of {@link Shell} with return code checking. + */ +class JUnitShell extends Shell { + + /** + * Instantiate with the given shell + * @param shell shell to use + */ + JUnitShell(String shell) { + super(shell) + } + + /** + * Instantiate with the given shell and user name + * @param shell shell to use + * @param user user + */ + JUnitShell(String shell, String user) { + super(shell, user) + } + + /** + * Instantiate with the default shell, {@link Shell#DEFAULT_SHELL} + */ + JUnitShell() { + } + + /** + * Execute a shell script expecting a specific exit code. + * The exit code is only checked at the end of the sequence + * @param expectedExitCode the expected exit code + * @param args shell script split into multiple Strings -one per line. + */ + void expectExitCode(int expectedExitCode, Object... args) { + exec(args) + assertExitCode(expectedExitCode) + } + + /** + * Execute expecting an exit code of "0", "success" + * @param args shell script split into multiple Strings + */ + void expectSuccess(Object... args) { + expectExitCode(0, args) + } + + /** + * Assert the shell exited with a given error code + * if not the output is printed and an assertion is raised + * @param expectedExitCode expected error code + * @throws AssertionError if the return code is wrong + */ + void assertExitCode(int expectedExitCode) { + int result = signCorrectedReturnCode() + if (result != expectedExitCode) { + dumpOutput() + Assert.assertEquals( + "Wrong exit code of script ${script}" as String, + expectedExitCode, result) + } + } + +} diff --git a/bigtop-test-framework/src/main/groovy/org/apache/bigtop/itest/shell/Shell.groovy b/bigtop-test-framework/src/main/groovy/org/apache/bigtop/itest/shell/Shell.groovy index ae3da68b..bddff008 100644 --- a/bigtop-test-framework/src/main/groovy/org/apache/bigtop/itest/shell/Shell.groovy +++ b/bigtop-test-framework/src/main/groovy/org/apache/bigtop/itest/shell/Shell.groovy @@ -108,4 +108,37 @@ class Shell { return this } + + /** + * Fix up the return code so that a value of 255 is mapped back to -1 + * @return twos complement return code from an unsigned byte + */ + int signCorrectedReturnCode() { + return (ret << 24) >> 24 + } + + /** + * String operation returns the exit code and any script built up + * @return a description of the contents of the instance. + */ + @Override + String toString() { + return signCorrectedReturnCode() + " =>\"" + (script ?: "(no script)") +"\"" + } + + /** + * Dump the command, return code and outputs to the log. + * stdout is logged at info; stderr at error. + */ + void dumpOutput() { + LOG.error(toString()) + LOG.error("return code = ${signCorrectedReturnCode()}") + if (out.size() != 0) { + LOG.info("\n<stdout>\n${out.join('\n')}\n</stdout>"); + } + if (err.size() != 0) { + LOG.error("\n<stderr>\n${err.join('\n')}\n</stderr>"); + } + } + } |