diff --git a/cluster/lib/logging.sh b/cluster/lib/logging.sh index 959f8474427..1c74f66528b 100644 --- a/cluster/lib/logging.sh +++ b/cluster/lib/logging.sh @@ -28,7 +28,16 @@ kube::log::errexit() { set +o xtrace local code="${1:-1}" - kube::log::error_exit "'${BASH_COMMAND}' exited with status $err" "${1:-1}" 1 + # Print out the stack trace described by $function_stack + if [ ${#FUNCNAME[@]} -gt 2 ] + then + kube::log::error "Call tree:" + for ((i=1;i<${#FUNCNAME[@]}-1;i++)) + do + kube::log::error " $i: ${BASH_SOURCE[$i+1]}:${BASH_LINENO[$i]} ${FUNCNAME[$i]}(...)" + done + fi + kube::log::error_exit "Error in ${BASH_SOURCE[1]}:${BASH_LINENO[0]}. '${BASH_COMMAND}' exited with status $err" "${1:-1}" 1 } kube::log::install_errexit() { diff --git a/hack/lib/test.sh b/hack/lib/test.sh index 51b63695c74..6c7f41337c2 100644 --- a/hack/lib/test.sh +++ b/hack/lib/test.sh @@ -31,6 +31,15 @@ kube::test::clear_all() { fi } +# Prints the calling file and line number $1 levels deep +# Defaults to 2 levels so you can call this to find your own caller +kube::test::get_caller() { + local levels=${1:-2} + local caller_file="${BASH_SOURCE[$levels]}" + local caller_line="${BASH_LINENO[$levels-1]}" + echo "$(basename "${caller_file}"):${caller_line}" +} + # Force exact match of a returned result for a object query. Wrap this with || to support multiple # valid return types. kube::test::get_object_assert() { @@ -43,12 +52,12 @@ kube::test::get_object_assert() { if [[ "$res" =~ ^$expected$ ]]; then echo -n ${green} - echo "Successful get $object $request: $res" + echo "$(kube::test::get_caller): Successful get $object $request: $res" echo -n ${reset} return 0 else echo ${bold}${red} - echo "FAIL!" + echo "$(kube::test::get_caller): FAIL!" echo "Get $object $request" echo " Expected: $expected" echo " Got: $res" @@ -68,12 +77,12 @@ kube::test::get_object_jsonpath_assert() { if [[ "$res" =~ ^$expected$ ]]; then echo -n ${green} - echo "Successful get $object $request: $res" + echo "$(kube::test::get_caller): Successful get $object $request: $res" echo -n ${reset} return 0 else echo ${bold}${red} - echo "FAIL!" + echo "$(kube::test::get_caller): FAIL!" echo "Get $object $request" echo " Expected: $expected" echo " Got: $res" @@ -94,7 +103,7 @@ kube::test::describe_object_assert() { for match in ${matches}; do if [[ ! $(echo "$result" | grep ${match}) ]]; then echo ${bold}${red} - echo "FAIL!" + echo "$(kube::test::get_caller): FAIL!" echo "Describe $resource $object" echo " Expected Match: $match" echo " Not found in:" @@ -107,7 +116,7 @@ kube::test::describe_object_assert() { done echo -n ${green} - echo "Successful describe $resource $object:" + echo "$(kube::test::get_caller): Successful describe $resource $object:" echo "$result" echo -n ${reset} return 0 @@ -131,13 +140,13 @@ kube::test::describe_object_events_assert() { fi if [[ $showevents == $has_events ]]; then echo -n ${green} - echo "Successful describe" + echo "$(kube::test::get_caller): Successful describe" echo "$result" echo ${reset} return 0 else echo ${bold}${red} - echo "FAIL" + echo "$(kube::test::get_caller): FAIL" if [[ $showevents == "false" ]]; then echo " Events information should not be described in:" else