Search in sources :

Example 41 with BuildContext

use of com.google.cloud.tools.jib.configuration.BuildContext in project jib by GoogleContainerTools.

the class JavaContainerBuilderTest method testToJibContainerBuilder_mainClassNull.

@Test
public void testToJibContainerBuilder_mainClassNull() throws IOException, InvalidImageReferenceException, CacheDirectoryCreationException, URISyntaxException {
    BuildContext buildContext = JavaContainerBuilder.from("scratch").addClasses(getResource("core/application/classes/")).toContainerBuilder().toBuildContext(Containerizer.to(RegistryImage.named("hello")));
    Assert.assertNull(buildContext.getContainerConfiguration().getEntrypoint());
    try {
        JavaContainerBuilder.from("scratch").addJvmFlags("-flag1", "-flag2").toContainerBuilder();
        Assert.fail();
    } catch (IllegalStateException ex) {
        Assert.assertEquals("Failed to construct entrypoint on JavaContainerBuilder; jvmFlags were set, but " + "mainClass is null. Specify the main class using " + "JavaContainerBuilder#setMainClass(String), or consider using MainClassFinder to " + "infer the main class.", ex.getMessage());
    }
}
Also used : BuildContext(com.google.cloud.tools.jib.configuration.BuildContext) Test(org.junit.Test)

Example 42 with BuildContext

use of com.google.cloud.tools.jib.configuration.BuildContext in project jib by GoogleContainerTools.

the class JibContainerBuilderTest method setApplyContainerBuildPlan.

@Test
public void setApplyContainerBuildPlan() throws InvalidImageReferenceException, CacheDirectoryCreationException {
    FileEntriesLayer layer = FileEntriesLayer.builder().addEntry(Paths.get("/src/file/foo"), AbsoluteUnixPath.get("/path/in/container")).build();
    ContainerBuildPlan buildPlan = ContainerBuildPlan.builder().setBaseImage("some/base").setPlatforms(ImmutableSet.of(new Platform("testArchitecture", "testOS"))).setFormat(ImageFormat.OCI).setCreationTime(Instant.ofEpochMilli(30)).setEnvironment(ImmutableMap.of("env", "var")).setVolumes(ImmutableSet.of(AbsoluteUnixPath.get("/mnt/foo"), AbsoluteUnixPath.get("/bar"))).setLabels(ImmutableMap.of("com.example.label", "cool")).setExposedPorts(ImmutableSet.of(Port.tcp(443))).setLayers(Arrays.asList(layer)).setUser(":").setWorkingDirectory(AbsoluteUnixPath.get("/workspace")).setEntrypoint(Arrays.asList("foo", "entrypoint")).setCmd(Arrays.asList("bar", "cmd")).build();
    ImageConfiguration imageConfiguration = ImageConfiguration.builder(ImageReference.parse("initial/base")).build();
    JibContainerBuilder containerBuilder = new JibContainerBuilder(imageConfiguration, spyBuildContextBuilder).applyContainerBuildPlan(buildPlan);
    BuildContext buildContext = containerBuilder.toBuildContext(Containerizer.to(RegistryImage.named("target/image")));
    Assert.assertEquals("some/base", buildContext.getBaseImageConfiguration().getImage().toString());
    Assert.assertEquals(OciManifestTemplate.class, buildContext.getTargetFormat());
    Assert.assertEquals(1, buildContext.getLayerConfigurations().size());
    Assert.assertEquals(1, buildContext.getLayerConfigurations().get(0).getEntries().size());
    Assert.assertEquals(Arrays.asList(new FileEntry(Paths.get("/src/file/foo"), AbsoluteUnixPath.get("/path/in/container"), FilePermissions.fromOctalString("644"), Instant.ofEpochSecond(1))), buildContext.getLayerConfigurations().get(0).getEntries());
    ContainerConfiguration containerConfiguration = buildContext.getContainerConfiguration();
    Assert.assertEquals(Instant.ofEpochMilli(30), containerConfiguration.getCreationTime());
    Assert.assertEquals(ImmutableMap.of("env", "var"), containerConfiguration.getEnvironmentMap());
    Assert.assertEquals(ImmutableMap.of("com.example.label", "cool"), containerConfiguration.getLabels());
    Assert.assertEquals(ImmutableSet.of(AbsoluteUnixPath.get("/mnt/foo"), AbsoluteUnixPath.get("/bar")), containerConfiguration.getVolumes());
    Assert.assertEquals(ImmutableSet.of(Port.tcp(443)), containerConfiguration.getExposedPorts());
    Assert.assertEquals(":", containerConfiguration.getUser());
    Assert.assertEquals(AbsoluteUnixPath.get("/workspace"), containerConfiguration.getWorkingDirectory());
    Assert.assertEquals(Arrays.asList("foo", "entrypoint"), containerConfiguration.getEntrypoint());
    Assert.assertEquals(Arrays.asList("bar", "cmd"), containerConfiguration.getProgramArguments());
    ContainerBuildPlan convertedPlan = containerBuilder.toContainerBuildPlan();
    Assert.assertEquals(ImmutableSet.of(new Platform("testArchitecture", "testOS")), convertedPlan.getPlatforms());
}
Also used : Platform(com.google.cloud.tools.jib.api.buildplan.Platform) BuildContext(com.google.cloud.tools.jib.configuration.BuildContext) ImageConfiguration(com.google.cloud.tools.jib.configuration.ImageConfiguration) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) ContainerConfiguration(com.google.cloud.tools.jib.configuration.ContainerConfiguration) ContainerBuildPlan(com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan) Test(org.junit.Test)

Example 43 with BuildContext

use of com.google.cloud.tools.jib.configuration.BuildContext in project jib by GoogleContainerTools.

the class JibContainerBuilderTest method testToBuildContext.

@Test
public void testToBuildContext() throws InvalidImageReferenceException, CredentialRetrievalException, CacheDirectoryCreationException {
    ExecutorService executorService = MoreExecutors.newDirectExecutorService();
    RegistryImage targetImage = RegistryImage.named(ImageReference.of("gcr.io", "my-project/my-app", null)).addCredential("username", "password");
    Containerizer containerizer = Containerizer.to(targetImage).setBaseImageLayersCache(Paths.get("base/image/layers")).setApplicationLayersCache(Paths.get("application/layers")).setExecutorService(executorService).addEventHandler(mockJibEventConsumer).setAlwaysCacheBaseImage(false);
    ImageConfiguration baseImageConfiguration = ImageConfiguration.builder(ImageReference.parse("base/image")).setCredentialRetrievers(Collections.singletonList(mockCredentialRetriever)).build();
    JibContainerBuilder jibContainerBuilder = new JibContainerBuilder(baseImageConfiguration, spyBuildContextBuilder).setFileEntriesLayers(Arrays.asList(mockLayerConfiguration1, mockLayerConfiguration2));
    BuildContext buildContext = jibContainerBuilder.toBuildContext(containerizer);
    Assert.assertEquals(spyBuildContextBuilder.build().getContainerConfiguration(), buildContext.getContainerConfiguration());
    Assert.assertEquals("base/image", buildContext.getBaseImageConfiguration().getImage().toString());
    Assert.assertEquals(Arrays.asList(mockCredentialRetriever), buildContext.getBaseImageConfiguration().getCredentialRetrievers());
    Assert.assertEquals("gcr.io/my-project/my-app", buildContext.getTargetImageConfiguration().getImage().toString());
    Assert.assertEquals(1, buildContext.getTargetImageConfiguration().getCredentialRetrievers().size());
    Assert.assertEquals(Credential.from("username", "password"), buildContext.getTargetImageConfiguration().getCredentialRetrievers().get(0).retrieve().orElseThrow(AssertionError::new));
    Assert.assertEquals(ImmutableSet.of("latest"), buildContext.getAllTargetImageTags());
    Mockito.verify(spyBuildContextBuilder).setBaseImageLayersCacheDirectory(Paths.get("base/image/layers"));
    Mockito.verify(spyBuildContextBuilder).setApplicationLayersCacheDirectory(Paths.get("application/layers"));
    Assert.assertEquals(Arrays.asList(mockLayerConfiguration1, mockLayerConfiguration2), buildContext.getLayerConfigurations());
    Assert.assertSame(executorService, buildContext.getExecutorService());
    buildContext.getEventHandlers().dispatch(mockJibEvent);
    Mockito.verify(mockJibEventConsumer).accept(mockJibEvent);
    Assert.assertEquals("jib-core", buildContext.getToolName());
    Assert.assertSame(V22ManifestTemplate.class, buildContext.getTargetFormat());
    Assert.assertEquals("jib-core", buildContext.getToolName());
    // Changes jibContainerBuilder.
    buildContext = jibContainerBuilder.setFormat(ImageFormat.OCI).toBuildContext(containerizer.withAdditionalTag("tag1").withAdditionalTag("tag2").setToolName("toolName"));
    Assert.assertSame(OciManifestTemplate.class, buildContext.getTargetFormat());
    Assert.assertEquals(ImmutableSet.of("latest", "tag1", "tag2"), buildContext.getAllTargetImageTags());
    Assert.assertEquals("toolName", buildContext.getToolName());
    Assert.assertFalse(buildContext.getAlwaysCacheBaseImage());
}
Also used : BuildContext(com.google.cloud.tools.jib.configuration.BuildContext) ImageConfiguration(com.google.cloud.tools.jib.configuration.ImageConfiguration) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Example 44 with BuildContext

use of com.google.cloud.tools.jib.configuration.BuildContext in project jib by GoogleContainerTools.

the class JibContainerBuilderTest method testToBuildContext_containerConfigurationAdd.

@Test
public void testToBuildContext_containerConfigurationAdd() throws InvalidImageReferenceException, CacheDirectoryCreationException {
    ImageConfiguration imageConfiguration = ImageConfiguration.builder(ImageReference.parse("base/image")).build();
    JibContainerBuilder jibContainerBuilder = new JibContainerBuilder(imageConfiguration, spyBuildContextBuilder).addPlatform("testArchitecture", "testOS").setEntrypoint("entry", "point").setEnvironment(ImmutableMap.of("name", "value")).addEnvironmentVariable("environment", "variable").setExposedPorts(Port.tcp(1234), Port.udp(5678)).addExposedPort(Port.tcp(1337)).setLabels(ImmutableMap.of("key", "value")).addLabel("added", "label").setProgramArguments("program", "arguments");
    BuildContext buildContext = jibContainerBuilder.toBuildContext(Containerizer.to(RegistryImage.named("target/image")));
    ContainerConfiguration containerConfiguration = buildContext.getContainerConfiguration();
    Assert.assertEquals(ImmutableSet.of(new Platform("testArchitecture", "testOS"), new Platform("amd64", "linux")), containerConfiguration.getPlatforms());
    Assert.assertEquals(Arrays.asList("entry", "point"), containerConfiguration.getEntrypoint());
    Assert.assertEquals(ImmutableMap.of("name", "value", "environment", "variable"), containerConfiguration.getEnvironmentMap());
    Assert.assertEquals(ImmutableSet.of(Port.tcp(1234), Port.udp(5678), Port.tcp(1337)), containerConfiguration.getExposedPorts());
    Assert.assertEquals(ImmutableMap.of("key", "value", "added", "label"), containerConfiguration.getLabels());
    Assert.assertEquals(Arrays.asList("program", "arguments"), containerConfiguration.getProgramArguments());
    Assert.assertEquals(Instant.EPOCH, containerConfiguration.getCreationTime());
}
Also used : BuildContext(com.google.cloud.tools.jib.configuration.BuildContext) Platform(com.google.cloud.tools.jib.api.buildplan.Platform) ImageConfiguration(com.google.cloud.tools.jib.configuration.ImageConfiguration) ContainerConfiguration(com.google.cloud.tools.jib.configuration.ContainerConfiguration) Test(org.junit.Test)

Example 45 with BuildContext

use of com.google.cloud.tools.jib.configuration.BuildContext in project jib by GoogleContainerTools.

the class JavaContainerBuilderHelperTest method testFromExplodedWar.

@Test
public void testFromExplodedWar() throws URISyntaxException, IOException, InvalidImageReferenceException, CacheDirectoryCreationException {
    // Copy test files to a temporary directory that we can safely operate on
    Path resourceExplodedWar = Paths.get(Resources.getResource("plugins-common/exploded-war").toURI());
    FileOperations.copy(ImmutableList.of(resourceExplodedWar), temporaryFolder.getRoot().toPath());
    Path temporaryExplodedWar = temporaryFolder.getRoot().toPath().resolve("exploded-war");
    Files.createDirectories(temporaryExplodedWar.resolve("WEB-INF/classes/empty_dir"));
    Files.createFile(temporaryExplodedWar.resolve("WEB-INF/lib/project-dependency-1.0.0.jar"));
    Set<String> projectArtifacts = ImmutableSet.of("project-dependency-1.0.0.jar");
    JavaContainerBuilder javaContainerBuilder = JavaContainerBuilder.from(RegistryImage.named("base")).setAppRoot(AbsoluteUnixPath.get("/my/app"));
    JibContainerBuilder jibContainerBuilder = JavaContainerBuilderHelper.fromExplodedWar(javaContainerBuilder, temporaryExplodedWar, projectArtifacts);
    BuildContext buildContext = JibContainerBuilderTestHelper.toBuildContext(jibContainerBuilder, Containerizer.to(RegistryImage.named("target")).setExecutorService(MoreExecutors.newDirectExecutorService()));
    FileEntriesLayer resourcesLayerConfigurations = getLayerConfigurationByName(buildContext, LayerType.RESOURCES.getName());
    FileEntriesLayer classesLayerConfigurations = getLayerConfigurationByName(buildContext, LayerType.CLASSES.getName());
    FileEntriesLayer dependenciesLayerConfigurations = getLayerConfigurationByName(buildContext, LayerType.DEPENDENCIES.getName());
    FileEntriesLayer snapshotsLayerConfigurations = getLayerConfigurationByName(buildContext, LayerType.SNAPSHOT_DEPENDENCIES.getName());
    FileEntriesLayer projectDependenciesLayerConfigurations = getLayerConfigurationByName(buildContext, LayerType.PROJECT_DEPENDENCIES.getName());
    assertThat(projectDependenciesLayerConfigurations.getEntries()).comparingElementsUsing(SOURCE_FILE_OF).containsExactly(temporaryExplodedWar.resolve("WEB-INF/lib/project-dependency-1.0.0.jar"));
    assertThat(dependenciesLayerConfigurations.getEntries()).comparingElementsUsing(SOURCE_FILE_OF).containsExactly(temporaryExplodedWar.resolve("WEB-INF/lib/dependency-1.0.0.jar"));
    assertThat(snapshotsLayerConfigurations.getEntries()).comparingElementsUsing(SOURCE_FILE_OF).containsExactly(temporaryExplodedWar.resolve("WEB-INF/lib/dependencyX-1.0.0-SNAPSHOT.jar"));
    assertThat(resourcesLayerConfigurations.getEntries()).comparingElementsUsing(SOURCE_FILE_OF).containsExactly(temporaryExplodedWar.resolve("META-INF"), temporaryExplodedWar.resolve("META-INF/context.xml"), temporaryExplodedWar.resolve("Test.jsp"), temporaryExplodedWar.resolve("WEB-INF"), temporaryExplodedWar.resolve("WEB-INF/classes"), temporaryExplodedWar.resolve("WEB-INF/classes/empty_dir"), temporaryExplodedWar.resolve("WEB-INF/classes/package"), temporaryExplodedWar.resolve("WEB-INF/classes/package/test.properties"), temporaryExplodedWar.resolve("WEB-INF/lib"), temporaryExplodedWar.resolve("WEB-INF/web.xml"));
    assertThat(classesLayerConfigurations.getEntries()).comparingElementsUsing(SOURCE_FILE_OF).containsExactly(temporaryExplodedWar.resolve("WEB-INF/classes/HelloWorld.class"), temporaryExplodedWar.resolve("WEB-INF/classes/empty_dir"), temporaryExplodedWar.resolve("WEB-INF/classes/package"), temporaryExplodedWar.resolve("WEB-INF/classes/package/Other.class"));
    assertThat(dependenciesLayerConfigurations.getEntries()).comparingElementsUsing(EXTRACTION_PATH_OF).containsExactly("/my/app/WEB-INF/lib/dependency-1.0.0.jar");
    assertThat(snapshotsLayerConfigurations.getEntries()).comparingElementsUsing(EXTRACTION_PATH_OF).containsExactly("/my/app/WEB-INF/lib/dependencyX-1.0.0-SNAPSHOT.jar");
    assertThat(resourcesLayerConfigurations.getEntries()).comparingElementsUsing(EXTRACTION_PATH_OF).containsExactly("/my/app/META-INF", "/my/app/META-INF/context.xml", "/my/app/Test.jsp", "/my/app/WEB-INF", "/my/app/WEB-INF/classes", "/my/app/WEB-INF/classes/empty_dir", "/my/app/WEB-INF/classes/package", "/my/app/WEB-INF/classes/package/test.properties", "/my/app/WEB-INF/lib", "/my/app/WEB-INF/web.xml");
    assertThat(classesLayerConfigurations.getEntries()).comparingElementsUsing(EXTRACTION_PATH_OF).containsExactly("/my/app/WEB-INF/classes/HelloWorld.class", "/my/app/WEB-INF/classes/empty_dir", "/my/app/WEB-INF/classes/package", "/my/app/WEB-INF/classes/package/Other.class");
}
Also used : Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) BuildContext(com.google.cloud.tools.jib.configuration.BuildContext) JavaContainerBuilder(com.google.cloud.tools.jib.api.JavaContainerBuilder) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) JibContainerBuilder(com.google.cloud.tools.jib.api.JibContainerBuilder) Test(org.junit.Test)

Aggregations

BuildContext (com.google.cloud.tools.jib.configuration.BuildContext)45 Test (org.junit.Test)31 ImageConfiguration (com.google.cloud.tools.jib.configuration.ImageConfiguration)23 EventHandlers (com.google.cloud.tools.jib.event.EventHandlers)12 IOException (java.io.IOException)12 Set (java.util.Set)12 BuildResult (com.google.cloud.tools.jib.builder.steps.BuildResult)9 JibContainerBuilder (com.google.cloud.tools.jib.api.JibContainerBuilder)8 AbsoluteUnixPath (com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath)8 Platform (com.google.cloud.tools.jib.api.buildplan.Platform)8 TimerEventDispatcher (com.google.cloud.tools.jib.builder.TimerEventDispatcher)8 Preconditions (com.google.common.base.Preconditions)8 List (java.util.List)8 Optional (java.util.Optional)8 ExecutionException (java.util.concurrent.ExecutionException)8 Nullable (javax.annotation.Nullable)8 LogEvent (com.google.cloud.tools.jib.api.LogEvent)6 RegistryException (com.google.cloud.tools.jib.api.RegistryException)6 StepsRunner (com.google.cloud.tools.jib.builder.steps.StepsRunner)6 DockerClient (com.google.cloud.tools.jib.docker.DockerClient)6