Search in sources :

Example 1 with SakuliForwarderCheckedException

use of org.sakuli.exceptions.SakuliForwarderCheckedException in project sakuli by ConSol.

the class GearmanResultServiceImpl method tearDown.

@Override
public void tearDown(Optional<AbstractTestDataEntity> dataEntity, boolean asyncCall) {
    dataEntity.ifPresent(data -> {
        logger.info("======= SEND RESULTS TO GEARMAN SERVER ======");
        GearmanClient gearmanClient = getGearmanClient();
        GearmanJobServerConnection connection = getGearmanConnection(properties.getServerHost(), properties.getServerPort());
        List<NagiosCheckResult> results = new ArrayList<>();
        try {
            results.add(nagiosCheckResultBuilder.build(data));
            if (properties.isCacheEnabled()) {
                results.addAll(cacheService.getCachedResults());
                if (results.size() > 1) {
                    logger.info(String.format("Processing %s cached results first", results.size() - 1));
                }
            }
            if (!gearmanClient.addJobServer(connection)) {
                throw new SakuliForwarderCheckedException(String.format("Failed to connect to Gearman server '%s:%s'", properties.getServerHost(), properties.getServerPort()));
            } else {
                // sending in reverse original happened order
                Collections.reverse(results);
                results = results.stream().filter(checkResult -> !sendResult(gearmanClient, checkResult, asyncCall, data)).collect(Collectors.toList());
                Collections.reverse(results);
            }
        } catch (Exception e) {
            handleTeardownException((e instanceof SakuliForwarderException) ? e : new SakuliForwarderRuntimeException(String.format("Could not transfer Sakuli results to the Gearman server '%s:%s'", properties.getServerHost(), properties.getServerPort()), e), asyncCall, data);
        }
        // save all not send results
        if (properties.isCacheEnabled()) {
            try {
                cacheService.cacheResults(results);
            } catch (SakuliForwarderCheckedException e) {
                handleTeardownException(e, asyncCall, data);
            }
        }
        gearmanClient.shutdown();
        logger.info("======= FINISHED: SEND RESULTS TO GEARMAN SERVER ======");
    });
}
Also used : GearmanJobServerConnection(org.gearman.common.GearmanJobServerConnection) SakuliForwarderRuntimeException(org.sakuli.exceptions.SakuliForwarderRuntimeException) ArrayList(java.util.ArrayList) NagiosCheckResult(org.sakuli.services.forwarder.gearman.model.NagiosCheckResult) SakuliForwarderException(org.sakuli.exceptions.SakuliForwarderException) SakuliForwarderCheckedException(org.sakuli.exceptions.SakuliForwarderCheckedException) SakuliForwarderException(org.sakuli.exceptions.SakuliForwarderException) SakuliForwarderRuntimeException(org.sakuli.exceptions.SakuliForwarderRuntimeException) SakuliForwarderCheckedException(org.sakuli.exceptions.SakuliForwarderCheckedException)

Example 2 with SakuliForwarderCheckedException

use of org.sakuli.exceptions.SakuliForwarderCheckedException in project sakuli by ConSol.

the class GearmanCacheService method cacheResults.

/**
 * Writes results to Gearman cache file.
 *
 * @param results
 */
public void cacheResults(List<NagiosCheckResult> results) throws SakuliForwarderCheckedException {
    Path cacheFile = testSuiteProperties.getTestSuiteFolder().resolve(CACHE_FILE);
    File output = new File(cacheFile.toUri());
    if (!output.getParentFile().exists()) {
        output.getParentFile().mkdirs();
    }
    try (FileOutputStream fos = new FileOutputStream(output)) {
        for (NagiosCheckResult result : results) {
            fos.write((CACHE_SEPARATOR + " " + result.getQueueName() + ":" + result.getUuid() + LINE_SEPARATOR).getBytes(CHARSET_NAME));
            fos.write((result.getPayload().trim() + LINE_SEPARATOR).getBytes(CHARSET_NAME));
            fos.write((CACHE_SEPARATOR + LINE_SEPARATOR).getBytes(CHARSET_NAME));
        }
        if (results.isEmpty()) {
            fos.write(LINE_SEPARATOR.getBytes(CHARSET_NAME));
        }
        fos.flush();
    } catch (IOException e) {
        throw new SakuliForwarderCheckedException(e, "Failed to write Gearman cache file");
    }
}
Also used : Path(java.nio.file.Path) FileOutputStream(java.io.FileOutputStream) NagiosCheckResult(org.sakuli.services.forwarder.gearman.model.NagiosCheckResult) IOException(java.io.IOException) File(java.io.File) SakuliForwarderCheckedException(org.sakuli.exceptions.SakuliForwarderCheckedException)

Example 3 with SakuliForwarderCheckedException

use of org.sakuli.exceptions.SakuliForwarderCheckedException in project sakuli by ConSol.

the class ScreenshotDivConverterTest method testThrowException.

@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
@Test
public void testThrowException() throws Exception {
    Path screenshotPath = Paths.get("computerNOTVALID.png");
    ArgumentCaptor<Exception> excpCaptor = ArgumentCaptor.forClass(Exception.class);
    doNothing().when(sakuliExceptionHandler).handleException(excpCaptor.capture());
    ScreenshotDiv result = testling.convert(new SakuliExceptionWithScreenshot("test", screenshotPath));
    assertNull(result);
    verify(sakuliExceptionHandler).handleException(any(SakuliForwarderCheckedException.class));
    Exception excp = excpCaptor.getValue();
    assertTrue(excp instanceof SakuliForwarderCheckedException);
    assertEquals(excp.getMessage(), "error during the BASE64 encoding of the screenshot 'computerNOTVALID.png'");
    assertTrue(excp.getSuppressed()[0] instanceof NoSuchFileException);
}
Also used : Path(java.nio.file.Path) SakuliExceptionWithScreenshot(org.sakuli.exceptions.SakuliExceptionWithScreenshot) NoSuchFileException(java.nio.file.NoSuchFileException) NoSuchFileException(java.nio.file.NoSuchFileException) SakuliForwarderCheckedException(org.sakuli.exceptions.SakuliForwarderCheckedException) SakuliForwarderCheckedException(org.sakuli.exceptions.SakuliForwarderCheckedException) Test(org.testng.annotations.Test)

Example 4 with SakuliForwarderCheckedException

use of org.sakuli.exceptions.SakuliForwarderCheckedException in project sakuli by ConSol.

the class AbstractTemplateOutputBuilder method createOutput.

/**
 * Converts the current test data entity to a string based on the template for the concrete converter.
 *
 * @param abstractTestDataEntity Test data entity, which has to be converted
 * @return A string representation of the provided test data entity
 */
public String createOutput(AbstractTestDataEntity abstractTestDataEntity) throws SakuliForwarderCheckedException {
    try {
        JtwigModel model = createModel(abstractTestDataEntity);
        EnvironmentConfiguration configuration = EnvironmentConfigurationBuilder.configuration().extensions().add(new SpacelessExtension(new SpacelessConfiguration(new LeadingWhitespaceRemover()))).and().functions().add(new IsBlankFunction()).add(new GetOutputStateFunction()).add(new GetOutputDurationFunction()).add(new ExtractScreenshotFunction(screenshotDivConverter)).add(new AbbreviateFunction()).add(new UnixTimestampConverterFunction()).add(new GetTestDataEntityTypeFunction()).and().build();
        JtwigTemplate template = JtwigTemplate.fileTemplate(getTemplatePath().toFile(), configuration);
        logger.debug(String.format("Render model into JTwig template. Model: '%s'", model));
        return template.render(model);
    } catch (Exception e) {
        throw new SakuliForwarderCheckedException(e, "Exception during rendering of Twig template occurred!");
    }
}
Also used : SpacelessExtension(org.jtwig.spaceless.SpacelessExtension) JtwigTemplate(org.jtwig.JtwigTemplate) FileNotFoundException(java.io.FileNotFoundException) SakuliForwarderCheckedException(org.sakuli.exceptions.SakuliForwarderCheckedException) SpacelessConfiguration(org.jtwig.spaceless.configuration.SpacelessConfiguration) JtwigModel(org.jtwig.JtwigModel) EnvironmentConfiguration(org.jtwig.environment.EnvironmentConfiguration) SakuliForwarderCheckedException(org.sakuli.exceptions.SakuliForwarderCheckedException)

Example 5 with SakuliForwarderCheckedException

use of org.sakuli.exceptions.SakuliForwarderCheckedException in project sakuli by ConSol.

the class ScreenshotDivConverter method extractScreenshotAsBase64.

protected String extractScreenshotAsBase64(Exception exception) {
    if (exception instanceof SakuliExceptionWithScreenshot) {
        Path screenshotPath = ((SakuliExceptionWithScreenshot) exception).getScreenshot();
        if (screenshotPath != null) {
            try {
                byte[] binaryScreenshot = Files.readAllBytes(screenshotPath);
                String base64String = new BASE64Encoder().encode(binaryScreenshot);
                for (String newLine : Arrays.asList("\n", "\r")) {
                    base64String = StringUtils.remove(base64String, newLine);
                }
                return base64String;
            } catch (IOException e) {
                exceptionHandler.handleException(new SakuliForwarderCheckedException(e, String.format("error during the BASE64 encoding of the screenshot '%s'", screenshotPath.toString())));
            }
        }
    }
    return null;
}
Also used : Path(java.nio.file.Path) BASE64Encoder(sun.misc.BASE64Encoder) SakuliExceptionWithScreenshot(org.sakuli.exceptions.SakuliExceptionWithScreenshot) IOException(java.io.IOException) SakuliForwarderCheckedException(org.sakuli.exceptions.SakuliForwarderCheckedException)

Aggregations

SakuliForwarderCheckedException (org.sakuli.exceptions.SakuliForwarderCheckedException)8 IOException (java.io.IOException)5 Path (java.nio.file.Path)4 NagiosCheckResult (org.sakuli.services.forwarder.gearman.model.NagiosCheckResult)3 ArrayList (java.util.ArrayList)2 SakuliExceptionWithScreenshot (org.sakuli.exceptions.SakuliExceptionWithScreenshot)2 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 NoSuchFileException (java.nio.file.NoSuchFileException)1 GearmanJobServerConnection (org.gearman.common.GearmanJobServerConnection)1 JtwigModel (org.jtwig.JtwigModel)1 JtwigTemplate (org.jtwig.JtwigTemplate)1 EnvironmentConfiguration (org.jtwig.environment.EnvironmentConfiguration)1 SpacelessExtension (org.jtwig.spaceless.SpacelessExtension)1 SpacelessConfiguration (org.jtwig.spaceless.configuration.SpacelessConfiguration)1 SakuliForwarderException (org.sakuli.exceptions.SakuliForwarderException)1 SakuliForwarderRuntimeException (org.sakuli.exceptions.SakuliForwarderRuntimeException)1 Test (org.testng.annotations.Test)1 BASE64Encoder (sun.misc.BASE64Encoder)1