Repository: GoogleContainerTools/jib
File: jib-core/src/integration-test/java/com/google/cloud/tools/jib/registry/ManifestPullerIntegrationTest.java (Java)

Code before:

    // Ensures call to image at KNOWN_MANIFEST_LIST_SHA returns a manifest list
    V22ManifestListTemplate manifestListTargeted =
        registryClient
            .pullManifest(KNOWN_MANIFEST_LIST_SHA, V22ManifestListTemplate.class)
            .getManifest();
    Assert.assertEquals(2, manifestListTargeted.getSchemaVersion());
    Assert.assertTrue(manifestListTargeted.getManifests().size() > 0);

    // Generic call to image at KNOWN_MANIFEST_LIST_SHA, should also return a manifest list
    ManifestTemplate manifestListGeneric =
        registryClient.pullManifest(KNOWN_MANIFEST_LIST_SHA).getManifest();
    Assert.assertEquals(2, manifestListGeneric.getSchemaVersion());
    MatcherAssert.assertThat(
        manifestListGeneric, CoreMatchers.instanceOf(V22ManifestListTemplate.class));
    Assert.assertTrue(((V22ManifestListTemplate) manifestListGeneric).getManifests().size() > 0);
  }

  @Test
  public void testPull_ociIndex() throws IOException, RegistryException {
    RegistryClient registryClient =
        RegistryClient.factory(EventHandlers.NONE, "gcr.io", "distroless/base", httpClient)
            .newRegistryClient();

    // Ensures call to image at KNOWN_OCI_INDEX_SHA returns an OCI index
    OciIndexTemplate manifestListTargeted =
        registryClient.pullManifest(KNOWN_OCI_INDEX_SHA, OciIndexTemplate.class).getManifest();
    Assert.assertEquals(2, manifestListTargeted.getSchemaVersion());
    Assert.assertTrue((manifestListTargeted.getManifests().size() > 0));

    // Generic call to image at KNOWN_OCI_INDEX_SHA, should also return an OCI index
    ManifestTemplate manifestListGeneric =
        registryClient.pullManifest(KNOWN_OCI_INDEX_SHA).getManifest();
    Assert.assertEquals(2, manifestListGeneric.getSchemaVersion());
    MatcherAssert.assertThat(manifestListGeneric, CoreMatchers.instanceOf(OciIndexTemplate.class));
    Assert.assertTrue(((OciIndexTemplate) manifestListGeneric).getManifests().size() > 0);
  }

  @Test
  public void testPull_unknownManifest() throws RegistryException, IOException {
    try {
      RegistryClient registryClient =
          RegistryClient.factory(EventHandlers.NONE, dockerHost + ":5000", "busybox", httpClient)
              .newRegistryClient();
      registryClient.pullManifest("nonexistent-tag");
      Assert.fail("Trying to pull nonexistent image should have errored");

    } catch (RegistryErrorException ex) {
      MatcherAssert.assertThat(
          ex.getMessage(),
          CoreMatchers.containsString(

Review comment (suggestion):
Alt: instead of `KNOWN_OCI_INDEX_SHA`, this newly added test can keep using the "latest" tag (which keeps it susceptible for breakage but able to detect future changes in this image, as noted in https://github.com/GoogleContainerTools/jib/issues/3963#issuecomment-1481405637).

Code after:

    // Ensures call to image at the specified SHA returns a manifest list
    V22ManifestListTemplate manifestListTargeted =
        registryClient
            .pullManifest(KNOWN_MANIFEST_LIST_SHA, V22ManifestListTemplate.class)
            .getManifest();
    assertThat(manifestListTargeted.getSchemaVersion()).isEqualTo(2);
    assertThat(manifestListTargeted.getManifests()).isNotEmpty();

    // Generic call to image at the specified SHA, should also return a manifest list
    ManifestTemplate manifestListGeneric =
        registryClient.pullManifest(KNOWN_MANIFEST_LIST_SHA).getManifest();
    assertThat(manifestListGeneric.getSchemaVersion()).isEqualTo(2);
    assertThat(manifestListGeneric).isInstanceOf(V22ManifestListTemplate.class);
    assertThat(((V22ManifestListTemplate) manifestListGeneric).getManifests()).isNotEmpty();
  }

  @Test
  public void testPull_ociIndex() throws IOException, RegistryException {
    RegistryClient registryClient =
        RegistryClient.factory(EventHandlers.NONE, "gcr.io", "distroless/base", httpClient)
            .newRegistryClient();

    // Ensures call to image at the specified SHA returns an OCI index
    OciIndexTemplate manifestListTargeted =
        registryClient.pullManifest(KNOWN_OCI_INDEX_SHA, OciIndexTemplate.class).getManifest();
    assertThat(manifestListTargeted.getSchemaVersion()).isEqualTo(2);
    assertThat((manifestListTargeted.getManifests().size() > 0)).isTrue();

    // Generic call to image at the specified SHA, should also return an OCI index
    ManifestTemplate manifestListGeneric =
        registryClient.pullManifest(KNOWN_OCI_INDEX_SHA).getManifest();
    assertThat(manifestListGeneric.getSchemaVersion()).isEqualTo(2);
    assertThat(manifestListGeneric).isInstanceOf(OciIndexTemplate.class);
    assertThat(((OciIndexTemplate) manifestListGeneric).getManifests()).isNotEmpty();
  }

  @Test
  public void testPull_unknownManifest() throws RegistryException, IOException {
    try {
      RegistryClient registryClient =
          RegistryClient.factory(EventHandlers.NONE, dockerHost + ":5000", "busybox", httpClient)
              .newRegistryClient();
      registryClient.pullManifest("nonexistent-tag");
      Assert.fail("Trying to pull nonexistent image should have errored");

    } catch (RegistryErrorException ex) {
      assertThat(ex)
          .hasMessageThat()
          .contains("pull image manifest for " + dockerHost + ":5000/busybox:nonexistent-tag");
    }