Skip to content

test: Introduce simpler patch test helpers #133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion common/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ go 1.21
require (
github.com/onsi/gomega v1.27.10
github.com/spf13/pflag v1.0.5
gomodules.xyz/jsonpatch/v2 v2.4.0
k8s.io/apiextensions-apiserver v0.28.1
k8s.io/apimachinery v0.28.1
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2
Expand All @@ -22,7 +23,10 @@ require (
github.com/blang/semver v3.5.1+incompatible // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/coredns/caddy v1.1.0 // indirect
github.com/coredns/corefile-migration v1.0.21 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
Expand All @@ -46,6 +50,7 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.16.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
Expand All @@ -59,7 +64,6 @@ require (
golang.org/x/term v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/time v0.3.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230525234035-dd9d682886f9 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect
Expand All @@ -70,6 +74,7 @@ require (
k8s.io/api v0.28.1 // indirect
k8s.io/apiserver v0.28.1 // indirect
k8s.io/client-go v0.28.1 // indirect
k8s.io/cluster-bootstrap v0.27.2 // indirect
k8s.io/component-base v0.28.1 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 // indirect
Expand Down
269 changes: 269 additions & 0 deletions common/go.sum

Large diffs are not rendered by default.

161 changes: 161 additions & 0 deletions common/pkg/testutils/capitest/patches.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
// Copyright 2023 D2iQ, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package capitest

import (
"bytes"
"context"
"encoding/json"
"testing"

"github.com/onsi/gomega"
"github.com/onsi/gomega/gstruct"
gomegatypes "github.com/onsi/gomega/types"
"gomodules.xyz/jsonpatch/v2"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/uuid"
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1"
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"

"github.com/d2iq-labs/capi-runtime-extensions/common/pkg/handlers/mutation"
)

type PatchTestDef struct {
Name string
Vars []runtimehooksv1.Variable
RequestItem runtimehooksv1.GeneratePatchesRequestItem
ExpectedPatchMatchers []JSONPatchMatcher
}

type JSONPatchMatcher struct {
Operation string
Path string
ValueMatcher gomegatypes.GomegaMatcher
}

func ValidateGeneratePatches[T mutation.GeneratePatches](
t *testing.T,
handlerCreator func() T,
testDefs ...PatchTestDef,
) {
t.Helper()

t.Parallel()

for testIdx := range testDefs {
tt := testDefs[testIdx]

t.Run(tt.Name, func(t *testing.T) {
t.Parallel()

g := gomega.NewWithT(t)
h := handlerCreator()
req := &runtimehooksv1.GeneratePatchesRequest{
Variables: tt.Vars,
Items: []runtimehooksv1.GeneratePatchesRequestItem{tt.RequestItem},
}
resp := &runtimehooksv1.GeneratePatchesResponse{}
h.GeneratePatches(context.Background(), req, resp)
g.Expect(resp.Status).To(gomega.Equal(runtimehooksv1.ResponseStatusSuccess))

if len(tt.ExpectedPatchMatchers) == 0 {
g.Expect(resp.Items).To(gomega.BeEmpty())
return
}

patchMatchers := make([]interface{}, 0, len(tt.ExpectedPatchMatchers))
for patchIdx := range tt.ExpectedPatchMatchers {
expectedPatch := tt.ExpectedPatchMatchers[patchIdx]
patchMatchers = append(patchMatchers, gstruct.MatchAllFields(gstruct.Fields{
"Operation": gomega.Equal(expectedPatch.Operation),
"Path": gomega.Equal(expectedPatch.Path),
"Value": expectedPatch.ValueMatcher,
}))
}

g.Expect(resp.Items).
To(gomega.ContainElement(gstruct.MatchFields(gstruct.IgnoreExtras, gstruct.Fields{
"UID": gomega.Equal(tt.RequestItem.UID),
"PatchType": gomega.Equal(runtimehooksv1.JSONPatchType),
"Patch": gomega.WithTransform(
func(data []byte) ([]jsonpatch.Operation, error) {
operations := []jsonpatch.Operation{}
if err := json.Unmarshal(data, &operations); err != nil {
return nil, err
}
return operations, nil
},
gomega.ContainElements(patchMatchers...),
),
})))
})
}
}

// v returns a runtimehooksv1.Variable with the passed name and value.
func VariableWithValue(name string, value any) runtimehooksv1.Variable {
return runtimehooksv1.Variable{
Name: name,
Value: apiextensionsv1.JSON{Raw: toJSON(value)},
}
}

func toJSON(v any) []byte {
data, err := json.Marshal(v)
if err != nil {
panic(err)
}
compacted := &bytes.Buffer{}
if err := json.Compact(compacted, data); err != nil {
panic(err)
}
return compacted.Bytes()
}

// requestItem returns a GeneratePatchesRequestItem with the given variables and object.
func requestItem(
object any,
holderRef *runtimehooksv1.HolderReference,
) runtimehooksv1.GeneratePatchesRequestItem {
return runtimehooksv1.GeneratePatchesRequestItem{
UID: uuid.NewUUID(),
Object: runtime.RawExtension{
Raw: toJSON(object),
},
HolderReference: *holderRef,
}
}

func NewKubeadmConfigTemplateRequestItem() runtimehooksv1.GeneratePatchesRequestItem {
return requestItem(
&bootstrapv1.KubeadmConfigTemplate{
TypeMeta: metav1.TypeMeta{
Kind: "KubeadmConfigTemplate",
APIVersion: bootstrapv1.GroupVersion.String(),
},
},
&runtimehooksv1.HolderReference{
Kind: "MachineDeployment",
FieldPath: "spec.template.spec.infrastructureRef",
},
)
}

func NewKubeadmControlPlaneTemplateRequestItem() runtimehooksv1.GeneratePatchesRequestItem {
return requestItem(
&controlplanev1.KubeadmControlPlaneTemplate{
TypeMeta: metav1.TypeMeta{
Kind: "KubeadmControlPlaneTemplate",
APIVersion: controlplanev1.GroupVersion.String(),
},
},
&runtimehooksv1.HolderReference{
Kind: "Cluster",
FieldPath: "spec.controlPlaneRef",
},
)
}
2 changes: 1 addition & 1 deletion common/pkg/testutils/capitest/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type VariableTestDef struct {
ExpectError bool
}

func ValidateVariable[T mutation.DiscoverVariables](
func ValidateDiscoverVariables[T mutation.DiscoverVariables](
t *testing.T,
variableName string,
variableSchema *clusterv1.VariableSchema,
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ require (
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.4
golang.org/x/sync v0.3.0
gomodules.xyz/jsonpatch/v2 v2.4.0
k8s.io/api v0.28.1
k8s.io/apiextensions-apiserver v0.28.1
k8s.io/apimachinery v0.28.1
k8s.io/apiserver v0.28.1
k8s.io/client-go v0.28.1
k8s.io/component-base v0.28.1
k8s.io/klog/v2 v2.100.1
Expand Down Expand Up @@ -79,15 +79,15 @@ require (
golang.org/x/term v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/time v0.3.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230525234035-dd9d682886f9 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiserver v0.28.1 // indirect
k8s.io/cluster-bootstrap v0.27.2 // indirect
k8s.io/cluster-bootstrap v0.28.1 // indirect
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,8 @@ k8s.io/apiserver v0.28.1 h1:dw2/NKauDZCnOUAzIo2hFhtBRUo6gQK832NV8kuDbGM=
k8s.io/apiserver v0.28.1/go.mod h1:d8aizlSRB6yRgJ6PKfDkdwCy2DXt/d1FDR6iJN9kY1w=
k8s.io/client-go v0.28.1 h1:pRhMzB8HyLfVwpngWKE8hDcXRqifh1ga2Z/PU9SXVK8=
k8s.io/client-go v0.28.1/go.mod h1:pEZA3FqOsVkCc07pFVzK076R+P/eXqsgx5zuuRWukNE=
k8s.io/cluster-bootstrap v0.27.2 h1:OL3onrOwrUD7NQxBUqQwTl1Uu2GQKCkw9BMHpc4PbiA=
k8s.io/cluster-bootstrap v0.27.2/go.mod h1:b++PF0mjUOiTKdPQFlDw7p4V2VquANZ8SfhAwzxZJFM=
k8s.io/cluster-bootstrap v0.28.1 h1:4+Va4UQtl8xLTg3E1Z6Xab7aQaN1fhmwSu15e0I7/do=
k8s.io/cluster-bootstrap v0.28.1/go.mod h1:46zKGL1KhvS5JVO2Io5vz2I8+CnVZ1IM03uxvcvWPBU=
k8s.io/component-base v0.28.1 h1:LA4AujMlK2mr0tZbQDZkjWbdhTV5bRyEyAFe0TJxlWg=
k8s.io/component-base v0.28.1/go.mod h1:jI11OyhbX21Qtbav7JkhehyBsIRfnO8oEgoAR12ArIU=
k8s.io/klog/v2 v2.100.1 h1:7WCHKK6K8fNhTqfBhISHQ97KrnJNFZMcQvKp7gP/tmg=
Expand Down
Loading