1616
1717package com .google .cloud .tools .jib .image ;
1818
19+ import com .google .cloud .tools .jib .blob .Blob ;
20+ import com .google .cloud .tools .jib .blob .Blobs ;
1921import com .google .common .io .CharStreams ;
2022import com .google .common .io .Resources ;
2123import java .io .BufferedOutputStream ;
2729import java .nio .file .Files ;
2830import java .nio .file .Path ;
2931import java .nio .file .Paths ;
32+ import java .nio .file .StandardOpenOption ;
33+ import java .nio .file .attribute .FileTime ;
3034import java .util .ArrayList ;
3135import java .util .Arrays ;
3236import org .apache .commons .compress .archivers .tar .TarArchiveEntry ;
3337import org .apache .commons .compress .archivers .tar .TarArchiveInputStream ;
38+ import org .hamcrest .CoreMatchers ;
3439import org .junit .Assert ;
3540import org .junit .Rule ;
3641import org .junit .Test ;
@@ -51,7 +56,8 @@ public void testBuild() throws URISyntaxException, IOException {
5156
5257 String extractionPathBase = "extract/here" ;
5358 LayerBuilder layerBuilder =
54- new LayerBuilder (new ArrayList <>(Arrays .asList (layerDirectory , blobA )), extractionPathBase );
59+ new LayerBuilder (
60+ new ArrayList <>(Arrays .asList (layerDirectory , blobA )), extractionPathBase , false );
5561
5662 // Writes the layer tar to a temporary file.
5763 UnwrittenLayer unwrittenLayer = layerBuilder .build ();
@@ -111,4 +117,55 @@ public void testBuild() throws URISyntaxException, IOException {
111117 Assert .assertEquals (expectedFileString , extractedFileString );
112118 }
113119 }
120+
121+ @ Test
122+ public void testToBlob_reproducibility () throws IOException {
123+ Path testRoot = temporaryFolder .getRoot ().toPath ();
124+ Path root1 = Files .createDirectories (testRoot .resolve ("files1" ));
125+ Path root2 = Files .createDirectories (testRoot .resolve ("files2" ));
126+
127+ String extractionPath = "/somewhere" ;
128+
129+ // TODO: Currently this test only covers variation in order and modified time, even though
130+ // TODO: the code is designed to clean up userid/groupid, this test does not check that yet.
131+ String contentA = "abcabc" ;
132+ Path fileA1 = createFile (root1 , "fileA" , contentA , 10000 );
133+ Path fileA2 = createFile (root2 , "fileA" , contentA , 20000 );
134+ String contentB = "yumyum" ;
135+ Path fileB1 = createFile (root1 , "fileB" , contentB , 10000 );
136+ Path fileB2 = createFile (root2 , "fileB" , contentB , 20000 );
137+
138+ // check if modified times are off
139+ Assert .assertNotEquals (Files .getLastModifiedTime (fileA1 ), Files .getLastModifiedTime (fileA2 ));
140+ Assert .assertNotEquals (Files .getLastModifiedTime (fileB1 ), Files .getLastModifiedTime (fileB2 ));
141+
142+ // create layers of exact same content but ordered differently and with different timestamps
143+ Blob layer =
144+ new LayerBuilder (Arrays .asList (fileA1 , fileB1 ), extractionPath , true ).build ().getBlob ();
145+ Blob reproduced =
146+ new LayerBuilder (Arrays .asList (fileB2 , fileA2 ), extractionPath , true ).build ().getBlob ();
147+
148+ // this is a control layer that is the same as 'layerReproduced' without reproducibility on
149+ Blob notReproduced =
150+ new LayerBuilder (Arrays .asList (fileB2 , fileA2 ), extractionPath , false ).build ().getBlob ();
151+
152+ byte [] layerContent = Blobs .writeToByteArray (layer );
153+ byte [] reproducedLayerContent = Blobs .writeToByteArray (reproduced );
154+ byte [] notReproducedLayerContent = Blobs .writeToByteArray (notReproduced );
155+
156+ Assert .assertThat (layerContent , CoreMatchers .is (reproducedLayerContent ));
157+ Assert .assertThat (layerContent , CoreMatchers .not (notReproducedLayerContent ));
158+ }
159+
160+ private Path createFile (Path root , String filename , String content , long lastModifiedTime )
161+ throws IOException {
162+
163+ Path newFile =
164+ Files .write (
165+ root .resolve (filename ),
166+ content .getBytes (StandardCharsets .UTF_8 ),
167+ StandardOpenOption .CREATE_NEW );
168+ Files .setLastModifiedTime (newFile , FileTime .fromMillis (lastModifiedTime ));
169+ return newFile ;
170+ }
114171}
0 commit comments