Skip to content

Commit

Permalink
test: add failing unit test for undesired behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
dkoshkin committed Feb 9, 2024
1 parent 218b05a commit 0cc2133
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 25 deletions.
65 changes: 40 additions & 25 deletions common/pkg/testutils/capitest/patches.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ type PatchTestDef struct {
Vars []runtimehooksv1.Variable
RequestItem runtimehooksv1.GeneratePatchesRequestItem
ExpectedPatchMatchers []JSONPatchMatcher
ExpectedFailure bool
// UnexpectedPatchMatchers used to test patches that should not be present
UnexpectedPatchMatchers []JSONPatchMatcher
ExpectedFailure bool
}

type JSONPatchMatcher struct {
Expand Down Expand Up @@ -66,32 +68,14 @@ func ValidateGeneratePatches[T mutation.GeneratePatches](
g.Expect(resp.Items).To(gomega.BeEmpty())
return
}
expectedPatchMatchers := getPatchMatchers(tt.ExpectedPatchMatchers)
g.Expect(resp.Items).To(containElementMatcher(tt.RequestItem, expectedPatchMatchers))

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,
}))
if len(tt.UnexpectedPatchMatchers) == 0 {
return
}

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...),
),
})))
unexpectedPatchMatchers := getPatchMatchers(tt.UnexpectedPatchMatchers)
g.Expect(resp.Items).ToNot(containElementMatcher(tt.RequestItem, unexpectedPatchMatchers))
})
}
}
Expand Down Expand Up @@ -120,3 +104,34 @@ func VariableWithValue(
Value: apiextensionsv1.JSON{Raw: serializer.ToJSON(value)},
}
}

func getPatchMatchers(jsonMatcher []JSONPatchMatcher) []interface{} {
patchMatchers := make([]interface{}, 0, len(jsonMatcher))
for patchIdx := range jsonMatcher {
unexpectedPatch := jsonMatcher[patchIdx]
patchMatchers = append(patchMatchers, gstruct.MatchAllFields(gstruct.Fields{
"Operation": gomega.Equal(unexpectedPatch.Operation),
"Path": gomega.Equal(unexpectedPatch.Path),
"Value": unexpectedPatch.ValueMatcher,
}))
}

return patchMatchers
}

func containElementMatcher(requestItem runtimehooksv1.GeneratePatchesRequestItem, patchMatchers []interface{}) gomega.OmegaMatcher {
return gomega.ContainElement(gstruct.MatchFields(gstruct.IgnoreExtras, gstruct.Fields{
"UID": gomega.Equal(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...),
),
}))
}
14 changes: 14 additions & 0 deletions pkg/handlers/aws/mutation/ami/tests/generate_patches.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ func TestControlPlaneGeneratePatches(
ValueMatcher: gomega.Equal("testOS"),
},
},
UnexpectedPatchMatchers: []capitest.JSONPatchMatcher{
{
Operation: "add",
Path: "/spec/template/spec/ami/id",
ValueMatcher: gomega.Equal(""),
},
},
},
)
}
Expand Down Expand Up @@ -157,6 +164,13 @@ func TestWorkerGeneratePatches(
ValueMatcher: gomega.Equal("testOS"),
},
},
UnexpectedPatchMatchers: []capitest.JSONPatchMatcher{
{
Operation: "add",
Path: "/spec/template/spec/ami/id",
ValueMatcher: gomega.Equal(""),
},
},
},
)
}

0 comments on commit 0cc2133

Please sign in to comment.