ValidateInstanceMemory implements InstallServiceValidator { @Override public void validate(final InstallServiceValidationContext validationContext) throws RestErrorException { logger.info("Validating isntance memory"); final Service service = validationContext.getService(); final Cloud cloud = validationContext.getCloud(); if (service == null) { return; } if (IsolationUtils.isDedicated(service)) { return; } String serviceTemplate = null; if (service.getCompute() != null) { serviceTemplate = service.getCompute().getTemplate(); } if (serviceTemplate == null) { serviceTemplate = cloud.getCloudCompute().getTemplates().entrySet().iterator().next().getKey(); } final int machineTemplateMemory = cloud.getCloudCompute().getTemplates().get(serviceTemplate).getMachineMemoryMB(); final int reservedMachineMemory = cloud.getProvider().getReservedMemoryCapacityPerMachineInMB(); final long instanceMemoryMB = IsolationUtils.getInstanceMemoryMB(service); if (instanceMemoryMB > machineTemplateMemory - reservedMachineMemory) { throw new RestErrorException(CloudifyMessageKeys.INSUFFICIENT_MEMORY.getName(), service.getName(), instanceMemoryMB, machineTemplateMemory, reservedMachineMemory); } }  @Override void validate(final InstallServiceValidationContext validationContext);  }
@Test public void testInsufficientMemoryInInstallService() { initInsufficientMemory(false); InstallServiceValidationContext context = new InstallServiceValidationContext(); context.setCloud(byonCloud); context.setService(service); ValidatorsTestsUtils.validate(validateInstanceMemory, context, ERR_MSG); }
@Test public void testInsufficientReservedMemoryInInstallService() { initInsufficientMemory(true); InstallServiceValidationContext context = new InstallServiceValidationContext(); context.setCloud(byonCloud); context.setService(service); ValidatorsTestsUtils.validate(validateInstanceMemory, context, ERR_MSG); }
@Test public void testInsufficientMemoryInInstallApplication() { initInsufficientMemory(false); InstallApplicationValidationContext context = new InstallApplicationValidationContext(); context.setCloud(byonCloud); Application application = new Application(); application.setService(service); context.setApplication(application); ValidatorsTestsUtils.validate(validateApplicationServices, context, ERR_MSG); }
@Test public void testInsufficientReservedMemoryInInstallApplication() { initInsufficientMemory(true); InstallApplicationValidationContext context = new InstallApplicationValidationContext(); context.setCloud(byonCloud); Application application = new Application(); application.setService(service); context.setApplication(application); ValidatorsTestsUtils.validate(validateApplicationServices, context, ERR_MSG); }