Skip to content

Commit b12a4aa

Browse files
committed
GoogleContainerTools#3158 - [Jib core] Tar archives with same contents are not reproducible
1 parent 9c1141b commit b12a4aa

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

jib-core/src/main/java/com/google/cloud/tools/jib/tar/TarStreamBuilder.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public void addTarArchiveEntry(TarArchiveEntry entry) {
7777
public void addByteEntry(byte[] contents, String name, Instant modificationTime) {
7878
TarArchiveEntry entry = new TarArchiveEntry(name);
7979
entry.setSize(contents.length);
80-
entry.setModTime(modificationTime.getEpochSecond());
80+
entry.setModTime(modificationTime.toEpochMilli());
8181
archiveMap.put(entry, Blobs.from(outputStream -> outputStream.write(contents)));
8282
}
8383

@@ -93,7 +93,7 @@ public void addByteEntry(byte[] contents, String name, Instant modificationTime)
9393
public void addBlobEntry(Blob blob, long size, String name, Instant modificationTime) {
9494
TarArchiveEntry entry = new TarArchiveEntry(name);
9595
entry.setSize(size);
96-
entry.setModTime(modificationTime.getEpochSecond());
96+
entry.setModTime(modificationTime.toEpochMilli());
9797
archiveMap.put(entry, blob);
9898
}
9999
}

jib-core/src/test/java/com/google/cloud/tools/jib/tar/TarStreamBuilderTest.java

+11-5
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,12 @@
3838
import org.junit.Before;
3939
import org.junit.Test;
4040

41+
import static java.time.temporal.ChronoUnit.SECONDS;
42+
4143
/** Tests for {@link TarStreamBuilder}. */
4244
public class TarStreamBuilderTest {
4345

46+
4447
private Path fileA;
4548
private Path fileB;
4649
private Path directoryA;
@@ -97,12 +100,15 @@ public void testToBlob_stringsAndTarArchiveEntriesWithCompression() throws IOExc
97100

98101
@Test
99102
public void testToBlob_multiByte() throws IOException {
103+
Instant MODIFICATION_TIME = Instant.ofEpochMilli(1618041179516l);
104+
Instant TIME_FROM_TAR_ARCHIVE_ENTRY = MODIFICATION_TIME.truncatedTo(SECONDS);
105+
100106
testTarStreamBuilder.addByteEntry(
101-
"日本語".getBytes(StandardCharsets.UTF_8), "test", Instant.EPOCH);
107+
"日本語".getBytes(StandardCharsets.UTF_8), "test", MODIFICATION_TIME);
102108
testTarStreamBuilder.addByteEntry(
103-
"asdf".getBytes(StandardCharsets.UTF_8), "crepecake", Instant.EPOCH);
109+
"asdf".getBytes(StandardCharsets.UTF_8), "crepecake", MODIFICATION_TIME);
104110
testTarStreamBuilder.addBlobEntry(
105-
Blobs.from("jib"), "jib".getBytes(StandardCharsets.UTF_8).length, "jib", Instant.EPOCH);
111+
Blobs.from("jib"), "jib".getBytes(StandardCharsets.UTF_8).length, "jib", MODIFICATION_TIME);
106112

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

130136
headerFile = tarArchiveInputStream.getNextTarEntry();
131137
Assert.assertEquals("jib", headerFile.getName());
132138
Assert.assertEquals(
133139
"jib", new String(ByteStreams.toByteArray(tarArchiveInputStream), StandardCharsets.UTF_8));
134-
Assert.assertEquals(Instant.EPOCH, headerFile.getModTime().toInstant());
140+
Assert.assertEquals(TIME_FROM_TAR_ARCHIVE_ENTRY, headerFile.getModTime().toInstant());
135141

136142
Assert.assertNull(tarArchiveInputStream.getNextTarEntry());
137143
}

0 commit comments

Comments
 (0)