Skip to content

Commit 2dbdf6c

Browse files
Add E2E tests for Logback (#4599)
* Add E2E tests for Logback * Format code * code review fixes * enable system tests for logback on GH --------- Co-authored-by: Sentry Github Bot <[email protected]>
1 parent 2091db3 commit 2dbdf6c

File tree

5 files changed

+148
-2
lines changed

5 files changed

+148
-2
lines changed

.github/workflows/system-tests-backend.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ jobs:
5454
- sample: "sentry-samples-console"
5555
agent: "false"
5656
agent-auto-init: "true"
57+
- sample: "sentry-samples-logback"
58+
agent: "false"
59+
agent-auto-init: "true"
5760
steps:
5861
- uses: actions/checkout@v4
5962
with:
Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,80 @@
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.logback.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 {
26+
freeCompilerArgs = listOf("-Xjsr305=strict")
27+
jvmTarget = JavaVersion.VERSION_17.toString()
28+
}
1229
}
1330

1431
dependencies {
1532
implementation(projects.sentryLogback)
1633
implementation(libs.logback.classic)
34+
35+
testImplementation(kotlin(Config.kotlinStdLib))
36+
testImplementation(projects.sentry)
37+
testImplementation(projects.sentrySystemTestSupport)
38+
testImplementation(libs.kotlin.test.junit)
39+
testImplementation(libs.slf4j.api)
40+
testImplementation(libs.slf4j.jdk14)
41+
}
42+
43+
// Configure the Shadow JAR (executable JAR with all dependencies)
44+
tasks.shadowJar {
45+
manifest { attributes["Main-Class"] = "io.sentry.samples.logback.Main" }
46+
archiveClassifier.set("") // Remove the classifier so it replaces the regular JAR
47+
mergeServiceFiles()
48+
}
49+
50+
// Make the regular jar task depend on shadowJar
51+
tasks.jar {
52+
enabled = false
53+
dependsOn(tasks.shadowJar)
54+
}
55+
56+
// Fix the startScripts task dependency
57+
tasks.startScripts { dependsOn(tasks.shadowJar) }
58+
59+
configure<SourceSetContainer> { test { java.srcDir("src/test/java") } }
60+
61+
tasks.register<Test>("systemTest").configure {
62+
group = "verification"
63+
description = "Runs the System tests"
64+
65+
outputs.upToDateWhen { false }
66+
67+
maxParallelForks = 1
68+
69+
// Cap JVM args per test
70+
minHeapSize = "128m"
71+
maxHeapSize = "1g"
72+
73+
filter { includeTestsMatching("io.sentry.systemtest*") }
74+
}
75+
76+
tasks.named("test").configure {
77+
require(this is Test)
78+
79+
filter { excludeTestsMatching("io.sentry.systemtest.*") }
1780
}
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package io.sentry.systemtest
2+
3+
import io.sentry.SentryLevel
4+
import io.sentry.systemtest.util.TestHelper
5+
import java.util.concurrent.TimeUnit
6+
import org.junit.Assert
7+
import org.junit.Before
8+
import org.junit.Test
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 `logback application sends expected events when run as JAR`() {
21+
val jarFile = testHelper.findJar("sentry-samples-logback")
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 ->
44+
ex.type == "RuntimeException" && ex.value == "Invalid productId=445"
45+
} == true &&
46+
event.message?.formatted == "Something went wrong" &&
47+
event.level?.name == "ERROR"
48+
}
49+
50+
testHelper.ensureErrorReceived { event ->
51+
event.breadcrumbs?.firstOrNull {
52+
it.message == "Hello Sentry!" && it.level == SentryLevel.DEBUG
53+
} != null
54+
}
55+
56+
testHelper.ensureErrorReceived { event ->
57+
event.breadcrumbs?.firstOrNull {
58+
it.message == "User has made a purchase of product: 445" && it.level == SentryLevel.INFO
59+
} != null
60+
}
61+
62+
testHelper.ensureLogsReceived { logs, _ ->
63+
testHelper.doesContainLogWithBody(logs, "User has made a purchase of product: 445") &&
64+
testHelper.doesContainLogWithBody(logs, "Something went wrong")
65+
}
66+
}
67+
}

test/system-test-runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,7 @@ def get_available_modules(self) -> List[ModuleConfig]:
614614
ModuleConfig("sentry-samples-spring-boot-jakarta-opentelemetry", "true", "false", "false"),
615615
ModuleConfig("sentry-samples-console", "false", "true", "false"),
616616
ModuleConfig("sentry-samples-console-opentelemetry-noagent", "false", "true", "false"),
617+
ModuleConfig("sentry-samples-logback", "false", "true", "false"),
617618
]
618619

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

0 commit comments

Comments
 (0)