Repository navigation
Expand file tree
/
Copy pathjunit.sh
More file actions
204 lines (185 loc) · 7.25 KB
/
Copy pathjunit.sh
File metadata and controls
204 lines (185 loc) · 7.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/bin/bash
# This utility file contains functions that format test output to be parsed into jUnit XML
# os::test::junit::declare_suite_start prints a message declaring the start of a test suite
# Any number of suites can be in flight at any time, so there is no failure condition for this
# script based on the number of suites in flight.
#
# Globals:
# - JUNIT_REPORT_OUTPUT
# - NUM_OS_JUNIT_SUITES_IN_FLIGHT
# Arguments:
# - 1: the suite name that is starting
# Returns:
# - increment NUM_OS_JUNIT_SUITES_IN_FLIGHT
function os::test::junit::declare_suite_start() {
local suite_name=$1
local num_suites=${NUM_OS_JUNIT_SUITES_IN_FLIGHT:-0}
echo "=== BEGIN TEST SUITE github.com/openshift/origin/test/${suite_name} ===" >> "${JUNIT_REPORT_OUTPUT:-/dev/null}"
NUM_OS_JUNIT_SUITES_IN_FLIGHT=$(( ${num_suites} + 1 ))
export NUM_OS_JUNIT_SUITES_IN_FLIGHT
}
readonly -f os::test::junit::declare_suite_start
# os::test::junit::declare_suite_end prints a message declaring the end of a test suite
# If there aren't any suites in flight, this function will fail.
#
# Globals:
# - JUNIT_REPORT_OUTPUT
# - NUM_OS_JUNIT_SUITES_IN_FLIGHT
# Arguments:
# - 1: the suite name that is starting
# Returns:
# - export/decrement NUM_OS_JUNIT_SUITES_IN_FLIGHT
function os::test::junit::declare_suite_end() {
local num_suites=${NUM_OS_JUNIT_SUITES_IN_FLIGHT:-0}
if [[ "${num_suites}" -lt "1" ]]; then
# we can't end a suite if none have been started yet
echo "[ERROR] jUnit suite marker could not be placed, expected suites in flight, got ${num_suites}"
return 1
fi
echo "=== END TEST SUITE ===" >> "${JUNIT_REPORT_OUTPUT:-/dev/null}"
NUM_OS_JUNIT_SUITES_IN_FLIGHT=$(( ${num_suites} - 1 ))
export NUM_OS_JUNIT_SUITES_IN_FLIGHT
}
readonly -f os::test::junit::declare_suite_end
# os::test::junit::declare_test_start prints a message declaring the start of a test case
# If there is already a test marked as being in flight, this function will fail.
#
# Globals:
# - JUNIT_REPORT_OUTPUT
# - NUM_OS_JUNIT_TESTS_IN_FLIGHT
# Arguments:
# None
# Returns:
# - increment NUM_OS_JUNIT_TESTS_IN_FLIGHT
function os::test::junit::declare_test_start() {
local num_tests=${NUM_OS_JUNIT_TESTS_IN_FLIGHT:-0}
if [[ "${num_tests}" -ne "0" ]]; then
# someone's declaring the starting of a test when a test is already in flight
echo "[ERROR] jUnit test marker could not be placed, expected no tests in flight, got ${num_tests}"
return 1
fi
local num_suites=${NUM_OS_JUNIT_SUITES_IN_FLIGHT:-0}
if [[ "${num_suites}" -lt "1" ]]; then
# we can't end a test if no suites are in flight
echo "[ERROR] jUnit test marker could not be placed, expected suites in flight, got ${num_suites}"
return 1
fi
echo "=== BEGIN TEST CASE ===" >> "${JUNIT_REPORT_OUTPUT:-/dev/null}"
NUM_OS_JUNIT_TESTS_IN_FLIGHT=$(( ${num_tests} + 1 ))
export NUM_OS_JUNIT_TESTS_IN_FLIGHT
}
readonly -f os::test::junit::declare_test_start
# os::test::junit::declare_test_end prints a message declaring the end of a test case
# If there is no test marked as being in flight, this function will fail.
#
# Globals:
# - JUNIT_REPORT_OUTPUT
# - NUM_OS_JUNIT_TESTS_IN_FLIGHT
# Arguments:
# None
# Returns:
# - decrement NUM_OS_JUNIT_TESTS_IN_FLIGHT
function os::test::junit::declare_test_end() {
local num_tests=${NUM_OS_JUNIT_TESTS_IN_FLIGHT:-0}
if [[ "${num_tests}" -ne "1" ]]; then
# someone's declaring the end of a test when a test is not in flight
echo "[ERROR] jUnit test marker could not be placed, expected one test in flight, got ${num_tests}"
return 1
fi
echo "=== END TEST CASE ===" >> "${JUNIT_REPORT_OUTPUT:-/dev/null}"
NUM_OS_JUNIT_TESTS_IN_FLIGHT=$(( ${num_tests} - 1 ))
export NUM_OS_JUNIT_TESTS_IN_FLIGHT
}
readonly -f os::test::junit::declare_test_end
# os::test::junit::check_test_counters checks that we do not have any test suites or test cases in flight
# This function should be called at the very end of any test script using jUnit markers to make sure no error in
# marking has occurred.
#
# Globals:
# - NUM_OS_JUNIT_SUITES_IN_FLIGHT
# - NUM_OS_JUNIT_TESTS_IN_FLIGHT
# Arguments:
# None
# Returns:
# None
function os::test::junit::check_test_counters() {
if [[ "${NUM_OS_JUNIT_SUITES_IN_FLIGHT-}" -ne "0" ]]; then
echo "[ERROR] Expected no test suites to be marked as in-flight at the end of testing, got ${NUM_OS_JUNIT_SUITES_IN_FLIGHT-}"
return 1
elif [[ "${NUM_OS_JUNIT_TESTS_IN_FLIGHT-}" -ne "0" ]]; then
echo "[ERROR] Expected no test cases to be marked as in-flight at the end of testing, got ${NUM_OS_JUNIT_TESTS_IN_FLIGHT-}"
return 1
fi
}
readonly -f os::test::junit::check_test_counters
# os::test::junit::reconcile_output appends the necessary suite and test end statements to the jUnit output file
# in order to ensure that the file is in a consistent state to allow for parsing
#
# Globals:
# - NUM_OS_JUNIT_SUITES_IN_FLIGHT
# - NUM_OS_JUNIT_TESTS_IN_FLIGHT
# Arguments:
# None
# Returns:
# None
function os::test::junit::reconcile_output() {
if [[ "${NUM_OS_JUNIT_TESTS_IN_FLIGHT:-0}" = "1" ]]; then
os::test::junit::declare_test_end
fi
for (( i = 0; i < ${NUM_OS_JUNIT_SUITES_IN_FLIGHT:-0}; i++ )); do
os::test::junit::declare_suite_end
done
}
readonly -f os::test::junit::reconcile_output
# os::test::junit::generate_report determines which type of report is to
# be generated and does so from the raw output of the tests.
#
# Globals:
# - JUNIT_REPORT_OUTPUT
# - ARTIFACT_DIR
# Arguments:
# None
# Returns:
# None
function os::test::junit::generate_report() {
if [[ -z "${JUNIT_REPORT_OUTPUT:-}" ||
-n "${JUNIT_REPORT_OUTPUT:-}" && ! -s "${JUNIT_REPORT_OUTPUT:-}" ]]; then
# we can't generate a report
return
fi
if grep -q "=== END TEST CASE ===" "${JUNIT_REPORT_OUTPUT}"; then
os::test::junit::reconcile_output
os::test::junit::check_test_counters
os::test::junit::internal::generate_report "oscmd"
else
os::test::junit::internal::generate_report "gotest"
fi
}
# os::test::junit::internal::generate_report generats an XML jUnit
# report for either `os::cmd` or `go test`, based on the passed
# argument. If the `junitreport` binary is not present, it will be built.
#
# Globals:
# - JUNIT_REPORT_OUTPUT
# - ARTIFACT_DIR
# Arguments:
# - 1: specify which type of tests command output should junitreport read
# Returns:
# export JUNIT_REPORT_NUM_FAILED
function os::test::junit::internal::generate_report() {
local report_type="$1"
os::util::ensure::built_binary_exists 'junitreport'
local report_file
report_file="$( mktemp "${ARTIFACT_DIR}/${report_type}_report_XXXXX" ).xml"
os::log::info "jUnit XML report placed at $( os::util::repository_relative_path ${report_file} )"
junitreport --type "${report_type}" \
--suites nested \
--roots github.com/openshift/origin \
--output "${report_file}" \
<"${JUNIT_REPORT_OUTPUT}"
local summary
summary=$( junitreport summarize <"${report_file}" )
JUNIT_REPORT_NUM_FAILED="$( grep -oE "[0-9]+ failed" <<<"${summary}" )"
export JUNIT_REPORT_NUM_FAILED
echo "${summary}"
}