bigtop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r..@apache.org
Subject git commit: BIGTOP-1316 enhanced shell
Date Tue, 01 Jul 2014 18:22:54 GMT
Repository: bigtop
Updated Branches:
  refs/heads/master 7e987077b -> 6d37e274f


BIGTOP-1316 enhanced shell


Project: http://git-wip-us.apache.org/repos/asf/bigtop/repo
Commit: http://git-wip-us.apache.org/repos/asf/bigtop/commit/6d37e274
Tree: http://git-wip-us.apache.org/repos/asf/bigtop/tree/6d37e274
Diff: http://git-wip-us.apache.org/repos/asf/bigtop/diff/6d37e274

Branch: refs/heads/master
Commit: 6d37e274f1e6d00b1fa2b5eb506a55d8429a075d
Parents: 7e98707
Author: Steve Loughran <stevel@apache.org>
Authored: Mon Jun 30 16:56:31 2014 +0100
Committer: Roman Shaposhnik <rvs@apache.org>
Committed: Tue Jul 1 11:22:42 2014 -0700

----------------------------------------------------------------------
 .../apache/bigtop/itest/shell/JUnitShell.groovy | 86 ++++++++++++++++++++
 .../org/apache/bigtop/itest/shell/Shell.groovy  | 33 ++++++++
 2 files changed, 119 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/bigtop/blob/6d37e274/bigtop-test-framework/src/main/groovy/org/apache/bigtop/itest/shell/JUnitShell.groovy
----------------------------------------------------------------------
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 0000000..5fd6da6
--- /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)
+    }
+  }
+
+}

http://git-wip-us.apache.org/repos/asf/bigtop/blob/6d37e274/bigtop-test-framework/src/main/groovy/org/apache/bigtop/itest/shell/Shell.groovy
----------------------------------------------------------------------
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 ae3da68..bddff00 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>");
+    }
+  }
+
 }


Mime
View raw message