Skip to content

Commit

Permalink
Removed jetty client from tests to reduce dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
danielflower committed Dec 21, 2024
1 parent 9abda5e commit 4c99644
Show file tree
Hide file tree
Showing 14 changed files with 172 additions and 186 deletions.
18 changes: 0 additions & 18 deletions app-runner-router-lib/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,6 @@
<artifactId>murp</artifactId>
<version>1.1.5</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-http</artifactId>
<version>${jetty.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
<version>${jetty.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-client</artifactId>
<version>${jetty.version}</version>
<scope>test</scope>
</dependency>
<!-- end web server stuff -->

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import org.junit.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

public class CreateAppHandlerTest {

Expand Down
11 changes: 5 additions & 6 deletions app-runner-router-lib/src/test/java/e2e/AvailabilityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.danielflower.apprunner.router.lib.AppRunnerRouterSettings;
import com.danielflower.apprunner.router.lib.mgmt.SystemInfo;
import io.muserver.MuServerBuilder;
import org.eclipse.jetty.client.api.ContentResponse;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.*;
Expand Down Expand Up @@ -49,7 +48,7 @@ public void create() throws Exception {
httpClient.registerRunner(unhealthyRunner.id(), unhealthyRunner.httpUrl(), 10);

httpClient.deploy("app1");
Waiter.waitForApp(httpClient.targetURI(), "app1");
Waiter.waitForApp(httpClient.targetURI(), "app1").close();
assertThat(httpClient.get("/app1/"), ContentResponseMatcher.equalTo(200, containsString("My Maven App")));

unhealthyRunner.shutDown();
Expand All @@ -71,8 +70,8 @@ public static void deleteRunners() {
public void appsReturnsPartialListWhenAnAppRunnerIsAvailableAndHasErrorMessages() throws Exception {

// querying for all the apps returns a combined list
ContentResponse appsResponse = httpClient.get("/api/v1/apps");
JSONObject actual = new JSONObject(appsResponse.getContentAsString());
var appsResponse = httpClient.get("/api/v1/apps");
JSONObject actual = new JSONObject(appsResponse.body());
JSONAssert.assertEquals("{ " +
"'appCount': 1," +
"'apps': [ { 'name': 'app1', 'url': '" + httpClient.targetURI().resolve("/app1/") + "', 'appRunnerInstanceId': 'healthy-app-runner', } ]" +
Expand All @@ -84,8 +83,8 @@ public void appsReturnsPartialListWhenAnAppRunnerIsAvailableAndHasErrorMessages(

@Test
public void systemCallStillReturnsEvenWhenRunnersAreUnavailable() throws Exception {
ContentResponse systemResponse = httpClient.get("/api/v1/system");
JSONObject all = new JSONObject(systemResponse.getContentAsString());
var systemResponse = httpClient.get("/api/v1/system");
JSONObject all = new JSONObject(systemResponse.body());
assertThat(all.getBoolean("appRunnerStarted"), equalTo(false));
JSONArray runners = all.getJSONArray("runners");
assertThat(runners.length(), equalTo(2));
Expand Down
3 changes: 2 additions & 1 deletion app-runner-router-lib/src/test/java/e2e/HttpsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import scaffolding.RestClient;

import java.io.File;
import java.net.ConnectException;
import java.util.concurrent.ExecutionException;

import static io.muserver.MuServerBuilder.muServer;
Expand Down Expand Up @@ -62,7 +63,7 @@ private static void assertDoesNotWork(RestClient client) throws Exception {
try {
client.getAppRunners();
Assert.fail("Should not have worked");
} catch (ExecutionException e) {
} catch (ConnectException e) {
// correct behaviour
}
}
Expand Down
82 changes: 40 additions & 42 deletions app-runner-router-lib/src/test/java/e2e/RoutingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import io.muserver.murp.ReverseProxyBuilder;
import jakarta.ws.rs.BadRequestException;
import jakarta.ws.rs.WebApplicationException;
import org.eclipse.jetty.client.api.ContentResponse;
import org.hamcrest.CoreMatchers;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
Expand All @@ -33,8 +32,7 @@
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.startsWith;
import static org.hamcrest.Matchers.*;
import static scaffolding.ContentResponseMatcher.equalTo;
import static scaffolding.Photocopier.projectRoot;

Expand Down Expand Up @@ -108,17 +106,17 @@ public void appRunnersCanBeRegisteredAndDeregisteredWithTheRestAPIWithAnHTTPRout
assertThat(httpClient.registerRunner(latestAppRunnerWithoutNode.id(), urlOfLatest, 1), equalTo(201, containsString(latestAppRunnerWithoutNode.id())));
assertThat(httpClient.registerRunner(oldAppRunner.id(), oldAppRunner.httpUrl(), 2), equalTo(201, containsString(oldAppRunner.id())));

ContentResponse appRunners = httpClient.getAppRunners();
assertThat(appRunners.getStatus(), is(200));
var appRunners = httpClient.getAppRunners();
assertThat(appRunners.statusCode(), is(200));
JSONAssert.assertEquals("{ 'runners': [" +
" { 'id': 'app-runner-1', 'url': '" + urlOfLatest + "', 'appCount': 0, 'maxApps': 1, " + "'systemUrl': '" + urlOfLatest.resolve("/api/v1/system") + "', 'appsUrl': '" + urlOfLatest.resolve("/api/v1/apps") + "' }," +
" { 'id': 'app-runner-2', 'url': '" + oldAppRunner.httpUrl().toString() + "', 'appCount': 0, 'maxApps': 2 }" +
"]}", appRunners.getContentAsString(), JSONCompareMode.LENIENT);
"]}", appRunners.body(), JSONCompareMode.LENIENT);

ContentResponse appRunner = httpClient.getRunner("app-runner-2");
assertThat(appRunner.getStatus(), is(200));
var appRunner = httpClient.getRunner("app-runner-2");
assertThat(appRunner.statusCode(), is(200));
JSONAssert.assertEquals("{ 'id': 'app-runner-2', 'url': '" + oldAppRunner.httpUrl().toString() + "', 'maxApps': 2 }"
, appRunner.getContentAsString(), JSONCompareMode.LENIENT);
, appRunner.body(), JSONCompareMode.LENIENT);

assertThat(httpClient.deleteRunner(oldAppRunner.id()), equalTo(200, containsString(oldAppRunner.id())));
assertThat(httpClient.getRunner(oldAppRunner.id()), equalTo(404, containsString(oldAppRunner.id())));
Expand All @@ -127,7 +125,7 @@ public void appRunnersCanBeRegisteredAndDeregisteredWithTheRestAPIWithAnHTTPRout

@Test
public void runnersWithInvalidUrlsAreRejected() throws Exception {
ContentResponse resp = httpsClient.registerRunner("someid", URI.create("https://badurl.example.org"), 1);
var resp = httpsClient.registerRunner("someid", URI.create("https://badurl.example.org"), 1);
assertThat(resp, equalTo(400, containsString("That is an invalid runner URL")));
}

Expand All @@ -144,22 +142,22 @@ public void proxyHeadersAreForwardedCorrectlyToTheApp() throws Exception {
httpsClient.deploy("app1");
Waiter.waitForApp(httpsClient.targetURI(), "app1");

String headersFromOne = httpsClient.get("/app1/headers").getContentAsString();
String headersFromOne = httpsClient.get("/app1/headers").body();

httpsClient.createApp(app2.gitUrl(), "app2");
httpsClient.deploy("app2");
Waiter.waitForApp(httpsClient.targetURI(), "app2");

String headersFromTwo = httpsClient.get("/app2/headers").getContentAsString();
String headersFromTwo = httpsClient.get("/app2/headers").body();

assertThat(headersFromOne, containsString(";proto=https\r\n"));
assertThat(headersFromOne, containsString(";proto=http\r\n"));
assertThat(headersFromOne.indexOf(";proto=https\r\n"), Matchers.lessThan(headersFromOne.lastIndexOf(";proto=http\r\n")));

assertThat(headersFromOne, containsString("X-Forwarded-Proto:https\r\n"));
assertThat(headersFromTwo, containsString("X-Forwarded-Proto:https\r\n"));
assertThat(headersFromOne, containsString("\r\nHost:" + httpsClient.targetURI().getAuthority() + "\r\n"));
assertThat(headersFromTwo, containsString("\r\nHost:" + httpsClient.targetURI().getAuthority() + "\r\n"));
assertThat(headersFromOne, containsString("Host:" + httpsClient.targetURI().getAuthority() + "\r\n"));
assertThat(headersFromTwo, containsString("Host:" + httpsClient.targetURI().getAuthority() + "\r\n"));

httpsClient.stop("app1");
httpsClient.stop("app2");
Expand Down Expand Up @@ -204,7 +202,7 @@ public void slowRequestsTimeOut() throws Exception {
httpsClient.createApp(app1.gitUrl(), appName);
httpsClient.deploy(appName);
Waiter.waitForApp(httpsClient.targetURI(), appName);
assertThat(httpsClient.get("/" + appName + "/slow?millis=" + waitTime).getStatus(), is(504));
assertThat(httpsClient.get("/" + appName + "/slow?millis=" + waitTime).statusCode(), is(504));
httpsClient.stop(appName);
}
}
Expand All @@ -213,13 +211,13 @@ private static void appsCanBeAddedToTheRouterAndItWillDistributeThoseToTheRunner
AppRepo app1 = AppRepo.create("maven");
AppRepo app2 = AppRepo.create("maven");

ContentResponse app1Creation = client.createApp(app1.gitUrl(), "app1");
assertThat("Error returned: " + app1Creation.getContentAsString(), app1Creation.getStatus(), is(201));
var app1Creation = client.createApp(app1.gitUrl(), "app1");
assertThat("Error returned: " + app1Creation.body(), app1Creation.statusCode(), is(201));

ContentResponse app1DuplicateCreation = client.createApp(app1.gitUrl(), "app1");
assertThat("Body returned: " + app1DuplicateCreation.getContentAsString(), app1DuplicateCreation.getStatus(), is(409));
var app1DuplicateCreation = client.createApp(app1.gitUrl(), "app1");
assertThat("Body returned: " + app1DuplicateCreation.body(), app1DuplicateCreation.statusCode(), is(409));

JSONObject app1Json = new JSONObject(app1Creation.getContentAsString());
JSONObject app1Json = new JSONObject(app1Creation.body());
assertThat(app1Json.getString("url"), startsWith(client.routerUrl));

client.deploy("app1");
Expand All @@ -236,38 +234,38 @@ private static void appsCanBeAddedToTheRouterAndItWillDistributeThoseToTheRunner
assertThat(numberOfApps(oldAppRunner), is(1));

// a runner can be deleted and added back and the number reported is accurate
JSONObject savedInfo = new JSONObject(client.getRunner(latestAppRunner.id()).getContentAsString());
JSONObject savedInfo = new JSONObject(client.getRunner(latestAppRunner.id()).body());
client.deleteRunner(latestAppRunner.id());
client.registerRunner(latestAppRunner.id(), URI.create(savedInfo.getString("url")), savedInfo.getInt("maxApps"));
assertThat(numberOfApps(latestAppRunner), is(1));


// querying for all the apps returns a combined list
ContentResponse appsResponse = client.get("/api/v1/apps");
var appsResponse = client.get("/api/v1/apps");
JSONAssert.assertEquals("{ " +
"'appCount': 2," +
"'apps': [ " +
"{ 'name': 'app1', 'url': '" + client.targetURI().resolve("/app1/") + "' }," +
"{ 'name': 'app2', 'url': '" + client.targetURI().resolve("/app2/") + "' }" +
"] }", appsResponse.getContentAsString(), JSONCompareMode.STRICT_ORDER);
"] }", appsResponse.body(), JSONCompareMode.STRICT_ORDER);

assertThat(appsResponse.getStatus(), is(200));
assertThat(appsResponse.statusCode(), is(200));

assertThat(client.get("/api/v1/swagger.json"), equalTo(200, containsString("/apps/{name}")));

JSONAssert.assertEquals("{ runners: [" +
"{ id: " + latestAppRunner.id() + ", 'appCount': 1 }," +
"{ id: " + oldAppRunner.id() + ", 'appCount': 1 }" +
"] }",
client.getAppRunners().getContentAsString(), JSONCompareMode.LENIENT);
client.getAppRunners().body(), JSONCompareMode.LENIENT);

client.stop("app1");
client.stop("app2");
}

private static int numberOfApps(AppRunnerInstance appRunner) throws Exception {
RestClient c = RestClient.create(appRunner.httpUrl().toString());
JSONObject apps = new JSONObject(c.get("/api/v1/apps").getContentAsString());
JSONObject apps = new JSONObject(c.get("/api/v1/apps").body());
return apps.getJSONArray("apps").length();
}

Expand Down Expand Up @@ -343,7 +341,7 @@ public void runnerDetailsCanBeUpdated() throws Exception {
assertThat(httpClient.updateRunner(latestAppRunnerWithoutNode.id(), oldAppRunner.httpUrl(), 100),
equalTo(200, Matchers.containsString("100")));

JSONArray runners = new JSONObject(httpClient.getAppRunners().getContentAsString()).getJSONArray("runners");
JSONArray runners = new JSONObject(httpClient.getAppRunners().body()).getJSONArray("runners");
assertThat(runners.length(), is(1));
JSONAssert.assertEquals("{ 'id': '" + latestAppRunnerWithoutNode.id() + "', 'url': '" + oldAppRunner.httpUrl() + "', maxApps: 100 }", runners.get(0).toString(), JSONCompareMode.LENIENT);
}
Expand All @@ -355,9 +353,9 @@ public void appsAddedToAnInstanceBeforeItJoinsTheClusterAreAvailable() throws Ex
direct.createApp(app1.gitUrl(), "app1");
direct.deploy(app1.name);
httpClient.registerRunner(latestAppRunnerWithoutNode.id(), latestAppRunnerWithoutNode.httpUrl(), 1);
ContentResponse contentResponse = httpClient.get("/api/v1/apps/app1");
assertThat(contentResponse.getStatus(), is(200));
JSONObject json = new JSONObject(contentResponse.getContentAsString());
var contentResponse = httpClient.get("/api/v1/apps/app1");
assertThat(contentResponse.statusCode(), is(200));
JSONObject json = new JSONObject(contentResponse.body());
JSONAssert.assertEquals("{ 'name': 'app1', 'url': '" + httpClient.routerUrl + "/app1/' }", json, JSONCompareMode.LENIENT);

httpClient.stop(app1.name);
Expand All @@ -369,20 +367,20 @@ public void theRunnerAppsAPIGivesTheAppsOfARunner() throws Exception {
AppRepo app1 = AppRepo.create("maven");
httpClient.createApp(app1.gitUrl(), "my-app");

ContentResponse resp = httpClient.getRunnerApps(latestAppRunnerWithoutNode.id());
assertThat(resp.getStatus(), is(200));
assertThat(resp.getHeaders().get("Content-Type"), Matchers.equalTo("application/json"));
JSONObject appJson = new JSONObject(resp.getContentAsString());
var resp = httpClient.getRunnerApps(latestAppRunnerWithoutNode.id());
assertThat(resp.statusCode(), is(200));
assertThat(resp.headers().allValues("Content-Type"), contains("application/json"));
JSONObject appJson = new JSONObject(resp.body());
assertThat(appJson.getJSONArray("apps").length(), is(1));
}

@Test
public void theRunnerAppsAPIGivesTheSystemOfARunner() throws Exception {
httpClient.registerRunner(latestAppRunnerWithoutNode.id(), latestAppRunnerWithoutNode.httpUrl(), 1);
ContentResponse resp = httpClient.getRunnerSystem(latestAppRunnerWithoutNode.id());
assertThat(resp.getStatus(), is(200));
assertThat(resp.getHeaders().get("Content-Type"), Matchers.equalTo("application/json"));
JSONObject json = new JSONObject(resp.getContentAsString());
var resp = httpClient.getRunnerSystem(latestAppRunnerWithoutNode.id());
assertThat(resp.statusCode(), is(200));
assertThat(resp.headers().allValues("Content-Type"), contains("application/json"));
JSONObject json = new JSONObject(resp.body());
assertThat(json.getBoolean("appRunnerStarted"), is(true));
}

Expand Down Expand Up @@ -410,7 +408,7 @@ public void theSystemApiReturnsTheSetOfAllSampleAppsAndRunnerInfoAcrossAllTheIns
httpClient.registerRunner(latestAppRunnerWithoutNode.id(), latestAppRunnerWithoutNode.httpUrl(), 1);
httpClient.registerRunner(oldAppRunner.id(), oldAppRunner.httpUrl(), 2);

JSONObject system = new JSONObject(httpClient.getSystem().getContentAsString());
JSONObject system = new JSONObject(httpClient.getSystem().body());

JSONArray runners = system.getJSONArray("runners");
assertThat(runners.length(), is(2));
Expand All @@ -426,9 +424,9 @@ public void theSystemApiReturnsTheSetOfAllSampleAppsAndRunnerInfoAcrossAllTheIns
ids.add(SystemResource.getSampleID(sample));
String zipUrl = sample.getString("url");
assertThat(zipUrl, containsString(httpClient.routerUrl));
ContentResponse zip = httpClient.getAbsolute(zipUrl);
MatcherAssert.assertThat(zipUrl, zip.getStatus(), CoreMatchers.is(200));
MatcherAssert.assertThat(zipUrl, zip.getHeaders().get("Content-Type"), CoreMatchers.is("application/zip"));
var zip = httpClient.getAbsolute(zipUrl);
MatcherAssert.assertThat(zipUrl, zip.statusCode(), CoreMatchers.is(200));
MatcherAssert.assertThat(zipUrl, zip.headers().allValues("Content-Type"), contains("application/zip"));
}

assertThat(ids, hasItem(CoreMatchers.equalTo("maven")));
Expand Down
13 changes: 6 additions & 7 deletions app-runner-router-lib/src/test/java/manual/RunLocal.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import io.muserver.MuServerBuilder;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.eclipse.jetty.client.api.ContentResponse;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
Expand Down Expand Up @@ -94,7 +93,7 @@ private static void setupSampleApps(RestClientThatThrows client) throws Exceptio
File repoRoot = new File(projectRoot(), "target/local/repos/" + System.currentTimeMillis());
log.info("Creating git repos for apps at " + fullPath(repoRoot));
FileUtils.forceMkdir(repoRoot);
JSONObject system = new JSONObject(client.getSystem().getContentAsString());
JSONObject system = new JSONObject(client.getSystem().body());
JSONArray samples = system.getJSONArray("samples");
for (Object sampleObj : samples) {
JSONObject sample = (JSONObject) sampleObj;
Expand All @@ -106,8 +105,8 @@ private static void setupSampleApps(RestClientThatThrows client) throws Exceptio
}
log.info("Going to download " + zipUrl);
File zip = new File(repoRoot, id + ".zip");
ContentResponse zipResponse = client.getAbsolute(zipUrl);
FileUtils.writeByteArrayToFile(zip, zipResponse.getContent());
var zipResponse = client.getAbsoluteByteArray(zipUrl);
FileUtils.writeByteArrayToFile(zip, zipResponse.body());
File target = new File(repoRoot, id);
if (!target.mkdir()) {
throw new RuntimeException("Could not make " + fullPath(target));
Expand Down Expand Up @@ -143,9 +142,9 @@ private static void unzip(File zip, File outputDir) throws IOException {

private static void registerRunner(RestClientThatThrows client, AppRunnerInstance runner, int maxInstances) throws Exception {
log.info("Registering " + runner.httpUrl() + " with the router");
ContentResponse contentResponse = client.registerRunner(runner.id(), runner.httpUrl(), maxInstances);
if (contentResponse.getStatus() != 201) {
throw new RuntimeException("Could not register " + runner.httpUrl() + ": " + contentResponse.getStatus() + " - " + contentResponse.getContentAsString());
var contentResponse = client.registerRunner(runner.id(), runner.httpUrl(), maxInstances);
if (contentResponse.statusCode() != 201) {
throw new RuntimeException("Could not register " + runner.httpUrl() + ": " + contentResponse.statusCode() + " - " + contentResponse.body());
}
}

Expand Down
Loading

0 comments on commit 4c99644

Please sign in to comment.