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());
}
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);
}
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;
}
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();
}
}
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
}
}
Aggregations