use of org.ligoj.app.plugin.vm.model.VmExecution 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));
}
use of org.ligoj.app.plugin.vm.model.VmExecution in project plugin-vm by ligoj.
the class VmResourceTest method execute.
@Test
public void execute() throws Exception {
final VmResource resource = new VmResource();
applicationContext.getAutowireCapableBeanFactory().autowireBean(resource);
resource.locator = mockServicePluginLocator;
Mockito.doNothing().when(mockVmTool).execute(ArgumentMatchers.argThat(new ArgumentMatcher<VmExecution>() {
@Override
public boolean matches(final VmExecution argument) {
argument.setVm("my-vm");
argument.setStatusText("status");
return true;
}
}));
final int id = resource.execute(subscription, VmOperation.OFF);
final VmExecution execution = vmExecutionRepository.findOneExpected(id);
Assertions.assertEquals("my-vm", execution.getVm());
Assertions.assertEquals("status", execution.getStatusText());
Assertions.assertEquals(VmOperation.OFF, execution.getOperation());
}
use of org.ligoj.app.plugin.vm.model.VmExecution in project plugin-vm by ligoj.
the class VmResource method execute.
/**
* Execute a {@link VmOperation} to the associated VM. This a synchronous call, but the effective execution is
* delayed.
*
* @param subscription
* The {@link Subscription} associated to the VM.
* @param operation
* the operation to execute.
* @return The execution identifier. Only useful for the correlation. May be <code>null</code> when skipped.
*/
@Transactional
public Integer execute(final Subscription subscription, final VmOperation operation) {
final String node = subscription.getNode().getId();
final String trigger = securityHelper.getLogin();
log.info("Operation {} on subscription {}, node {} is requested by {}", operation, subscription.getId(), node, trigger);
final VmExecution execution = new VmExecution();
execution.setOperation(operation);
execution.setSubscription(subscription);
execution.setTrigger(trigger);
execution.setDate(new Date());
try {
// Execute the operation if plug-in still available
locator.getResourceExpected(node, VmServicePlugin.class).execute(execution);
log.info("Operation {} (->{}) on subscription {}, node {} : succeed", operation, execution.getOperation(), subscription.getId(), node);
execution.setSucceed(true);
} catch (final Exception e) {
// Something goes wrong for this VM, this log would be considered for reporting
execution.setError(e.getMessage());
log.error("Operation {} on subscription {}, node {} : failed", operation, subscription.getId(), e);
} finally {
// Save as needed
saveAndFlush(execution, operation);
}
return execution.getId();
}
use of org.ligoj.app.plugin.vm.model.VmExecution in project plugin-vm by ligoj.
the class VmResourceTest method executeDefault.
/**
* Coverage only
*/
@Test
public void executeDefault() throws Exception {
final VmExecution execution = new VmExecution();
final Subscription subscription = new Subscription();
subscription.setId(1);
execution.setSubscription(subscription);
new VmServicePlugin() {
@Override
public String getKey() {
return null;
}
@Override
public Vm getVmDetails(Map<String, String> parameters) throws Exception {
return null;
}
}.execute(execution);
}
use of org.ligoj.app.plugin.vm.model.VmExecution in project plugin-vm by ligoj.
the class VmResourceTest method executeUnavailablePlugin.
@Test
public void executeUnavailablePlugin() throws Exception {
final VmResource resource = new VmResource();
final Subscription entity = subscriptionRepository.findOneExpected(subscription);
final Node node = new Node();
node.setId("_deleted_plugin_");
node.setName("any");
nodeRepository.saveAndFlush(node);
entity.setNode(node);
subscriptionRepository.saveAndFlush(entity);
applicationContext.getAutowireCapableBeanFactory().autowireBean(resource);
resource.locator = mockServicePluginLocator;
final Integer id = resource.execute(entity, VmOperation.OFF);
// Execution is logged but failed
final VmExecution execution = vmExecutionRepository.findOneExpected(id);
Assertions.assertNull(execution.getVm());
Assertions.assertNull(execution.getVm());
Assertions.assertFalse(execution.isSucceed());
Assertions.assertEquals("fdaugan", execution.getTrigger());
Assertions.assertEquals(VmOperation.OFF, execution.getOperation());
Assertions.assertEquals("Plugin issue for _deleted_plugin_:Not found", execution.getError());
}
Aggregations