Repository: GoogleContainerTools/skaffold
Pull request #7772 (2022-08-12 23:22:22)

chore: unit test for CreateManifestList
Fixes: #7632
**Related**: #7082 #7745

**Description**
Unit test for manifest list (#7745 )

codecov[bot]: # [Codecov](https://codecov.io/gh/GoogleContainerTools/skaffold/pull/7772?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleContainerTools) Report
> Merging [#7772](https://codecov.io/gh/GoogleContainerTools/skaffold/pull/7772?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleContainerTools) (dc42c2a) into [main](https://codecov.io/gh/GoogleContainerTools/skaffold/commit/290280e8c6c19231e9ed5b5c027f2c01d22f75be?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleContainerTools) (290280e) will **decrease** coverage by `3.90%`.
> The diff coverage is `53.59%`.

```diff
@@            Coverage Diff             @@
##             main    #7772      +/-   ##
==========================================
- Coverage   70.48%   66.58%   -3.91%     
==========================================
  Files         515      590      +75     
  Lines       23150    28428    +5278     
==========================================
+ Hits        16317    18928    +2611     
- Misses       5776     8109    +2333     
- Partials     1057     1391     +334     
```


| [Impacted Files](https://codecov.io/gh/GoogleContainerTools/skaffold/pull/7772?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleContainerTools) | Coverage Δ | |
|---|---|---|
| [cmd/skaffold/app/cmd/credits/export.go](https://codecov.io/gh/GoogleContainerTools/skaffold/pull/7772/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleContainerTools#diff-Y21kL3NrYWZmb2xkL2FwcC9jbWQvY3JlZGl0cy9leHBvcnQuZ28=) | `0.00% <0.00%> (ø)` | |
| [cmd/skaffold/app/cmd/deploy.go](https://codecov.io/gh/GoogleContainerTools/skaffold/pull/7772/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleContainerTools#diff-Y21kL3NrYWZmb2xkL2FwcC9jbWQvZGVwbG95Lmdv) | `40.90% <0.00%> (-12.94%)` | :arrow_down: |
| [cmd/skaffold/app/cmd/test.go](https://codecov.io/gh/GoogleContainerTools/skaffold/pull/7772/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleContainerTools#diff-Y21kL3NrYWZmb2xkL2FwcC9jbWQvdGVzdC5nbw==) | `44.44% <0.00%> (ø)` | |
| [cmd/skaffold/skaffold.go](https://codecov.io/gh/GoogleContainerTools/skaffold/pull/7772/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleContainerTools#diff-Y21kL3NrYWZmb2xkL3NrYWZmb2xkLmdv) | `0.00% <0.00%> (ø)` | |
| [cmd/skaffold/app/cmd/inspect\_tests.go](https://codecov.io/gh/GoogleContainerTools/skaffold/pull/7772/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleContainerTools#diff-Y21kL3NrYWZmb2xkL2FwcC9jbWQvaW5zcGVjdF90ZXN0cy5nbw==) | `62.50% <14.28%> (-1.14%)` | :arrow_down: |
| [cmd/skaffold/app/cmd/render.go](https://codecov.io/gh/GoogleContainerTools/skaffold/pull/7772/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleContainerTools#diff-Y21kL3NrYWZmb2xkL2FwcC9jbWQvcmVuZGVyLmdv) | `35.48% <18.18%> (-5.90%)` | :arrow_down: |
| [cmd/skaffold/app/cmd/lsp.go](https://codecov.io/gh/GoogleContainerTools/skaffold/pull/7772/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleContainerTools#diff-Y21kL3NrYWZmb2xkL2FwcC9jbWQvbHNwLmdv) | `28.12% <28.12%> (ø)` | |
| [cmd/skaffold/app/cmd/run.go](https://codecov.io/gh/GoogleContainerTools/skaffold/pull/7772/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleContainerTools#diff-Y21kL3NrYWZmb2xkL2FwcC9jbWQvcnVuLmdv) | `64.28% <33.33%> (-9.63%)` | :arrow_down: |
| [cmd/skaffold/app/cmd/fix.go](https://codecov.io/gh/GoogleContainerTools/skaffold/pull/7772/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleContainerTools#diff-Y21kL3NrYWZmb2xkL2FwcC9jbWQvZml4Lmdv) | `56.41% <37.50%> (-20.07%)` | :arrow_down: |
| [cmd/skaffold/app/cmd/verify.go](https://codecov.io/gh/GoogleContainerTools/skaffold/pull/7772/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleContainerTools#diff-Y21kL3NrYWZmb2xkL2FwcC9jbWQvdmVyaWZ5Lmdv) | `41.17% <41.17%> (ø)` | |
| ... and [351 more](https://codecov.io/gh/GoogleContainerTools/skaffold/pull/7772/diff?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleContainerTools) | |

:mega: We’re building smart automated test selection to slash your CI/CD build times. [Learn more](https://about.codecov.io/iterative-testing/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=GoogleContainerTools)


Patch:
diff --git a/pkg/skaffold/docker/manifest_list.go b/pkg/skaffold/docker/manifest_list.go
index 0b140bb64..27de3b1f3 100644
--- a/pkg/skaffold/docker/manifest_list.go
+++ b/pkg/skaffold/docker/manifest_list.go
@@ -30,6 +30,12 @@ import (
 	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/output/log"
 )
 
+// for testing
+var (
+	mutateAppendManifest = mutate.AppendManifests
+	remoteWriteIndex     = remote.WriteIndex
+)
+
 // Describes the result of an image build.
 type SinglePlatformImage struct {
 	// Platform (OS + architecture) associated with the image built.
@@ -49,7 +55,7 @@ func CreateManifestList(ctx context.Context, images []SinglePlatformImage, targe
 			return "", err
 		}
 
-		img, err := remote.Image(ref)
+		img, err := remoteImage(ref)
 		if err != nil {
 			return "", err
 		}
@@ -61,13 +67,13 @@ func CreateManifestList(ctx context.Context, images []SinglePlatformImage, targe
 			},
 		}
 	}
-	idx := mutate.AppendManifests(mutate.IndexMediaType(empty.Index, types.DockerManifestList), adds...)
+	idx := mutateAppendManifest(mutate.IndexMediaType(empty.Index, types.DockerManifestList), adds...)
 	targetRef, err := name.ParseReference(targetTag, name.WeakValidation)
 	if err != nil {
 		return "", err
 	}
 
-	err = remote.WriteIndex(targetRef, idx, remote.WithAuthFromKeychain(primaryKeychain))
+	err = remoteWriteIndex(targetRef, idx, remote.WithAuthFromKeychain(primaryKeychain))
 	if err != nil {
 		return "", err
 	}


Test patch:
diff --git a/pkg/skaffold/docker/manifest_list_test.go b/pkg/skaffold/docker/manifest_list_test.go
new file mode 100644
index 000000000..3ad6b4941
--- /dev/null
+++ b/pkg/skaffold/docker/manifest_list_test.go
@@ -0,0 +1,74 @@
+/*
+Copyright 2022 The Skaffold Authors
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package docker
+
+import (
+	"context"
+	"strings"
+	"testing"
+
+	"github.com/google/go-containerregistry/pkg/name"
+	v1 "github.com/google/go-containerregistry/pkg/v1"
+	"github.com/google/go-containerregistry/pkg/v1/mutate"
+	"github.com/google/go-containerregistry/pkg/v1/remote"
+
+	"github.com/GoogleContainerTools/skaffold/testutil"
+)
+
+func (i fakeImage) Descriptor() (*v1.Descriptor, error) {
+	return &v1.Descriptor{}, nil
+}
+
+func TestCreateManifestList(t *testing.T) {
+	ctx := context.Background()
+	targetTag := "gcr.io/skaffold/example:latest"
+	images := []SinglePlatformImage{
+		{Platform: &v1.Platform{OS: "linux", Architecture: "amd64"}, Image: "gcr.io/skaffold/example:b1234_linux_amd64"},
+		{Platform: &v1.Platform{OS: "linux", Architecture: "arm64"}, Image: "gcr.io/skaffold/example:b1234_linux_arm64"},
+	}
+
+	testutil.Run(t, "", func(t *testutil.T) {
+		t.Override(&remoteImage, func(ref name.Reference, options ...remote.Option) (v1.Image, error) {
+			return &fakeImage{
+				Reference: ref,
+			}, nil
+		})
+
+		t.Override(&mutateAppendManifest, func(base v1.ImageIndex, adds ...mutate.IndexAddendum) v1.ImageIndex {
+			for i, add := range adds {
+				img := add.Add.(*fakeImage).Reference.Name()
+				t.CheckDeepEqual(images[i].Image, img)
+			}
+
+			return &fakeImageIndex{}
+		})
+
+		t.Override(&remoteWriteIndex, func(ref name.Reference, ii v1.ImageIndex, options ...remote.Option) (rerr error) {
+			return nil
+		})
+
+		manifestTag, err := CreateManifestList(ctx, images, targetTag)
+
+		if err != nil {
+			t.Fatalf("Error generating manifest list with target tag %s\n", targetTag)
+		}
+
+		if !strings.HasPrefix(manifestTag, targetTag) {
+			t.Fatalf("Error in tag %s, %s not found\n", manifestTag, targetTag)
+		}
+	})
+}