-
Notifications
You must be signed in to change notification settings - Fork 12
[WIP] Add a common utility function which makes debugging test failures #161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
d8dfb16
122ba6d
1f96aea
0c1ef09
13da9f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -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 | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this specific case, if we really need
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just read this comment after coming to the same conclusion. 👍 |
||||||
|
|
||||||
|
|
@@ -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) | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| 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'" | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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}" | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example, in this scenario avoiding another
runand mutating output will help to preserve original command results. And so we'll do assert against the original output, step withjqfiltering here is overcomplication:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That change is fine, but in addition to checking for a specific string, we should also check that the output is valid JSON.
One of the commands in one of the BATS tests returned a JSON array concatenated to a JSON dict:
Both of those objects individually are valid JSON, but simply concatenating them together produces invalid JSON.
So we do still need some way to validate that the produced output is valid JSON. This might be good to do in a custom
assert_function, and could even simply wrapjq.But however we check for JSON validity, this simplification is a good one.