From c3219ae62fe308f6392078d2552fef25e2ce596d Mon Sep 17 00:00:00 2001 From: Craig Newton Date: Tue, 1 Oct 2024 10:00:45 +0200 Subject: [PATCH] feat(3): add unit tests for platform-specific images --- go.mod | 2 ++ pkg/resolve/resolve_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/go.mod b/go.mod index e513a8c..a5def2c 100644 --- a/go.mod +++ b/go.mod @@ -31,6 +31,7 @@ require ( github.com/spf13/cobra v1.8.1 github.com/spf13/pflag v1.0.5 github.com/spf13/viper v1.19.0 + github.com/stretchr/testify v1.9.0 go.uber.org/zap v1.27.0 gomodules.xyz/jsonpatch/v2 v2.4.0 k8s.io/api v0.31.1 @@ -112,6 +113,7 @@ require ( github.com/opencontainers/image-spec v1.1.0 // indirect github.com/pelletier/go-toml/v2 v2.2.2 // indirect github.com/pkg/errors v0.9.1 // indirect + github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/prometheus/client_golang v1.19.1 // indirect github.com/prometheus/client_model v0.6.1 // indirect github.com/prometheus/common v0.55.0 // indirect diff --git a/pkg/resolve/resolve_test.go b/pkg/resolve/resolve_test.go index 48b6ba8..c85dfa1 100644 --- a/pkg/resolve/resolve_test.go +++ b/pkg/resolve/resolve_test.go @@ -19,6 +19,8 @@ import ( "crypto/sha256" "encoding/hex" "fmt" + v1 "github.com/google/go-containerregistry/pkg/v1" + "github.com/stretchr/testify/assert" "strings" "testing" @@ -58,6 +60,11 @@ func init() { } sumBytes := sha256.Sum256([]byte(image)) digest := strings.TrimRight(hex.EncodeToString(sumBytes[:]), "\r\n") + if platform != "" { + if _, err := v1.ParsePlatform(platform); err != nil { + return "", err + } + } return fmt.Sprintf("sha256:%s", digest), nil } } @@ -163,6 +170,29 @@ func Test_ImageTags_Deployment(t *testing.T) { assertContainer(t, node, "image3@sha256:b0542da3f90bad69318e16ec7fcb6b13b089971886999e08bec91cea34891f0f", "spec", "template", "spec", "initContainers", "[name=initcontainer1]") } +func Test_ImageTags_ForPlatform(t *testing.T) { + node, err := createDeploymentNode([]string{"image0", "image1"}, []string{"image2", "image3"}) + if err != nil { + t.Fatalf("could not create deployment node: %v", err) + } + + if err := ImageTags(ctx, log, nil, node, []string{}, "linux/amd64"); err != nil { + t.Fatalf("problem resolving image tags: %v", err) + } + t.Log(node.MustString()) +} + +func Test_ImageTags_InvalidPlatform(t *testing.T) { + node, err := createDeploymentNode([]string{"image0", "image1"}, []string{"image2", "image3"}) + if err != nil { + t.Fatalf("could not create deployment node: %v", err) + } + + err = ImageTags(ctx, log, nil, node, []string{}, "some/other/linux/variant") + assert.ErrorContains(t, err, "too many slashes in platform spec") + t.Log(node.MustString()) +} + func assertContainer(t *testing.T, n *yaml.RNode, imageWithDigest string, path ...string) { container, err := n.Pipe(yaml.Lookup(path...), yaml.Get("image")) if err != nil {