Repository: GoogleContainerTools/jib
File: jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/PushImageStepTest.java (Java)

Code before:
  @Test
  public void testMakeList_multiPlatform_enabled() throws IOException, RegistryException {
    Image asd = Image.builder(V22ManifestTemplate.class).setArchitecture("wasm").build();

    Mockito.when(containerConfig.isPlatformTag()).thenReturn(true);

    ImmutableList<PushImageStep> pushImageStepList =
        PushImageStep.makeList(
            buildContext,
            progressDispatcherFactory,
            registryClient,
            new BlobDescriptor(mockDescriptorDigest),
            asd,
            false);

    ArgumentCaptor<String> tagCAtcher = ArgumentCaptor.forClass(String.class);
    Mockito.when(registryClient.pushManifest(Mockito.any(), tagCAtcher.capture())).thenReturn(null);

    Assert.assertEquals(2, pushImageStepList.size());
    for (PushImageStep pushImageStep : pushImageStepList) {
      BuildResult buildResult = pushImageStep.call();
      Assert.assertEquals(
          "sha256:0dd75658cf52608fbd72eb95ff5fc5946966258c3676b35d336bfcc7ac5006f1",
          buildResult.getImageDigest().toString());
      Assert.assertEquals("mockDescriptorDigest", buildResult.getImageId().toString());
    }
    Set<String> allValues = ImmutableSet.copyOf(tagCAtcher.getAllValues());
    Set<String> expectedTags = ImmutableSet.of("tag1-wasm", "tag2-wasm");
    Assert.assertEquals(expectedTags, allValues);
  }

  @Test
  public void testMakeList_multiPlatform_disabled() throws IOException, RegistryException {
    Image asd = Image.builder(V22ManifestTemplate.class).setArchitecture("wasm").build();
    Mockito.when(containerConfig.isPlatformTag()).thenReturn(false);

    ImmutableList<PushImageStep> pushImageStepList =
        PushImageStep.makeList(
            buildContext,
            progressDispatcherFactory,
            registryClient,
            new BlobDescriptor(mockDescriptorDigest),
            asd,
            false);

    ArgumentCaptor<String> tagCAtcher = ArgumentCaptor.forClass(String.class);
    Mockito.when(registryClient.pushManifest(Mockito.any(), tagCAtcher.capture())).thenReturn(null);

    Assert.assertEquals(1, pushImageStepList.size());
    for (PushImageStep pushImageStep : pushImageStepList) {
      BuildResult buildResult = pushImageStep.call();

Review comment (suggestion):
Let's test these out explicitly. So instead of using a for loop, let's add assertions for  `pushImageStepList.get(0)` and `pushImageStepList.get(1)`.

Code after:

  @Test
  public void testMakeList_multiPlatform_platformTags() throws IOException, RegistryException {
    Image image = Image.builder(V22ManifestTemplate.class).setArchitecture("wasm").build();

    when(buildContext.getEnablePlatformTags()).thenReturn(true);

    List<PushImageStep> pushImageStepList =
        PushImageStep.makeList(
            buildContext,
            progressDispatcherFactory,
            registryClient,
            new BlobDescriptor(mockDescriptorDigest),
            image,
            false);

    ArgumentCaptor<String> tagCatcher = ArgumentCaptor.forClass(String.class);
    when(registryClient.pushManifest(any(), tagCatcher.capture())).thenReturn(null);

    assertThat(pushImageStepList).hasSize(2);
    pushImageStepList.get(0).call();
    pushImageStepList.get(1).call();

    assertThat(tagCatcher.getAllValues()).containsExactly("tag1-wasm", "tag2-wasm");
  }

  @Test
  public void testMakeList_multiPlatform_nonPlatformTags() throws IOException, RegistryException {
    Image image = Image.builder(V22ManifestTemplate.class).setArchitecture("wasm").build();
    when(buildContext.getEnablePlatformTags()).thenReturn(false);

    List<PushImageStep> pushImageStepList =
        PushImageStep.makeList(
            buildContext,
            progressDispatcherFactory,
            registryClient,
            new BlobDescriptor(mockDescriptorDigest),
            image,
            false);

    ArgumentCaptor<String> tagCatcher = ArgumentCaptor.forClass(String.class);
    when(registryClient.pushManifest(any(), tagCatcher.capture())).thenReturn(null);

    assertThat(pushImageStepList).hasSize(1);
    pushImageStepList.get(0).call();
    assertThat(tagCatcher.getAllValues())
        .containsExactly("sha256:0dd75658cf52608fbd72eb95ff5fc5946966258c3676b35d336bfcc7ac5006f1");
  }

  @Test
  public void testMakeListForManifestList_singlePlatform() throws IOException {