Fix test for v0.4 endpoint

Agent returns an error if no content sent, so test was failing.  Need to send empty content for test to succeed.
This commit is contained in:
Tyler Benson 2018-02-26 13:50:32 +10:00
parent 92e0ede2c0
commit ac1b57d7a2
3 changed files with 46 additions and 25 deletions

View File

@ -52,7 +52,7 @@ test {
}
if (project.hasProperty("disableShadowRelocate") && disableShadowRelocate) {
exclude 'datadog/trace/agent/ShadowPackageRenamingTest.class'
exclude 'datadog/trace/agent/integration/classloading/ShadowPackageRenamingTest.class'
}
}

View File

@ -10,10 +10,10 @@ import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@ -40,8 +40,8 @@ public class DDApi {
private final ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
public DDApi(final String host, final int port) {
if (endpointAvailable("http://" + host + ":" + port + TRACES_ENDPOINT_V4)
&& endpointAvailable("http://" + host + ":" + port + SERVICES_ENDPOINT_V4)) {
if (traceEndpointAvailable("http://" + host + ":" + port + TRACES_ENDPOINT_V4)
&& serviceEndpointAvailable("http://" + host + ":" + port + SERVICES_ENDPOINT_V4)) {
this.tracesEndpoint = "http://" + host + ":" + port + TRACES_ENDPOINT_V4;
this.servicesEndpoint = "http://" + host + ":" + port + SERVICES_ENDPOINT_V4;
} else {
@ -163,11 +163,15 @@ public class DDApi {
}
}
private boolean endpointAvailable(final String endpoint) {
return endpointAvailable(endpoint, true);
private boolean traceEndpointAvailable(final String endpoint) {
return endpointAvailable(endpoint, Collections.emptyList(), true);
}
private boolean endpointAvailable(final String endpoint, final boolean retry) {
private boolean serviceEndpointAvailable(final String endpoint) {
return endpointAvailable(endpoint, Collections.emptyMap(), true);
}
private boolean endpointAvailable(final String endpoint, final Object data, final boolean retry) {
try {
final HttpURLConnection httpCon = getHttpURLConnection(endpoint);
@ -175,13 +179,14 @@ public class DDApi {
httpCon.setConnectTimeout((int) TimeUnit.SECONDS.toMillis(1));
httpCon.setReadTimeout((int) TimeUnit.SECONDS.toMillis(1));
final OutputStreamWriter out = new OutputStreamWriter(httpCon.getOutputStream());
final OutputStream out = httpCon.getOutputStream();
objectMapper.writeValue(out, data);
out.flush();
out.close();
return httpCon.getResponseCode() == 200;
} catch (final IOException e) {
if (retry) {
return endpointAvailable(endpoint, false);
return endpointAvailable(endpoint, data, false);
}
}
return false;

View File

@ -30,10 +30,12 @@ class DDApiTest extends Specification {
def agent = ratpack {
handlers {
put("v0.4/traces") {
response.status(200).send()
def status = request.contentLength > 0 ? 200 : 500
response.status(status).send()
}
put("v0.4/services") {
response.status(200).send()
def status = request.contentLength > 0 ? 200 : 500
response.status(status).send()
}
}
}
@ -56,7 +58,8 @@ class DDApiTest extends Specification {
response.status(404).send()
}
put("v0.4/services") {
response.status(200).send()
def status = request.contentLength > 0 ? 200 : 500
response.status(status).send()
}
}
}
@ -87,7 +90,8 @@ class DDApiTest extends Specification {
}
}
put("v0.4/services") {
response.status(200).send()
def status = request.contentLength > 0 ? 200 : 500
response.status(status).send()
}
}
}
@ -144,10 +148,12 @@ class DDApiTest extends Specification {
def agent = ratpack {
handlers {
put("v0.4/traces") {
response.status(200).send()
def status = request.contentLength > 0 ? 200 : 500
response.status(status).send()
}
put("v0.4/services") {
response.status(200).send()
def status = request.contentLength > 0 ? 200 : 500
response.status(status).send()
}
}
}
@ -167,7 +173,8 @@ class DDApiTest extends Specification {
def agent = ratpack {
handlers {
put("v0.4/traces") {
response.status(200).send()
def status = request.contentLength > 0 ? 200 : 500
response.status(status).send()
}
put("v0.4/services") {
response.status(404).send()
@ -193,7 +200,8 @@ class DDApiTest extends Specification {
def agent = ratpack {
handlers {
put("v0.4/traces") {
response.status(200).send()
def status = request.contentLength > 0 ? 200 : 500
response.status(status).send()
}
put("v0.4/services") {
requestContentType.set(request.contentType)
@ -243,11 +251,13 @@ class DDApiTest extends Specification {
def agent = ratpack {
handlers {
put("v0.4/traces") {
response.status(200).send('{"hello":"test"}')
def status = request.contentLength > 0 ? 200 : 500
response.status(status).send('{"hello":"test"}')
}
put("v0.4/services") {
if (servicesAvailable) {
response.status(200).send('{"service-response":"from-test"}')
def status = request.contentLength > 0 ? 200 : 500
response.status(status).send('{"service-response":"from-test"}')
} else {
response.status(404).send('{"service-response":"from-test"}')
}
@ -280,10 +290,12 @@ class DDApiTest extends Specification {
def v3Agent = ratpack {
handlers {
put("v0.3/traces") {
response.status(200).send()
def status = request.contentLength > 0 ? 200 : 500
response.status(status).send()
}
put("v0.3/services") {
response.status(200).send()
def status = request.contentLength > 0 ? 200 : 500
response.status(status).send()
}
}
}
@ -307,21 +319,25 @@ class DDApiTest extends Specification {
def agent = ratpack {
handlers {
put("v0.3/traces") {
response.status(200).send()
def status = request.contentLength > 0 ? 200 : 500
response.status(status).send()
}
put("v0.3/services") {
response.status(200).send()
def status = request.contentLength > 0 ? 200 : 500
response.status(status).send()
}
put("v0.4/traces") {
Blocking.exec {
Thread.sleep(delayTrace)
response.status(200).send()
def status = request.contentLength > 0 ? 200 : 500
response.status(status).send()
}
}
put("v0.4/services") {
Blocking.exec {
Thread.sleep(delayServices)
response.status(200).send()
def status = request.contentLength > 0 ? 200 : 500
response.status(status).send()
}
}
}