]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
ceph-helpers: implement test_expect_failure
authorLoic Dachary <ldachary@redhat.com>
Tue, 21 Apr 2015 14:11:33 +0000 (16:11 +0200)
committerLoic Dachary <ldachary@redhat.com>
Tue, 21 Apr 2015 21:35:28 +0000 (23:35 +0200)
To display the output in case the command did not fail with the expected
output.

Signed-off-by: Loic Dachary <ldachary@redhat.com>
src/test/ceph-helpers.sh

index 1e34aca5aa6be994f07e49a35519471ce5f99ac5..104d25a91d999f98446dd118443f1439a7e762e9 100755 (executable)
@@ -934,6 +934,57 @@ function test_repair() {
 
 #######################################################################
 
+##
+# Run the *command* and expect it to fail (i.e. return a non zero status).
+# The output (stderr and stdout) is stored in a temporary file in *dir*
+# and is expected to contain the string *expected*.
+#
+# Return 0 if the command failed and the string was found. Otherwise
+# return 1 and cat the full output of the command on stderr for debug.
+#
+# @param dir temporary directory to store the output
+# @param expected string to look for in the output
+# @param command ... the command and its arguments
+# @return 0 on success, 1 on error
+#
+
+function expect_failure() {
+    local dir=$1
+    shift
+    local expected="$1"
+    shift
+    local success
+
+    if "$@" > $dir/out 2>&1 ; then
+        success=true
+    else
+        success=false
+    fi
+
+    if $success || ! grep --quiet "$expected" $dir/out ; then
+        cat $dir/out >&2
+        return 1
+    else
+        return 0
+    fi
+}
+
+function test_expect_failure() {
+    local dir=$1
+
+    setup $dir || return 1
+    expect_failure $dir FAIL bash -c 'echo FAIL ; exit 1' || return 1
+    # the command did not fail
+    ! expect_failure $dir FAIL bash -c 'echo FAIL ; exit 0' > $dir/out || return 1
+    grep --quiet FAIL $dir/out || return 1
+    # the command failed but the output does not contain the expected string
+    ! expect_failure $dir FAIL bash -c 'echo UNEXPECTED ; exit 1' > $dir/out || return 1
+    ! grep --quiet FAIL $dir/out || return 1
+    teardown $dir || return 1
+}
+
+#######################################################################
+
 ##
 # Call the **run** function (which must be defined by the caller) with
 # the **dir** argument followed by the caller argument list. The