Skip to content
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

Quote values in the tmt-report-results.yaml file #3231

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/releases.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ generate the XUnit file. It doesn't do any extra modifications to the XML tree
using an ``ElementTree`` anymore. Also the schema is now validated against the
XSD.

The values in the generated ``tmt-report-results.yaml`` file are
now wrapped in double quotes, and any double quotes within the
values are escaped to ensure that the resulting file is always
valid YAML.

The :ref:`/plugins/report/junit` report plugin now validates all the XML
flavors against their respective XSD schemas and tries to prettify the final
XML output. These functionalities are always disabled for ``custom`` flavors.
Expand Down
24 changes: 12 additions & 12 deletions tests/execute/restraint/report-result/data/report.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,43 @@ rlJournalStart
rlRun "rstrnt-report-result --server http://test-example.com report SKIP" 0 "Generating Restraint report of skipped test."
rlRun "ls $RESULT_FILE" 0 "Result report successfully generated."
rlRun -s "cat $RESULT_FILE"
rlAssertGrep 'result: skip' $rlRun_LOG
rlAssertGrep 'result: "skip"' $rlRun_LOG
rlRun "rm -f $RESULT_FILE" 0 "Result report successfully deleted."
rlRun "rstrnt-report-result --server http://test-example.com --port 55 --disable-plugin avc --message 'Example output message.' -o /tmp/example_output.txt report PASS 66" 0 "Generating Restraint report of passed test."
rlRun -s "cat $RESULT_FILE"
rlAssertGrep "- report_example_output.txt" $rlRun_LOG
rlAssertGrep 'name: /report' $rlRun_LOG
rlAssertGrep 'result: pass' $rlRun_LOG
rlAssertGrep '- "report_example_output.txt"' $rlRun_LOG
rlAssertGrep 'name: "/report"' $rlRun_LOG
rlAssertGrep 'result: "pass"' $rlRun_LOG
rlRun "rm -f $RESULT_FILE" 0 "Result report successfully deleted."
rlRun "rstrnt-report-result --server http://test-example.com report WARN" 0 "Generating Restraint report of warned test."
rlRun -s "cat $RESULT_FILE"
rlAssertGrep 'result: warn' $rlRun_LOG
rlAssertGrep 'result: "warn"' $rlRun_LOG
rlRun "rm -f $RESULT_FILE" 0 "Result report successfully deleted."
rlRun "rstrnt-report-result --server http://test-example.com report FAIL" 0 "Generating Restraint report of failed test."
rlRun -s "cat $RESULT_FILE"
rlAssertGrep 'result: fail' $rlRun_LOG
rlAssertGrep 'result: "fail"' $rlRun_LOG
rlRun "rm -f $RESULT_FILE" 0 "Result report successfully deleted."
rlPhaseEnd

rlPhaseStartTest 'Verify mocked RHTS rhts-report-result file generated correctly.'
rlRun "rhts-report-result rhts-report SKIP /tmp/example_output.txt" 0 "Generating RHTS report of skipped test without optional metric."
rlRun "ls $RESULT_FILE" 0 "Result report successfully generated."
rlRun -s "cat $RESULT_FILE"
rlAssertGrep 'result: skip' $rlRun_LOG
rlAssertGrep 'result: "skip"' $rlRun_LOG
rlRun "rm -f $RESULT_FILE" 0 "Result report successfully deleted."
rlRun "rhts-report-result rhts-report PASS /tmp/example_output.txt 66" 0 "Generating RHTS report of passed test."
rlRun -s "cat $RESULT_FILE"
rlAssertGrep "- rhts-report_example_output.txt" $rlRun_LOG
rlAssertGrep 'name: /rhts-report' $rlRun_LOG
rlAssertGrep 'result: pass' $rlRun_LOG
rlAssertGrep '- "rhts-report_example_output.txt"' $rlRun_LOG
rlAssertGrep 'name: "/rhts-report"' $rlRun_LOG
rlAssertGrep 'result: "pass"' $rlRun_LOG
rlRun "rm -f $RESULT_FILE" 0 "Result report successfully deleted."
rlRun "rhts-report-result rhts-report WARN /tmp/example_output.txt 66" 0 "Generating RHTS report of warned test."
rlRun -s "cat $RESULT_FILE"
rlAssertGrep 'result: warn' $rlRun_LOG
rlAssertGrep 'result: "warn"' $rlRun_LOG
rlRun "rm -f $RESULT_FILE" 0 "Result report successfully deleted."
rlRun "rhts-report-result rhts-report FAIL /tmp/example_output.txt 66" 0 "Generating RHTS report of failed test."
rlRun -s "cat $RESULT_FILE"
rlAssertGrep 'result: fail' $rlRun_LOG
rlAssertGrep 'result: "fail"' $rlRun_LOG
rlPhaseEnd

rlPhaseStartCleanup
Expand Down
3 changes: 3 additions & 0 deletions tests/execute/result/main.fmf
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
/repeated:
summary: Repeated test should provide multiple results
test: ./repeated.sh
/special:
summary: Test special characters generated to tmt-report-results.yaml
test: ./special.sh
26 changes: 26 additions & 0 deletions tests/execute/result/special.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash
seberm marked this conversation as resolved.
Show resolved Hide resolved
. /usr/share/beakerlib/beakerlib.sh || exit 1

rlJournalStart
rlPhaseStartSetup
rlRun "run=\$(mktemp -d)" 0 "Create run directory"
rlRun "pushd special"
rlPhaseEnd

rlPhaseStartTest "Check characters are correctly escaped in tmt-report-result output"
rlRun -s "tmt run -v -i $run" 0

RESULT_FILE="$run/special-chars/execute/data/guest/default-0/0-7-special-characters-in-the-name-1/data/tmt-report-results.yaml"
rlRun "yq -e '.' $RESULT_FILE" 0 "Check the YAML is valid"

rlAssertGrep 'name: "/0\.\.7 \\"special\\": \\" characters: \*\$@|&>< in: the: name"' "$RESULT_FILE"
rlAssertGrep "result: \"pass\"" "$RESULT_FILE"
rlAssertGrep "end-time: \".*\"" "$RESULT_FILE"
rlAssertGrep "data-path: \"/.*\"" "$RESULT_FILE"
rlPhaseEnd

rlPhaseStartCleanup
rlRun "popd"
rlRun "rm -r ${run}" 0 "Remove run directory"
rlPhaseEnd
rlJournalEnd
1 change: 1 addition & 0 deletions tests/execute/result/special/.fmf/version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
10 changes: 10 additions & 0 deletions tests/execute/result/special/special-chars.fmf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
execute:
how: tmt
discover:
how: shell
tests:
- name: '0..7 special: characters: *$@|&>< in: the: name'
result: restraint
test: "tmt-report-result '0..7 \"special\": \" characters: *$@|&>< in: the: name' PASS"
seberm marked this conversation as resolved.
Show resolved Hide resolved
provision:
how: container
14 changes: 10 additions & 4 deletions tmt/steps/execute/scripts/tmt-report-result
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ die() { echo "$*" >&2; exit 2; } # complain to STDERR and exit with error
needs_arg() { if [ -z "$OPTARG" ]; then die "No arg for --$OPT option"; fi; }

write_report_file () {
echo " - name: ${TESTNAME}" >> "$REPORT_RESULT_OUTPUTFILE"
echo " result: ${TESTRESULT}" >> "$REPORT_RESULT_OUTPUTFILE"
echo " - name: \"${TESTNAME}\"" >> "$REPORT_RESULT_OUTPUTFILE"
psss marked this conversation as resolved.
Show resolved Hide resolved
echo " result: \"${TESTRESULT}\"" >> "$REPORT_RESULT_OUTPUTFILE"
if [ -n "${outputFile}" ]; then
# FIXME: The UNIX filenames can contain double quotes in the filename
# (`$outputFile`). We should escape them as well as for TESTNAME.
# Otherwise, the resulting YAML file can be invalid.
echo " log:" >> "$REPORT_RESULT_OUTPUTFILE"
echo " - ${outputFile}" >> "$REPORT_RESULT_OUTPUTFILE"
echo " - \"${outputFile}\"" >> "$REPORT_RESULT_OUTPUTFILE"
fi
echo " end-time: \"$(date --utc +%FT%T.%6N%:z)\"" >> "$REPORT_RESULT_OUTPUTFILE"
echo " data-path: ${TMT_TEST_DATA/$TMT_TEST_EXE}" >> "$REPORT_RESULT_OUTPUTFILE"
echo " data-path: \"${TMT_TEST_DATA/$TMT_TEST_EXE}\"" >> "$REPORT_RESULT_OUTPUTFILE"
}

copy_outputfile_to_data_dir () {
Expand Down Expand Up @@ -85,7 +88,10 @@ if [ $# -lt 2 ] || [ $help == True ]; then
echo ""
exit 1
fi

TESTNAME=$1
# If test name contains double quotes, escape them with a backslash
TESTNAME=${TESTNAME//\"/\\\"}
# If last character is a ':' then delete it.
[[ ${TESTNAME: -1} == ':' ]] && TESTNAME=${TESTNAME::-1}
# If the first character isn't a '/' then add it.
Expand Down
Loading