Search in sources :

Example 31 with ImageConfiguration

use of org.eclipse.jkube.kit.config.image.ImageConfiguration in project jkube by eclipse.

the class JavaExecGeneratorMainClassDeterminationTest method testMainClassDeterminationFromDetectionOnNonFatJar.

/**
 * The main class is determined via main class detection in a non-fat-jar deployment
 */
@Test
public void testMainClassDeterminationFromDetectionOnNonFatJar(@Injectable File baseDir) {
    processorConfig.getConfig().put("java-exec", Collections.singletonMap("name", "TheNonFatJarImageName"));
    new Expectations() {

        {
            project.getBaseDirectory();
            result = baseDir;
            fatJarDetector.scan();
            result = null;
            mainClassDetector.getMainClass();
            result = "the.detected.MainClass";
        }
    };
    final GeneratorContext generatorContext = GeneratorContext.builder().project(project).config(processorConfig).strategy(JKubeBuildStrategy.docker).logger(log).build();
    JavaExecGenerator generator = new JavaExecGenerator(generatorContext);
    List<ImageConfiguration> customized = generator.customize(new ArrayList<>(), false);
    assertEquals("1 images returned", 1, customized.size());
    ImageConfiguration imageConfig = customized.get(0);
    assertEquals("Image name", "TheNonFatJarImageName", imageConfig.getName());
    assertEquals("Main Class set as environment variable", "the.detected.MainClass", imageConfig.getBuildConfiguration().getEnv().get(JavaExecGenerator.JAVA_MAIN_CLASS_ENV_VAR));
}
Also used : Expectations(mockit.Expectations) ImageConfiguration(org.eclipse.jkube.kit.config.image.ImageConfiguration) GeneratorContext(org.eclipse.jkube.generator.api.GeneratorContext) Test(org.junit.Test)

Example 32 with ImageConfiguration

use of org.eclipse.jkube.kit.config.image.ImageConfiguration in project jkube by eclipse.

the class KarafGeneratorTest method customizeWithKarafMavenPluginAndCustomConfigShouldAddImageConfiguration.

@Test
public void customizeWithKarafMavenPluginAndCustomConfigShouldAddImageConfiguration(@Mocked Plugin plugin) {
    // Given
    final List<ImageConfiguration> originalImageConfigurations = new ArrayList<>();
    Properties props = new Properties();
    props.put("jkube.generator.karaf.baseDir", "/other-dir");
    props.put("jkube.generator.karaf.webPort", "8080");
    // @formatter:off
    new Expectations() {

        {
            generatorContext.getProject().getBuildDirectory();
            result = temporaryFolder.getRoot();
            generatorContext.getProject().getVersion();
            result = "1.33.7-SNAPSHOT";
            generatorContext.getProject().getProperties();
            result = props;
        }
    };
    // @formatter:on
    // When
    final List<ImageConfiguration> result = new KarafGenerator(generatorContext).customize(originalImageConfigurations, false);
    // Then
    assertThat(result, hasSize(1));
    final ImageConfiguration imageConfiguration = result.iterator().next();
    assertThat(imageConfiguration.getBuildConfiguration().getPorts(), contains("8080", "8778"));
    assertThat(imageConfiguration.getBuildConfiguration().getEnv(), hasEntry("DEPLOYMENTS_DIR", "/other-dir"));
}
Also used : Expectations(mockit.Expectations) ImageConfiguration(org.eclipse.jkube.kit.config.image.ImageConfiguration) ArrayList(java.util.ArrayList) Properties(java.util.Properties) Test(org.junit.Test)

Example 33 with ImageConfiguration

use of org.eclipse.jkube.kit.config.image.ImageConfiguration in project jkube by eclipse.

the class KarafGeneratorTest method customizeWithKarafMavenPluginShouldAddImageConfiguration.

@Test
public void customizeWithKarafMavenPluginShouldAddImageConfiguration(@Mocked Plugin plugin) {
    // Given
    final List<ImageConfiguration> originalImageConfigurations = new ArrayList<>();
    // @formatter:off
    new Expectations() {

        {
            plugin.getGroupId();
            result = "org.apache.karaf.tooling";
            minTimes = 0;
            plugin.getArtifactId();
            result = "karaf-maven-plugin";
            minTimes = 0;
            generatorContext.getProject().getPlugins();
            result = Collections.singletonList(plugin);
            minTimes = 0;
            generatorContext.getProject().getBuildDirectory();
            result = temporaryFolder.getRoot();
            generatorContext.getProject().getVersion();
            result = "1.33.7-SNAPSHOT";
            generatorContext.getConfig();
            result = new ProcessorConfig();
        }
    };
    // @formatter:on
    // When
    final List<ImageConfiguration> result = new KarafGenerator(generatorContext).customize(originalImageConfigurations, false);
    // Then
    assertThat(originalImageConfigurations, sameInstance(result));
    assertThat(result, hasSize(1));
    final ImageConfiguration imageConfiguration = result.iterator().next();
    assertThat(imageConfiguration.getName(), equalTo("%g/%a:%l"));
    assertThat(imageConfiguration.getAlias(), equalTo("karaf"));
    final BuildConfiguration bc = imageConfiguration.getBuildConfiguration();
    assertThat(bc.getTags(), contains("latest"));
    assertThat(bc.getPorts(), contains("8181", "8778"));
    assertThat(bc.getEnv(), hasEntry("DEPLOYMENTS_DIR", "/deployments"));
    assertThat(bc.getEnv(), hasEntry("KARAF_HOME", "/deployments/karaf"));
    final AssemblyConfiguration ac = bc.getAssembly();
    assertThat(ac.getName(), equalTo("deployments"));
    assertThat(ac.isExcludeFinalOutputArtifact(), equalTo(false));
    assertThat(ac.getLayers(), hasSize(1));
    assertThat(ac.getLayers().iterator().next().getFileSets(), contains(allOf(hasProperty("directory", equalTo(new File(temporaryFolder.getRoot(), "assembly"))), hasProperty("outputDirectory", equalTo(new File("karaf"))), hasProperty("directoryMode", equalTo("0775"))), allOf(hasProperty("directory", equalTo(temporaryFolder.getRoot().toPath().resolve("assembly").resolve("bin").toFile())), hasProperty("outputDirectory", equalTo(new File("karaf", "bin"))), hasProperty("fileMode", equalTo("0777")), hasProperty("directoryMode", equalTo("0775")))));
}
Also used : Expectations(mockit.Expectations) BuildConfiguration(org.eclipse.jkube.kit.config.image.build.BuildConfiguration) AssemblyConfiguration(org.eclipse.jkube.kit.common.AssemblyConfiguration) ImageConfiguration(org.eclipse.jkube.kit.config.image.ImageConfiguration) ArrayList(java.util.ArrayList) File(java.io.File) ProcessorConfig(org.eclipse.jkube.kit.config.resource.ProcessorConfig) Test(org.junit.Test)

Example 34 with ImageConfiguration

use of org.eclipse.jkube.kit.config.image.ImageConfiguration in project jkube by eclipse.

the class WebAppGenerator method customize.

@Override
public List<ImageConfiguration> customize(List<ImageConfiguration> configs, boolean prePackagePhase) {
    final AppServerHandler handler = getAppServerHandler(getContext());
    if (getContext().getRuntimeMode() == RuntimeMode.OPENSHIFT && getContext().getStrategy() == JKubeBuildStrategy.s2i && !prePackagePhase && !handler.supportsS2iBuild()) {
        throw new IllegalArgumentException("S2I not yet supported for the webapp-generator. Use " + "-Djkube.build.strategy=docker for OpenShift mode. Please refer to the reference manual at " + "https://www.eclipse.org/jkube/docs for details about build modes.");
    }
    log.info("Using %s as base image for webapp", handler.getFrom());
    final ImageConfiguration.ImageConfigurationBuilder imageBuilder = ImageConfiguration.builder();
    final BuildConfiguration.BuildConfigurationBuilder buildBuilder = BuildConfiguration.builder();
    buildBuilder.from(getFrom(handler)).ports(handler.exposedPorts()).env(getEnv(handler));
    String dockerRunCommand = getDockerRunCommand(handler);
    if (dockerRunCommand != null) {
        buildBuilder.cmd(Arguments.builder().shell(dockerRunCommand).build());
    }
    handler.runCmds().forEach(buildBuilder::runCmd);
    addSchemaLabels(buildBuilder, log);
    if (!prePackagePhase) {
        buildBuilder.assembly(createAssembly(handler));
    }
    addLatestTagIfSnapshot(buildBuilder);
    imageBuilder.name(getImageName()).alias(getAlias()).build(buildBuilder.build());
    configs.add(imageBuilder.build());
    return configs;
}
Also used : BuildConfiguration(org.eclipse.jkube.kit.config.image.build.BuildConfiguration) ImageConfiguration(org.eclipse.jkube.kit.config.image.ImageConfiguration) CustomAppServerHandler(org.eclipse.jkube.generator.webapp.handler.CustomAppServerHandler)

Example 35 with ImageConfiguration

use of org.eclipse.jkube.kit.config.image.ImageConfiguration in project jkube by eclipse.

the class WebAppGeneratorTest method customizeWithOverriddenPropertiesShouldAddImageConfiguration.

@Test
public void customizeWithOverriddenPropertiesShouldAddImageConfiguration() throws IOException {
    // Given
    final List<ImageConfiguration> originalImageConfigurations = new ArrayList<>();
    final File buildDirectory = temporaryFolder.newFolder("build");
    final File artifactFile = new File(buildDirectory, "artifact.war");
    assertTrue(artifactFile.createNewFile());
    final Properties projectProperties = new Properties();
    projectProperties.put("jkube.generator.webapp.targetDir", "/other-dir");
    projectProperties.put("jkube.generator.webapp.user", "root");
    projectProperties.put("jkube.generator.webapp.cmd", "sleep 3600");
    projectProperties.put("jkube.generator.webapp.path", "/some-context");
    projectProperties.put("jkube.generator.webapp.ports", "8082,80");
    projectProperties.put("jkube.generator.webapp.supportsS2iBuild", "true");
    projectProperties.put("jkube.generator.from", "image-to-trigger-custom-app-server-handler");
    when(generatorContext.getProject().getBuildDirectory()).thenReturn(buildDirectory);
    when(generatorContext.getProject().getBuildFinalName()).thenReturn("artifact");
    when(generatorContext.getProject().getPackaging()).thenReturn("war");
    when(generatorContext.getProject().getVersion()).thenReturn("1.33.7-SNAPSHOT");
    when(generatorContext.getRuntimeMode()).thenReturn(RuntimeMode.OPENSHIFT);
    when(generatorContext.getStrategy()).thenReturn(JKubeBuildStrategy.s2i);
    when(generatorContext.getProject().getProperties()).thenReturn(projectProperties);
    // When
    final List<ImageConfiguration> result = new WebAppGenerator(generatorContext).customize(originalImageConfigurations, false);
    // Then
    assertThat(result).isSameAs(originalImageConfigurations).hasSize(1).first().hasFieldOrPropertyWithValue("name", "%a:%l").hasFieldOrPropertyWithValue("alias", "webapp").extracting(ImageConfiguration::getBuildConfiguration).hasFieldOrPropertyWithValue("tags", Collections.singletonList("latest")).hasFieldOrPropertyWithValue("ports", Arrays.asList("8082", "80")).hasFieldOrPropertyWithValue("env", Collections.singletonMap("DEPLOY_DIR", "/other-dir")).hasFieldOrPropertyWithValue("cmd.shell", "sleep 3600").extracting(BuildConfiguration::getAssembly).hasFieldOrPropertyWithValue("excludeFinalOutputArtifact", true).hasFieldOrPropertyWithValue("user", "root").extracting("inline.files").asList().extracting("destName").containsExactly("some-context.war");
}
Also used : BuildConfiguration(org.eclipse.jkube.kit.config.image.build.BuildConfiguration) ImageConfiguration(org.eclipse.jkube.kit.config.image.ImageConfiguration) ArrayList(java.util.ArrayList) Properties(java.util.Properties) File(java.io.File) Test(org.junit.Test)

Aggregations

ImageConfiguration (org.eclipse.jkube.kit.config.image.ImageConfiguration)109 Test (org.junit.Test)75 BuildConfiguration (org.eclipse.jkube.kit.config.image.build.BuildConfiguration)29 RunImageConfiguration (org.eclipse.jkube.kit.config.image.RunImageConfiguration)28 File (java.io.File)26 ArrayList (java.util.ArrayList)19 Expectations (mockit.Expectations)15 GroupArtifactVersion (org.eclipse.jkube.kit.config.resource.GroupArtifactVersion)11 Before (org.junit.Before)11 IOException (java.io.IOException)9 Properties (java.util.Properties)8 KitLogger (org.eclipse.jkube.kit.common.KitLogger)8 KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)7 JKubeServiceException (org.eclipse.jkube.kit.config.service.JKubeServiceException)7 WatchImageConfiguration (org.eclipse.jkube.kit.config.image.WatchImageConfiguration)6 ResourceConfig (org.eclipse.jkube.kit.config.resource.ResourceConfig)6 List (java.util.List)5 JKubeConfiguration (org.eclipse.jkube.kit.common.JKubeConfiguration)5 VolumeConfig (org.eclipse.jkube.kit.config.resource.VolumeConfig)5 Container (io.fabric8.kubernetes.api.model.Container)4