use of org.ligoj.app.resource.plugin.PluginsClassLoader in project plugin-prov by ligoj.
the class TerraformResourceTest method install.
@Test
public void install() throws Exception {
final TerraformResource resource = newResource(Mockito.mock(Terraforming.class), false, "error=0", "Terraform v2.0.1");
final PluginsClassLoader classLoader = Mockito.mock(PluginsClassLoader.class);
Mockito.when(classLoader.getHomeDirectory()).thenReturn(MOCK_PATH.toPath());
// Replace the Terraform utility
resource.terraformUtils = new TerraformUtils() {
public void install(final String version) {
Assertions.assertEquals("2.0.0", version);
}
@Override
protected PluginsClassLoader getClassLoader() {
return classLoader;
}
public String getLastestVersion() {
return "2.0.0";
}
};
final TerraformInformation version = resource.install("2.0.0");
Assertions.assertEquals("2.0.0", version.getLastVersion());
}
use of org.ligoj.app.resource.plugin.PluginsClassLoader in project plugin-prov by ligoj.
the class TerraformResourceTest method newResource.
private TerraformResource newResource(final Terraforming providerResource, final BiFunction<Subscription, String[], File> toFile, final boolean dryRun, final String... customArgs) {
final TerraformResource resource = new TerraformResource() {
@Override
protected File toFile(final Subscription subscription, final String file) {
return toFile.apply(subscription, new String[] { file });
}
/**
* Prepare the Terraform environment to apply the new environment. Note there is no concurrency check.
*/
@Override
protected File applyTerraform(final Subscription entity, final Terraforming terra, final QuoteVo configuration) throws IOException, InterruptedException {
if (dryRun) {
// Ignore this call
return null;
}
return super.applyTerraform(entity, terra, configuration);
}
};
super.applicationContext.getAutowireCapableBeanFactory().autowireBean(resource);
// Replace the plugin locator
final ServicePluginLocator locator = Mockito.mock(ServicePluginLocator.class);
resource.locator = locator;
// Replace the runner
resource.runner = new TerraformRunnerResource();
super.applicationContext.getAutowireCapableBeanFactory().autowireBean(resource.runner);
Mockito.when(locator.getResource("service:prov:test:account", Terraforming.class)).thenReturn(providerResource);
final PluginsClassLoader classLoader = Mockito.mock(PluginsClassLoader.class);
Mockito.when(classLoader.getHomeDirectory()).thenReturn(MOCK_PATH.toPath());
// Replace the CLI runner
resource.terraformUtils = new TerraformUtils() {
@Override
public ProcessBuilder newBuilder(String... args) {
return new ProcessBuilder(ArrayUtils.addAll(new String[] { "java", "-cp", MOCK_PATH.getParent(), "org.ligoj.app.plugin.prov.terraform.Main" }, customArgs.length > 0 ? customArgs : args));
}
@Override
protected boolean isInstalled() {
return true;
}
@Override
protected PluginsClassLoader getClassLoader() {
return classLoader;
}
public String getLastestVersion() {
return "2.0.0";
}
};
return resource;
}
use of org.ligoj.app.resource.plugin.PluginsClassLoader in project plugin-prov by ligoj.
the class TerraformUtilsTest method newBuilderNotInstalled.
@Test
public void newBuilderNotInstalled() {
final PluginsClassLoader classLoader = Mockito.mock(PluginsClassLoader.class);
Mockito.when(classLoader.getHomeDirectory()).thenReturn(EMPTY_PATH.toPath());
final TerraformUtils utils = new TerraformUtils() {
@Override
protected PluginsClassLoader getClassLoader() {
return classLoader;
}
};
applicationContext.getAutowireCapableBeanFactory().autowireBean(utils);
Assertions.assertEquals("terraform-not-found", Assertions.assertThrows(BusinessException.class, () -> utils.newBuilder("arg1", "arg2")).getMessage());
}
use of org.ligoj.app.resource.plugin.PluginsClassLoader in project plugin-prov by ligoj.
the class TerraformUtilsTest method prepareForInstall.
private TerraformUtils prepareForInstall(final String file) throws IOException {
// Prepare the download
final File pathDownload = new File("target/test-classes/terraform-download").getAbsoluteFile();
pathDownload.mkdirs();
final PluginsClassLoader classLoader = Mockito.mock(PluginsClassLoader.class);
Mockito.when(classLoader.getHomeDirectory()).thenReturn(pathDownload.toPath());
configuration.saveOrUpdate("service:prov:terraform:repository", "http://localhost:" + MOCK_PORT);
// Index
InputStream inputStream = new ClassPathResource("mock-server/prov/terraform/terraform-index.html").getInputStream();
httpServer.stubFor(get(urlEqualTo("/")).willReturn(aResponse().withStatus(HttpStatus.SC_OK).withBody(IOUtils.toByteArray(inputStream))));
IOUtils.closeQuietly(inputStream);
// ZIP file
inputStream = new ClassPathResource(file).getInputStream();
httpServer.stubFor(get(urlEqualTo("/0.11.5/terraform_0.11.5_linux_amd64.zip")).willReturn(aResponse().withStatus(HttpStatus.SC_OK).withBody(IOUtils.toByteArray(inputStream))));
IOUtils.closeQuietly(inputStream);
httpServer.start();
final TerraformUtils utils = new TerraformUtils() {
@Override
protected PluginsClassLoader getClassLoader() {
return classLoader;
}
@Override
protected String getCurrentOs() {
return "linux";
}
};
applicationContext.getAutowireCapableBeanFactory().autowireBean(utils);
// Remove state files
FileUtils.deleteQuietly(new File(pathDownload, "prov/terraform"));
FileUtils.deleteQuietly(new File(pathDownload, "prov/terraform.exe"));
return utils;
}
use of org.ligoj.app.resource.plugin.PluginsClassLoader in project plugin-prov by ligoj.
the class TerraformUtilsTest method checkNewBuilder.
private void checkNewBuilder(final String os, final String[] args, final String command) {
final PluginsClassLoader classLoader = Mockito.mock(PluginsClassLoader.class);
Mockito.when(classLoader.getHomeDirectory()).thenReturn(MOCK_PATH.toPath());
final TerraformUtils utils = new TerraformUtils() {
@Override
protected PluginsClassLoader getClassLoader() {
return classLoader;
}
@Override
protected String getCurrentOs() {
return os;
}
};
applicationContext.getAutowireCapableBeanFactory().autowireBean(utils);
final ProcessBuilder newBuilder = utils.newBuilder("arg1", "arg2");
Assertions.assertEquals(args[0], newBuilder.command().get(0));
Assertions.assertEquals(args[1], newBuilder.command().get(1));
Assertions.assertTrue(newBuilder.command().get(2).endsWith(command));
}
Aggregations