diff options
author | Harald Musum <musum@verizonmedia.com> | 2020-01-24 08:58:43 +0100 |
---|---|---|
committer | Harald Musum <musum@verizonmedia.com> | 2020-01-24 08:58:43 +0100 |
commit | 40312693a7375fcd96c4ca9723c85ab7f7448862 (patch) | |
tree | d8331c45c7316e3836ed2830d0b132befd2d223a /configserver | |
parent | 6cfd02c6d774a6469dcd2c8b92421b0828090b49 (diff) |
Implement more methods in tester API
Diffstat (limited to 'configserver')
5 files changed, 138 insertions, 29 deletions
diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/ApplicationRepository.java b/configserver/src/main/java/com/yahoo/vespa/config/server/ApplicationRepository.java index 3af7c7fdacc..92e7bf0300b 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/ApplicationRepository.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/ApplicationRepository.java @@ -572,9 +572,12 @@ public class ApplicationRepository implements com.yahoo.config.provision.Deploye return testerClient.getLog(getTesterHostname(applicationId), getTesterPort(applicationId), after); } - // TODO: Not implemented in TesterClient yet - public HttpResponse startTests(ApplicationId applicationId, String suite, String config) { - return testerClient.startTests(getTesterHostname(applicationId), suite, config); + public HttpResponse startTests(ApplicationId applicationId, String suite, byte[] config) { + return testerClient.startTests(getTesterHostname(applicationId), getTesterPort(applicationId), suite, config); + } + + public HttpResponse isTesterReady(ApplicationId applicationId) { + return testerClient.isTesterReady(getTesterHostname(applicationId), getTesterPort(applicationId)); } private String getTesterHostname(ApplicationId applicationId) { diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/http/TesterClient.java b/configserver/src/main/java/com/yahoo/vespa/config/server/http/TesterClient.java index 92c04bfb8e7..1418b2a0bcf 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/http/TesterClient.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/http/TesterClient.java @@ -7,8 +7,10 @@ import com.yahoo.log.LogLevel; import com.yahoo.yolean.Exceptions; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.client.utils.URIBuilder; +import org.apache.http.entity.ByteArrayEntity; import java.io.IOException; import java.net.URI; @@ -24,17 +26,7 @@ public class TesterClient { private static final Logger logger = Logger.getLogger(TesterClient.class.getName()); public HttpResponse getStatus(String testerHostname, int port) { - URI testerUri; - try { - testerUri = new URIBuilder() - .setScheme("https") - .setHost(testerHostname) - .setPort(port) - .setPath("/tester/v1/status") - .build(); - } catch (URISyntaxException e) { - throw new IllegalArgumentException(e); - } + URI testerUri = createURI(testerHostname, port, "/tester/v1/status"); return execute(new HttpGet(testerUri), "Failed to get tester status"); } @@ -42,11 +34,7 @@ public class TesterClient { public HttpResponse getLog(String testerHostname, int port, Long after) { URI testerUri; try { - testerUri = new URIBuilder() - .setScheme("https") - .setHost(testerHostname) - .setPort(port) - .setPath("/tester/v1/log") + testerUri = createBuilder(testerHostname, port, "/tester/v1/log") .addParameter("after", String.valueOf(after)) .build(); } catch (URISyntaxException e) { @@ -56,10 +44,19 @@ public class TesterClient { return execute(new HttpGet(testerUri), "Failed to get tester logs"); } - public HttpResponse startTests(String testerHostname, String suite, String config) { - throw new UnsupportedOperationException("Not implemented yet"); + public HttpResponse startTests(String testerHostname, int port, String suite, byte[] config) { + URI testerUri = createURI(testerHostname, port, "/tester/v1/run/" + suite); + HttpPost request = new HttpPost(testerUri); + request.setEntity(new ByteArrayEntity(config)); + + return execute(request, "Failed to start tests"); } + public HttpResponse isTesterReady(String testerHostname, int port) { + URI testerUri = createURI(testerHostname, port, "/status.html"); + + return execute(new HttpGet(testerUri), "/status.html did not return 200 OK"); + } private HttpResponse execute(HttpUriRequest request, String messageIfRequestFails) { // TODO: Change log level to DEBUG @@ -72,4 +69,20 @@ public class TesterClient { } } + private URIBuilder createBuilder(String testerHostname, int port, String path) { + return new URIBuilder() + .setScheme("https") + .setHost(testerHostname) + .setPort(port) + .setPath(path); + } + + private URI createURI(String testerHostname, int port, String path) { + try { + return createBuilder(testerHostname, port, path).build(); + } catch (URISyntaxException e) { + throw new IllegalArgumentException(e); + } + } + } diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandler.java b/configserver/src/main/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandler.java index d9a5aa7055d..833c9ca4fba 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandler.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandler.java @@ -11,6 +11,7 @@ import com.yahoo.config.provision.TenantName; import com.yahoo.config.provision.Zone; import com.yahoo.container.jdisc.HttpRequest; import com.yahoo.container.jdisc.HttpResponse; +import com.yahoo.io.IOUtils; import com.yahoo.jdisc.Response; import com.yahoo.jdisc.application.BindingMatch; import com.yahoo.jdisc.application.UriPattern; @@ -22,6 +23,7 @@ import com.yahoo.vespa.config.server.http.HttpHandler; import com.yahoo.vespa.config.server.http.JSONResponse; import com.yahoo.vespa.config.server.http.NotFoundException; +import java.io.IOException; import java.time.Duration; import java.util.List; import java.util.Objects; @@ -46,6 +48,7 @@ public class ApplicationHandler extends HttpHandler { "http://*/application/v2/tenant/*/application/*/environment/*/region/*/instance/*/clustercontroller/*/status/*", "http://*/application/v2/tenant/*/application/*/environment/*/region/*/instance/*/metrics", "http://*/application/v2/tenant/*/application/*/environment/*/region/*/instance/*/logs", + "http://*/application/v2/tenant/*/application/*/environment/*/region/*/instance/*/tester/*/*", "http://*/application/v2/tenant/*/application/*/environment/*/region/*/instance/*/tester/*", "http://*/application/v2/tenant/*/application/*/environment/*/region/*/instance/*", "http://*/application/v2/tenant/*/application/*") @@ -138,13 +141,11 @@ public class ApplicationHandler extends HttpHandler { switch (testerCommand) { case "status": return applicationRepository.getTesterStatus(applicationId); - case "logs": + case "log": Long after = Long.valueOf(request.getProperty("after")); return applicationRepository.getTesterLog(applicationId, after); - case "startTests": - String suite = "foo"; // TODO - String config = "bar"; // TODO - return applicationRepository.startTests(applicationId, suite, config); + case "ready": + return applicationRepository.isTesterReady(applicationId); default: throw new IllegalArgumentException("Unknown tester command in request " + request.getUri().toString()); } @@ -156,9 +157,19 @@ public class ApplicationHandler extends HttpHandler { @Override public HttpResponse handlePOST(HttpRequest request) { ApplicationId applicationId = getApplicationIdFromRequest(request); - if (request.getUri().getPath().endsWith("restart")) + if (request.getUri().getPath().endsWith("restart")) { return restart(request, applicationId); - throw new NotFoundException("Illegal POST request '" + request.getUri() + "': Must end with /restart"); + } else if (isTesterStartTestsRequest(request)) { + byte[] data; + try { + data = IOUtils.readBytes(request.getData(), 1024 * 1000); + } catch (IOException e) { + throw new IllegalArgumentException("Could not read data in request " + request); + } + return applicationRepository.startTests(applicationId, getSuiteFromRequest(request), data); + } else { + throw new NotFoundException("Illegal POST request '" + request.getUri() + "'"); + } } private HttpResponse restart(HttpRequest request, ApplicationId applicationId) { @@ -233,6 +244,12 @@ public class ApplicationHandler extends HttpHandler { request.getUri().getPath().contains("/tester"); } + private static boolean isTesterStartTestsRequest(HttpRequest request) { + System.out.println(getBindingMatch(request).groupCount()); + return getBindingMatch(request).groupCount() == 9 && + request.getUri().getPath().contains("/tester/run/"); + } + private static String getHostNameFromRequest(HttpRequest req) { BindingMatch<?> bm = getBindingMatch(req); return bm.group(7); @@ -243,6 +260,11 @@ public class ApplicationHandler extends HttpHandler { return bm.group(7); } + private static String getSuiteFromRequest(HttpRequest req) { + BindingMatch<?> bm = getBindingMatch(req); + return bm.group(8); + } + private static String getPathSuffix(HttpRequest req) { BindingMatch<?> bm = getBindingMatch(req); return bm.group(8); diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/MockTesterClient.java b/configserver/src/test/java/com/yahoo/vespa/config/server/MockTesterClient.java index 7cb878a5f2c..db687857dae 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/MockTesterClient.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/MockTesterClient.java @@ -23,6 +23,16 @@ public class MockTesterClient extends TesterClient { return new MockLogResponse(); } + @Override + public HttpResponse startTests(String testerHostname, int port, String suite, byte[] config) { + return new MockStartTestsResponse(); + } + + @Override + public HttpResponse isTesterReady(String testerHostname, int port) { + return new MockTesterReadyResponse(); + } + private static class MockStatusResponse extends HttpResponse { private MockStatusResponse() { @@ -49,4 +59,26 @@ public class MockTesterClient extends TesterClient { } -}
\ No newline at end of file + private static class MockStartTestsResponse extends HttpResponse { + + private MockStartTestsResponse() { + super(200); + } + + @Override + public void render(OutputStream outputStream) { } + + } + + private static class MockTesterReadyResponse extends HttpResponse { + + private MockTesterReadyResponse() { + super(200); + } + + @Override + public void render(OutputStream outputStream) { } + + } + +} diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandlerTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandlerTest.java index 438ccc5d783..67677822317 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandlerTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandlerTest.java @@ -32,10 +32,13 @@ import org.junit.Before; import org.junit.Test; import javax.ws.rs.client.Client; +import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.net.URI; +import java.nio.charset.StandardCharsets; import java.time.Clock; import static com.yahoo.config.model.api.container.ContainerServiceType.CLUSTERCONTROLLER_CONTAINER; @@ -241,6 +244,42 @@ public class ApplicationHandlerTest { assertEquals("OK", baos.toString()); } + @Test + public void testTesterGetLog() throws IOException { + applicationRepository.deploy(testApp, prepareParams(applicationId)); + String url = toUrlPath(applicationId, Zone.defaultZone(), true) + "/tester/log?after=1234"; + ApplicationHandler mockHandler = createApplicationHandler(); + + HttpResponse response = mockHandler.handle(HttpRequest.createTestRequest(url, com.yahoo.jdisc.http.HttpRequest.Method.GET)); + assertEquals(200, response.getStatus()); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + response.render(baos); + assertEquals("log", baos.toString()); + } + + @Test + public void testTesterStartTests() { + applicationRepository.deploy(testApp, prepareParams(applicationId)); + String url = toUrlPath(applicationId, Zone.defaultZone(), true) + "/tester/run/staging-test"; + ApplicationHandler mockHandler = createApplicationHandler(); + + InputStream requestData = new ByteArrayInputStream("foo".getBytes(StandardCharsets.UTF_8)); + HttpRequest testRequest = HttpRequest.createTestRequest(url, com.yahoo.jdisc.http.HttpRequest.Method.POST, requestData); + HttpResponse response = mockHandler.handle(testRequest); + assertEquals(200, response.getStatus()); + } + + @Test + public void testTesterReady() { + applicationRepository.deploy(testApp, prepareParams(applicationId)); + String url = toUrlPath(applicationId, Zone.defaultZone(), true) + "/tester/ready"; + ApplicationHandler mockHandler = createApplicationHandler(); + HttpRequest testRequest = HttpRequest.createTestRequest(url, com.yahoo.jdisc.http.HttpRequest.Method.GET); + HttpResponse response = mockHandler.handle(testRequest); + assertEquals(200, response.getStatus()); + } + private void assertNotAllowed(com.yahoo.jdisc.http.HttpRequest.Method method) throws IOException { String url = "http://myhost:14000/application/v2/tenant/" + mytenantName + "/application/default"; deleteAndAssertResponse(url, Response.Status.METHOD_NOT_ALLOWED, HttpErrorResponse.errorCodes.METHOD_NOT_ALLOWED, "{\"error-code\":\"METHOD_NOT_ALLOWED\",\"message\":\"Method '" + method + "' is not supported\"}", |