Skip to content

Commit

Permalink
Merge pull request apache#120 from apache/FELIX-6472
Browse files Browse the repository at this point in the history
FELIX-6472 - Add test for BundlesStartedCheck
  • Loading branch information
cschneider authored Nov 26, 2021
2 parents e3b491e + 03a65e5 commit cd18b16
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 18 deletions.
12 changes: 12 additions & 0 deletions healthcheck/generalchecks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -69,14 +70,14 @@ public class BundlesStartedCheck implements HealthCheck {

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);
}
Expand All @@ -91,17 +92,17 @@ public Result execute() {

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;
Expand All @@ -125,20 +126,21 @@ public Result execute() {
log.warn(msg, msgObjs);
}
bundleIsLogged = true;
inctiveCount++;
inactiveCount++;
}
}
if(!bundleIsLogged) {
log.debug("Bundle {} {}: {}", bundle.getBundleId(), bundleSymbolicName, getStateLabel(bundleState));
}
}

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);
Expand All @@ -156,7 +158,7 @@ private static String getStateLabel(int state) {
case Bundle.STARTING: return "STARTING";
case Bundle.STOPPING: return "STOPPING";
case Bundle.ACTIVE: return "ACTIVE";
default: return ""+state;
default: return Integer.toString(state);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class HttpRequestsCheckTest {
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);
Expand All @@ -56,8 +56,11 @@ public void testRequestSpecParsing() throws Exception {
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>();
Expand All @@ -68,8 +71,9 @@ public void testRequestSpecParsing() throws Exception {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -36,6 +36,7 @@ static void assertJmxValue(String objectName, String attributeName, String const
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>());

Expand Down

0 comments on commit cd18b16

Please sign in to comment.