Skip to content

Commit a8cb434

Browse files
Mark JUnit 5 setup and teardown action spans as failed if there is an error
1 parent 36fb475 commit a8cb434

File tree

17 files changed

+985
-14
lines changed

17 files changed

+985
-14
lines changed

dd-java-agent/agent-ci-visibility/src/testFixtures/groovy/datadog/trace/civisibility/CiVisibilityInstrumentationTest.groovy

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -263,15 +263,13 @@ abstract class CiVisibilityInstrumentationTest extends AgentTestRunner {
263263
] + replacements
264264

265265
// uncomment to generate expected data templates
266-
// def baseTemplatesPath = CiVisibilityInstrumentationTest.classLoader
267-
// .getResource("test-succeed")
268-
// .toURI()
269-
// .schemeSpecificPart
270-
// .replace('build/resources/test', 'src/test/resources')
271-
// .replace('build/resources/latestDepTest', 'src/test/resources')
272-
// .replace("test-succeed", testcaseName)
273-
// CiVisibilityTestUtils.generateTemplates(baseTemplatesPath, events, coverages, additionalReplacements)
274-
// return [:]
266+
// def clazz = this.getClass()
267+
// def resourceName = clazz.name.replace('.', '/') + ".class"
268+
// def classfilePath = clazz.getResource(resourceName).toURI().schemeSpecificPart
269+
// def modulePath = classfilePath.substring(0, classfilePath.indexOf("/build/classes"))
270+
// def baseTemplatesPath = modulePath + "/src/test/resources/" + testcaseName
271+
// CiVisibilityTestUtils.generateTemplates(baseTemplatesPath, events, coverages, additionalReplacements)
272+
// return [:]
275273

276274
return CiVisibilityTestUtils.assertData(testcaseName, events, coverages, additionalReplacements)
277275
}

dd-java-agent/instrumentation/junit-5.3/junit-5.8/src/main/java/datadog/trace/instrumentation/junit5/BeforeAfterOperationsTracer.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ private static void traceInvocation(
5353
agentSpan.setTag(Tags.TEST_CALLBACK, operationName);
5454
try (AgentScope agentScope = AgentTracer.activateSpan(agentSpan)) {
5555
invocation.proceed();
56+
} catch (Throwable t) {
57+
agentSpan.addThrowable(t);
58+
throw t;
5659
} finally {
5760
agentSpan.finish();
5861
}

dd-java-agent/instrumentation/junit-5.3/junit-5.8/src/test/groovy/JUnit58Test.groovy

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ class JUnit58Test extends CiVisibilityInstrumentationTest {
2222
testcaseName | tests | expectedTracesCount
2323
"test-before-each-after-each" | [TestSucceedBeforeEachAfterEach] | 2
2424
"test-before-all-after-all" | [TestSucceedBeforeAllAfterAll] | 2
25+
"test-failed-before-all" | [TestFailedBeforeAll] | 2
26+
"test-failed-after-all" | [TestFailedAfterAll] | 2
27+
"test-failed-before-each" | [TestFailedBeforeEach] | 2
28+
"test-failed-after-each" | [TestFailedAfterEach] | 2
2529
}
2630

2731
private static void runTests(List<Class<?>> tests) {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.example;
2+
3+
import static org.junit.jupiter.api.Assertions.assertTrue;
4+
5+
import org.junit.jupiter.api.AfterAll;
6+
import org.junit.jupiter.api.Test;
7+
8+
public class TestFailedAfterAll {
9+
10+
@AfterAll
11+
public static void tearDown() {
12+
throw new RuntimeException("suite teardown failed");
13+
}
14+
15+
@Test
16+
public void test_succeed() {
17+
assertTrue(true);
18+
}
19+
20+
@Test
21+
public void another_test_succeed() {
22+
assertTrue(true);
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.example;
2+
3+
import static org.junit.jupiter.api.Assertions.assertTrue;
4+
5+
import org.junit.jupiter.api.AfterEach;
6+
import org.junit.jupiter.api.Test;
7+
8+
public class TestFailedAfterEach {
9+
10+
@AfterEach
11+
public void tearDown() {
12+
throw new RuntimeException("testcase teardown failed");
13+
}
14+
15+
@Test
16+
public void test_succeed() {
17+
assertTrue(true);
18+
}
19+
20+
@Test
21+
public void another_test_succeed() {
22+
assertTrue(true);
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.example;
2+
3+
import static org.junit.jupiter.api.Assertions.assertTrue;
4+
5+
import org.junit.jupiter.api.BeforeAll;
6+
import org.junit.jupiter.api.Test;
7+
8+
public class TestFailedBeforeAll {
9+
10+
@BeforeAll
11+
public static void setUp() {
12+
throw new RuntimeException("suite setup failed");
13+
}
14+
15+
@Test
16+
public void test_succeed() {
17+
assertTrue(true);
18+
}
19+
20+
@Test
21+
public void another_test_succeed() {
22+
assertTrue(true);
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.example;
2+
3+
import static org.junit.jupiter.api.Assertions.assertTrue;
4+
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
8+
public class TestFailedBeforeEach {
9+
10+
@BeforeEach
11+
public void setUp() {
12+
throw new RuntimeException("testcase setup failed");
13+
}
14+
15+
@Test
16+
public void test_succeed() {
17+
assertTrue(true);
18+
}
19+
20+
@Test
21+
public void another_test_succeed() {
22+
assertTrue(true);
23+
}
24+
}

dd-java-agent/instrumentation/junit-5.3/junit-5.8/src/test/resources/test-before-all-after-all/events.ftl

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"_dd.trace_span_attribute_schema" : 0
1717
},
1818
"meta" : {
19+
"_dd.p.tid" : ${content_meta__dd_p_tid},
1920
"test.type" : "test",
2021
"_dd.tracer_host" : ${content_meta__dd_tracer_host},
2122
"test.status" : "pass",
@@ -46,9 +47,10 @@
4647
"duration" : ${content_duration_2},
4748
"error" : 0,
4849
"metrics" : {
49-
"_dd.host.vcpu_count" : ${content_metrics__dd_host_vcpu_count}
50+
"_dd.host.vcpu_count" : ${content_metrics__dd_host_vcpu_count_2}
5051
},
5152
"meta" : {
53+
"_dd.p.tid" : ${content_meta__dd_p_tid_2},
5254
"test.type" : "test",
5355
"test.module" : "junit-5.8",
5456
"test.status" : "pass",
@@ -76,16 +78,20 @@
7678
"duration" : ${content_duration_3},
7779
"error" : 0,
7880
"metrics" : {
79-
"_dd.host.vcpu_count" : ${content_metrics__dd_host_vcpu_count}
81+
"_dd.host.vcpu_count" : ${content_metrics__dd_host_vcpu_count_3},
82+
"test.source.end" : 19,
83+
"test.source.start" : 11
8084
},
8185
"meta" : {
86+
"_dd.p.tid" : ${content_meta__dd_p_tid_3},
8287
"test.type" : "test",
8388
"test.source.file" : "dummy_source_path",
8489
"test.module" : "junit-5.8",
8590
"test.status" : "pass",
8691
"test_session.name" : "session-name",
8792
"env" : "none",
8893
"dummy_ci_tag" : "dummy_ci_tag_value",
94+
"test.codeowners" : "[\"owner1\",\"owner2\"]",
8995
"library_version" : ${content_meta_library_version},
9096
"component" : "junit",
9197
"span.kind" : "test_suite_end",
@@ -114,7 +120,7 @@
114120
"process_id" : ${content_metrics_process_id},
115121
"_dd.profiling.enabled" : 0,
116122
"_dd.trace_span_attribute_schema" : 0,
117-
"_dd.host.vcpu_count" : ${content_metrics__dd_host_vcpu_count},
123+
"_dd.host.vcpu_count" : ${content_metrics__dd_host_vcpu_count_4},
118124
"test.source.end" : 18,
119125
"test.source.start" : 12
120126
},
@@ -161,7 +167,7 @@
161167
"process_id" : ${content_metrics_process_id},
162168
"_dd.profiling.enabled" : 0,
163169
"_dd.trace_span_attribute_schema" : 0,
164-
"_dd.host.vcpu_count" : ${content_metrics__dd_host_vcpu_count},
170+
"_dd.host.vcpu_count" : ${content_metrics__dd_host_vcpu_count_5},
165171
"test.source.end" : 18,
166172
"test.source.start" : 12
167173
},
@@ -203,6 +209,7 @@
203209
"error" : 0,
204210
"metrics" : { },
205211
"meta" : {
212+
"_dd.p.tid" : ${content_meta__dd_p_tid_4},
206213
"test.callback" : "BeforeAll",
207214
"library_version" : ${content_meta_library_version},
208215
"env" : "none"
@@ -223,6 +230,7 @@
223230
"error" : 0,
224231
"metrics" : { },
225232
"meta" : {
233+
"_dd.p.tid" : ${content_meta__dd_p_tid_5},
226234
"test.callback" : "AfterAll",
227235
"library_version" : ${content_meta_library_version},
228236
"env" : "none"

dd-java-agent/instrumentation/junit-5.3/junit-5.8/src/test/resources/test-before-each-after-each/events.ftl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"_dd.trace_span_attribute_schema" : 0
1717
},
1818
"meta" : {
19+
"_dd.p.tid" : ${content_meta__dd_p_tid},
1920
"test.type" : "test",
2021
"_dd.tracer_host" : ${content_meta__dd_tracer_host},
2122
"test.status" : "pass",
@@ -49,6 +50,7 @@
4950
"_dd.host.vcpu_count" : ${content_metrics__dd_host_vcpu_count_2}
5051
},
5152
"meta" : {
53+
"_dd.p.tid" : ${content_meta__dd_p_tid_2},
5254
"test.type" : "test",
5355
"test.module" : "junit-5.8",
5456
"test.status" : "pass",
@@ -76,16 +78,20 @@
7678
"duration" : ${content_duration_3},
7779
"error" : 0,
7880
"metrics" : {
79-
"_dd.host.vcpu_count" : ${content_metrics__dd_host_vcpu_count_3}
81+
"_dd.host.vcpu_count" : ${content_metrics__dd_host_vcpu_count_3},
82+
"test.source.end" : 19,
83+
"test.source.start" : 11
8084
},
8185
"meta" : {
86+
"_dd.p.tid" : ${content_meta__dd_p_tid_3},
8287
"test.type" : "test",
8388
"test.source.file" : "dummy_source_path",
8489
"test.module" : "junit-5.8",
8590
"test.status" : "pass",
8691
"test_session.name" : "session-name",
8792
"env" : "none",
8893
"dummy_ci_tag" : "dummy_ci_tag_value",
94+
"test.codeowners" : "[\"owner1\",\"owner2\"]",
8995
"library_version" : ${content_meta_library_version},
9096
"component" : "junit",
9197
"span.kind" : "test_suite_end",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[ ]

0 commit comments

Comments
 (0)