Search in sources :

Example 41 with FileEntry

use of com.google.cloud.tools.jib.api.buildplan.FileEntry in project jib by google.

the class LayerEntriesSelectorTest method testGenerateSelector_ownersModified.

@Test
public void testGenerateSelector_ownersModified() throws IOException {
    Path layerFile = temporaryFolder.newFolder("testFolder").toPath().resolve("file");
    Files.write(layerFile, "hello".getBytes(StandardCharsets.UTF_8));
    FileEntry layerEntry111 = new FileEntry(layerFile, AbsoluteUnixPath.get("/extraction/path"), FilePermissions.fromOctalString("111"), FileEntriesLayer.DEFAULT_MODIFICATION_TIME, "0:0");
    FileEntry layerEntry222 = new FileEntry(layerFile, AbsoluteUnixPath.get("/extraction/path"), FilePermissions.fromOctalString("222"), FileEntriesLayer.DEFAULT_MODIFICATION_TIME, "foouser");
    // Verify that changing ownership generates a different selector
    Assert.assertNotEquals(LayerEntriesSelector.generateSelector(ImmutableList.of(layerEntry111)), LayerEntriesSelector.generateSelector(ImmutableList.of(layerEntry222)));
}
Also used : AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Path(java.nio.file.Path) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) Test(org.junit.Test)

Example 42 with FileEntry

use of com.google.cloud.tools.jib.api.buildplan.FileEntry in project jib by google.

the class BuildAndCacheApplicationLayerStepTest method testRun.

@Test
public void testRun() throws LayerPropertyNotFoundException, IOException, CacheCorruptedException {
    ImmutableList<FileEntriesLayer> fakeLayerConfigurations = ImmutableList.of(fakeDependenciesLayerConfiguration, fakeSnapshotDependenciesLayerConfiguration, fakeResourcesLayerConfiguration, fakeClassesLayerConfiguration, fakeExtraFilesLayerConfiguration);
    Mockito.when(mockBuildContext.getLayerConfigurations()).thenReturn(fakeLayerConfigurations);
    // Populates the cache.
    List<Layer> applicationLayers = buildFakeLayersToCache();
    Assert.assertEquals(5, applicationLayers.size());
    ImmutableList<FileEntry> dependenciesLayerEntries = ImmutableList.copyOf(fakeLayerConfigurations.get(0).getEntries());
    ImmutableList<FileEntry> snapshotDependenciesLayerEntries = ImmutableList.copyOf(fakeLayerConfigurations.get(1).getEntries());
    ImmutableList<FileEntry> resourcesLayerEntries = ImmutableList.copyOf(fakeLayerConfigurations.get(2).getEntries());
    ImmutableList<FileEntry> classesLayerEntries = ImmutableList.copyOf(fakeLayerConfigurations.get(3).getEntries());
    ImmutableList<FileEntry> extraFilesLayerEntries = ImmutableList.copyOf(fakeLayerConfigurations.get(4).getEntries());
    CachedLayer dependenciesCachedLayer = cache.retrieve(dependenciesLayerEntries).orElseThrow(AssertionError::new);
    CachedLayer snapshotDependenciesCachedLayer = cache.retrieve(snapshotDependenciesLayerEntries).orElseThrow(AssertionError::new);
    CachedLayer resourcesCachedLayer = cache.retrieve(resourcesLayerEntries).orElseThrow(AssertionError::new);
    CachedLayer classesCachedLayer = cache.retrieve(classesLayerEntries).orElseThrow(AssertionError::new);
    CachedLayer extraFilesCachedLayer = cache.retrieve(extraFilesLayerEntries).orElseThrow(AssertionError::new);
    // Verifies that the cached layers are up-to-date.
    Assert.assertEquals(applicationLayers.get(0).getBlobDescriptor().getDigest(), dependenciesCachedLayer.getDigest());
    Assert.assertEquals(applicationLayers.get(1).getBlobDescriptor().getDigest(), snapshotDependenciesCachedLayer.getDigest());
    Assert.assertEquals(applicationLayers.get(2).getBlobDescriptor().getDigest(), resourcesCachedLayer.getDigest());
    Assert.assertEquals(applicationLayers.get(3).getBlobDescriptor().getDigest(), classesCachedLayer.getDigest());
    Assert.assertEquals(applicationLayers.get(4).getBlobDescriptor().getDigest(), extraFilesCachedLayer.getDigest());
    // Verifies that the cache reader gets the same layers as the newest application layers.
    assertBlobsEqual(applicationLayers.get(0).getBlob(), dependenciesCachedLayer.getBlob());
    assertBlobsEqual(applicationLayers.get(1).getBlob(), snapshotDependenciesCachedLayer.getBlob());
    assertBlobsEqual(applicationLayers.get(2).getBlob(), resourcesCachedLayer.getBlob());
    assertBlobsEqual(applicationLayers.get(3).getBlob(), classesCachedLayer.getBlob());
    assertBlobsEqual(applicationLayers.get(4).getBlob(), extraFilesCachedLayer.getBlob());
}
Also used : FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) CachedLayer(com.google.cloud.tools.jib.cache.CachedLayer) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) FileEntriesLayer(com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer) Layer(com.google.cloud.tools.jib.image.Layer) CachedLayer(com.google.cloud.tools.jib.cache.CachedLayer) Test(org.junit.Test)

Example 43 with FileEntry

use of com.google.cloud.tools.jib.api.buildplan.FileEntry in project jib by google.

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 44 with FileEntry

use of com.google.cloud.tools.jib.api.buildplan.FileEntry in project jib by GoogleContainerTools.

the class LayerEntriesSelectorTest method setUp.

@Before
public void setUp() throws IOException {
    Path folder = temporaryFolder.newFolder().toPath();
    Path file1 = Files.createDirectory(folder.resolve("files"));
    Path file2 = Files.createFile(folder.resolve("files").resolve("two"));
    Path file3 = Files.createFile(folder.resolve("gile"));
    FileEntry testLayerEntry1 = defaultLayerEntry(file1, AbsoluteUnixPath.get("/extraction/path"));
    FileEntry testLayerEntry2 = defaultLayerEntry(file2, AbsoluteUnixPath.get("/extraction/path"));
    FileEntry testLayerEntry3 = defaultLayerEntry(file3, AbsoluteUnixPath.get("/extraction/path"));
    FileEntry testLayerEntry4 = new FileEntry(file3, AbsoluteUnixPath.get("/extraction/path"), FilePermissions.fromOctalString("755"), FileEntriesLayer.DEFAULT_MODIFICATION_TIME);
    FileEntry testLayerEntry5 = defaultLayerEntry(file3, AbsoluteUnixPath.get("/extraction/patha"));
    FileEntry testLayerEntry6 = new FileEntry(file3, AbsoluteUnixPath.get("/extraction/patha"), FilePermissions.fromOctalString("755"), FileEntriesLayer.DEFAULT_MODIFICATION_TIME);
    outOfOrderLayerEntries = ImmutableList.of(testLayerEntry4, testLayerEntry2, testLayerEntry6, testLayerEntry3, testLayerEntry1, testLayerEntry5);
    inOrderLayerEntries = ImmutableList.of(testLayerEntry1, testLayerEntry2, testLayerEntry3, testLayerEntry4, testLayerEntry5, testLayerEntry6);
}
Also used : AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Path(java.nio.file.Path) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) Before(org.junit.Before)

Example 45 with FileEntry

use of com.google.cloud.tools.jib.api.buildplan.FileEntry in project jib by GoogleContainerTools.

the class LayerEntriesSelectorTest method testGenerateSelector_permissionsModified.

@Test
public void testGenerateSelector_permissionsModified() throws IOException {
    Path layerFile = temporaryFolder.newFolder("testFolder").toPath().resolve("file");
    Files.write(layerFile, "hello".getBytes(StandardCharsets.UTF_8));
    FileEntry layerEntry111 = new FileEntry(layerFile, AbsoluteUnixPath.get("/extraction/path"), FilePermissions.fromOctalString("111"), FileEntriesLayer.DEFAULT_MODIFICATION_TIME);
    FileEntry layerEntry222 = new FileEntry(layerFile, AbsoluteUnixPath.get("/extraction/path"), FilePermissions.fromOctalString("222"), FileEntriesLayer.DEFAULT_MODIFICATION_TIME);
    // Verify that changing permissions generates a different selector
    Assert.assertNotEquals(LayerEntriesSelector.generateSelector(ImmutableList.of(layerEntry111)), LayerEntriesSelector.generateSelector(ImmutableList.of(layerEntry222)));
}
Also used : AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Path(java.nio.file.Path) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) Test(org.junit.Test)

Aggregations

FileEntry (com.google.cloud.tools.jib.api.buildplan.FileEntry)64 Test (org.junit.Test)45 Path (java.nio.file.Path)39 AbsoluteUnixPath (com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath)34 FileEntriesLayer (com.google.cloud.tools.jib.api.buildplan.FileEntriesLayer)24 ContainerBuildPlan (com.google.cloud.tools.jib.api.buildplan.ContainerBuildPlan)16 ArrayList (java.util.ArrayList)13 FilePermissions (com.google.cloud.tools.jib.api.buildplan.FilePermissions)10 Blob (com.google.cloud.tools.jib.blob.Blob)10 VisibleForTesting (com.google.common.annotations.VisibleForTesting)9 List (java.util.List)9 BufferedOutputStream (java.io.BufferedOutputStream)8 OutputStream (java.io.OutputStream)8 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)8 PathMatcher (java.nio.file.PathMatcher)7 Instant (java.time.Instant)7 Optional (java.util.Optional)7 Collectors (java.util.stream.Collectors)7 Platform (com.google.cloud.tools.jib.api.buildplan.Platform)6 CachedLayer (com.google.cloud.tools.jib.cache.CachedLayer)6