Atlas › Test

report_with_alerts

Exact test identity: go.temporal.io/server/tools/testrunner/TestJUnitXMLWellFormed/report_with_alerts

Package
go.temporal.io/server/tools/testrunner
Suite / test hierarchy
TestJUnitXMLWellFormed/report_with_alerts
Test
report_with_alerts
Introduced at
report_with_alerts, report_with_special_characters Frontier kind: Test frontier
Covered ranges
21
Covered lines
82
Covered files
2

Co-introduced tests

1 other test enter at the same concept.

Covered source

Expand a file to inspect source; the > gutter marks covered lines.

go.temporal.io/server/tools/testrunner/junit.go 77 covered LOC · 18 ranges

Open complete file

38 }
39
40 > func (j *junitReport) read() error { junit.go
41 > f, err := os.Open(j.path)
42 > if err != nil {
43 return fmt.Errorf("failed to open junit report file: %w", err)
44 }
45 > defer f.Close() junit.go
46 >
47 > decoder := xml.NewDecoder(f)
48 > if err = decoder.Decode(&j.Testsuites); err != nil {
49 return fmt.Errorf("failed to read junit report file: %w", err)
50 }
51 > return nil junit.go
52 }
53
76 }
77
78 > func generateFailure(kind failureType, data string) *junit.Result { junit.go
79 > return &junit.Result{
80 > Message: string(kind),
81 > Type: string(kind),
82 > Data: data,
83 > }
84 > }
85
86 > func (j *junitReport) write() error { junit.go
87 > f, err := os.Create(j.path)
88 > if err != nil {
89 return fmt.Errorf("failed to open junit report file: %w", err)
90 }
91 > defer f.Close() junit.go
92 >
93 > encoder := xml.NewEncoder(f)
94 > encoder.Indent("", " ")
95 > if err = encoder.Encode(j.Testsuites); err != nil {
96 return fmt.Errorf("failed to write junit report file: %w", err)
97 }
98 > log.Printf("wrote junit report to %s", j.path) junit.go
99 > return nil
100 }
101
130 // appendAlertsSuite adds a synthetic JUnit suite summarizing high-priority alerts
131 // (data races, panics, fatals) so that CI surfaces them prominently.
132 > func (j *junitReport) appendAlertsSuite(alerts []alert) { junit.go
133 > // Deduplicate by type+details to avoid noisy repeats across retries.
134 > alerts = dedupeAlerts(alerts)
135 > if len(alerts) == 0 {
136 return
137 }
138
139 // Convert alerts to JUnit test cases.
140 > var cases []junit.Testcase junit.go
141 > for _, a := range alerts {
142 > name := fmt.Sprintf("%s: %s", a.Type, a.Summary)
143 > if p := primaryTestName(a.Tests); p != "" {
144 > name = fmt.Sprintf("%s — in %s", name, p)
145 > }
146 > var sb strings.Builder
147 > if a.Details != "" {
148 > sb.WriteString(truncateAlertDetails(sanitizeXML(a.Details)))
149 > sb.WriteByte('\n')
150 > }
151 > if len(a.Tests) > 0 {
152 > fmt.Fprintf(&sb, "Detected in tests:\n\t%s", strings.Join(a.Tests, "\n\t"))
153 > }
154 > f := generateFailure(a.Type, strings.TrimRight(sb.String(), "\n"))
155 > cases = append(cases, junit.Testcase{
156 > Name: name,
157 > Failure: f,
158 > })
159 }
160
161 // Append the alerts suite to the report.
162 > suite := junit.Testsuite{ junit.go
163 > Name: alertsSuiteName,
164 > Failures: len(cases),
165 > Tests: len(cases),
166 > Testcases: cases,
167 > }
168 > j.Suites = append(j.Suites, suite)
169 > j.Failures += suite.Failures
170 > j.Tests += suite.Tests
171 }
172
174 // escapes <, >, & etc., but control characters other than \t, \n, \r are not
175 // legal XML and cause parsers to reject the document.
176 > func sanitizeXML(s string) string { junit.go
177 > return strings.Map(func(r rune) rune {
178 > switch r {
179 > case '\t', '\n', '\r':
180 > return r
181 case 0xFFFE, 0xFFFF:
182 return -1 // Reserved Unicode noncharacters; disallowed in XML 1.0.
183 }
184 > if r < 0x20 { junit.go
185 // 0x20 is space; lower code points are ASCII control characters.
186 return -1
187 }
188 > return r junit.go
189 }, s)
190 }
191
192 // truncateAlertDetails keeps alert payloads from bloating the JUnit artifact.
193 > func truncateAlertDetails(s string) string { junit.go
194 > if len(s) <= junitAlertDetailsMaxBytes {
195 > return s junit.go
196 > }
197 const marker = "\n... (truncated) ...\n"
198 return s[:junitAlertDetailsMaxBytes-len(marker)] + marker
201 // dedupeAlerts removes duplicate alerts (e.g., repeated across retries) based
202 // on type and details while preserving the first-seen order.
203 > func dedupeAlerts(alerts []alert) []alert { junit.go
204 > seen := make(map[string]struct{}, len(alerts))
205 > var out []alert
206 > for _, a := range alerts {
207 > key := string(a.Type) + "\n" + a.Details
208 > if _, ok := seen[key]; ok {
209 continue
210 }
211 > seen[key] = struct{}{} junit.go
212 > out = append(out, a)
213 }
214 > return out junit.go
215 }
216
go.temporal.io/server/tools/testrunner/log.go 5 covered LOC · 3 ranges

Open complete file

86 // 1) Fully-qualified test name containing ".Test"
87 // 2) First detected test name
88 > func primaryTestName(tests []string) string { log.go
89 > if len(tests) == 0 {
90 return ""
91 }
92 > for _, t := range tests { log.go
93 > if strings.Contains(t, ".Test") {
94 return t
95 }
96 }
97 > return tests[0] log.go
98 }
99