@VisibleForTesting
  List<Image> getCachedBaseImages()
      throws IOException, CacheCorruptedException, BadContainerConfigurationFormatException,
          LayerCountMismatchException, UnlistedPlatformInManifestListException,
          PlatformNotFoundInBaseImageException {
    ImageReference baseImage = buildContext.getBaseImageConfiguration().getImage();
    Optional<ImageMetadataTemplate> metadata =
        buildContext.getBaseImageLayersCache().retrieveMetadata(baseImage);
    if (!metadata.isPresent()) {
      return Collections.emptyList();
    }

    ManifestTemplate manifestList = metadata.get().getManifestList();
    List<ManifestAndConfigTemplate> manifestsAndConfigs = metadata.get().getManifestsAndConfigs();

    if (manifestList == null) {
      Verify.verify(manifestsAndConfigs.size() == 1);
      ManifestAndConfigTemplate manifestAndConfig = manifestsAndConfigs.get(0);
      Optional<Image> cachedImage = getBaseImageIfAllLayersCached(manifestAndConfig, true);
      if (!cachedImage.isPresent()) {
        return Collections.emptyList();
      }
      return Collections.singletonList(cachedImage.get());
    }

    // Manifest list cached. Identify matching platforms and check if all of them are cached.
    ImmutableList.Builder<Image> images = ImmutableList.builder();
    for (Platform platform : buildContext.getContainerConfiguration().getPlatforms()) {
      String manifestDigest =
          lookUpPlatformSpecificImageManifest((ManifestListTemplate) manifestList, platform);

      Optional<ManifestAndConfigTemplate> manifestAndConfigFound =
          manifestsAndConfigs.stream()
              .filter(entry -> manifestDigest.equals(entry.getManifestDigest()))
              .findFirst();
      if (!manifestAndConfigFound.isPresent()) {
        return Collections.emptyList();
      }
      Optional<Image> cachedImage =
          getBaseImageIfAllLayersCached(manifestAndConfigFound.get(), false);
      if (!cachedImage.isPresent()) {
        return Collections.emptyList();
      }
      images.add(cachedImage.get());
    }
    return images.build();
  }
@Test
  public void testGetCachedBaseImages_manifestListCached()
      throws InvalidImageReferenceException, IOException, CacheCorruptedException,
          UnlistedPlatformInManifestListException, BadContainerConfigurationFormatException,
          LayerCountMismatchException, PlatformNotFoundInBaseImageException {
    ImageReference imageReference = ImageReference.parse("cat");
    Mockito.when(buildContext.getBaseImageConfiguration())
        .thenReturn(ImageConfiguration.builder(imageReference).build());

    ContainerConfigurationTemplate containerConfigJson1 = new ContainerConfigurationTemplate();
    ContainerConfigurationTemplate containerConfigJson2 = new ContainerConfigurationTemplate();
    containerConfigJson1.setContainerUser("user1");
    containerConfigJson2.setContainerUser("user2");

    ManifestListTemplate manifestList = Mockito.mock(ManifestListTemplate.class);
    Mockito.when(manifestList.getDigestsForPlatform("arch1", "os1"))
        .thenReturn(Arrays.asList("sha256:digest1"));
    Mockito.when(manifestList.getDigestsForPlatform("arch2", "os2"))
        .thenReturn(Arrays.asList("sha256:digest2"));

    ImageMetadataTemplate imageMetadata =
        new ImageMetadataTemplate(
            manifestList,
            Arrays.asList(
                new ManifestAndConfigTemplate(
                    Mockito.mock(BuildableManifestTemplate.class),
                    containerConfigJson1,
                    "sha256:digest1"),
                new ManifestAndConfigTemplate(
                    Mockito.mock(BuildableManifestTemplate.class),
                    containerConfigJson2,
                    "sha256:digest2")));
    Mockito.when(cache.retrieveMetadata(imageReference)).thenReturn(Optional.of(imageMetadata));
    Mockito.when(
            cache.areAllLayersCached(imageMetadata.getManifestsAndConfigs().get(0).getManifest()))
        .thenReturn(true);
    Mockito.when(
            cache.areAllLayersCached(imageMetadata.getManifestsAndConfigs().get(1).getManifest()))
        .thenReturn(true);

    Mockito.when(containerConfig.getPlatforms())
        .thenReturn(ImmutableSet.of(new Platform("arch1", "os1"), new Platform("arch2", "os2")));

    List<Image> images = pullBaseImageStep.getCachedBaseImages();

    Assert.assertEquals(2, images.size());
    Assert.assertEquals("user1", images.get(0).getUser());
    Assert.assertEquals("user2", images.get(1).getUser());
  }