Merge pull request #120 from apache/FELIX-6472
FELIX-6472 - Add test for BundlesStartedCheck
diff --git a/healthcheck/generalchecks/pom.xml b/healthcheck/generalchecks/pom.xml
index 4a09b78..6102034 100644
--- a/healthcheck/generalchecks/pom.xml
+++ b/healthcheck/generalchecks/pom.xml
@@ -227,6 +227,18 @@
<version>2.4.13</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.util.converter</artifactId>
+ <version>1.0.8</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-simple</artifactId>
+ <version>1.7.6</version>
+ <scope>test</scope>
+ </dependency>
<!-- END test scope dependencies -->
</dependencies>
diff --git a/healthcheck/generalchecks/src/main/java/org/apache/felix/hc/generalchecks/BundlesStartedCheck.java b/healthcheck/generalchecks/src/main/java/org/apache/felix/hc/generalchecks/BundlesStartedCheck.java
index c33c49f..36fc239 100644
--- a/healthcheck/generalchecks/src/main/java/org/apache/felix/hc/generalchecks/BundlesStartedCheck.java
+++ b/healthcheck/generalchecks/src/main/java/org/apache/felix/hc/generalchecks/BundlesStartedCheck.java
@@ -17,6 +17,7 @@
*/
package org.apache.felix.hc.generalchecks;
+import java.util.Optional;
import java.util.regex.Pattern;
import org.apache.felix.hc.annotation.HealthCheckService;
@@ -69,14 +70,14 @@
private BundleContext bundleContext;
private Pattern includesRegex;
- private Pattern excludesRegex;
+ private Optional<Pattern> excludesRegex;
boolean useCriticalForInactive;
@Activate
protected void activate(BundleContext bundleContext, Config config) {
this.bundleContext = bundleContext;
this.includesRegex = Pattern.compile(config.includesRegex());
- this.excludesRegex = StringUtils.isNotBlank(config.excludesRegex()) ? Pattern.compile(config.excludesRegex()) : null;
+ this.excludesRegex = StringUtils.isNotBlank(config.excludesRegex()) ? Optional.of(Pattern.compile(config.excludesRegex())) : Optional.empty();
this.useCriticalForInactive = config.useCriticalForInactive();
LOG.debug("Activated bundles started HC for includesRegex={} excludesRegex={}% useCriticalForInactive={}", includesRegex, excludesRegex, useCriticalForInactive);
}
@@ -91,17 +92,17 @@
int countExcluded = 0;
int relevantBundlesCount = 0;
- int inctiveCount = 0;
+ int inactiveCount = 0;
for (Bundle bundle : bundles) {
String bundleSymbolicName = bundle.getSymbolicName();
int bundleState = bundle.getState();
- if(!includesRegex.matcher(bundleSymbolicName).matches()) {
+ if (!includesRegex.matcher(bundleSymbolicName).matches()) {
LOG.debug("Bundle {} not matched by {}", bundleSymbolicName, includesRegex);
continue;
}
- if(excludesRegex!=null && excludesRegex.matcher(bundleSymbolicName).matches()) {
+ if (excludesRegex.isPresent() && excludesRegex.get().matcher(bundleSymbolicName).matches()) {
LOG.debug("Bundle {} excluded {}", bundleSymbolicName, excludesRegex);
countExcluded ++;
continue;
@@ -125,7 +126,7 @@
log.warn(msg, msgObjs);
}
bundleIsLogged = true;
- inctiveCount++;
+ inactiveCount++;
}
}
if(!bundleIsLogged) {
@@ -133,12 +134,13 @@
}
}
- String baseMsg = relevantBundlesCount+" bundles"+(!includesRegex.pattern().equals(".*")?" for pattern "+includesRegex.pattern(): "");
- String excludedMsg = countExcluded > 0 ? " (" + countExcluded + " excluded via pattern "+excludesRegex.pattern()+")" : "";
- if (inctiveCount > 0) {
- log.info("Found "+inctiveCount + " inactive of "+baseMsg + excludedMsg);
+ String includeMsg = !includesRegex.pattern().equals(".*") ? " for pattern " + includesRegex.pattern(): "";
+ String baseMsg = relevantBundlesCount + " bundles" + includeMsg;
+ String excludedMsg = countExcluded > 0 ? " (" + countExcluded + " excluded via pattern " + excludesRegex.get().pattern() + ")" : "";
+ if (inactiveCount > 0) {
+ log.info("Found " + inactiveCount + " inactive of " + baseMsg + excludedMsg);
} else {
- log.info("All "+baseMsg+" are started" + excludedMsg);
+ log.info("All " + baseMsg + " are started" + excludedMsg);
}
return new Result(log);
@@ -156,7 +158,7 @@
case Bundle.STARTING: return "STARTING";
case Bundle.STOPPING: return "STOPPING";
case Bundle.ACTIVE: return "ACTIVE";
- default: return ""+state;
+ default: return Integer.toString(state);
}
}
diff --git a/healthcheck/generalchecks/src/test/java/org/apache/felix/hc/generalchecks/BundlesStartedCheckTest.java b/healthcheck/generalchecks/src/test/java/org/apache/felix/hc/generalchecks/BundlesStartedCheckTest.java
new file mode 100644
index 0000000..b55ea43
--- /dev/null
+++ b/healthcheck/generalchecks/src/test/java/org/apache/felix/hc/generalchecks/BundlesStartedCheckTest.java
@@ -0,0 +1,175 @@
+/*
+ * 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.felix.hc.generalchecks;
+
+import static java.util.Collections.emptyMap;
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.osgi.framework.Constants.ACTIVATION_LAZY;
+import static org.osgi.framework.Constants.BUNDLE_ACTIVATIONPOLICY;
+import static org.osgi.framework.Constants.FRAGMENT_HOST;
+
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Map;
+
+import org.apache.felix.hc.api.Result;
+import org.apache.felix.hc.api.Result.Status;
+import org.apache.felix.hc.generalchecks.BundlesStartedCheck.Config;
+import org.junit.Before;
+import org.junit.Test;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.util.converter.Converters;
+
+public class BundlesStartedCheckTest {
+
+ private BundleContext context;
+
+ @Before
+ public void before() {
+ context = mock(BundleContext.class);
+ }
+
+ @Test
+ public void testOKResultWithNoBundles() {
+ BundlesStartedCheck check = createCheck(emptyMap());
+ Result result = executeCheck(check);
+ assertThat(result.getStatus(), equalTo(Status.OK));
+ }
+
+ @Test
+ public void testOKResultWithResolvedNotIncludedBundle() {
+ BundlesStartedCheck check = createCheck(configWith("includesRegex", "a.*"));
+ Bundle bundle = mockBundle("mybundle", Bundle.RESOLVED);
+ Result result = executeCheck(check, bundle);
+ assertThat(result.getStatus(), equalTo(Status.OK));
+ }
+
+ @Test
+ public void testOKResultWithResolvedExcludedBundle() {
+ BundlesStartedCheck check = createCheck(configWith("excludesRegex", "mybundle"));
+ Bundle bundle = mockBundle("mybundle", Bundle.RESOLVED);
+ Bundle other = mockBundle("other", Bundle.ACTIVE);
+ Result result = executeCheck(check, bundle, other);
+ assertThat(result.getStatus(), equalTo(Status.OK));
+ }
+
+ @Test
+ public void testOKResultWithActiveBundle() {
+ BundlesStartedCheck check = createCheck(emptyMap());
+ Bundle bundle = mockBundle("mybundle", Bundle.ACTIVE);
+ Result result = executeCheck(check, bundle);
+ assertThat(result.getStatus(), equalTo(Status.OK));
+ }
+
+ @Test
+ public void testWARNResultWithResolvedBundle() {
+ BundlesStartedCheck check = createCheck(emptyMap());
+ Bundle bundle = mockBundle("mybundle", Bundle.RESOLVED);
+ Result result = executeCheck(check, bundle);
+ assertThat(result.getStatus(), equalTo(Status.WARN));
+ }
+
+ @Test
+ public void testCRITICALResultWithResolvedBundleAndConfig() {
+ BundlesStartedCheck check = createCheck(configWith("useCriticalForInactive", "true"));
+ Bundle bundle = mockBundle("mybundle", Bundle.RESOLVED);
+ Result result = executeCheck(check, bundle);
+ assertThat(result.getStatus(), equalTo(Status.CRITICAL));
+ }
+
+ @Test
+ public void testOKResultWithResolvedFragmentBundle() {
+ BundlesStartedCheck check = createCheck(emptyMap());
+ Bundle bundle = mockBundle("mybundle", Bundle.RESOLVED, withHeader(FRAGMENT_HOST, "fragmentbundle"));
+ Result result = executeCheck(check, bundle);
+ assertThat(result.getStatus(), equalTo(Status.OK));
+ }
+
+ @Test
+ public void testOKResultWithStartingLazyBundle() {
+ BundlesStartedCheck check = createCheck(emptyMap());
+ Bundle bundle = mockBundle("mybundle", Bundle.STARTING, withHeader(BUNDLE_ACTIVATIONPOLICY, ACTIVATION_LAZY));
+ Result result = executeCheck(check, bundle);
+ assertThat(result.getStatus(), equalTo(Status.OK));
+ System.out.println(result);
+ }
+
+ @Test
+ public void testWarnResultWithStartingBundle() {
+ BundlesStartedCheck check = createCheck(emptyMap());
+ Bundle bundle = mockBundle("mybundle", Bundle.STARTING);
+ Result result = executeCheck(check, bundle);
+ assertThat(result.getStatus(), equalTo(Status.WARN));
+ System.out.println(result);
+ }
+
+ @Test
+ public void testWARNResultWithOtherStatuses() {
+ BundlesStartedCheck check = createCheck(emptyMap());
+ Bundle bundle = mockBundle("mybundle", Bundle.INSTALLED);
+ Bundle bundle2 = mockBundle("uninstalledbundle", Bundle.UNINSTALLED);
+ Bundle bundle3 = mockBundle("stoppingbundle", Bundle.STOPPING);
+ Bundle bundle4 = mockBundle("startunkownstatebundle", 50);
+ Result result = executeCheck(check, bundle, bundle2, bundle3, bundle4);
+ assertThat(result.getStatus(), equalTo(Status.WARN));
+ }
+
+ private Hashtable<String, String> withHeader(String key, String value) {
+ Hashtable<String, String> headers = new Hashtable<String, String>();
+ headers.put(key, value);
+ return headers;
+ }
+
+ private Map<String, String> configWith(String ... contents) {
+ Map<String, String> props = new HashMap<>();
+ if (contents.length >= 2) {
+ props.put(contents[0], contents[1]);
+ }
+ return props;
+ }
+
+ private BundlesStartedCheck createCheck(Map<String, String> props) {
+ BundlesStartedCheck check = new BundlesStartedCheck();
+ Config config = Converters.standardConverter().convert(props).to(BundlesStartedCheck.Config.class);
+ check.activate(context, config);
+ return check;
+ }
+
+ private Result executeCheck(BundlesStartedCheck check, Bundle... bundles) {
+ when(context.getBundles()).thenReturn(bundles);
+ Result result = check.execute();
+ System.out.println(result);
+ return result;
+ }
+
+ private Bundle mockBundle(String symbolicName, Integer state) {
+ return mockBundle(symbolicName, state, new Hashtable<>());
+ }
+
+ private Bundle mockBundle(String symbolicName, Integer state, Hashtable<String, String> headers) {
+ Bundle bundle = mock(Bundle.class);
+ when(bundle.getSymbolicName()).thenReturn(symbolicName);
+ when(bundle.getState()).thenReturn(state);
+ when(bundle.getHeaders()).thenReturn(headers);
+ return bundle;
+ }
+}
diff --git a/healthcheck/generalchecks/src/test/java/org/apache/felix/hc/generalchecks/HttpRequestsCheckTest.java b/healthcheck/generalchecks/src/test/java/org/apache/felix/hc/generalchecks/HttpRequestsCheckTest.java
index 6cca68b..f80f01f 100644
--- a/healthcheck/generalchecks/src/test/java/org/apache/felix/hc/generalchecks/HttpRequestsCheckTest.java
+++ b/healthcheck/generalchecks/src/test/java/org/apache/felix/hc/generalchecks/HttpRequestsCheckTest.java
@@ -45,7 +45,7 @@
HttpRequestsCheck.Response simple200HtmlResponse = new HttpRequestsCheck.Response(200, "OK", null, "<html><head><title>test</title></head><body>body text</body></html>", 200);
@Test
- public void testRequestSpecParsing() throws Exception {
+ public void testRequestSpecParsingPath() throws Exception {
HttpRequestsCheck.RequestSpec requestSpec = new HttpRequestsCheck.RequestSpec("/path/to/page.html");
assertEquals("/path/to/page.html", requestSpec.url);
@@ -56,8 +56,11 @@
assertNull(requestSpec.connectTimeoutInMs);
assertNull(requestSpec.readTimeoutInMs);
assertNull(requestSpec.proxy);
-
- requestSpec = new HttpRequestsCheck.RequestSpec("-X POST -H \"X-Test: Test\" -d \"{ 1,2,3 }\" -u admin:admin --connect-timeout 4 -m 5 --proxy http://proxy:2000 /path/to/page.html => 201");
+ }
+
+ @Test
+ public void testRequestSpecParsingPost() throws Exception {
+ RequestSpec requestSpec = new HttpRequestsCheck.RequestSpec("-X POST -H \"X-Test: Test\" -d \"{ 1,2,3 }\" -u admin:admin --connect-timeout 4 -m 5 --proxy http://proxy:2000 /path/to/page.html => 201");
assertEquals("/path/to/page.html", requestSpec.url);
assertEquals("POST", requestSpec.method);
HashMap<String, String> expectedHeaders = new HashMap<String,String>();
@@ -68,8 +71,9 @@
assertEquals("admin", requestSpec.user);
assertEquals((Integer) 4000, requestSpec.connectTimeoutInMs);
assertEquals((Integer) 5000, requestSpec.readTimeoutInMs);
- assertEquals("proxy:2000", requestSpec.proxy.address().toString());
-
+ String proxyAddress = requestSpec.proxy.address().toString();
+ assertThat(proxyAddress, containsString("proxy"));
+ assertThat(proxyAddress, containsString(":2000"));
}
@Test
diff --git a/healthcheck/generalchecks/src/test/java/org/apache/felix/hc/generalchecks/JmxAttributeHealthCheckTest.java b/healthcheck/generalchecks/src/test/java/org/apache/felix/hc/generalchecks/JmxAttributeHealthCheckTest.java
index b012c67..c2525e1 100644
--- a/healthcheck/generalchecks/src/test/java/org/apache/felix/hc/generalchecks/JmxAttributeHealthCheckTest.java
+++ b/healthcheck/generalchecks/src/test/java/org/apache/felix/hc/generalchecks/JmxAttributeHealthCheckTest.java
@@ -24,7 +24,7 @@
import java.util.HashMap;
import org.apache.felix.hc.api.Result;
-import org.apache.felix.hc.generalchecks.JmxAttributeCheck;
+import org.apache.felix.hc.api.Result.Status;
import org.junit.Test;
public class JmxAttributeHealthCheckTest {
@@ -36,6 +36,7 @@
when(configuration.mbean_name()).thenReturn(objectName);
when(configuration.attribute_name()).thenReturn(attributeName);
when(configuration.attribute_value_constraint()).thenReturn(constraint);
+ when(configuration.statusForFailedContraint()).thenReturn(Status.WARN);
hc.activate(configuration, new HashMap<String,Object>());