Skip to content

Commit

Permalink
GoogleContainerTools#3158 - [Jib core] Tar archives with same content…
Browse files Browse the repository at this point in the history
…s are not reproducible
  • Loading branch information
davidtron committed Mar 20, 2021
1 parent 5cfc9c8 commit 562e2b8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.LinkedHashMap;
import java.util.Map;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;

/** Builds a tarball archive. */
public class TarStreamBuilder {
public static final Instant DEFAULT_MODIFICATION_TIME = Instant.ofEpochSecond(1);

/**
* Maps from {@link TarArchiveEntry} to a {@link Blob}. The order of the entries is the order they
Expand Down Expand Up @@ -89,6 +91,7 @@ public void addByteEntry(byte[] contents, String name) {
public void addBlobEntry(Blob blob, long size, String name) {
TarArchiveEntry entry = new TarArchiveEntry(name);
entry.setSize(size);
entry.setModTime(DEFAULT_MODIFICATION_TIME.getEpochSecond());
archiveMap.put(entry, blob);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
Expand Down Expand Up @@ -131,6 +132,26 @@ public void testToBlob_multiByte() throws IOException {
Assert.assertNull(tarArchiveInputStream.getNextTarEntry());
}

@Test
public void testTarStreamAreReproducible() throws IOException, InterruptedException {
testTarStreamBuilder.addBlobEntry(
Blobs.from("jib"), "jib".getBytes(StandardCharsets.UTF_8).length, "jib");
ByteArrayOutputStream firstTarByteOutputStream = new ByteArrayOutputStream();
testTarStreamBuilder.writeAsTarArchiveTo(firstTarByteOutputStream);

Thread.sleep(2000);

TarStreamBuilder anotherTestTarStreamBuilder = new TarStreamBuilder();
anotherTestTarStreamBuilder.addBlobEntry(
Blobs.from("jib"), "jib".getBytes(StandardCharsets.UTF_8).length, "jib");
ByteArrayOutputStream secondTarByteOutputStream = new ByteArrayOutputStream();
anotherTestTarStreamBuilder.writeAsTarArchiveTo(secondTarByteOutputStream);

Assert.assertTrue(
Arrays.equals(
firstTarByteOutputStream.toByteArray(), secondTarByteOutputStream.toByteArray()));
}

/** Creates a TarStreamBuilder using TarArchiveEntries. */
private void setUpWithTarEntries() {
// Prepares a test TarStreamBuilder.
Expand Down

0 comments on commit 562e2b8

Please sign in to comment.