Search in sources :

Example 1 with FileEntry

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

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

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

the class ReproducibleLayerBuilder method build.

/**
 * Builds and returns the layer {@link Blob}.
 *
 * @return the new layer
 * @throws IOException if an I/O error occurs
 */
public Blob build() throws IOException {
    UniqueTarArchiveEntries uniqueTarArchiveEntries = new UniqueTarArchiveEntries();
    // Adds all the layer entries as tar entries.
    for (FileEntry layerEntry : layerEntries) {
        // Adds the entries to uniqueTarArchiveEntries, which makes sure all entries are unique and
        // adds parent directories for each extraction path.
        TarArchiveEntry entry = new TarArchiveEntry(layerEntry.getSourceFile(), layerEntry.getExtractionPath().toString());
        // Sets the entry's permissions by masking out the permission bits from the entry's mode (the
        // lowest 9 bits) then using a bitwise OR to set them to the layerEntry's permissions.
        entry.setMode((entry.getMode() & ~0777) | layerEntry.getPermissions().getPermissionBits());
        entry.setModTime(layerEntry.getModificationTime().toEpochMilli());
        setUserAndGroup(entry, layerEntry);
        uniqueTarArchiveEntries.add(entry);
    }
    // Gets the entries sorted by extraction path.
    List<TarArchiveEntry> sortedFilesystemEntries = uniqueTarArchiveEntries.getSortedEntries();
    Set<String> names = new HashSet<>();
    // Adds all the files to a tar stream.
    TarStreamBuilder tarStreamBuilder = new TarStreamBuilder();
    for (TarArchiveEntry entry : sortedFilesystemEntries) {
        Verify.verify(!names.contains(entry.getName()));
        names.add(entry.getName());
        tarStreamBuilder.addTarArchiveEntry(entry);
    }
    return Blobs.from(tarStreamBuilder::writeAsTarArchiveTo, false);
}
Also used : FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) HashSet(java.util.HashSet) TarStreamBuilder(com.google.cloud.tools.jib.tar.TarStreamBuilder)

Example 3 with FileEntry

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

the class LayerEntriesSelector method toSortedJsonTemplates.

/**
 * Converts a list of {@link FileEntry}s into a list of {@link LayerEntryTemplate}. The list is
 * sorted by source file first, then extraction path (see {@link LayerEntryTemplate#compareTo}).
 *
 * @param layerEntries the list of {@link FileEntry} to convert
 * @return list of {@link LayerEntryTemplate} after sorting
 * @throws IOException if checking the file creation time of a layer entry fails
 */
@VisibleForTesting
static List<LayerEntryTemplate> toSortedJsonTemplates(List<FileEntry> layerEntries) throws IOException {
    List<LayerEntryTemplate> jsonTemplates = new ArrayList<>();
    for (FileEntry entry : layerEntries) {
        jsonTemplates.add(new LayerEntryTemplate(entry));
    }
    Collections.sort(jsonTemplates);
    return jsonTemplates;
}
Also used : ArrayList(java.util.ArrayList) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 4 with FileEntry

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

the class BuildAndCacheApplicationLayerStep method call.

@Override
public PreparedLayer call() throws IOException, CacheCorruptedException {
    String description = String.format(DESCRIPTION, layerName);
    EventHandlers eventHandlers = buildContext.getEventHandlers();
    eventHandlers.dispatch(LogEvent.progress(description + "..."));
    try (ProgressEventDispatcher ignored = progressEventDispatcherFactory.create("building " + layerName + " layer", 1);
        TimerEventDispatcher ignored2 = new TimerEventDispatcher(eventHandlers, description)) {
        Cache cache = buildContext.getApplicationLayersCache();
        ImmutableList<FileEntry> layerEntries = ImmutableList.copyOf(layerConfiguration.getEntries());
        // Don't build the layer if it exists already.
        Optional<CachedLayer> optionalCachedLayer = cache.retrieve(layerEntries);
        if (optionalCachedLayer.isPresent()) {
            return new PreparedLayer.Builder(optionalCachedLayer.get()).setName(layerName).build();
        }
        Blob layerBlob = new ReproducibleLayerBuilder(layerEntries).build();
        CachedLayer cachedLayer = cache.writeUncompressedLayer(layerBlob, layerEntries);
        eventHandlers.dispatch(LogEvent.debug(description + " built " + cachedLayer.getDigest()));
        return new PreparedLayer.Builder(cachedLayer).setName(layerName).build();
    }
}
Also used : Blob(com.google.cloud.tools.jib.blob.Blob) ProgressEventDispatcher(com.google.cloud.tools.jib.builder.ProgressEventDispatcher) ReproducibleLayerBuilder(com.google.cloud.tools.jib.image.ReproducibleLayerBuilder) TimerEventDispatcher(com.google.cloud.tools.jib.builder.TimerEventDispatcher) CachedLayer(com.google.cloud.tools.jib.cache.CachedLayer) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) EventHandlers(com.google.cloud.tools.jib.event.EventHandlers) ReproducibleLayerBuilder(com.google.cloud.tools.jib.image.ReproducibleLayerBuilder) Cache(com.google.cloud.tools.jib.cache.Cache)

Example 5 with FileEntry

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

the class ReproducibleLayerBuilderTest method testBuild_parentDirBehavior.

@Test
public void testBuild_parentDirBehavior() throws IOException {
    Path testRoot = temporaryFolder.getRoot().toPath();
    // the path doesn't really matter on source files, but these are structured
    Path parent = Files.createDirectories(testRoot.resolve("dirA"));
    Path fileA = Files.createFile(parent.resolve("fileA"));
    Path ignoredParent = Files.createDirectories(testRoot.resolve("dirB-ignored"));
    Path fileB = Files.createFile(ignoredParent.resolve("fileB"));
    Path fileC = Files.createFile(Files.createDirectories(testRoot.resolve("dirC-absent")).resolve("fileC"));
    Blob layer = new ReproducibleLayerBuilder(ImmutableList.of(new FileEntry(parent, AbsoluteUnixPath.get("/root/dirA"), FilePermissions.fromOctalString("111"), Instant.ofEpochSecond(10)), new FileEntry(fileA, AbsoluteUnixPath.get("/root/dirA/fileA"), FilePermissions.fromOctalString("222"), Instant.ofEpochSecond(20)), new FileEntry(fileB, AbsoluteUnixPath.get("/root/dirB-ignored/fileB"), FilePermissions.fromOctalString("333"), Instant.ofEpochSecond(30)), new FileEntry(ignoredParent, AbsoluteUnixPath.get("/root/dirB-ignored"), FilePermissions.fromOctalString("444"), Instant.ofEpochSecond(40)), new FileEntry(fileC, AbsoluteUnixPath.get("/root/dirC-absent/file3"), FilePermissions.fromOctalString("555"), Instant.ofEpochSecond(50)))).build();
    Path tarFile = temporaryFolder.newFile().toPath();
    try (OutputStream out = new BufferedOutputStream(Files.newOutputStream(tarFile))) {
        layer.writeTo(out);
    }
    try (TarArchiveInputStream in = new TarArchiveInputStream(Files.newInputStream(tarFile))) {
        // root (default folder permissions)
        TarArchiveEntry root = in.getNextTarEntry();
        assertThat(root.getMode()).isEqualTo(040755);
        assertThat(root.getModTime().toInstant()).isEqualTo(Instant.ofEpochSecond(1));
        assertThat(root.getLongUserId()).isEqualTo(0);
        assertThat(root.getLongGroupId()).isEqualTo(0);
        assertThat(root.getUserName()).isEmpty();
        assertThat(root.getGroupName()).isEmpty();
        // parentAAA (custom permissions, custom timestamp)
        TarArchiveEntry rootParentA = in.getNextTarEntry();
        assertThat(rootParentA.getMode()).isEqualTo(040111);
        assertThat(rootParentA.getModTime().toInstant()).isEqualTo(Instant.ofEpochSecond(10));
        assertThat(rootParentA.getLongUserId()).isEqualTo(0);
        assertThat(rootParentA.getLongGroupId()).isEqualTo(0);
        assertThat(rootParentA.getUserName()).isEmpty();
        assertThat(rootParentA.getGroupName()).isEmpty();
        // skip over fileA
        in.getNextTarEntry();
        // parentBBB (default permissions - ignored custom permissions, since fileB added first)
        TarArchiveEntry rootParentB = in.getNextTarEntry();
        // TODO (#1650): we want 040444 here.
        assertThat(rootParentB.getMode()).isEqualTo(040755);
        // TODO (#1650): we want Instant.ofEpochSecond(40) here.
        assertThat(rootParentB.getModTime().toInstant()).isEqualTo(Instant.ofEpochSecond(1));
        assertThat(rootParentB.getLongUserId()).isEqualTo(0);
        assertThat(rootParentB.getLongGroupId()).isEqualTo(0);
        assertThat(rootParentB.getUserName()).isEmpty();
        assertThat(rootParentB.getGroupName()).isEmpty();
        // skip over fileB
        in.getNextTarEntry();
        // parentCCC (default permissions - no entry provided)
        TarArchiveEntry rootParentC = in.getNextTarEntry();
        assertThat(rootParentC.getMode()).isEqualTo(040755);
        assertThat(rootParentC.getModTime().toInstant()).isEqualTo(Instant.ofEpochSecond(1));
        assertThat(rootParentC.getLongUserId()).isEqualTo(0);
        assertThat(rootParentC.getLongGroupId()).isEqualTo(0);
        assertThat(rootParentC.getUserName()).isEmpty();
        assertThat(rootParentC.getGroupName()).isEmpty();
    // we don't care about fileC
    }
}
Also used : Path(java.nio.file.Path) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) Blob(com.google.cloud.tools.jib.blob.Blob) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileEntry(com.google.cloud.tools.jib.api.buildplan.FileEntry) BufferedOutputStream(java.io.BufferedOutputStream) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) 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