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 Apr 10, 2021
1 parent 9c1141b commit b12a4aa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void addTarArchiveEntry(TarArchiveEntry entry) {
public void addByteEntry(byte[] contents, String name, Instant modificationTime) {
TarArchiveEntry entry = new TarArchiveEntry(name);
entry.setSize(contents.length);
entry.setModTime(modificationTime.getEpochSecond());
entry.setModTime(modificationTime.toEpochMilli());
archiveMap.put(entry, Blobs.from(outputStream -> outputStream.write(contents)));
}

Expand All @@ -93,7 +93,7 @@ public void addByteEntry(byte[] contents, String name, Instant modificationTime)
public void addBlobEntry(Blob blob, long size, String name, Instant modificationTime) {
TarArchiveEntry entry = new TarArchiveEntry(name);
entry.setSize(size);
entry.setModTime(modificationTime.getEpochSecond());
entry.setModTime(modificationTime.toEpochMilli());
archiveMap.put(entry, blob);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@
import org.junit.Before;
import org.junit.Test;

import static java.time.temporal.ChronoUnit.SECONDS;

/** Tests for {@link TarStreamBuilder}. */
public class TarStreamBuilderTest {


private Path fileA;
private Path fileB;
private Path directoryA;
Expand Down Expand Up @@ -97,12 +100,15 @@ public void testToBlob_stringsAndTarArchiveEntriesWithCompression() throws IOExc

@Test
public void testToBlob_multiByte() throws IOException {
Instant MODIFICATION_TIME = Instant.ofEpochMilli(1618041179516l);
Instant TIME_FROM_TAR_ARCHIVE_ENTRY = MODIFICATION_TIME.truncatedTo(SECONDS);

testTarStreamBuilder.addByteEntry(
"日本語".getBytes(StandardCharsets.UTF_8), "test", Instant.EPOCH);
"日本語".getBytes(StandardCharsets.UTF_8), "test", MODIFICATION_TIME);
testTarStreamBuilder.addByteEntry(
"asdf".getBytes(StandardCharsets.UTF_8), "crepecake", Instant.EPOCH);
"asdf".getBytes(StandardCharsets.UTF_8), "crepecake", MODIFICATION_TIME);
testTarStreamBuilder.addBlobEntry(
Blobs.from("jib"), "jib".getBytes(StandardCharsets.UTF_8).length, "jib", Instant.EPOCH);
Blobs.from("jib"), "jib".getBytes(StandardCharsets.UTF_8).length, "jib", MODIFICATION_TIME);

// Writes the BLOB and captures the output.
ByteArrayOutputStream tarByteOutputStream = new ByteArrayOutputStream();
Expand All @@ -125,13 +131,13 @@ public void testToBlob_multiByte() throws IOException {
Assert.assertEquals("crepecake", headerFile.getName());
Assert.assertEquals(
"asdf", new String(ByteStreams.toByteArray(tarArchiveInputStream), StandardCharsets.UTF_8));
Assert.assertEquals(Instant.EPOCH, headerFile.getModTime().toInstant());
Assert.assertEquals(TIME_FROM_TAR_ARCHIVE_ENTRY, headerFile.getModTime().toInstant());

headerFile = tarArchiveInputStream.getNextTarEntry();
Assert.assertEquals("jib", headerFile.getName());
Assert.assertEquals(
"jib", new String(ByteStreams.toByteArray(tarArchiveInputStream), StandardCharsets.UTF_8));
Assert.assertEquals(Instant.EPOCH, headerFile.getModTime().toInstant());
Assert.assertEquals(TIME_FROM_TAR_ARCHIVE_ENTRY, headerFile.getModTime().toInstant());

Assert.assertNull(tarArchiveInputStream.getNextTarEntry());
}
Expand Down

0 comments on commit b12a4aa

Please sign in to comment.