|
| 1 | +package datadog.smoketest |
| 2 | + |
| 3 | +import datadog.smoketest.appsec.AbstractAppSecServerSmokeTest |
| 4 | +import datadog.trace.agent.test.utils.OkHttpUtils |
| 5 | +import okhttp3.Request |
| 6 | +import okhttp3.Response |
| 7 | +import spock.lang.Shared |
| 8 | + |
| 9 | +import java.nio.file.Files |
| 10 | + |
| 11 | +import static java.util.concurrent.TimeUnit.SECONDS |
| 12 | + |
| 13 | +class AppSecPlayNettySmokeTest extends AbstractAppSecServerSmokeTest { |
| 14 | + |
| 15 | + @Shared |
| 16 | + File playDirectory = new File("${buildDirectory}/stage/main") |
| 17 | + |
| 18 | + @Override |
| 19 | + ProcessBuilder createProcessBuilder() { |
| 20 | + // If the server is not shut down correctly, this file can be left there and will block |
| 21 | + // the start of a new test |
| 22 | + def runningPid = new File(playDirectory.getPath(), "RUNNING_PID") |
| 23 | + if (runningPid.exists()) { |
| 24 | + runningPid.delete() |
| 25 | + } |
| 26 | + def command = isWindows() ? 'main.bat' : 'main' |
| 27 | + ProcessBuilder processBuilder = new ProcessBuilder("${playDirectory}/bin/${command}") |
| 28 | + processBuilder.directory(playDirectory) |
| 29 | + processBuilder.environment().put("JAVA_OPTS", |
| 30 | + (defaultAppSecProperties + defaultJavaProperties).collect({ it.replace(' ', '\\ ')}).join(" ") |
| 31 | + + " -Dconfig.file=${playDirectory}/conf/application.conf" |
| 32 | + + " -Dhttp.port=${httpPort}" |
| 33 | + + " -Dhttp.address=127.0.0.1" |
| 34 | + + " -Dplay.server.provider=play.core.server.NettyServerProvider" |
| 35 | + + " -Ddd.writer.type=MultiWriter:TraceStructureWriter:${output.getAbsolutePath()},DDAgentWriter") |
| 36 | + return processBuilder |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + File createTemporaryFile() { |
| 41 | + return new File("${buildDirectory}/tmp/trace-structure-play-2.4-appsec-netty.out") |
| 42 | + } |
| 43 | + |
| 44 | + void 'API Security samples only one request per endpoint'() { |
| 45 | + given: |
| 46 | + def url = "http://localhost:${httpPort}/api_security/sampling/200?test=value" |
| 47 | + def client = OkHttpUtils.clientBuilder().build() |
| 48 | + def request = new Request.Builder() |
| 49 | + .url(url) |
| 50 | + .addHeader('X-My-Header', "value") |
| 51 | + .get() |
| 52 | + .build() |
| 53 | + |
| 54 | + when: |
| 55 | + List<Response> responses = (1..3).collect { |
| 56 | + client.newCall(request).execute() |
| 57 | + } |
| 58 | + |
| 59 | + then: |
| 60 | + responses.each { |
| 61 | + assert it.code() == 200 |
| 62 | + } |
| 63 | + waitForTraceCount(3) |
| 64 | + def spans = rootSpans.toList().toSorted { it.span.duration } |
| 65 | + spans.size() == 3 |
| 66 | + def sampledSpans = spans.findAll { it.meta.keySet().any { it.startsWith('_dd.appsec.s.req.') } } |
| 67 | + sampledSpans.size() == 1 |
| 68 | + def span = sampledSpans[0] |
| 69 | + span.meta.containsKey('_dd.appsec.s.req.query') |
| 70 | + span.meta.containsKey('_dd.appsec.s.req.headers') |
| 71 | + } |
| 72 | + |
| 73 | + // Ensure to clean up server and not only the shell script that starts it |
| 74 | + def cleanupSpec() { |
| 75 | + def pid = runningServerPid() |
| 76 | + if (pid) { |
| 77 | + def commands = isWindows() ? ['taskkill', '/PID', pid, '/T', '/F'] : ['kill', '-9', pid] |
| 78 | + new ProcessBuilder(commands).start().waitFor(10, SECONDS) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + def runningServerPid() { |
| 83 | + def runningPid = new File(playDirectory.getPath(), 'RUNNING_PID') |
| 84 | + if (runningPid.exists()) { |
| 85 | + return Files.lines(runningPid.toPath()).findAny().orElse(null) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + static isWindows() { |
| 90 | + return System.getProperty('os.name').toLowerCase().contains('win') |
| 91 | + } |
| 92 | +} |
0 commit comments