Search in sources :

Example 1 with PluginsClassLoader

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());
}
Also used : PluginsClassLoader(org.ligoj.app.resource.plugin.PluginsClassLoader) AbstractAppTest(org.ligoj.app.AbstractAppTest) Test(org.junit.jupiter.api.Test)

Example 2 with PluginsClassLoader

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;
}
Also used : QuoteVo(org.ligoj.app.plugin.prov.QuoteVo) Subscription(org.ligoj.app.model.Subscription) PluginsClassLoader(org.ligoj.app.resource.plugin.PluginsClassLoader) ServicePluginLocator(org.ligoj.app.resource.ServicePluginLocator)

Example 3 with PluginsClassLoader

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());
}
Also used : PluginsClassLoader(org.ligoj.app.resource.plugin.PluginsClassLoader) Test(org.junit.jupiter.api.Test) AbstractServerTest(org.ligoj.app.AbstractServerTest)

Example 4 with PluginsClassLoader

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;
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) File(java.io.File) PluginsClassLoader(org.ligoj.app.resource.plugin.PluginsClassLoader) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 5 with PluginsClassLoader

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));
}
Also used : PluginsClassLoader(org.ligoj.app.resource.plugin.PluginsClassLoader)

Aggregations

PluginsClassLoader (org.ligoj.app.resource.plugin.PluginsClassLoader)6 Test (org.junit.jupiter.api.Test)3 File (java.io.File)2 AbstractAppTest (org.ligoj.app.AbstractAppTest)2 Subscription (org.ligoj.app.model.Subscription)2 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 URLClassLoader (java.net.URLClassLoader)1 ThreadClassLoaderScope (org.eclipse.jetty.util.thread.ThreadClassLoaderScope)1 AbstractServerTest (org.ligoj.app.AbstractServerTest)1 QuoteVo (org.ligoj.app.plugin.prov.QuoteVo)1 ServicePluginLocator (org.ligoj.app.resource.ServicePluginLocator)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1