[Gluster-devel] Suggestion/request: use of functions in test scripts

Jeff Darcy jdarcy at redhat.com
Thu Mar 26 13:10:41 UTC 2015


While debugging YASRTF (Yet Another Spurious Regression Test Failure), I
came across the following construct:

        bash_function () {
                command_1
                [ $? -ne 0 ] && return 1
                command_2
                return $?
        }

        TEST bash_function

First, the "return $?" is unnecessary.  The return value for the
function will *automatically* be the return value from the last thing it
called, even in pipelines.  Try it.  Second, this construct loses
information.  If the TEST fails, is it because of command_1 or
command_2?  We'll never know.  Sometimes, doing the same commands
"inline" in the main body of the script, with TEST on each one, is both
shorter and more debuggable.

        TEST command_1
        TEST command_2

For longer functions, you can at least provide a more informative
result.  If you provide that result as output instead of as a return
value, it will even show up in test reports.

        bash_function () {

                command_1
                if [ $? -ne 0 ]; then
                        echo "command_1 failed"
                        return
                fi

                command_2
                if [ $? -ne 0 ]; then
                        echo "command_2 failed"
                        return
                fi

                echo "ok"
        }

        EXPECT "ok" bash_function

Yes, this time it's longer, but storage is cheaper than stalling the
whole project because of "mysterious" regression-test failures.


More information about the Gluster-devel mailing list