Skip to content

Commit

Permalink
Router tests now use a random available port from the test host
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorackerman committed Jul 8, 2016
1 parent 187d113 commit e2a5d6f
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public boolean loadDatabase() throws IOException, org.apache.wicket.ajax.json.JS
@Override
protected File downloadDatabase(final String url, final File existingDb) {
if (fetcher == null) {
LOGGER.warn("Waiting for federations configuration to be processed, unable download federations");
LOGGER.warn("[" + getClass().getSimpleName() + "] Waiting for configuration to be processed, unable to download from '" + url + "'");
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public synchronized boolean updateDatabase() {
if (!hasBeenLoaded || needsUpdating(existingDB)) {
final Request request = getRequest(urls.nextUrl());
if (request != null) {
asyncHttpClient.executeRequest(request, new UpdateHandler()); // AsyncHandlers are NOT thread safe; one instance per request
asyncHttpClient.executeRequest(request, new UpdateHandler(request)); // AsyncHandlers are NOT thread safe; one instance per request
return true;
}
} else {
Expand Down Expand Up @@ -250,8 +250,9 @@ protected boolean needsUpdating(final File existingDB) {
}

private class UpdateHandler extends AsyncCompletionHandler<Object> {

public UpdateHandler() {
final Request request;
public UpdateHandler(final Request request) {
this.request = request;
}

@Override
Expand All @@ -270,7 +271,7 @@ public Integer onCompleted(final Response response) throws JSONException, IOExce

@Override
public void onThrowable(final Throwable t){
LOGGER.warn(t,t);
LOGGER.warn("Failed request " + request.getUrl() + ": " + t, t);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.apache.catalina.core.StandardHost;
import org.apache.catalina.core.StandardService;
import org.apache.catalina.startup.Catalina;
import org.springframework.util.SocketUtils;

import java.util.logging.Level;

public class CatalinaTrafficRouter {
Expand Down Expand Up @@ -49,11 +51,13 @@ public CatalinaTrafficRouter(String serverXmlPath, String appBase) {
Connector[] connectors = trafficRouterService.findConnectors();
for (Connector connector : connectors) {
if (connector.getPort() == 80) {
connector.setPort(8888);
connector.setPort(Integer.parseInt(System.getProperty("routerHttpPort", "8888")));
}

SocketUtils.findAvailableTcpPort();

if (connector.getPort() == 443) {
connector.setPort(8443);
connector.setPort(Integer.parseInt(System.getProperty("routerSecurePort", "8443")));
}
System.out.println("[" + System.currentTimeMillis() + "] >>>>>>>>>>>>>>>> Traffic Router listening on port " + connector.getPort() + " " + connector.getScheme());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@
public class ExternalTestSuite {
public static final String TRAFFIC_MONITOR_BOOTSTRAP_LOCAL = "TRAFFIC_MONITOR_BOOTSTRAP_LOCAL";
public static final String TRAFFIC_MONITOR_HOSTS = "TRAFFIC_MONITOR_HOSTS";
public static final String FAKE_SERVER = "localhost:8889;";
public static String FAKE_SERVER;
private static CatalinaTrafficRouter catalinaTrafficRouter;
private static HttpDataServer httpDataServer;
private static File tmpDeployDir;
private static int testHttpServerPort;

@SuppressWarnings("unchecked")
public static void addToEnv(Map<String, String> envVars) throws Exception {
Expand All @@ -93,6 +94,7 @@ public static void setupFakeServers() throws Exception {
// federations
// steering
// fake setting a cookie
FAKE_SERVER = "localhost:" + testHttpServerPort + ";";

Map<String, String> additionalEnvironment = new HashMap<>();

Expand All @@ -112,12 +114,18 @@ public static void setupFakeServers() throws Exception {
assertThat(System.getenv(TRAFFIC_MONITOR_BOOTSTRAP_LOCAL), equalTo("true"));
assertThat(System.getenv(TRAFFIC_MONITOR_HOSTS), equalTo(FAKE_SERVER));

httpDataServer = new HttpDataServer();
httpDataServer.start(8889);
httpDataServer = new HttpDataServer(testHttpServerPort);
httpDataServer.start(testHttpServerPort);
}

@BeforeClass
public static void beforeClass() throws Exception {
testHttpServerPort = findAvailableTcpPort();

System.setProperty("testHttpServerPort", "" + testHttpServerPort);
System.setProperty("routerHttpPort", "" + findAvailableTcpPort());
System.setProperty("routerSecurePort", "" + findAvailableTcpPort());

setupFakeServers();

tmpDeployDir = Files.createTempDirectory("ext-test-").toFile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand All @@ -32,6 +33,11 @@
public class HttpDataServer implements HttpHandler {
private HttpServer httpServer;
private boolean receivedPost = false;
private int testHttpServerPort;

public HttpDataServer(int testHttpServerPort) {
this.testHttpServerPort = testHttpServerPort;
}

public void start(int port) throws IOException {
httpServer = HttpServer.create(new InetSocketAddress(InetAddress.getLoopbackAddress(), port),10);
Expand Down Expand Up @@ -116,30 +122,53 @@ public void run() {
} catch (IOException e) {
System.out.println("Failed closing output stream!: " + e.getMessage());
}
return;
}
}

if (!path.contains("CrConfig")) {
try (OutputStream os = httpExchange.getResponseBody()) {
httpExchange.sendResponseHeaders(200, 0);

OutputStream os = null;
try {
httpExchange.sendResponseHeaders(200, 0);
os = httpExchange.getResponseBody();
final byte[] buffer = new byte[0x10000];
int count;
final byte[] buffer = new byte[0x10000];
int count;

while ((count = inputStream.read(buffer)) >= 0) {
os.write(buffer,0,count);
while ((count = inputStream.read(buffer)) >= 0) {
os.write(buffer, 0, count);
}
} catch (Exception e) {
System.out.println("Failed sending data for " + path + " : " + e.getMessage());
}
} catch (Exception e) {
System.out.println("Failed sending data for " + path + " : " + e.getMessage());
} finally {
} else {
try {
if (inputStream != null) inputStream.close();
if (os != null) os.close();
final byte[] buffer = new byte[0x10000];
StringBuilder stringBuilder = new StringBuilder();

while (inputStream.read(buffer) >= 0) {
stringBuilder.append(new String(buffer));
}

String body = stringBuilder.toString();

if (path.contains("CrConfig")) {
body = body.replaceAll("localhost:8889" , "localhost:" + testHttpServerPort);
}

httpExchange.sendResponseHeaders(200, 0);
httpExchange.getResponseBody().write(body.getBytes());
httpExchange.getResponseBody().close();

} catch (Exception e) {
System.out.println("Failed closing stream!: " + e.getMessage());
System.out.println("Failed sending data for " + path + " : " + e.getMessage());
}
}

try {
inputStream.close();
} catch (Exception e) {
System.out.println("Failed closing stream!: " + e.getMessage());
}

}
}).start();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ public class RouterTest {
private String secureDeliveryServiceId;
private List<String> secureValidLocations = new ArrayList<>();
private String secureDeliveryServiceDomain;

private String routerHttpPort = System.getProperty("routerHttpPort", "8888");
private String routerSecurePort = System.getProperty("routerSecurePort", "8443");

@Before
public void before() throws Exception {
ObjectMapper objectMapper = new ObjectMapper(new JsonFactory());
Expand Down Expand Up @@ -159,7 +161,7 @@ public void after() throws IOException {

@Test
public void itHasAHomePage() throws IOException {
HttpGet httpGet = new HttpGet("http://localhost:8888/index.html");
HttpGet httpGet = new HttpGet("http://localhost:" + routerHttpPort + "/index.html");

CloseableHttpResponse response = null;
try {
Expand All @@ -172,7 +174,7 @@ public void itHasAHomePage() throws IOException {

@Test
public void itRedirectsValidHttpRequests() throws IOException, InterruptedException {
HttpGet httpGet = new HttpGet("http://localhost:8888/stuff?fakeClientIpAddress=12.34.56.78");
HttpGet httpGet = new HttpGet("http://localhost:" + routerHttpPort + "/stuff?fakeClientIpAddress=12.34.56.78");
httpGet.addHeader("Host", "tr." + deliveryServiceId + ".bar");
CloseableHttpResponse response = null;

Expand All @@ -189,7 +191,7 @@ public void itRedirectsValidHttpRequests() throws IOException, InterruptedExcept

@Test
public void itDoesRoutingThroughPathsStartingWithCrs() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:8888/crs/stats?fakeClientIpAddress=12.34.56.78");
HttpGet httpGet = new HttpGet("http://localhost:" + routerHttpPort + "/crs/stats?fakeClientIpAddress=12.34.56.78");
httpGet.addHeader("Host", "foo." + deliveryServiceId + ".bar");
CloseableHttpResponse response = null;

Expand All @@ -203,7 +205,7 @@ public void itDoesRoutingThroughPathsStartingWithCrs() throws Exception {

@Test
public void itConsistentlyRedirectsValidRequests() throws IOException, InterruptedException {
HttpGet httpGet = new HttpGet("http://localhost:8888/stuff?fakeClientIpAddress=12.34.56.78");
HttpGet httpGet = new HttpGet("http://localhost:" + routerHttpPort + "/stuff?fakeClientIpAddress=12.34.56.78");
httpGet.addHeader("Host", "tr." + deliveryServiceId + ".bar");
CloseableHttpResponse response = null;

Expand All @@ -226,7 +228,7 @@ public void itConsistentlyRedirectsValidRequests() throws IOException, Interrupt

@Test
public void itRejectsInvalidRequests() throws IOException {
HttpGet httpGet = new HttpGet("http://localhost:8888/stuff?fakeClientIpAddress=12.34.56.78");
HttpGet httpGet = new HttpGet("http://localhost:" + routerHttpPort + "/stuff?fakeClientIpAddress=12.34.56.78");
httpGet.addHeader("Host", "foo.invalid-delivery-service-id.bar");
CloseableHttpResponse response = null;

Expand All @@ -240,7 +242,7 @@ public void itRejectsInvalidRequests() throws IOException {

@Test
public void itRedirectsHttpsRequests() throws Exception {
HttpGet httpGet = new HttpGet("https://localhost:8443/stuff?fakeClientIpAddress=12.34.56.78");
HttpGet httpGet = new HttpGet("https://localhost:" + routerSecurePort + "/stuff?fakeClientIpAddress=12.34.56.78");
httpGet.addHeader("Host", "tr." + secureDeliveryServiceId + ".thecdn.example.com");
CloseableHttpResponse response = null;

Expand All @@ -258,7 +260,7 @@ public void itRedirectsHttpsRequests() throws Exception {

@Test
public void itRedirectsFromHttpToHttps() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:8888/stuff?fakeClientIpAddress=12.34.56.78");
HttpGet httpGet = new HttpGet("http://localhost:" + routerHttpPort + "/stuff?fakeClientIpAddress=12.34.56.78");
httpGet.addHeader("Host", "tr." + secureDeliveryServiceId + ".bar");
CloseableHttpResponse response = null;

Expand All @@ -276,7 +278,7 @@ public void itRedirectsFromHttpToHttps() throws Exception {

@Test
public void itRejectsHttpsRequestsForHttpDeliveryService() throws Exception {
HttpGet httpGet = new HttpGet("https://localhost:8443/stuff?fakeClientIpAddress=12.34.56.78");
HttpGet httpGet = new HttpGet("https://localhost:" + routerSecurePort + "/stuff?fakeClientIpAddress=12.34.56.78");
httpGet.addHeader("Host", "tr." + deliveryServiceId + ".bar");
CloseableHttpResponse response = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ public void itGetsLocationsByIp() throws Exception {

Map<String, Object> data = new ObjectMapper().readValue(actual, new TypeReference<HashMap<String, Object>>() { });

assertThat((String) data.get("requestIp"), equalTo("8.8.8.8"));
assertThat((String) data.get("locationByFederation"), equalTo("not found"));
assertThat((String) data.get("locationByCoverageZone"), equalTo("not found"));
assertThat(data.get("requestIp"), equalTo("8.8.8.8"));
assertThat(data.get("locationByFederation"), equalTo("not found"));
assertThat(data.get("locationByCoverageZone"), equalTo("not found"));

Map<String, Object> locationByGeo = (Map<String, Object>) data.get("locationByGeo");
assertThat(locationByGeo.keySet(), containsInAnyOrder("city", "countryCode", "latitude", "longitude", "postalCode", "countryName"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public class SteeringTest {
Map<String, Integer> targetWeights = new HashMap<String, Integer>();
CloseableHttpClient httpClient;
List<String> validLocations = new ArrayList<String>();
String routerHttpPort = System.getProperty("routerHttpPort", "8888");
String testHttpPort = System.getProperty("testHttpServerPort", "8889");

JsonNode getJsonForResourcePath(String resourcePath) throws IOException {
ObjectMapper objectMapper = new ObjectMapper(new JsonFactory());
Expand Down Expand Up @@ -140,7 +142,7 @@ public void before() throws Exception {

@Test
public void itUsesSteeredDeliveryServiceIdInRedirect() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:8888/stuff?fakeClientIpAddress=12.34.56.78");
HttpGet httpGet = new HttpGet("http://localhost:" + routerHttpPort + "/stuff?fakeClientIpAddress=12.34.56.78");
httpGet.addHeader("Host", "foo." + steeringDeliveryServiceId + ".bar");
CloseableHttpResponse response = null;

Expand All @@ -155,7 +157,7 @@ public void itUsesSteeredDeliveryServiceIdInRedirect() throws Exception {

@Test
public void itUsesTargetFiltersForSteering() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:8888/qwerytuiop/force-to-eight/asdfghjkl?fakeClientIpAddress=12.34.56.78");
HttpGet httpGet = new HttpGet("http://localhost:" + routerHttpPort + "/qwerytuiop/force-to-eight/asdfghjkl?fakeClientIpAddress=12.34.56.78");
httpGet.addHeader("Host", "foo.mm-test.thecdn.example.com");
CloseableHttpResponse response = null;

Expand All @@ -170,7 +172,7 @@ public void itUsesTargetFiltersForSteering() throws Exception {

@Test
public void itUsesXtcSteeringOptionForOverride() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:8888/qwerytuiop/force-to-eight/asdfghjkl?fakeClientIpAddress=12.34.56.78");
HttpGet httpGet = new HttpGet("http://localhost:" + routerHttpPort + "/qwerytuiop/force-to-eight/asdfghjkl?fakeClientIpAddress=12.34.56.78");
httpGet.addHeader("Host", "foo.mm-test.thecdn.example.com");
httpGet.addHeader("X-TC-Steering-Option", "ds-05");

Expand All @@ -187,7 +189,7 @@ public void itUsesXtcSteeringOptionForOverride() throws Exception {

@Test
public void itReturns503ForBadDeliveryServiceInXtcSteeringOption() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:8888/qwerytuiop/asdfghjkl?fakeClientIpAddress=12.34.56.78");
HttpGet httpGet = new HttpGet("http://localhost:" + routerHttpPort + "/qwerytuiop/asdfghjkl?fakeClientIpAddress=12.34.56.78");
httpGet.addHeader("Host", "foo.mm-test.thecdn.example.com");
httpGet.addHeader("X-TC-Steering-Option", "ds-02");
CloseableHttpResponse response = null;
Expand Down Expand Up @@ -222,7 +224,7 @@ public void itUsesWeightedDistributionForRequestPath() throws Exception {

for (int i = 0; i < count; i++) {
String path = generateRandomPath();
HttpGet httpGet = new HttpGet("http://localhost:8888" + path + "?fakeClientIpAddress=12.34.56.78");
HttpGet httpGet = new HttpGet("http://localhost:" + routerHttpPort + path + "?fakeClientIpAddress=12.34.56.78");
httpGet.addHeader("Host", "foo." + steeringDeliveryServiceId + ".bar");
CloseableHttpResponse response = null;

Expand Down Expand Up @@ -294,7 +296,7 @@ public void z_itemsMigrateFromSmallerToLargerBucket() throws Exception {
hashedPaths.put(largerTarget, new ArrayList<String>());

for (String path : randomPaths) {
HttpGet httpGet = new HttpGet("http://localhost:8888" + path + "?fakeClientIpAddress=12.34.56.78");
HttpGet httpGet = new HttpGet("http://localhost:" + routerHttpPort + path + "?fakeClientIpAddress=12.34.56.78");
httpGet.addHeader("Host", "foo." + steeringDeliveryServiceId + ".bar");
CloseableHttpResponse response = null;

Expand All @@ -314,7 +316,7 @@ public void z_itemsMigrateFromSmallerToLargerBucket() throws Exception {
}

// Change the steering attributes
HttpPost httpPost = new HttpPost("http://localhost:8889/steering");
HttpPost httpPost = new HttpPost("http://localhost:" + testHttpPort + "/steering");
httpClient.execute(httpPost).close();

// steering is checked every 15 seconds by default.
Expand All @@ -325,7 +327,7 @@ public void z_itemsMigrateFromSmallerToLargerBucket() throws Exception {
rehashedPaths.put(largerTarget, new ArrayList<String>());

for (String path : randomPaths) {
HttpGet httpGet = new HttpGet("http://localhost:8888" + path + "?fakeClientIpAddress=12.34.56.78");
HttpGet httpGet = new HttpGet("http://localhost:" + routerHttpPort + path + "?fakeClientIpAddress=12.34.56.78");
httpGet.addHeader("Host", "foo." + steeringDeliveryServiceId + ".bar");
CloseableHttpResponse response = null;

Expand Down

0 comments on commit e2a5d6f

Please sign in to comment.