use of org.ligoj.app.plugin.vm.model.VmExecution in project plugin-vm by ligoj.
the class VmResourceTest method downloadHistoryReport.
@Test
public void downloadHistoryReport() throws Exception {
final VmResource resource = new VmResource();
applicationContext.getAutowireCapableBeanFactory().autowireBean(resource);
resource.locator = mockServicePluginLocator;
final AtomicReference<VmOperation> operation = new AtomicReference<>(null);
// The third call is skipped
Mockito.doNothing().when(mockVmTool).execute(ArgumentMatchers.argThat(new ArgumentMatcher<VmExecution>() {
@Override
public boolean matches(final VmExecution argument) {
argument.setOperation(operation.get());
return true;
}
}));
// Report without executions
ByteArrayOutputStream output = new ByteArrayOutputStream();
((StreamingOutput) resource.downloadHistoryReport(subscription, "file1").getEntity()).write(output);
List<String> lines = IOUtils.readLines(new ByteArrayInputStream(output.toByteArray()), StandardCharsets.UTF_8);
Assertions.assertEquals(1, lines.size());
Assertions.assertEquals("subscription;project;projectKey;projectName;node;dateHMS;timestamp;operation;vm;trigger;succeed;statusText;errorText", lines.get(0));
output.close();
// Manual execution
operation.set(VmOperation.OFF);
Integer id = resource.execute(subscription, VmOperation.OFF);
vmExecutionRepository.findOneExpected(id).setStatusText("status1");
// Manual execution by schedule, by pass the security check
securityHelper.setUserName(SecurityHelper.SYSTEM_USERNAME);
final Subscription entity = subscriptionRepository.findOneExpected(subscription);
operation.set(VmOperation.SHUTDOWN);
id = resource.execute(entity, VmOperation.ON);
vmExecutionRepository.findOneExpected(id).setVm("vm1");
// This call will be skipped
operation.set(null);
Assertions.assertNull(resource.execute(entity, VmOperation.REBOOT));
// Restore the current user
initSpringSecurityContext(getAuthenticationName());
// Report contains these executions (OFF/SHUTDOWN/[REBOOT = skipped])
output = new ByteArrayOutputStream();
((StreamingOutput) resource.downloadHistoryReport(subscription, "file1").getEntity()).write(output);
lines = IOUtils.readLines(new ByteArrayInputStream(output.toByteArray()), StandardCharsets.UTF_8);
Assertions.assertEquals(3, lines.size());
Assertions.assertTrue(lines.get(2).matches("\\d+;\\d+;gfi-gstack;gStack;service:vm:test:test;.+;.+;OFF;;fdaugan;true;status1;"));
Assertions.assertTrue(lines.get(1).matches("\\d+;\\d+;gfi-gstack;gStack;service:vm:test:test;.+;.+;SHUTDOWN;vm1;_system;true;;"));
Assertions.assertEquals(2, vmExecutionRepository.findAllBy("subscription.id", subscription).size());
Assertions.assertEquals(subscription, vmExecutionRepository.findAllBy("subscription.id", subscription).get(0).getSubscription().getId().intValue());
// Delete includes executions
resource.delete(subscription, true);
Assertions.assertEquals(0, vmExecutionRepository.findAllBy("subscription.id", subscription).size());
}
use of org.ligoj.app.plugin.vm.model.VmExecution in project plugin-vm by ligoj.
the class VmResource method writeSchedules.
/**
* Write all schedules.
*/
private void writeSchedules(final OutputStream output, Collection<VmSchedule> schedules, final Map<Integer, VmExecution> lastExecutions) throws IOException {
final Writer writer = new BufferedWriter(new OutputStreamWriter(output, "cp1252"));
final FastDateFormat df = FastDateFormat.getInstance("yyyy/MM/dd HH:mm:ss");
final Date now = DateUtils.newCalendar().getTime();
writer.write("subscription;project;projectKey;projectName;node;cron;operation;lastDateHMS;lastTimestamp;lastOperation;vm;lastTrigger;lastSucceed;lastStatusText;lastErrorText;nextDateHMS;nextTimestamp");
for (final VmSchedule schedule : schedules) {
// The last execution of the related schedule
final VmExecution execution = lastExecutions.get(schedule.getSubscription().getId());
writeCommon(writer, schedule.getSubscription());
writer.write(';');
writer.write(schedule.getCron());
writer.write(';');
writer.write(schedule.getOperation().name());
if (execution == null) {
writer.write(";;;;;;;;");
} else {
// Last execution
writeExecutionStatus(writer, execution, df);
}
// Next execution
try {
final Date next = new CronExpression(schedule.getCron()).getNextValidTimeAfter(now);
writer.write(';');
writer.write(df.format(next));
writer.write(';');
writer.write(String.valueOf(next.getTime()));
} catch (final ParseException pe) {
// Non blocking error
log.error("Invalid CRON expression {}", schedule.getCron());
writer.write(";ERROR;ERROR");
}
}
// Ensure buffer is flushed
writer.flush();
}
use of org.ligoj.app.plugin.vm.model.VmExecution in project plugin-vm by ligoj.
the class VmResource method writeHistory.
/**
* Write all executions related to given subscription, from the oldest to the newest.
*/
private void writeHistory(final OutputStream output, Collection<VmExecution> executions) throws IOException {
final Writer writer = new BufferedWriter(new OutputStreamWriter(output, "cp1252"));
final FastDateFormat df = FastDateFormat.getInstance("yyyy/MM/dd HH:mm:ss");
writer.write("subscription;project;projectKey;projectName;node;dateHMS;timestamp;operation;vm;trigger;succeed;statusText;errorText");
for (final VmExecution execution : executions) {
writeCommon(writer, execution.getSubscription());
writeExecutionStatus(writer, execution, df);
}
// Ensure buffer is flushed
writer.flush();
}
Aggregations