This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.hc.it-1.0.4 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-hc-it.git
commit b8922071c6e8f88ab42e9268abecd0c828915b95 Author: Bertrand Delacretaz <[email protected]> AuthorDate: Mon Aug 5 15:18:38 2013 +0000 SLING-2987 - HealthCheckSelector added git-svn-id: https://svn.apache.org/repos/asf/sling/branches/SLING-2987-healthcheck-redesign/it@1510554 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 8 +- .../org/apache/sling/hc/it/core/AllRulesTest.java | 68 ------- .../sling/hc/it/core/HealthCheckSelectorTest.java | 204 +++++++++++++++++++++ .../hc/it/core/OsgiHealthCheckFacadeTest.java | 124 ------------- .../org/apache/sling/hc/it/core/OsgiRulesTest.java | 61 ------ src/test/java/org/apache/sling/hc/it/core/U.java | 66 ++----- 6 files changed, 226 insertions(+), 305 deletions(-) diff --git a/pom.xml b/pom.xml index 7398f8d..f3e5062 100644 --- a/pom.xml +++ b/pom.xml @@ -25,6 +25,7 @@ <exam.version>3.0.3</exam.version> <url.version>1.5.2</url.version> <org.ops4j.pax.logging.DefaultServiceLog.level>INFO</org.ops4j.pax.logging.DefaultServiceLog.level> + <felix.shell>false</felix.shell> </properties> <build> @@ -36,6 +37,7 @@ <systemProperties> <org.ops4j.pax.logging.DefaultServiceLog.level>${org.ops4j.pax.logging.DefaultServiceLog.level}</org.ops4j.pax.logging.DefaultServiceLog.level> <sling.hc.core.version>${project.version}</sling.hc.core.version> + <felix.shell>${felix.shell}</felix.shell> </systemProperties> </configuration> </plugin> @@ -58,12 +60,6 @@ <scope>test</scope> </dependency> <dependency> - <groupId>org.apache.sling</groupId> - <artifactId>org.apache.sling.hc.rules</artifactId> - <version>${project.version}</version> - <scope>provided</scope> - </dependency> - <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.2</version> diff --git a/src/test/java/org/apache/sling/hc/it/core/AllRulesTest.java b/src/test/java/org/apache/sling/hc/it/core/AllRulesTest.java deleted file mode 100644 index 730c04f..0000000 --- a/src/test/java/org/apache/sling/hc/it/core/AllRulesTest.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The SF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ -package org.apache.sling.hc.it.core; - -import static org.junit.Assert.assertEquals; - -import java.io.IOException; -import java.util.List; - -import javax.inject.Inject; - -import org.apache.sling.hc.api.EvaluationResult; -import org.apache.sling.hc.api.HealthCheckFacade; -import org.apache.sling.hc.api.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.ops4j.pax.exam.Configuration; -import org.ops4j.pax.exam.Option; -import org.ops4j.pax.exam.junit.PaxExam; - -/** Test our various types of {@link Rule} together */ -@RunWith(PaxExam.class) -public class AllRulesTest { - - @Inject - private HealthCheckFacade facade; - - @Configuration - public Option[] config() { - return U.config(true); - } - - @Test - public void testMixOfRules() throws IOException { - // There should be at least one rule builder, but not a lot - final String [] rules = { - "osgi:bundle.state:org.apache.sling.hc.core:active", - "osgi:bundle.state:some.nonexistenbundle:active", - "jmxbeans:java.lang#type=ClassLoading:LoadedClassCount:> 100", - "healthcheck:RuleBuilderCount:between 2 and 10", - "healthcheck:RuleBuilderCount:between 2000 and 10000" - }; - final List<EvaluationResult> r = U.evaluateRules(facade, rules); - - assertEquals(5, r.size()); - int i=0; - U.assertResult(r.get(i++), true, "Rule: bundle.state:org.apache.sling.hc.core active"); - U.assertResult(r.get(i++), false, "Rule: bundle.state:some.nonexistenbundle active"); - U.assertResult(r.get(i++), true, "Rule: java.lang:type=ClassLoading:LoadedClassCount > 100"); - U.assertResult(r.get(i++), true, "Rule: RuleBuilderCount between 2 and 10"); - U.assertResult(r.get(i++), false, "Rule: RuleBuilderCount between 2000 and 10000"); - } -} diff --git a/src/test/java/org/apache/sling/hc/it/core/HealthCheckSelectorTest.java b/src/test/java/org/apache/sling/hc/it/core/HealthCheckSelectorTest.java new file mode 100644 index 0000000..1caf75b --- /dev/null +++ b/src/test/java/org/apache/sling/hc/it/core/HealthCheckSelectorTest.java @@ -0,0 +1,204 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The SF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package org.apache.sling.hc.it.core; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Dictionary; +import java.util.Hashtable; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import javax.inject.Inject; + +import org.apache.sling.hc.api.Constants; +import org.apache.sling.hc.api.HealthCheck; +import org.apache.sling.hc.api.HealthCheckSelector; +import org.apache.sling.hc.api.Result; +import org.apache.sling.hc.api.ResultLog; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.ops4j.pax.exam.Configuration; +import org.ops4j.pax.exam.Option; +import org.ops4j.pax.exam.junit.PaxExam; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceRegistration; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@RunWith(PaxExam.class) +public class HealthCheckSelectorTest { + + private final Logger log = LoggerFactory.getLogger(getClass()); + + @Inject + private HealthCheckSelector selector; + + @Inject + private BundleContext bundleContext; + + private List<TestHealthCheck> testServices = new ArrayList<TestHealthCheck>(); + private static int instanceCounter = 0; + + class TestHealthCheck implements HealthCheck { + + private final int id; + private final ServiceRegistration<?> reg; + final String [] tags; + + TestHealthCheck(String ... tags) { + id = instanceCounter++; + this.tags = tags; + final Dictionary<String, Object> props = new Hashtable<String, Object>(); + if(tags != null) { + props.put(Constants.HC_TAGS, tags); + } + props.put(Constants.HC_TAGS, tags); + reg = bundleContext.registerService(HealthCheck.class.getName(), this, props); + log.info("Registered {} with {}={}", new Object[] { this, Constants.HC_TAGS, props.get(Constants.HC_TAGS)}); + } + + @Override + public String toString() { + return Arrays.asList(tags).toString(); + } + + @Override + public boolean equals(Object other) { + return other instanceof TestHealthCheck && ((TestHealthCheck)other).id == id; + } + + @Override + public int hashCode() { + return id; + } + + @Override + public Result execute(ResultLog log) { + return null; + } + + @Override + public Map<String, String> getInfo() { + return null; + } + + void unregister() { + reg.unregister(); + } + } + + @Configuration + public Option[] config() { + return U.config(); + } + + @Before + public void setup() { + testServices.add(new TestHealthCheck("foo")); + testServices.add(new TestHealthCheck("bar")); + testServices.add(new TestHealthCheck("foo", "bar")); + testServices.add(new TestHealthCheck("other", "thing")); + } + + @After + public void cleanup() { + for(TestHealthCheck tc : testServices) { + tc.unregister(); + } + } + + /** @param included true or false, in the same order as testServices */ + private void assertServices(List<HealthCheck> s, boolean ... included) { + final Iterator<TestHealthCheck> it = testServices.iterator(); + for(boolean inc : included) { + final TestHealthCheck thc = it.next(); + if(inc) { + assertTrue("Expecting list of services to include " + thc, s.contains(thc)); + } else { + assertFalse("Not expecting list of services to include " + thc, s.contains(thc)); + } + } + } + + @Test + public void testSelectorService() { + assertNotNull("Expecting HealthCheckSelector service to be provided", selector); + } + + @Test + public void testAllServices() { + final List<HealthCheck> s = selector.getTaggedHealthCheck(); + assertServices(s, true, true, true, true); + } + + @Test + public void testFooTag() { + final List<HealthCheck> s = selector.getTaggedHealthCheck("foo"); + assertServices(s, true, false, true, false); + } + + @Test + public void testBarTag() { + final List<HealthCheck> s = selector.getTaggedHealthCheck("bar"); + assertServices(s, false, true, true, false); + } + + @Test + public void testFooAndBar() { + final List<HealthCheck> s = selector.getTaggedHealthCheck("foo", "bar"); + assertServices(s, false, false, true, false); + } + + @Test + public void testFooMinusBar() { + final List<HealthCheck> s = selector.getTaggedHealthCheck("foo", "-bar"); + assertServices(s, true, false, false, false); + } + + @Test + public void testOther() { + final List<HealthCheck> s = selector.getTaggedHealthCheck("other"); + assertServices(s, false, false, false, true); + } + + @Test + public void testMinusOther() { + final List<HealthCheck> s = selector.getTaggedHealthCheck("-other"); + assertServices(s, true, true, true, false); + } + + @Test + public void testMinusOtherFoo() { + final List<HealthCheck> s = selector.getTaggedHealthCheck("-other", "-foo"); + assertServices(s, false, true, false, false); + } + + @Test + public void testNoResults() { + final List<HealthCheck> s = selector.getTaggedHealthCheck("NOT A TAG"); + assertTrue("Expecting no services", s.isEmpty()); + } +} diff --git a/src/test/java/org/apache/sling/hc/it/core/OsgiHealthCheckFacadeTest.java b/src/test/java/org/apache/sling/hc/it/core/OsgiHealthCheckFacadeTest.java deleted file mode 100644 index 1ac26c3..0000000 --- a/src/test/java/org/apache/sling/hc/it/core/OsgiHealthCheckFacadeTest.java +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The SF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ -package org.apache.sling.hc.it.core; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -import java.io.IOException; -import java.util.List; - -import javax.inject.Inject; - -import org.apache.sling.hc.api.EvaluationResult; -import org.apache.sling.hc.api.HealthCheckFacade; -import org.apache.sling.hc.api.Rule; -import org.apache.sling.hc.api.RuleBuilder; -import org.apache.sling.hc.api.SystemAttribute; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.ops4j.pax.exam.Configuration; -import org.ops4j.pax.exam.Option; -import org.ops4j.pax.exam.junit.PaxExam; -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceRegistration; -import org.slf4j.Logger; - -@RunWith(PaxExam.class) -public class OsgiHealthCheckFacadeTest { - - @Inject - private HealthCheckFacade facade; - - @Inject - private BundleContext bundleContext; - - @Configuration - public Option[] config() { - return U.config(false); - } - - @Test - public void testFacadePresent() { - assertNotNull("Expecting HealthCheckFacade service to be provided", facade); - } - - @Test - public void testDefaultRules() throws IOException { - // There should be at least one rule builder, but not a lot - final String [] rules = { - "healthcheck:RuleBuilderCount:> 0", - "healthcheck:RuleBuilderCount:> 42" - }; - final List<EvaluationResult> r = U.evaluateRules(facade, rules); - - assertEquals(2, r.size()); - int i=0; - U.assertResult(r.get(i++), true, "Rule: RuleBuilderCount > 0"); - U.assertResult(r.get(i++), false, "Rule: RuleBuilderCount > 42"); - } - - @Test - public void testAddingCustomRule() throws IOException { - final String [] rules = { - "healthcheck:RuleBuilderCount:> 0", - "healthcheck:RuleBuilderCount:> 42", - "test:constant:5", - "test:constant:12", - }; - - final SystemAttribute five = new SystemAttribute() { - @Override - public String toString() { - return "five"; - } - @Override - public Object getValue(Logger logger) { - return 5; - } - }; - - // To add new rule types, just register RuleBuilder services - final RuleBuilder rb = new RuleBuilder() { - @Override - public Rule buildRule(String namespace, String ruleName, String qualifier, String expression) { - if("test".equals(namespace) && "constant".equals(ruleName)) { - return new Rule(five, expression); - } - return null; - } - }; - - final ServiceRegistration<?> reg = bundleContext.registerService(RuleBuilder.class.getName(), rb, null); - - try { - final List<EvaluationResult> r = U.evaluateRules(facade, rules); - assertEquals(4, r.size()); - int i=0; - U.assertResult(r.get(i++), true, "Rule: RuleBuilderCount > 0"); - U.assertResult(r.get(i++), false, "Rule: RuleBuilderCount > 42"); - U.assertResult(r.get(i++), true, "Rule: five 5"); - U.assertResult(r.get(i++), false, "Rule: five 12"); - } finally { - reg.unregister(); - } - - final List<EvaluationResult> r = U.evaluateRules(facade, rules); - assertEquals("Expecting custom RuleBuilder to be gone", 2, r.size()); - } -} diff --git a/src/test/java/org/apache/sling/hc/it/core/OsgiRulesTest.java b/src/test/java/org/apache/sling/hc/it/core/OsgiRulesTest.java deleted file mode 100644 index 887e7e8..0000000 --- a/src/test/java/org/apache/sling/hc/it/core/OsgiRulesTest.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The SF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ -package org.apache.sling.hc.it.core; - -import static org.junit.Assert.assertEquals; - -import java.io.IOException; -import java.util.List; - -import javax.inject.Inject; - -import org.apache.sling.hc.api.EvaluationResult; -import org.apache.sling.hc.api.HealthCheckFacade; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.ops4j.pax.exam.Configuration; -import org.ops4j.pax.exam.Option; -import org.ops4j.pax.exam.junit.PaxExam; - -@RunWith(PaxExam.class) -public class OsgiRulesTest { - - @Inject - private HealthCheckFacade facade; - - @Configuration - public Option[] config() { - return U.config(true); - } - - - @Test - public void testBundleStateRules() throws IOException { - // There should be at least one rule builder, but not a lot - final String [] rules = { - "osgi:bundle.state:org.apache.sling.hc.core:active", - "osgi:bundle.state:some.nonexistenbundle:active" - }; - final List<EvaluationResult> r = U.evaluateRules(facade, rules); - - assertEquals(2, r.size()); - int i=0; - U.assertResult(r.get(i++), true, "Rule: bundle.state:org.apache.sling.hc.core active"); - U.assertResult(r.get(i++), false, "Rule: bundle.state:some.nonexistenbundle active"); - } -} diff --git a/src/test/java/org/apache/sling/hc/it/core/U.java b/src/test/java/org/apache/sling/hc/it/core/U.java index 49bb523..8717f6a 100644 --- a/src/test/java/org/apache/sling/hc/it/core/U.java +++ b/src/test/java/org/apache/sling/hc/it/core/U.java @@ -17,66 +17,40 @@ */ package org.apache.sling.hc.it.core; -import static org.junit.Assert.assertEquals; import static org.ops4j.pax.exam.CoreOptions.junitBundles; import static org.ops4j.pax.exam.CoreOptions.mavenBundle; import static org.ops4j.pax.exam.CoreOptions.options; import static org.ops4j.pax.exam.CoreOptions.provision; -import static org.ops4j.pax.exam.CoreOptions.when; import static org.ops4j.pax.exam.CoreOptions.systemProperty; +import static org.ops4j.pax.exam.CoreOptions.when; -import java.io.IOException; -import java.io.StringReader; -import java.util.List; - -import org.apache.sling.hc.api.EvaluationResult; -import org.apache.sling.hc.api.HealthCheckFacade; -import org.apache.sling.hc.api.RulesEngine; import org.ops4j.pax.exam.Option; /** Test utilities */ public class U { - static Option[] config(boolean includeRules) { + static Option[] config() { final String coreVersion = System.getProperty("sling.hc.core.version"); - String localRepo = System.getProperty("maven.repo.local", ""); + final String localRepo = System.getProperty("maven.repo.local", ""); + final boolean felixShell = "true".equals(System.getProperty("felix.shell", "false")); - if(includeRules) { - return options( - when(localRepo.length() > 0).useOptions( - systemProperty("org.ops4j.pax.url.mvn.localRepository").value(localRepo) - ), - junitBundles(), - provision( - mavenBundle("org.apache.sling", "org.apache.sling.hc.core", coreVersion), - mavenBundle("org.apache.sling", "org.apache.sling.hc.rules", coreVersion) - ) - ); - } else { - return options( - when(localRepo.length() > 0).useOptions( - systemProperty("org.ops4j.pax.url.mvn.localRepository").value(localRepo) - ), - junitBundles(), + return options( + when(localRepo.length() > 0).useOptions( + systemProperty("org.ops4j.pax.url.mvn.localRepository").value(localRepo) + ), + junitBundles(), + when(felixShell).useOptions( provision( - mavenBundle("org.apache.sling", "org.apache.sling.hc.core", coreVersion) + mavenBundle("org.apache.felix", "org.apache.felix.gogo.shell", "0.10.0"), + mavenBundle("org.apache.felix", "org.apache.felix.gogo.runtime", "0.10.0"), + mavenBundle("org.apache.felix", "org.apache.felix.gogo.command", "0.12.0") ) - ); - } - } - - static List<EvaluationResult> evaluateRules(HealthCheckFacade facade, String [] rules) throws IOException { - final RulesEngine e = facade.getNewRulesEngine(); - final StringBuilder b = new StringBuilder(); - for(String line : rules) { - b.append(line).append("\n"); - } - e.addRules(facade.parseSimpleTextRules(new StringReader(b.toString()))); - return e.evaluateRules(); - } - - static void assertResult(EvaluationResult rr, boolean expectOk, String ruleString) { - assertEquals("Expecting " + rr.getRule() + " result to match", expectOk, !rr.anythingToReport()); - assertEquals("Expecting " + rr.getRule() + " string to match", ruleString, rr.getRule().toString()); + ), + provision( + mavenBundle("org.apache.felix", "org.apache.felix.scr", "1.6.2"), + mavenBundle("org.apache.sling", "org.apache.sling.hc.core", coreVersion), + mavenBundle("org.apache.sling", "org.apache.sling.commons.osgi", "2.2.0") + ) + ); } } -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
