static void checkManifestPlatform(
      BuildContext buildContext, ContainerConfigurationTemplate containerConfig)
      throws PlatformNotFoundInBaseImageException {
    Optional<Path> path = buildContext.getBaseImageConfiguration().getTarPath();
    String baseImageName =
        path.map(Path::toString)
            .orElse(buildContext.getBaseImageConfiguration().getImage().toString());

    Set<Platform> platforms = buildContext.getContainerConfiguration().getPlatforms();
    Verify.verify(!platforms.isEmpty());

    if (platforms.size() != 1) {
      String msg =
          String.format(
              "cannot build for multiple platforms since the base image '%s' is not a manifest list.",
              baseImageName);
      throw new PlatformNotFoundInBaseImageException(msg);
    } else {
      Platform platform = platforms.iterator().next();
      if (!platform.getArchitecture().equals(containerConfig.getArchitecture())
          || !platform.getOs().equals(containerConfig.getOs())) {

        // Unfortunately, "platforms" has amd64/linux by default even if the user didn't explicitly
        // configure it. Skip reporting to suppress false alarm.
        if (!(platform.getArchitecture().equals("amd64") && platform.getOs().equals("linux"))) {
          String msg =
              String.format(
                  "the configured platform (%s/%s) doesn't match the platform (%s/%s) of the base image (%s)",
                  platform.getArchitecture(),
                  platform.getOs(),
                  containerConfig.getArchitecture(),
                  containerConfig.getOs(),
                  baseImageName);
          throw new PlatformNotFoundInBaseImageException(msg);
        }
      }
    }
  }
@Test
  public void testCheckManifestPlatform_tarBaseImage() {
    Path tar = Paths.get("/foo/bar.tar");
    Mockito.when(buildContext.getBaseImageConfiguration())
        .thenReturn(ImageConfiguration.builder(ImageReference.scratch()).setTarPath(tar).build());
    Mockito.when(containerConfig.getPlatforms())
        .thenReturn(ImmutableSet.of(new Platform("amd64", "linux"), new Platform("arch", "os")));

    Exception ex =
        assertThrows(
            PlatformNotFoundInBaseImageException.class,
            () ->
                PlatformChecker.checkManifestPlatform(
                    buildContext, new ContainerConfigurationTemplate()));
    assertThat(ex)
        .hasMessageThat()
        .isEqualTo(
            "cannot build for multiple platforms since the base image '"
                + tar
                + "' is not a manifest list.");
  }