Repository: incubator-brooklyn Updated Branches: refs/heads/master f0612bd74 -> 78df97a0c
added timeout to HTTPCall assertions Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/4d2129a3 Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/4d2129a3 Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/4d2129a3 Branch: refs/heads/master Commit: 4d2129a375f793841c45253d7233d772efa5fd44 Parents: d372c19 Author: John McCabe <[email protected]> Authored: Mon Nov 9 11:28:17 2015 +0000 Committer: Mark McKenna <[email protected]> Committed: Mon Nov 9 20:56:21 2015 +0000 ---------------------------------------------------------------------- .../brooklyn-sandbox-test-framework/README.md | 15 +-- .../etc/testhttpcall-examples.yml | 98 +++++++++++++++++++- .../brooklyn/test/framework/TestHttpCall.java | 7 +- .../test/framework/TestHttpCallImpl.java | 21 +++-- 4 files changed, 124 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/4d2129a3/sandbox/brooklyn-sandbox-test-framework/README.md ---------------------------------------------------------------------- diff --git a/sandbox/brooklyn-sandbox-test-framework/README.md b/sandbox/brooklyn-sandbox-test-framework/README.md index 6405af6..a4ca928 100644 --- a/sandbox/brooklyn-sandbox-test-framework/README.md +++ b/sandbox/brooklyn-sandbox-test-framework/README.md @@ -71,6 +71,7 @@ Entity that makes a HTTP Request and tests the response | --- | ----------- | -------- | | url | The URL to test | yes | | assert | Assertions to be evaluated | yes | +| timeout | The duration to wait for assertion result | no | ##### Assertions | Key | Description | @@ -83,21 +84,23 @@ Entity that makes a HTTP Request and tests the response - type: org.apache.brooklyn.test.framework.TestHttpCall name: Status Code 200 url: $brooklyn:component("tomcat").attributeWhenReady("main.uri") + timeout: 1m assert: status: 200 - type: org.apache.brooklyn.test.framework.TestHttpCall - name: Status Code 404 - url: $brooklyn:formatString("%s/invalidpath/", component("tomcat").attributeWhenReady("main.uri")) - assert: - status: 404 - - type: org.apache.brooklyn.test.framework.TestHttpCall name: String match url: $brooklyn:component("tomcat").attributeWhenReady("main.uri") + timeout: 1m assert: string: Sample Brooklyn Deployed - type: org.apache.brooklyn.test.framework.TestHttpCall + name: Status Code 404 + url: $brooklyn:formatString("%s/invalidpath/", component("tomcat").attributeWhenReady("webapp.url")) + assert: + status: 404 + - type: org.apache.brooklyn.test.framework.TestHttpCall name: Regex match - url: $brooklyn:component("tomcat").attributeWhenReady("main.uri") + url: $brooklyn:component("tomcat").attributeWhenReady("webapp.url") # the regex assert uses java.lang.String under the hood so if the url is expected to returns # a multi-line response you should use the embedded dotall flag expression `(?s)` in your regex. # See: http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/4d2129a3/sandbox/brooklyn-sandbox-test-framework/etc/testhttpcall-examples.yml ---------------------------------------------------------------------- diff --git a/sandbox/brooklyn-sandbox-test-framework/etc/testhttpcall-examples.yml b/sandbox/brooklyn-sandbox-test-framework/etc/testhttpcall-examples.yml index 0ada000..a418fd8 100644 --- a/sandbox/brooklyn-sandbox-test-framework/etc/testhttpcall-examples.yml +++ b/sandbox/brooklyn-sandbox-test-framework/etc/testhttpcall-examples.yml @@ -10,22 +10,112 @@ services: war: "http://search.maven.org/remotecontent?filepath=io/brooklyn/example/brooklyn-example-hello-world-sql-webapp/0.6.0/brooklyn-example-hello-world-sql-webapp-0.6.0.war" - type: org.apache.brooklyn.test.framework.TestHttpCall name: Status Code 200 - url: $brooklyn:component("tomcat").attributeWhenReady("main.uri") + url: $brooklyn:component("tomcat").attributeWhenReady("webapp.url") assert: status: 200 - type: org.apache.brooklyn.test.framework.TestHttpCall name: Status Code 404 - url: $brooklyn:formatString("%s/invalidpath/", component("tomcat").attributeWhenReady("main.uri")) + url: $brooklyn:formatString("%s/invalidpath/", component("tomcat").attributeWhenReady("webapp.url")) + timeout: 10s assert: status: 404 - type: org.apache.brooklyn.test.framework.TestHttpCall name: String match - url: $brooklyn:component("tomcat").attributeWhenReady("main.uri") + url: $brooklyn:component("tomcat").attributeWhenReady("webapp.url") assert: string: Sample Brooklyn Deployed - type: org.apache.brooklyn.test.framework.TestHttpCall name: Regex match - url: $brooklyn:component("tomcat").attributeWhenReady("main.uri") + url: $brooklyn:component("tomcat").attributeWhenReady("webapp.url") + # the regex assert uses java.lang.String under the hood so if the url is expected to returns + # a multi-line response you should use the embedded dotall flag expression `(?s)` in your regex. + # See: http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html + assert: + regex: "(?s).*illustrate(\\s)*how(\\s)*web(\\s)*applications.*" +... + +--- +name: HTTP Call Test with Effector +location: localhost +services: +- type: org.apache.brooklyn.test.framework.TestCase + brooklyn.children: + - type: org.apache.brooklyn.entity.webapp.tomcat.TomcatServer + id: tomcat + brooklyn.config: + war: "http://search.maven.org/remotecontent?filepath=io/brooklyn/example/brooklyn-example-hello-world-sql-webapp/0.6.0/brooklyn-example-hello-world-sql-webapp-0.6.0.war" + - type: org.apache.brooklyn.test.framework.TestHttpCall + name: / Status Code 200 + url: $brooklyn:component("tomcat").attributeWhenReady("webapp.url") + assert: + status: 200 + - type: org.apache.brooklyn.test.framework.TestHttpCall + name: /newcontext Status Code 404 + url: $brooklyn:formatString("%s/newcontext/", component("tomcat").attributeWhenReady("webapp.url")) + assert: + status: 404 + - type: org.apache.brooklyn.test.framework.TestEffector + name: Deploy WAR in /newcontext + target: $brooklyn:component("tomcat") + effector: deploy + params: + url: http://search.maven.org/remotecontent?filepath=io/brooklyn/example/brooklyn-example-hello-world-sql-webapp/0.6.0/brooklyn-example-hello-world-sql-webapp-0.6.0.war + targetName: newcontext + - type: org.apache.brooklyn.test.framework.TestHttpCall + name: /newcontext Status Code 200 + url: $brooklyn:formatString("%s/newcontext/", component("tomcat").attributeWhenReady("webapp.url")) + # Give Tomcat time to make the newly deployed War accessible + timeout: 10s + assert: + status: 200 +... + +--- +name: HTTP Call Test with Eventual String and Regex Matches +location: localhost +services: +- type: org.apache.brooklyn.test.framework.TestCase + brooklyn.children: + - type: org.apache.brooklyn.entity.webapp.tomcat.TomcatServer + id: tomcat + brooklyn.config: + war: "http://search.maven.org/remotecontent?filepath=io/brooklyn/example/brooklyn-example-hello-world-sql-webapp/0.6.0/brooklyn-example-hello-world-sql-webapp-0.6.0.war" + - type: org.apache.brooklyn.test.framework.TestEffector + name: Deploy WAR in /newcontext + target: $brooklyn:component("tomcat") + effector: deploy + params: + url: http://search.maven.org/remotecontent?filepath=io/brooklyn/example/brooklyn-example-hello-world-sql-webapp/0.6.0/brooklyn-example-hello-world-sql-webapp-0.6.0.war + targetName: newcontext + - type: org.apache.brooklyn.test.framework.TestHttpCall + name: Regex match + url: $brooklyn:formatString("%s/newcontext/", component("tomcat").attributeWhenReady("webapp.url")) + timeout: 10s + assert: + regex: "(?s).*illustrate(\\s)*how(\\s)*web(\\s)*applications.*" + - type: org.apache.brooklyn.test.framework.TestEffector + name: Deploy WAR in /newcontext2 + target: $brooklyn:component("tomcat") + effector: deploy + params: + url: http://search.maven.org/remotecontent?filepath=io/brooklyn/example/brooklyn-example-hello-world-sql-webapp/0.6.0/brooklyn-example-hello-world-sql-webapp-0.6.0.war + targetName: newcontext2 + - type: org.apache.brooklyn.test.framework.TestHttpCall + name: String match + url: $brooklyn:formatString("%s/newcontext2/", component("tomcat").attributeWhenReady("webapp.url")) + timeout: 10s + assert: + string: Sample Brooklyn Deployed + assert: + status: 404 + - type: org.apache.brooklyn.test.framework.TestHttpCall + name: String match + url: $brooklyn:component("tomcat").attributeWhenReady("webapp.url") + assert: + string: Sample Brooklyn Deployed + - type: org.apache.brooklyn.test.framework.TestHttpCall + name: Regex match + url: $brooklyn:component("tomcat").attributeWhenReady("webapp.url") # the regex assert uses java.lang.String under the hood so if the url is expected to returns # a multi-line response you should use the embedded dotall flag expression `(?s)` in your regex. # See: http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/4d2129a3/sandbox/brooklyn-sandbox-test-framework/src/main/java/org/apache/brooklyn/test/framework/TestHttpCall.java ---------------------------------------------------------------------- diff --git a/sandbox/brooklyn-sandbox-test-framework/src/main/java/org/apache/brooklyn/test/framework/TestHttpCall.java b/sandbox/brooklyn-sandbox-test-framework/src/main/java/org/apache/brooklyn/test/framework/TestHttpCall.java index 04d551f..5dfa1c8 100644 --- a/sandbox/brooklyn-sandbox-test-framework/src/main/java/org/apache/brooklyn/test/framework/TestHttpCall.java +++ b/sandbox/brooklyn-sandbox-test-framework/src/main/java/org/apache/brooklyn/test/framework/TestHttpCall.java @@ -7,8 +7,10 @@ import org.apache.brooklyn.config.ConfigKey; import org.apache.brooklyn.core.config.ConfigKeys; import org.apache.brooklyn.core.entity.trait.Startable; import org.apache.brooklyn.util.core.flags.SetFromFlag; +import org.apache.brooklyn.util.time.Duration; import java.util.Map; +import java.util.concurrent.TimeUnit; /** * Entity that makes a HTTP Request and tests the respose @@ -19,10 +21,13 @@ import java.util.Map; public interface TestHttpCall extends Entity, Startable { @SetFromFlag(nullable = false) - ConfigKey<String> TARGET_URL = ConfigKeys.newStringConfigKey("url", "Url to test"); + ConfigKey<String> TARGET_URL = ConfigKeys.newStringConfigKey("url", "URL to test"); @SetFromFlag(nullable = false) ConfigKey<Map> ASSERTIONS = ConfigKeys.newConfigKey(Map.class, "assert", "Assertions to be evaluated", Maps.newLinkedHashMap()); + ConfigKey<Duration> TIMEOUT = ConfigKeys.newConfigKey(Duration.class, "timeout", + "The duration to wait for assertion result", new Duration(1L, TimeUnit.SECONDS)); + } http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/4d2129a3/sandbox/brooklyn-sandbox-test-framework/src/main/java/org/apache/brooklyn/test/framework/TestHttpCallImpl.java ---------------------------------------------------------------------- diff --git a/sandbox/brooklyn-sandbox-test-framework/src/main/java/org/apache/brooklyn/test/framework/TestHttpCallImpl.java b/sandbox/brooklyn-sandbox-test-framework/src/main/java/org/apache/brooklyn/test/framework/TestHttpCallImpl.java index 386f20c..7413066 100644 --- a/sandbox/brooklyn-sandbox-test-framework/src/main/java/org/apache/brooklyn/test/framework/TestHttpCallImpl.java +++ b/sandbox/brooklyn-sandbox-test-framework/src/main/java/org/apache/brooklyn/test/framework/TestHttpCallImpl.java @@ -1,6 +1,7 @@ package org.apache.brooklyn.test.framework; import com.google.api.client.util.Objects; +import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; import org.apache.brooklyn.api.location.Location; import org.apache.brooklyn.core.entity.AbstractEntity; @@ -8,13 +9,16 @@ import org.apache.brooklyn.core.entity.lifecycle.Lifecycle; import org.apache.brooklyn.core.entity.lifecycle.ServiceStateLogic; import org.apache.brooklyn.util.core.flags.TypeCoercions; import org.apache.brooklyn.util.exceptions.Exceptions; +import org.apache.brooklyn.util.time.Duration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Collection; import java.util.Map; -import static org.apache.brooklyn.util.http.HttpAsserts.*; +import static org.apache.brooklyn.util.http.HttpAsserts.assertContentEventuallyContainsText; +import static org.apache.brooklyn.util.http.HttpAsserts.assertContentEventuallyMatches; +import static org.apache.brooklyn.util.http.HttpAsserts.assertHttpStatusCodeEventuallyEquals; /** * {@inheritDoc} @@ -30,8 +34,9 @@ public class TestHttpCallImpl extends AbstractEntity implements TestHttpCall { ServiceStateLogic.setExpectedState(this, Lifecycle.STARTING); final String url = getConfig(TARGET_URL); final Map assertions = getConfig(ASSERTIONS); + final Duration timeout = getConfig(TIMEOUT); try { - checkAssertions(url, assertions); + checkAssertions(url.toString(), ImmutableMap.of("timeout", timeout), assertions); sensors().set(SERVICE_UP, true); ServiceStateLogic.setExpectedState(this, Lifecycle.RUNNING); } catch (Throwable t) { @@ -57,23 +62,27 @@ public class TestHttpCallImpl extends AbstractEntity implements TestHttpCall { * See the test/resources directory for examples. * * @param url The target URL to be tested + * @param flags Passed to {@link org.apache.brooklyn.util.http.HttpAsserts#assertContentEventuallyContainsText(Map, String, String, String...)}, + * {@link org.apache.brooklyn.util.http.HttpAsserts#assertContentEventuallyMatches(Map, String, String)}, + * {@link org.apache.brooklyn.util.http.HttpAsserts#assertHttpStatusCodeEventuallyEquals(Map, String, int)} + * * @param assertions The map of assertions */ - private void checkAssertions(final String url, final Map<?, ?> assertions) { + private void checkAssertions(final String url, final Map<String,?> flags, final Map<?, ?> assertions) { for (final Map.Entry<?, ?> entry : assertions.entrySet()) { if (Objects.equal(entry.getKey(), "regex")) { LOG.info("Testing if url [{}] matches regex [{}]", new Object[]{url, entry.getValue()}); - assertContentMatches(url, TypeCoercions.coerce(entry.getValue(), String.class)); + assertContentEventuallyMatches( flags, url, TypeCoercions.coerce(entry.getValue(), String.class)); } else if (Objects.equal(entry.getKey(), "string")) { LOG.debug("Testing if url [{}] contains string [{}]", new Object[]{url, entry.getValue()}); - assertContentContainsText(url, TypeCoercions.coerce(entry.getValue(), String.class)); + assertContentEventuallyContainsText(flags, url, TypeCoercions.coerce(entry.getValue(), String.class)); } else if (Objects.equal(entry.getKey(), "status")) { LOG.debug("Testing if url [{}] returns status code [{}]", new Object[]{url, entry.getValue()}); - assertHttpStatusCodeEquals(url, TypeCoercions.coerce(entry.getValue(), Integer.class)); + assertHttpStatusCodeEventuallyEquals(flags, url, TypeCoercions.coerce(entry.getValue(), Integer.class)); } } }
