Search in sources :

Example 6 with VmExecution

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());
}
Also used : VmExecution(org.ligoj.app.plugin.vm.model.VmExecution) AtomicReference(java.util.concurrent.atomic.AtomicReference) StreamingOutput(javax.ws.rs.core.StreamingOutput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) VmOperation(org.ligoj.app.plugin.vm.model.VmOperation) ArgumentMatcher(org.mockito.ArgumentMatcher) Subscription(org.ligoj.app.model.Subscription) Test(org.junit.jupiter.api.Test) AbstractServerTest(org.ligoj.app.AbstractServerTest)

Example 7 with VmExecution

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();
}
Also used : VmExecution(org.ligoj.app.plugin.vm.model.VmExecution) OutputStreamWriter(java.io.OutputStreamWriter) FastDateFormat(org.apache.commons.lang3.time.FastDateFormat) CronExpression(org.quartz.CronExpression) ParseException(java.text.ParseException) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) Date(java.util.Date) VmSchedule(org.ligoj.app.plugin.vm.model.VmSchedule) BufferedWriter(java.io.BufferedWriter)

Example 8 with VmExecution

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();
}
Also used : VmExecution(org.ligoj.app.plugin.vm.model.VmExecution) OutputStreamWriter(java.io.OutputStreamWriter) FastDateFormat(org.apache.commons.lang3.time.FastDateFormat) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) BufferedWriter(java.io.BufferedWriter)

Aggregations

VmExecution (org.ligoj.app.plugin.vm.model.VmExecution)8 Test (org.junit.jupiter.api.Test)5 AbstractServerTest (org.ligoj.app.AbstractServerTest)5 Subscription (org.ligoj.app.model.Subscription)4 ParseException (java.text.ParseException)3 Date (java.util.Date)3 ArgumentMatcher (org.mockito.ArgumentMatcher)3 BufferedWriter (java.io.BufferedWriter)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Writer (java.io.Writer)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 StreamingOutput (javax.ws.rs.core.StreamingOutput)2 FastDateFormat (org.apache.commons.lang3.time.FastDateFormat)2 VmOperation (org.ligoj.app.plugin.vm.model.VmOperation)2 VmSchedule (org.ligoj.app.plugin.vm.model.VmSchedule)2 SchedulerException (org.quartz.SchedulerException)2 Transactional (javax.transaction.Transactional)1