|
| 1 | +package datadog.smoketest.springboot |
| 2 | + |
| 3 | +import static datadog.trace.agent.test.server.http.TestHttpServer.httpServer |
| 4 | +import static java.util.concurrent.TimeUnit.SECONDS |
| 5 | + |
| 6 | +import datadog.smoketest.AbstractSmokeTest |
| 7 | +import datadog.trace.agent.test.server.http.TestHttpServer |
| 8 | +import datadog.trace.agent.test.server.http.TestHttpServer.HandlerApi.RequestApi |
| 9 | +import datadog.trace.agent.test.utils.OkHttpUtils |
| 10 | +import datadog.trace.agent.test.utils.PortUtils |
| 11 | +import groovy.json.JsonOutput |
| 12 | +import groovy.json.JsonSlurper |
| 13 | +import okhttp3.MediaType |
| 14 | +import okhttp3.OkHttpClient |
| 15 | +import okhttp3.Request |
| 16 | +import okhttp3.RequestBody |
| 17 | +import spock.lang.AutoCleanup |
| 18 | +import spock.lang.Shared |
| 19 | +import spock.util.concurrent.PollingConditions |
| 20 | + |
| 21 | +/** The SDK with the Datadog Java agent flushes its pending events when the application exits. */ |
| 22 | +abstract class AbstractOpenFeatureShutdownSmokeTest extends AbstractSmokeTest { |
| 23 | + |
| 24 | + @Shared |
| 25 | + protected int httpPort = PortUtils.randomOpenPort() |
| 26 | + |
| 27 | + protected OkHttpClient client = OkHttpUtils.client() |
| 28 | + |
| 29 | + @Shared |
| 30 | + protected final ufcConfig = new JsonSlurper().parse(fetchResource("ffe-system-test-data/ufc-config.json")) as Map<String, Object> |
| 31 | + |
| 32 | + /** Serves the flag configuration as the Datadog managed CDN does. */ |
| 33 | + @Shared |
| 34 | + @AutoCleanup |
| 35 | + protected TestHttpServer cdn = httpServer { |
| 36 | + handlers { |
| 37 | + prefix("/api/v2/feature-flagging/config/rules-based/server") { |
| 38 | + response.status(200).send(JsonOutput.toJson([ |
| 39 | + data: [type: 'universal-flag-configuration', attributes: ufcConfig] |
| 40 | + ])) |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + /** @return whether the application runs with tracing enabled. */ |
| 46 | + abstract boolean tracing() |
| 47 | + |
| 48 | + def setupSpec() { |
| 49 | + PortUtils.waitForPortToOpen(httpPort, 240, SECONDS, testedProcess) |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + ProcessBuilder createProcessBuilder() { |
| 54 | + final springBootShadowJar = System.getProperty("datadog.smoketest.springboot.shadowJar.path") |
| 55 | + final command = [javaPath()] |
| 56 | + command.addAll(defaultJavaProperties) |
| 57 | + command.add("-Ddd.trace.enabled=${tracing()}".toString()) |
| 58 | + command.addAll(['-jar', springBootShadowJar, "--server.port=${httpPort}".toString()]) |
| 59 | + final builder = new ProcessBuilder(command).directory(new File(buildDirectory)) |
| 60 | + builder.environment().put('DD_FEATURE_FLAGS_CONFIGURATION_SOURCE_AGENTLESS_BASE_URL', "http://localhost:${cdn.address.port}".toString()) |
| 61 | + return builder |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + boolean testTelemetry() { |
| 66 | + return false |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + Closure decodedTracesCallback() { |
| 71 | + return {} |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + Closure decodedEvpProxyMessageCallback() { |
| 76 | + return { String path, RequestApi request -> |
| 77 | + if (!path.contains('api/v2/flagevaluation')) { |
| 78 | + return null |
| 79 | + } |
| 80 | + return [ |
| 81 | + origin: request.getHeader('DD-EVP-ORIGIN'), |
| 82 | + originVersion: request.getHeader('DD-EVP-ORIGIN-VERSION'), |
| 83 | + body: new JsonSlurper().parse(request.body) |
| 84 | + ] |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + void 'test pending flag evaluations are flushed at application exit'() { |
| 89 | + when: |
| 90 | + // The first evaluation is flushed within the 10 seconds flush interval, which then restarts: |
| 91 | + // the next evaluation stays pending until the application exits. |
| 92 | + evaluate('test-case-boolean-false-assignment.json') |
| 93 | + new PollingConditions(timeout: 20).eventually { |
| 94 | + assert evaluatedFlags().contains('boolean-false-assignment') |
| 95 | + } |
| 96 | + evaluate('test-case-boolean-one-of-matches.json') |
| 97 | + |
| 98 | + then: |
| 99 | + !evaluatedFlags().contains('boolean-one-of-matches') |
| 100 | + |
| 101 | + when: |
| 102 | + testedProcess.destroy() |
| 103 | + |
| 104 | + then: |
| 105 | + testedProcess.waitFor(30, SECONDS) |
| 106 | + evaluatedFlags().contains('boolean-one-of-matches') |
| 107 | + evaluationRequests().every { it.origin == 'dd-trace-java' && it.originVersion } |
| 108 | + } |
| 109 | + |
| 110 | + protected void evaluate(final String testCaseFile) { |
| 111 | + final testCase = (new JsonSlurper().parse(fetchResource("ffe-system-test-data/evaluation-cases/${testCaseFile}")) as List<Map<String, Object>>).first() |
| 112 | + final request = new Request.Builder() |
| 113 | + .url("http://localhost:${httpPort}/openfeature/evaluate") |
| 114 | + .post(RequestBody.create(MediaType.parse('application/json'), JsonOutput.toJson(testCase))) |
| 115 | + .build() |
| 116 | + final response = client.newCall(request).execute() |
| 117 | + try { |
| 118 | + assert response.code() == 200 |
| 119 | + } finally { |
| 120 | + response.close() |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + protected List<String> evaluatedFlags() { |
| 125 | + return evaluationRequests()*.body*.flagEvaluations.flatten()*.flag*.key as List<String> |
| 126 | + } |
| 127 | + |
| 128 | + protected List<Map<String, Object>> evaluationRequests() { |
| 129 | + return evpProxyMessages*.getV2() as List<Map<String, Object>> |
| 130 | + } |
| 131 | + |
| 132 | + protected static URL fetchResource(final String name) { |
| 133 | + return Thread.currentThread().getContextClassLoader().getResource(name) |
| 134 | + } |
| 135 | +} |
0 commit comments