@Override public void run() { assertHttpStatusCodeEquals(url, expectedCode); } });
protected static void waitForApps(String webConsoleUri) { HttpTestUtils.assertHttpStatusCodeEquals(webConsoleUri+"/v1/applications", 200, 403); HttpTestUtils.assertHttpStatusCodeEventuallyEquals(webConsoleUri+"/v1/applications", 200); }
@Override public void run() { HttpTestUtils.assertContentContainsText("http://localhost1:"+port+"/hello-world", "Hello"); HttpTestUtils.assertHttpStatusCodeEquals("http://localhost1:"+port+"", 404); HttpTestUtils.assertContentContainsText("http://localhost:"+port+"", "Hello"); HttpTestUtils.assertHttpStatusCodeEquals("http://localhost:"+port+"/hello-world", 404); }}); }
public static int getHttpStatusCode(String url) throws Exception { URLConnection connection = connectToUrl(url); long startTime = System.currentTimeMillis(); int status = ((HttpURLConnection) connection).getResponseCode(); // read fully if possible, then close everything, trying to prevent cached threads at server consumeAndCloseQuietly((HttpURLConnection) connection); if (LOG.isDebugEnabled()) LOG.debug("connection to {} ({}ms) gives {}", new Object[] { url, (System.currentTimeMillis()-startTime), status }); return status; }
@Override protected void doTest(Location loc) throws Exception { final NodeJsWebAppService server = app.createAndManageChild(EntitySpec.create(NodeJsWebAppService.class) .configure("gitRepoUrl", GIT_REPO_URL) .configure("appFileName", APP_FILE) .configure("appName", APP_NAME)); app.start(ImmutableList.of(loc)); String url = server.getAttribute(NodeJsWebAppService.ROOT_URL); HttpTestUtils.assertHttpStatusCodeEventuallyEquals(url, 200); HttpTestUtils.assertContentContainsText(url, "Hello"); }
public static void assertHttpStatusCodeEventuallyEquals(final String url, final int expectedCode) { assertHttpStatusCodeEventuallyEquals(MutableMap.<String, Object>of(), url, expectedCode); }
String jbossUrl = origJboss.getAttribute(JBoss7Server.ROOT_URL); assertHttpStatusCodeEventuallyEquals(jbossUrl, 200); WebAppMonitor monitor = newWebAppMonitor(jbossUrl); DynamicWebAppCluster newCluster = (DynamicWebAppCluster) Iterables.find(app().getChildren(), Predicates.instanceOf(DynamicWebAppCluster.class)); assertHttpStatusCodeEquals(jbossUrl, 200); assertEquals(Iterables.size(newJbosses), 2); for (Entity j : newJbosses) { assertHttpStatusCodeEventuallyEquals(j.getAttribute(JBoss7Server.ROOT_URL), 200); assertUrlUnreachableEventually(jbossUrl);
@Test(groups = "Integration") public void testHttp() throws Exception { final JBoss7Server server = app.createAndManageChild(EntitySpec.create(JBoss7Server.class) .configure("war", getTestWar())); app.start(ImmutableList.of(localhostProvisioningLocation)); // Don't rely on hostname; differs from URL when tests run on AWS VM String httpUrl = server.getAttribute(JBoss7Server.ROOT_URL); assertEquals(httpUrl.toLowerCase(), ("http://"+URI.create(httpUrl).getHost()+":"+server.getAttribute(JBoss7Server.HTTP_PORT)+"/").toLowerCase()); HttpTestUtils.assertHttpStatusCodeEventuallyEquals(httpUrl, 200); HttpTestUtils.assertContentContainsText(httpUrl, "Hello"); String httpsUrl = "https://"+URI.create(httpUrl).getHost()+":"+server.getAttribute(JBoss7Server.HTTPS_PORT)+"/"; HttpTestUtils.assertUrlUnreachable(httpsUrl); Asserts.succeedsEventually(new Runnable() { @Override public void run() { assertNotNull(server.getAttribute(JBoss7Server.REQUEST_COUNT)); assertNotNull(server.getAttribute(JBoss7Server.ERROR_COUNT)); assertNotNull(server.getAttribute(JBoss7Server.TOTAL_PROCESSING_TIME)); assertNotNull(server.getAttribute(JBoss7Server.MAX_PROCESSING_TIME)); assertNotNull(server.getAttribute(JBoss7Server.BYTES_RECEIVED)); assertNotNull(server.getAttribute(JBoss7Server.BYTES_SENT)); }}); }
@Test(groups="Integration") public void testConfiguresController() { ControlledDynamicWebAppCluster cluster = app.createAndManageChild(EntitySpec.create(ControlledDynamicWebAppCluster.class) .configure("initialSize", 1) .configure("memberSpec", EntitySpec.create(TomcatServer.class).configure("war", getTestWar()))); app.start(locs); String url = cluster.getController().getAttribute(NginxController.ROOT_URL); HttpTestUtils.assertHttpStatusCodeEventuallyEquals(url, 200); HttpTestUtils.assertContentEventuallyContainsText(url, "Hello"); }
public static void assertContentMatches(String url, String regex) { String contents = getContent(url); Assert.assertNotNull(contents); Assert.assertTrue(contents.matches(regex), "Contents does not match expected regex ("+regex+"): "+contents); }
/** * Asserts that gets back any "valid" response - i.e. not an exception. This could be an unauthorized, * a redirect, a 404, or anything else that implies there is web-server listening on that port. */ public static void assertUrlReachable(String url) { try { getHttpStatusCode(url); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new RuntimeException("Interrupted for "+url+" (in assertion that is reachable)", e); } catch (Exception e) { throw new IllegalStateException("Server at "+url+" failed to respond (in assertion that is reachable): "+e, e); } }
@Override public void run() { assertContentContainsText(url, phrase, additionalPhrases); } });
@Test(groups={"Integration", "Broken"}) public void testStartsAppViaEffector() throws Exception { BrooklynNode brooklynNode = app.createAndManageChild(newBrooklynNodeSpecForTest()); app.start(locs); log.info("started "+app+" containing "+brooklynNode+" for "+JavaClassNames.niceClassAndMethod()); // note there is also a test for this in DeployApplication final URI webConsoleUri = brooklynNode.getAttribute(BrooklynNode.WEB_CONSOLE_URI); waitForApps(webConsoleUri.toString()); final String id = brooklynNode.invoke(BrooklynNode.DEPLOY_BLUEPRINT, ConfigBag.newInstance() .configure(DeployBlueprintEffector.BLUEPRINT_TYPE, BasicApplication.class.getName()) .getAllConfig()).get(); String apps = HttpTestUtils.getContent(webConsoleUri.toString()+"/v1/applications"); List<String> appType = parseJsonList(apps, ImmutableList.of("spec", "type"), String.class); assertEquals(appType, ImmutableList.of(BasicApplication.class.getName())); HttpTestUtils.assertContentEventuallyMatches( webConsoleUri.toString()+"/v1/applications/"+id+"/entities/"+id+"/sensors/service.state", "\"?(running|RUNNING)\"?"); }
HttpTestUtils.assertContentEventuallyContainsText("http://localhost1:"+port+"", "Hello"); HttpTestUtils.assertContentContainsText("http://localhost1:"+port+"", "Hello");
@Override public Void call() throws Exception { String url = entity.getAttribute(WebAppService.ROOT_URL) + "does_not_exist"; final int desiredMsgsPerSec = 10; Stopwatch stopwatch = Stopwatch.createStarted(); final AtomicInteger reqsSent = new AtomicInteger(); final Integer preRequestCount = entity.getAttribute(WebAppService.REQUEST_COUNT); // need to maintain n requests per second for the duration of the window size log.info("Applying load for "+WebAppServiceMethods.DEFAULT_WINDOW_DURATION); while (stopwatch.elapsed(TimeUnit.MILLISECONDS) < WebAppServiceMethods.DEFAULT_WINDOW_DURATION.toMilliseconds()) { long preReqsTime = stopwatch.elapsed(TimeUnit.MILLISECONDS); for (int i = 0; i < desiredMsgsPerSec; i++) { connectToUrl(url); } Time.sleep(1000 - (stopwatch.elapsed(TimeUnit.MILLISECONDS)-preReqsTime)); reqsSent.addAndGet(desiredMsgsPerSec); } Asserts.succeedsEventually(MutableMap.of("timeout", 4000), new Runnable() { @Override public void run() { Double avgReqs = entity.getAttribute(WebAppService.REQUESTS_PER_SECOND_IN_WINDOW); Integer requestCount = entity.getAttribute(WebAppService.REQUEST_COUNT); log.info("avg-requests="+avgReqs+"; total-requests="+requestCount); assertEquals(avgReqs.doubleValue(), desiredMsgsPerSec, 3.0d); assertEquals(requestCount.intValue(), preRequestCount+reqsSent.get()); }}); return null; }});
public static String getErrorContent(String url) { try { HttpURLConnection connection = (HttpURLConnection) connectToUrl(url); long startTime = System.currentTimeMillis(); String err; int status; try { InputStream errStream = connection.getErrorStream(); err = Streams.readFullyStringAndClose(errStream); status = connection.getResponseCode(); } finally { closeQuietly(connection); } if (LOG.isDebugEnabled()) LOG.debug("read of err {} ({}ms) complete; http code {}", new Object[] { url, Time.makeTimeStringRounded(System.currentTimeMillis()-startTime), status}); return err; } catch (Exception e) { throw Exceptions.propagate(e); } }
@Override public void run() { assertUrlUnreachable(url); } });
public static void assertContentEventuallyContainsText(final String url, final String phrase, final String ...additionalPhrases) { assertContentEventuallyContainsText(MutableMap.<String, Object>of(), url, phrase, additionalPhrases); }
public static void assertUrlUnreachableEventually(final String url) { assertUrlUnreachableEventually(MutableMap.<String, Object>of(), url); }
public static void assertContentEventuallyMatches(final String url, final String regex) { assertContentEventuallyMatches(MutableMap.<String, Object>of(), url, regex); }