Search in sources :

Example 1 with VmOperation

use of org.ligoj.app.plugin.vm.model.VmOperation in project plugin-vm by ligoj.

the class VmResourceTest method downloadNodeSchedulesReport.

@Test
public void downloadNodeSchedulesReport() 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.downloadNodeSchedulesReport("service:vm:test", "file1").getEntity()).write(output);
    List<String> lines = IOUtils.readLines(new ByteArrayInputStream(output.toByteArray()), StandardCharsets.UTF_8);
    Assertions.assertEquals(2, lines.size());
    Assertions.assertEquals("subscription;project;projectKey;projectName;node;cron;operation;lastDateHMS;lastTimestamp;lastOperation;vm;lastTrigger;lastSucceed;lastStatusText;lastErrorText;nextDateHMS;nextTimestamp", lines.get(0));
    // No last execution available
    Assertions.assertTrue(lines.get(1).matches("\\d+;\\d+;gfi-gstack;gStack;service:vm:test:test;0 0 0 1 1 \\? 2050;OFF;;;;;;;;;2050/01/01 00:00:00;2524604400000"), "Was : " + lines.get(1));
    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 only the last executions (OFF/SHUTDOWN/[REBOOT = skipped])
    output = new ByteArrayOutputStream();
    ((StreamingOutput) resource.downloadNodeSchedulesReport("service:vm:test", "file1").getEntity()).write(output);
    lines = IOUtils.readLines(new ByteArrayInputStream(output.toByteArray()), StandardCharsets.UTF_8);
    Assertions.assertEquals(2, lines.size());
    Assertions.assertTrue(lines.get(1).matches("\\d+;\\d+;gfi-gstack;gStack;service:vm:test:test;0 0 0 1 1 \\? 2050;OFF;.+;.+;SHUTDOWN;vm1;_system;true;;;2050/01/01 00:00:00;2524604400000"), "Was : " + lines.get(1));
    // Next execution where schedule CRON has been updated
    vmScheduleRepository.findBy("subscription.id", subscription).setCron("INVALID");
    operation.set(VmOperation.SHUTDOWN);
    id = resource.execute(entity, VmOperation.ON);
    vmExecutionRepository.findOneExpected(id).setVm("vm1");
    output = new ByteArrayOutputStream();
    ((StreamingOutput) resource.downloadNodeSchedulesReport("service:vm:test", "file1").getEntity()).write(output);
    lines = IOUtils.readLines(new ByteArrayInputStream(output.toByteArray()), StandardCharsets.UTF_8);
    Assertions.assertEquals(2, lines.size());
    Assertions.assertTrue(lines.get(1).matches("\\d+;\\d+;gfi-gstack;gStack;service:vm:test:test;INVALID;OFF;.+;.+;SHUTDOWN;vm1;fdaugan;true;;;ERROR;ERROR"));
    // Add another schedule to the same subscription, with an execution
    final VmSchedule schedule = new VmSchedule();
    schedule.setOperation(VmOperation.ON);
    schedule.setCron("0 0 0 1 1 ? 2049");
    schedule.setSubscription(entity);
    vmScheduleRepository.saveAndFlush(schedule);
    final VmExecution execution = new VmExecution();
    execution.setDate(new Date());
    execution.setSubscription(entity);
    execution.setTrigger("_system");
    execution.setOperation(VmOperation.ON);
    execution.setSucceed(true);
    vmExecutionRepository.saveAndFlush(execution);
    output = new ByteArrayOutputStream();
    ((StreamingOutput) resource.downloadNodeSchedulesReport("service:vm:test:test", "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;0 0 0 1 1 \\? 2049;ON;.+;.+;ON;;_system;true;;;2049/01/01 00:00:00;2493068400000"), "Was : " + lines.get(2));
}
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) Date(java.util.Date) ByteArrayInputStream(java.io.ByteArrayInputStream) VmOperation(org.ligoj.app.plugin.vm.model.VmOperation) ArgumentMatcher(org.mockito.ArgumentMatcher) Subscription(org.ligoj.app.model.Subscription) VmSchedule(org.ligoj.app.plugin.vm.model.VmSchedule) Test(org.junit.jupiter.api.Test) AbstractServerTest(org.ligoj.app.AbstractServerTest)

Example 2 with VmOperation

use of org.ligoj.app.plugin.vm.model.VmOperation 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)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 StreamingOutput (javax.ws.rs.core.StreamingOutput)2 Test (org.junit.jupiter.api.Test)2 AbstractServerTest (org.ligoj.app.AbstractServerTest)2 Subscription (org.ligoj.app.model.Subscription)2 VmExecution (org.ligoj.app.plugin.vm.model.VmExecution)2 VmOperation (org.ligoj.app.plugin.vm.model.VmOperation)2 ArgumentMatcher (org.mockito.ArgumentMatcher)2 Date (java.util.Date)1 VmSchedule (org.ligoj.app.plugin.vm.model.VmSchedule)1