Skip to content

Commit

Permalink
feat(3): add unit tests for platform-specific images
Browse files Browse the repository at this point in the history
  • Loading branch information
Craig Newton committed Oct 1, 2024
1 parent c1fc865 commit c3219ae
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions pkg/resolve/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit c3219ae

Please sign in to comment.