Skip to content

Commit 1d1c8e1

Browse files
committed
Test troubleshooting
1 parent 6c240c5 commit 1d1c8e1

File tree

4 files changed

+60
-10
lines changed

4 files changed

+60
-10
lines changed

.gitlab/generate-appsec.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@
145145
after_script:
146146
- mkdir -p "${CI_PROJECT_DIR}/artifacts"
147147
- find appsec/tests/integration/build/test-results -name "*.xml" -exec cp --parents '{}' "${CI_PROJECT_DIR}/artifacts/" \;
148+
- cp -r appsec/tests/integration/build/test-logs "${CI_PROJECT_DIR}/artifacts/" 2>/dev/null || true
148149
- .gitlab/upload-junit-to-datadog.sh "test.source.file:appsec"
149150
artifacts:
150151
reports:

appsec/tests/integration/build.gradle

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ dependencies {
3636
implementation 'org.msgpack:msgpack-core:0.9.9'
3737
implementation 'io.javalin:javalin:6.7.0'
3838

39-
implementation platform('org.testcontainers:testcontainers-bom:1.21.3')
40-
implementation 'org.testcontainers:mysql'
41-
implementation "org.testcontainers:junit-jupiter"
39+
implementation platform('org.testcontainers:testcontainers-bom:2.0.3')
40+
implementation 'org.testcontainers:testcontainers-mysql'
41+
implementation "org.testcontainers:testcontainers-junit-jupiter"
42+
implementation 'org.hamcrest:hamcrest:2.2'
4243
implementation 'com.flipkart.zjsonpatch:zjsonpatch:0.4.16'
4344
implementation 'org.junit.jupiter:junit-jupiter-engine:5.12.2'
4445
implementation 'org.junit.jupiter:junit-jupiter-params:5.12.2'
@@ -93,6 +94,11 @@ def creationDateOf(String image) {
9394
Date volumeDate = Date.from(OffsetDateTime.parse(imageModifiedStr).toInstant())
9495
volumeDate.time
9596
}
97+
def libddwafCommit() {
98+
def proc = ['git', '-C', "${projectDir}/../../third_party/libddwaf", 'rev-parse', 'HEAD'].execute()
99+
proc.waitForOrKill(5_000)
100+
proc.text.trim()
101+
}
96102

97103
def createVolumeTask = { String volumeName ->
98104
String taskName = "createVolume-$volumeName"
@@ -346,17 +352,17 @@ def buildAppSecTask = { String version, String variant ->
346352
],
347353
command: [
348354
'-e', '-c',
349-
'''
350-
git config --global --add safe.directory '*'
355+
"""
351356
cd /appsec
352357
test -f CMakeCache.txt || \\
353358
cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo \\
354359
-DCMAKE_INSTALL_PREFIX=/appsec \\
355360
-DDD_APPSEC_ENABLE_PATCHELF_LIBC=ON \\
361+
-DGIT_COMMIT=${libddwafCommit()} \\
356362
-DDD_APPSEC_TESTING=ON /project/appsec
357363
make -j extension ddappsec-helper && \\
358364
touch ddappsec.so libddappsec-helper.so
359-
'''
365+
"""
360366
]
361367
)
362368
}

appsec/tests/integration/src/main/groovy/com/datadog/appsec/php/docker/AppSecContainer.groovy

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ import java.util.function.Supplier
4444
class AppSecContainer<SELF extends AppSecContainer<SELF>> extends GenericContainer<SELF> {
4545
private final static Logger DOCKER_OUTPUT_LOGGER = LoggerFactory.getLogger('docker')
4646
private String imageName
47-
private Slf4jLogConsumer logConsumer
47+
private Consumer<OutputFrame> logConsumer
4848
private File logsDir
49+
// Set to true to include sidecar.log in stdout (very verbose)
50+
private static final boolean TAIL_SIDECAR_LOG = false
4951
private String wwwDir
5052
private String wwwSrcDir
5153
public final HttpClient httpClient = HttpClient.newBuilder()
@@ -89,7 +91,7 @@ class AppSecContainer<SELF extends AppSecContainer<SELF>> extends GenericContain
8991
withEnv 'DD_TRACE_GIT_METADATA_ENABLED', '0'
9092
withEnv 'DD_INSTRUMENTATION_TELEMETRY_ENABLED', '1'
9193
// very verbose:
92-
// withEnv '_DD_DEBUG_SIDECAR_LOG_METHOD', 'file:///tmp/logs/sidecar.log'
94+
withEnv '_DD_DEBUG_SIDECAR_LOG_METHOD', 'file:///tmp/logs/sidecar.log'
9395
withEnv 'DD_SPAWN_WORKER_USE_EXEC', '1' // gdb fails following child with fdexec
9496
withEnv 'DD_TELEMETRY_HEARTBEAT_INTERVAL', '10'
9597
withEnv 'DD_TELEMETRY_EXTENDED_HEARTBEAT_INTERVAL', '10'
@@ -104,12 +106,53 @@ class AppSecContainer<SELF extends AppSecContainer<SELF>> extends GenericContain
104106
@Override
105107
protected void doStart() {
106108
super.doStart()
107-
this.logConsumer = new Slf4jLogConsumer(DOCKER_OUTPUT_LOGGER)
109+
this.logConsumer = createLogConsumer()
108110
followOutput(logConsumer)
109111
overlayWww()
110112
runInitialize()
111113
}
112114

115+
private Consumer<OutputFrame> createLogConsumer() {
116+
Slf4jLogConsumer baseConsumer = new Slf4jLogConsumer(DOCKER_OUTPUT_LOGGER)
117+
if (TAIL_SIDECAR_LOG) {
118+
return baseConsumer
119+
}
120+
// Filter out sidecar.log content from stdout
121+
// tail -F outputs "==> /path/to/file <==" when switching files
122+
new SidecarLogFilteringConsumer(baseConsumer)
123+
}
124+
125+
@CompileStatic
126+
private static class SidecarLogFilteringConsumer implements Consumer<OutputFrame> {
127+
private final Consumer<OutputFrame> delegate
128+
private volatile boolean inSidecarLog = false
129+
130+
SidecarLogFilteringConsumer(Consumer<OutputFrame> delegate) {
131+
this.delegate = delegate
132+
}
133+
134+
@Override
135+
void accept(OutputFrame frame) {
136+
String text = frame.utf8String
137+
if (text == null) {
138+
delegate.accept(frame)
139+
return
140+
}
141+
142+
// Check for tail -F file header
143+
if (text.contains('==>') && text.contains('<==')) {
144+
inSidecarLog = text.contains('sidecar.log')
145+
if (inSidecarLog) {
146+
return // Skip the sidecar.log header line
147+
}
148+
}
149+
150+
if (!inSidecarLog) {
151+
delegate.accept(frame)
152+
}
153+
}
154+
}
155+
113156
Object nextCapturedTrace() {
114157
mockDatadogAgent.nextTrace(15000)
115158
}

0 commit comments

Comments
 (0)