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

Code before:
        containerConfigurationTemplate);

    Path savedConfigPath =
        cacheStorageFiles
            .getLocalDirectory()
            .resolve("config")
            .resolve("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
    Assert.assertTrue(Files.exists(savedConfigPath));
    ContainerConfigurationTemplate savedContainerConfig =
        JsonTemplateMapper.readJsonFromFile(savedConfigPath, ContainerConfigurationTemplate.class);
    Assert.assertEquals("wasm", savedContainerConfig.getArchitecture());
  }

  @Test
  public void testMoveIfDoesNotExist_exceptionAfterFailure() {
    Exception exception =
        assertThrows(
            IOException.class,
            () -> CacheStorageWriter.moveIfDoesNotExist(Paths.get("/foo"), Paths.get("/bar")));
    assertThat(exception)
        .hasMessageThat()
        .isEqualTo(
            "unable to move: /foo to /bar; such failures are often caused by interference from "
                + "antivirus (https://github.com/GoogleContainerTools/jib/issues/3127#issuecomment-796838294), "
                + "or rarely if the operation is not supported by the file system (for example: "
                + "special non-local file system)");
    assertThat(exception).hasCauseThat().isInstanceOf(NoSuchFileException.class);
    assertThat(exception.getCause()).hasMessageThat().isEqualTo("/foo");
  }

  private void verifyCachedLayer(CachedLayer cachedLayer, Blob uncompressedLayerBlob)
      throws IOException {
    BlobDescriptor layerBlobDescriptor = getDigest(compress(uncompressedLayerBlob));
    DescriptorDigest layerDiffId = getDigest(uncompressedLayerBlob).getDigest();

    // Verifies cachedLayer is correct.
    Assert.assertEquals(layerBlobDescriptor.getDigest(), cachedLayer.getDigest());
    Assert.assertEquals(layerDiffId, 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.getLayerFile(cachedLayer.getDigest(), cachedLayer.getDiffId())));
  }
}


Review comment (bug):
sometimes asserting on the whole message can make the test brittle. How about we assert on just the essential part? : `unable to move: /foo to /bar; such failures are often caused by interference from antivirus`

Code after:
        containerConfigurationTemplate);

    Path savedConfigPath =
        cacheStorageFiles
            .getLocalDirectory()
            .resolve("config")
            .resolve("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
    Assert.assertTrue(Files.exists(savedConfigPath));
    ContainerConfigurationTemplate savedContainerConfig =
        JsonTemplateMapper.readJsonFromFile(savedConfigPath, ContainerConfigurationTemplate.class);
    Assert.assertEquals("wasm", savedContainerConfig.getArchitecture());
  }

  @Test
  public void testMoveIfDoesNotExist_exceptionAfterFailure() {
    Exception exception =
        assertThrows(
            IOException.class,
            () -> CacheStorageWriter.moveIfDoesNotExist(Paths.get("foo"), Paths.get("bar")));
    assertThat(exception)
        .hasMessageThat()
        .contains(
            "unable to move: foo to bar; such failures are often caused by interference from "
                + "antivirus");
    assertThat(exception).hasCauseThat().isInstanceOf(NoSuchFileException.class);
    assertThat(exception.getCause()).hasMessageThat().isEqualTo("foo");
  }

  private void verifyCachedLayer(CachedLayer cachedLayer, Blob uncompressedLayerBlob)
      throws IOException {
    BlobDescriptor layerBlobDescriptor = getDigest(compress(uncompressedLayerBlob));
    DescriptorDigest layerDiffId = getDigest(uncompressedLayerBlob).getDigest();

    // Verifies cachedLayer is correct.
    Assert.assertEquals(layerBlobDescriptor.getDigest(), cachedLayer.getDigest());
    Assert.assertEquals(layerDiffId, 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.getLayerFile(cachedLayer.getDigest(), cachedLayer.getDiffId())));
  }
}