Repository: GoogleContainerTools/jib
File: jib-core/src/test/java/com/google/cloud/tools/jib/cache/CacheStorageWriterTest.java (Java)

Code before:
  @Test
  public void testWriteZstdCompressed() throws IOException {
    Blob uncompressedLayerBlob = Blobs.from("uncompressedLayerBlob");
    Blob compressedLayerBlob = compress(uncompressedLayerBlob, CompressorStreamFactory.ZSTANDARD);

    CachedLayer cachedLayer = cacheStorageWriter.writeCompressed(compressedLayerBlob);

    verifyCachedLayer(cachedLayer, uncompressedLayerBlob, compressedLayerBlob);
  }

  @Test
  public void testWriteUncompressed() throws IOException {
    Blob uncompressedLayerBlob = Blobs.from("uncompressedLayerBlob");
    DescriptorDigest layerDigest = getDigest(compress(uncompressedLayerBlob)).getDigest();
    DescriptorDigest selector = getDigest(Blobs.from("selector")).getDigest();

    CachedLayer cachedLayer = cacheStorageWriter.writeUncompressed(uncompressedLayerBlob, selector);

    verifyCachedLayer(cachedLayer, uncompressedLayerBlob, compress(uncompressedLayerBlob));

    // Verifies that the files are present.
    Path selectorFile = cacheStorageFiles.getSelectorFile(selector);
    Assert.assertTrue(Files.exists(selectorFile));
    Assert.assertEquals(layerDigest.getHash(), Blobs.writeToString(Blobs.from(selectorFile)));
  }

  @Test
  public void testWriteTarLayer() throws IOException {
    Blob uncompressedLayerBlob = Blobs.from("uncompressedLayerBlob");
    DescriptorDigest diffId = getDigest(uncompressedLayerBlob).getDigest();

    CachedLayer cachedLayer =
        cacheStorageWriter.writeTarLayer(diffId, compress(uncompressedLayerBlob));

    BlobDescriptor layerBlobDescriptor = getDigest(compress(uncompressedLayerBlob));

    // Verifies cachedLayer is correct.
    Assert.assertEquals(layerBlobDescriptor.getDigest(), cachedLayer.getDigest());
    Assert.assertEquals(diffId, cachedLayer.getDiffId());
    Assert.assertEquals(layerBlobDescriptor.getSize(), cachedLayer.getSize());
    Assert.assertArrayEquals(
        Blobs.writeToByteArray(uncompressedLayerBlob),
        Blobs.writeToByteArray(decompress(cachedLayer.getBlob())));

    // Verifies that the files are present.
    Assert.assertTrue(
        Files.exists(
            cacheStorageFiles
                .getLocalDirectory()
                .resolve(cachedLayer.getDiffId().getHash())
                .resolve(cachedLayer.getDigest().getHash())));

Review comment (performance):
The test is about uncompressed layers, but I noticed the verifyCachedLayer actually uses compression for asserts.
What is even more strange is that this test seems dependant on the gzip compression settings: it passes with JDK GZIPOutputStream, but not with common-compress GzipCompressorOutputStream.

Code after:
  @Test
  public void testWriteZstdCompressed() throws IOException {
    Blob uncompressedLayerBlob = Blobs.from("uncompressedLayerBlob");
    Blob compressedLayerBlob = compress(uncompressedLayerBlob, CompressorStreamFactory.ZSTANDARD);

    CachedLayer cachedLayer = cacheStorageWriter.writeCompressed(compressedLayerBlob);

    verifyCachedLayer(cachedLayer, uncompressedLayerBlob, compressedLayerBlob);
  }

  @Test(expected = IOException.class)
  public void testWriteCompressWhenUncompressed() throws IOException {
    Blob uncompressedLayerBlob = Blobs.from("uncompressedLayerBlob");
    // The detection of compression algorithm will fail
    cacheStorageWriter.writeCompressed(uncompressedLayerBlob);
  }

  @Test
  public void testWriteUncompressed() throws IOException {
    Blob uncompressedLayerBlob = Blobs.from("uncompressedLayerBlob");
    DescriptorDigest layerDigest = getDigest(compress(uncompressedLayerBlob)).getDigest();
    DescriptorDigest selector = getDigest(Blobs.from("selector")).getDigest();

    CachedLayer cachedLayer = cacheStorageWriter.writeUncompressed(uncompressedLayerBlob, selector);

    verifyCachedLayer(cachedLayer, uncompressedLayerBlob, compress(uncompressedLayerBlob));

    // Verifies that the files are present.
    Path selectorFile = cacheStorageFiles.getSelectorFile(selector);
    Assert.assertTrue(Files.exists(selectorFile));
    Assert.assertEquals(layerDigest.getHash(), Blobs.writeToString(Blobs.from(selectorFile)));
  }

  @Test
  public void testWriteTarLayer() throws IOException {
    Blob uncompressedLayerBlob = Blobs.from("uncompressedLayerBlob");
    DescriptorDigest diffId = getDigest(uncompressedLayerBlob).getDigest();

    CachedLayer cachedLayer =
        cacheStorageWriter.writeTarLayer(diffId, compress(uncompressedLayerBlob));

    BlobDescriptor layerBlobDescriptor = getDigest(compress(uncompressedLayerBlob));

    // Verifies cachedLayer is correct.
    Assert.assertEquals(layerBlobDescriptor.getDigest(), cachedLayer.getDigest());
    Assert.assertEquals(diffId, cachedLayer.getDiffId());
    Assert.assertEquals(layerBlobDescriptor.getSize(), cachedLayer.getSize());
    Assert.assertArrayEquals(
        Blobs.writeToByteArray(uncompressedLayerBlob),
        Blobs.writeToByteArray(decompress(cachedLayer.getBlob())));