Search in sources :

Example 6 with Result

use of org.apache.sling.hc.api.Result in project sling by apache.

the class CompositeHealthCheckTest method testExecution.

@Test
public void testExecution() {
    doReturn((Result) null).when(compositeHealthCheck).checkForRecursion(Matchers.<ServiceReference>any(), Matchers.<Set<String>>any());
    String[] testTags = new String[] { "tag1" };
    compositeHealthCheck.setFilterTags(testTags);
    List<HealthCheckExecutionResult> executionResults = new LinkedList<HealthCheckExecutionResult>();
    executionResults.add(createExecutionResult("Check 1", testTags, new Result(Result.Status.INFO, "Good")));
    executionResults.add(createExecutionResult("Check 2", testTags, new Result(Result.Status.CRITICAL, "Bad")));
    when(healthCheckExecutor.execute(any(HealthCheckSelector.class))).thenReturn(executionResults);
    Result result = compositeHealthCheck.execute();
    verify(healthCheckExecutor, times(1)).execute(argThat(selectorWithTags(testTags)));
    assertEquals(Result.Status.CRITICAL, result.getStatus());
}
Also used : HealthCheckSelector(org.apache.sling.hc.api.execution.HealthCheckSelector) LinkedList(java.util.LinkedList) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) ExecutionResult(org.apache.sling.hc.core.impl.executor.ExecutionResult) Result(org.apache.sling.hc.api.Result) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) Test(org.junit.Test)

Example 7 with Result

use of org.apache.sling.hc.api.Result in project sling by apache.

the class CompositeHealthCheckTest method testSimpleRecursion.

@Test
public void testSimpleRecursion() {
    // composite check referencing itself
    final String[] filterTags = new String[] { "check1" };
    final DummyHcServiceReference hcRef = new DummyHcServiceReference("Check 1", new String[] { "check1" }, filterTags);
    // test check is hcRef
    doReturn(hcRef).when(componentContext).getServiceReference();
    compositeHealthCheck.setFilterTags(filterTags);
    compositeHealthCheck.setHealthCheckFilter(new HealthCheckFilter(null) {

        @Override
        public ServiceReference[] getHealthCheckServiceReferences(HealthCheckSelector selector) {
            String[] tags = selector.tags();
            ServiceReference[] result = new ServiceReference[] {};
            if (tags.length > 0) {
                if (tags[0].equals(filterTags[0])) {
                    result = new ServiceReference[] { hcRef };
                }
            }
            return result;
        }
    });
    Result result = compositeHealthCheck.execute();
    verify(healthCheckExecutor, never()).execute(any(HealthCheckSelector.class));
    assertEquals(Result.Status.HEALTH_CHECK_ERROR, result.getStatus());
}
Also used : HealthCheckSelector(org.apache.sling.hc.api.execution.HealthCheckSelector) HealthCheckFilter(org.apache.sling.hc.util.HealthCheckFilter) ServiceReference(org.osgi.framework.ServiceReference) ExecutionResult(org.apache.sling.hc.core.impl.executor.ExecutionResult) Result(org.apache.sling.hc.api.Result) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) Test(org.junit.Test)

Example 8 with Result

use of org.apache.sling.hc.api.Result in project sling by apache.

the class AnnotatedHealthCheckSample method execute.

@Override
public Result execute() {
    final FormattingResultLog resultLog = new FormattingResultLog();
    resultLog.info("All good at {}", new Date());
    return new Result(resultLog);
}
Also used : FormattingResultLog(org.apache.sling.hc.util.FormattingResultLog) Date(java.util.Date) Result(org.apache.sling.hc.api.Result)

Example 9 with Result

use of org.apache.sling.hc.api.Result in project sling by apache.

the class SlowHealthCheckSample method execute.

@Override
public Result execute() {
    final FormattingResultLog resultLog = new FormattingResultLog();
    try {
        final long randomDelay = (long) (Math.random() * (maxExecutionTime - minExecutionTime));
        final long toSleep = minExecutionTime + randomDelay;
        resultLog.debug("Executing {} will last {} msec", this, toSleep);
        Thread.sleep(toSleep);
    } catch (InterruptedException iex) {
        resultLog.warn("{} during execution", iex.getClass().getSimpleName());
    }
    final String execMsg = "Done executing, execution counter=" + counter.incrementAndGet();
    resultLog.debug("{}:{}", this, execMsg);
    log.debug("{}:{}", this, execMsg);
    return new Result(resultLog);
}
Also used : FormattingResultLog(org.apache.sling.hc.util.FormattingResultLog) Result(org.apache.sling.hc.api.Result)

Example 10 with Result

use of org.apache.sling.hc.api.Result in project sling by apache.

the class DefaultLoginsHealthCheck method execute.

@Override
public Result execute() {
    final FormattingResultLog resultLog = new FormattingResultLog();
    int checked = 0;
    int failures = 0;
    for (String login : logins) {
        final String[] parts = login.split(":");
        if (parts.length != 2) {
            resultLog.warn("Expected login in the form username:password, got [{}]", login);
            continue;
        }
        checked++;
        final String username = parts[0].trim();
        final String password = parts[1].trim();
        final Credentials creds = new SimpleCredentials(username, password.toCharArray());
        Session s = null;
        try {
            s = repository.login(creds);
            if (s != null) {
                failures++;
                resultLog.warn("Login as [{}] succeeded, was expecting it to fail", username);
            } else {
                resultLog.debug("Login as [{}] didn't throw an Exception but returned null Session", username);
            }
        } catch (RepositoryException re) {
            resultLog.debug("Login as [{}] failed, as expected", username);
        } finally {
            if (s != null) {
                s.logout();
            }
        }
    }
    if (checked == 0) {
        resultLog.warn("Did not check any logins, configured logins={}", logins);
    } else if (failures != 0) {
        resultLog.warn("Checked {} logins, {} failures", checked, failures);
    } else {
        resultLog.debug("Checked {} logins, all successful", checked, failures);
    }
    return new Result(resultLog);
}
Also used : SimpleCredentials(javax.jcr.SimpleCredentials) FormattingResultLog(org.apache.sling.hc.util.FormattingResultLog) RepositoryException(javax.jcr.RepositoryException) SimpleCredentials(javax.jcr.SimpleCredentials) Credentials(javax.jcr.Credentials) Session(javax.jcr.Session) Result(org.apache.sling.hc.api.Result)

Aggregations

Result (org.apache.sling.hc.api.Result)50 HealthCheckExecutionResult (org.apache.sling.hc.api.execution.HealthCheckExecutionResult)25 Test (org.junit.Test)19 FormattingResultLog (org.apache.sling.hc.util.FormattingResultLog)16 HealthCheckExecutionOptions (org.apache.sling.hc.api.execution.HealthCheckExecutionOptions)9 ExecutionResult (org.apache.sling.hc.core.impl.executor.ExecutionResult)9 ArrayList (java.util.ArrayList)8 HealthCheckMetadata (org.apache.sling.hc.util.HealthCheckMetadata)8 Matchers.anyString (org.mockito.Matchers.anyString)8 Date (java.util.Date)7 ServiceReference (org.osgi.framework.ServiceReference)6 HealthCheck (org.apache.sling.hc.api.HealthCheck)5 HealthCheckSelector (org.apache.sling.hc.api.execution.HealthCheckSelector)5 Hashtable (java.util.Hashtable)4 DistributionAgent (org.apache.sling.distribution.agent.DistributionAgent)4 DistributionQueue (org.apache.sling.distribution.queue.DistributionQueue)4 SimpleDateFormat (java.text.SimpleDateFormat)3 HashSet (java.util.HashSet)3 ObjectName (javax.management.ObjectName)3 DistributionQueueEntry (org.apache.sling.distribution.queue.DistributionQueueEntry)3