use of org.sakuli.services.forwarder.gearman.model.NagiosCheckResult in project sakuli by ConSol.
the class NagiosCheckResultBuilder method build.
@Override
public NagiosCheckResult build() {
extractData(testSuite, gearmanProperties);
NagiosCheckResult result = new NagiosCheckResult(queueName, uuid);
SortedMap<PayLoadFields, String> payload = new TreeMap<>();
payload.put(TYPE, type);
//host name from properties file can overwrites the determined of the suite
if (hostProperties != null) {
payload.put(HOST, hostProperties);
} else {
payload.put(HOST, hostSuite);
}
payload.put(START_TIME, startTime);
payload.put(FINISH_TIME, finishTime);
payload.put(RETURN_CODE, returnCode);
payload.put(SERVICE_DESC, serviceDesc);
payload.put(OUTPUT, output.getOutputString());
result.setPayload(payload);
return result;
}
use of org.sakuli.services.forwarder.gearman.model.NagiosCheckResult 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 ======");
});
}
use of org.sakuli.services.forwarder.gearman.model.NagiosCheckResult 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");
}
}
use of org.sakuli.services.forwarder.gearman.model.NagiosCheckResult in project sakuli by ConSol.
the class GearmanCacheServiceTest method testCacheResultsRoundtrip.
@Test
public void testCacheResultsRoundtrip() throws Exception {
when(testSuiteProperties.getTestSuiteFolder()).thenReturn(Paths.get(getTestFolderPath()));
when(gearmanProperties.getServerQueue()).thenReturn("check_results");
List<NagiosCheckResult> originalResults = testling.getCachedResults();
try {
Assert.assertEquals(originalResults.size(), 2L);
Assert.assertEquals(originalResults.get(0).getQueueName(), "check_results");
Assert.assertEquals(originalResults.get(0).getUuid(), "example_xfce__2016_03_23_14_00_00_000");
Assert.assertTrue(originalResults.get(0).getPayload().contains("[OK] case \"case1\""));
Assert.assertEquals(originalResults.get(1).getQueueName(), "check_results");
Assert.assertEquals(originalResults.get(1).getUuid(), "example_xfce__2016_03_23_15_00_00_000");
Assert.assertTrue(originalResults.get(1).getPayload().contains("[OK] case \"case2\""));
testling.cacheResults(originalResults);
List<NagiosCheckResult> results = testling.getCachedResults();
Assert.assertEquals(results.size(), 2L);
Assert.assertEquals(results.get(0).getQueueName(), "check_results");
Assert.assertEquals(results.get(0).getUuid(), "example_xfce__2016_03_23_14_00_00_000");
Assert.assertTrue(results.get(0).getPayload().contains("[OK] case \"case1\""));
Assert.assertEquals(results.get(1).getQueueName(), "check_results");
Assert.assertEquals(results.get(1).getUuid(), "example_xfce__2016_03_23_15_00_00_000");
Assert.assertTrue(results.get(1).getPayload().contains("[OK] case \"case2\""));
NagiosCheckResult newResult = checkResultBuilder.build(testSuite);
results.add(0, newResult);
testling.cacheResults(results);
results = testling.getCachedResults();
Assert.assertEquals(results.size(), 3L);
Assert.assertEquals(results.get(0).getQueueName(), "check_results");
Assert.assertEquals(results.get(0).getUuid(), newResult.getUuid());
Assert.assertEquals(results.get(0).getPayload().trim(), newResult.getPayload().trim());
Assert.assertEquals(results.get(1).getQueueName(), "check_results");
Assert.assertEquals(results.get(1).getUuid(), "example_xfce__2016_03_23_14_00_00_000");
Assert.assertTrue(results.get(1).getPayload().contains("[OK] case \"case1\""));
Assert.assertEquals(results.get(2).getQueueName(), "check_results");
Assert.assertEquals(results.get(2).getUuid(), "example_xfce__2016_03_23_15_00_00_000");
Assert.assertTrue(results.get(2).getPayload().contains("[OK] case \"case2\""));
} finally {
testling.cacheResults(originalResults);
}
}
use of org.sakuli.services.forwarder.gearman.model.NagiosCheckResult in project sakuli by ConSol.
the class GearmanResultServiceImplTest method testSaveAllResults.
@Test
public void testSaveAllResults() throws Exception {
GearmanJob job = mock(GearmanJob.class);
doReturn(job).when(testling).creatJob(any(NagiosCheckResult.class));
Future future = mock(Future.class);
when(gearmanClient.submit(job)).thenReturn(future);
GearmanJobResult jobResult = mock(GearmanJobResult.class);
when(future.get()).thenReturn(jobResult);
when(jobResult.jobSucceeded()).thenReturn(true);
GearmanJobStatus jobStatus = mock(GearmanJobStatus.class);
when(jobStatus.isRunning()).thenReturn(false);
when(gearmanClient.getJobStatus(job)).thenReturn(jobStatus);
Date stopDate = new GregorianCalendar(2014, 14, 7, 13, 0).getTime();
TestSuite testSuite = new TestSuiteExampleBuilder().withHost("win7sakuli").withId("sakuli_demo22").withStopDate(stopDate).withStartDate(DateUtils.addSeconds(stopDate, -60)).buildExample();
when(checkResultBuilder.build(eq(testSuite))).thenReturn(new NagiosCheckResult(queueName, "sakuli_demo22__2015_03_07_12_59_00_00", testResult));
testling.tearDown(Optional.of(testSuite));
// checks
verify(gearmanCacheService, never()).cacheResults(anyList());
verify(gearmanCacheService, never()).getCachedResults();
verify(exceptionHandler, never()).handleException(any(Exception.class));
verify(exceptionHandler, never()).handleException(any(Exception.class), anyBoolean());
verify(testling).getGearmanClient();
verify(testling).getGearmanConnection(host, port);
verify(gearmanClient).addJobServer(connection);
verify(gearmanClient).submit(job);
verify(future).get();
verify(gearmanClient).shutdown();
ArgumentCaptor<NagiosCheckResult> checkresult = ArgumentCaptor.forClass(NagiosCheckResult.class);
verify(testling).creatJob(checkresult.capture());
assertEquals(checkresult.getValue().getQueueName(), queueName);
assertEquals(checkresult.getValue().getUuid(), testSuite.getGuid());
assertEquals(checkresult.getValue().getPayload(), testResult);
}
Aggregations