diff --git a/cli/test_pack_python3.bats b/cli/test_pack_python3.bats index f2ffa07..c35593d 100644 --- a/cli/test_pack_python3.bats +++ b/cli/test_pack_python3.bats @@ -1,5 +1,6 @@ load '../test_helpers/bats-support/load' load '../test_helpers/bats-assert/load' +load '../test_helpers/common' setup() { if [[ ! -d /opt/stackstorm/packs/examples ]]; then @@ -42,20 +43,15 @@ skip_tests_if_python3_is_not_available_or_if_already_running_under_python3() { @test "packs.setup_virtualenv without python3 flags works and defaults to Python 2" { skip_tests_if_python3_is_not_available_or_if_already_running_under_python3 - SETUP_VENV_RESULTS=$(st2 run packs.setup_virtualenv packs=examples -j) + SETUP_VENV_RESULTS=$(run_command_and_log_output st2 run packs.setup_virtualenv packs=examples -j) run eval "echo '$SETUP_VENV_RESULTS' | jq -r '.result.result'" assert_success - assert_output "Successfully set up virtualenv for the following packs: examples" run eval "echo '$SETUP_VENV_RESULTS' | jq -r '.status'" assert_success - assert_output "succeeded" - run st2-register-content --register-pack /opt/stackstorm/packs/examples/ --register-all - assert_success - run /opt/stackstorm/virtualenvs/examples/bin/python --version assert_output --partial "Python 2.7" @@ -66,7 +62,7 @@ skip_tests_if_python3_is_not_available_or_if_already_running_under_python3() { @test "packs.setup_virtualenv with python3 flag works" { skip_tests_if_python3_is_not_available_or_if_already_running_under_python3 - SETUP_VENV_RESULTS=$(st2 run packs.setup_virtualenv packs=examples python3=true -j) + SETUP_VENV_RESULTS=$(run_command_and_log_output st2 run packs.setup_virtualenv packs=examples python3=true -j) run eval "echo '$SETUP_VENV_RESULTS' | jq -r '.result.result'" assert_success @@ -81,7 +77,7 @@ skip_tests_if_python3_is_not_available_or_if_already_running_under_python3() { assert_output --partial "Python 3." - RESULT=$(st2 run examples.python_runner_print_python_version -j) + RESULT=$(run_command_and_log_output st2 run examples.python_runner_print_python_version -j) assert_success run eval "echo '$RESULT' | jq -r '.result.stdout'" @@ -99,7 +95,7 @@ skip_tests_if_python3_is_not_available_or_if_already_running_under_python3() { run eval "echo '$RESULT' | jq -r '.result.stdout'" assert_success - assert_output --regexp ".*PYTHONPATH: /usr/(local/)?lib/python3.*" + assert_output --regexp ".*PYTHONPATH: .*/usr/(local/)?lib/python3.*" } @test "python3 imports work correctly" { @@ -108,7 +104,7 @@ skip_tests_if_python3_is_not_available_or_if_already_running_under_python3() { run st2 pack install python3_test --python3 -j assert_success - RESULT=$(st2 run python3_test.test_stdlib_import -j) + RESULT=$(run_command_and_log_output st2 run python3_test.test_stdlib_import -j) assert_success run eval "echo '$RESULT' | jq -r '.result.result'" diff --git a/test_helpers/common.sh b/test_helpers/common.sh new file mode 100644 index 0000000..05b72e1 --- /dev/null +++ b/test_helpers/common.sh @@ -0,0 +1,69 @@ +# Copyright 2019 Extreme Networks, Inc. +# +# Licensed 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 +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# 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. + +# Common bash utility functions used by test code + +run_command_and_log_output() { + # Utility function which runs a bash command and logs the command output (stdout) and exit + # code. + # + # This comes in handy in scenarios where you want to save command stdout in a variable, but you + # also want to output the command stdout to make troubleshooting / debugging in test failures + # easier. + # + # Example usage: + # + # RESULT=$(run_command_and_log_output st2 run pack install packs=examples) + # + # 1. Run the command and capture the outputs + eval "$({ stderr=$({ stdout=$($@); exit_code=$?; } 2>&1; declare -p stdout exit_code >&2); declare -p stderr ; } 2>&1)" + + # 2. Log the output to stderr + >&2 echo "==========" + >&2 echo "Ran command: ${@}" + >&2 echo "stdout: ${stdout}" + >&2 echo "stderr: ${stderr}" + >&2 echo "exit code: ${exit_code}" + >&2 echo "==========" + + # 3. Return original command value + echo "${stdout}" +} + +run_command_and_log_output_get_stderr() { + # Utility function which runs a bash command and logs the command output (stdout) and exit + # code. + # + # This comes in handy in scenarios where you want to save command stdout in a variable, but you + # also want to output the command stdout to make troubleshooting / debugging in test failures + # easier. + # + # Example usage: + # + # RESULT=$(run_command_and_log_output_get_stderr st2 run pack install packs=examples) + # + # 1. Run the command and capture the outputs + eval "$({ stderr=$({ stdout=$($@); exit_code=$?; } 2>&1; declare -p stdout exit_code >&2); declare -p stderr ; } 2>&1)" + + # 2. Log the output to stderr + >&2 echo "==========" + >&2 echo "Ran command: ${@}" + >&2 echo "stdout: ${stdout}" + >&2 echo "stderr: ${stderr}" + >&2 echo "exit code: ${exit_code}" + >&2 echo "==========" + + # 3. Return original command error + echo "${stderr}" +}