Skip to content

Commit 74db9b7

Browse files
committed
Add E2E tests for Log4j2
1 parent b2ab4eb commit 74db9b7

File tree

4 files changed

+147
-2
lines changed

4 files changed

+147
-2
lines changed
Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,87 @@
1+
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
2+
13
plugins {
24
java
35
application
6+
kotlin("jvm")
47
alias(libs.plugins.gradle.versions)
8+
id("com.github.johnrengelman.shadow") version "8.1.1"
59
}
610

711
application { mainClass.set("io.sentry.samples.log4j2.Main") }
812

13+
java.sourceCompatibility = JavaVersion.VERSION_17
14+
15+
java.targetCompatibility = JavaVersion.VERSION_17
16+
17+
repositories { mavenCentral() }
18+
919
configure<JavaPluginExtension> {
10-
sourceCompatibility = JavaVersion.VERSION_1_8
11-
targetCompatibility = JavaVersion.VERSION_1_8
20+
sourceCompatibility = JavaVersion.VERSION_17
21+
targetCompatibility = JavaVersion.VERSION_17
22+
}
23+
24+
tasks.withType<KotlinCompile>().configureEach {
25+
kotlinOptions.jvmTarget = JavaVersion.VERSION_17.toString()
26+
}
27+
28+
tasks.withType<KotlinCompile>().configureEach {
29+
kotlinOptions {
30+
freeCompilerArgs = listOf("-Xjsr305=strict")
31+
jvmTarget = JavaVersion.VERSION_17.toString()
32+
}
1233
}
1334

1435
dependencies {
1536
implementation(projects.sentryLog4j2)
1637
implementation(libs.log4j.api)
1738
implementation(libs.log4j.core)
39+
40+
testImplementation(kotlin(Config.kotlinStdLib))
41+
testImplementation(projects.sentry)
42+
testImplementation(projects.sentrySystemTestSupport)
43+
testImplementation(libs.kotlin.test.junit)
44+
testImplementation(libs.slf4j.api)
45+
testImplementation(libs.slf4j.jdk14)
46+
}
47+
48+
// Configure the Shadow JAR (executable JAR with all dependencies)
49+
tasks.shadowJar {
50+
manifest { attributes["Main-Class"] = "io.sentry.samples.log4j2.Main" }
51+
archiveClassifier.set("") // Remove the classifier so it replaces the regular JAR
52+
mergeServiceFiles()
53+
// Use Log4j2 cache transformer to properly handle plugin files
54+
transform(com.github.jengelman.gradle.plugins.shadow.transformers.Log4j2PluginsCacheFileTransformer())
55+
}
56+
57+
// Make the regular jar task depend on shadowJar
58+
tasks.jar {
59+
enabled = false
60+
dependsOn(tasks.shadowJar)
61+
}
62+
63+
// Fix the startScripts task dependency
64+
tasks.startScripts { dependsOn(tasks.shadowJar) }
65+
66+
configure<SourceSetContainer> { test { java.srcDir("src/test/java") } }
67+
68+
tasks.register<Test>("systemTest").configure {
69+
group = "verification"
70+
description = "Runs the System tests"
71+
72+
outputs.upToDateWhen { false }
73+
74+
maxParallelForks = 1
75+
76+
// Cap JVM args per test
77+
minHeapSize = "128m"
78+
maxHeapSize = "1g"
79+
80+
filter { includeTestsMatching("io.sentry.systemtest*") }
81+
}
82+
83+
tasks.named("test").configure {
84+
require(this is Test)
85+
86+
filter { excludeTestsMatching("io.sentry.systemtest.*") }
1887
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.sentry
2+
3+
import kotlin.test.Test
4+
import kotlin.test.assertTrue
5+
6+
class DummyTest {
7+
@Test
8+
fun `the only test`() {
9+
// only needed to have more than 0 tests and not fail the build
10+
assertTrue(true)
11+
}
12+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package io.sentry.systemtest.io.sentry.systemtest
2+
3+
import io.sentry.SentryLevel
4+
import io.sentry.systemtest.util.TestHelper
5+
import org.junit.Assert
6+
import org.junit.Before
7+
import org.junit.Test
8+
import java.util.concurrent.TimeUnit
9+
10+
class ConsoleApplicationSystemTest {
11+
lateinit var testHelper: TestHelper
12+
13+
@Before
14+
fun setup() {
15+
testHelper = TestHelper("http://localhost:8000")
16+
testHelper.reset()
17+
}
18+
19+
@Test
20+
fun `log4j2 application sends expected events when run as JAR`() {
21+
val jarFile = testHelper.findJar("sentry-samples-log4j2")
22+
val process =
23+
testHelper.launch(
24+
jarFile,
25+
mapOf(
26+
"SENTRY_DSN" to testHelper.dsn,
27+
"SENTRY_TRACES_SAMPLE_RATE" to "1.0",
28+
"SENTRY_ENABLE_PRETTY_SERIALIZATION_OUTPUT" to "false",
29+
"SENTRY_DEBUG" to "true",
30+
),
31+
)
32+
33+
process.waitFor(30, TimeUnit.SECONDS)
34+
Assert.assertEquals(0, process.exitValue())
35+
36+
// Verify that we received the expected events
37+
verifyExpectedEvents()
38+
}
39+
40+
private fun verifyExpectedEvents() {
41+
// Verify we received the RuntimeException
42+
testHelper.ensureErrorReceived { event ->
43+
event.exceptions?.any { ex -> ex.type == "RuntimeException" && ex.value == "Invalid productId=445" } ==
44+
true &&
45+
event.message?.formatted == "Something went wrong" &&
46+
event.level?.name == "ERROR"
47+
}
48+
49+
testHelper.ensureErrorReceived { event ->
50+
event.breadcrumbs?.firstOrNull { it.message == "Hello Sentry!" && it.level == SentryLevel.DEBUG } != null
51+
}
52+
53+
testHelper.ensureErrorReceived { event ->
54+
event.breadcrumbs?.firstOrNull { it.message == "User has made a purchase of product: 445" && it.level == SentryLevel.INFO } != null
55+
}
56+
57+
testHelper.ensureLogsReceived { logs, _ ->
58+
testHelper.doesContainLogWithBody(logs, "User has made a purchase of product: 445") &&
59+
testHelper.doesContainLogWithBody(logs, "Something went wrong")
60+
}
61+
}
62+
}

test/system-test-runner.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,8 @@ def get_available_modules(self) -> List[ModuleConfig]:
588588
ModuleConfig("sentry-samples-spring-boot-jakarta-opentelemetry", "true", "false", "false"),
589589
ModuleConfig("sentry-samples-console", "false", "true", "false"),
590590
ModuleConfig("sentry-samples-console-opentelemetry-noagent", "false", "true", "false"),
591+
ModuleConfig("sentry-samples-logback", "false", "true", "false"),
592+
ModuleConfig("sentry-samples-log4j2", "false", "true", "false"),
591593
]
592594

593595
def _find_module_number(self, module_name: str, agent: str, auto_init: str) -> int:

0 commit comments

Comments
 (0)