Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Commit

Permalink
add Test Case
Browse files Browse the repository at this point in the history
Signed-off-by: Shubham Gupta <[email protected]>
  • Loading branch information
shubham-cmyk committed Oct 2, 2023
1 parent 2cebdb9 commit b8e511a
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pkg/test/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,10 +433,10 @@ func pathMatches(pattern, path string) bool {
}

func metaTypeMatches(assertArray harness.AssertArray, obj client.Object) bool {
unstructuredObj := obj.(*unstructured.Unstructured).Object

if assertArray.Match != nil {
if err := testutils.IsSubset(assertArray.Match, unstructuredObj, "/", testutils.DefaultStrategyFactory()); err != nil {
expected, _ := runtime.DefaultUnstructuredConverter.ToUnstructured(assertArray.Match)

Check failure on line 437 in pkg/test/step.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `runtime.DefaultUnstructuredConverter.ToUnstructured` is not checked (errcheck)
actual, _ := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)

Check failure on line 438 in pkg/test/step.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `runtime.DefaultUnstructuredConverter.ToUnstructured` is not checked (errcheck)
if err := testutils.IsSubset(expected, actual, "/", testutils.DefaultStrategyFactory()); err != nil {
return false
}
}
Expand Down
100 changes: 100 additions & 0 deletions pkg/test/step_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -396,3 +397,102 @@ func TestPopulateObjectsByFileName(t *testing.T) {
})
}
}

func TestMetaTypeMatches(t *testing.T) {
tests := []struct {
name string
assertArray harness.AssertArray
obj *metav1.PartialObjectMetadata
expectedBool bool
}{
{
name: "Matching Subset",
assertArray: harness.AssertArray{
Match: &metav1.PartialObjectMetadata{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"key": "value",
},
},
},
},
obj: &metav1.PartialObjectMetadata{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"key": "value",
"foo": "bar",
},
},
},
expectedBool: true,
},
{
name: "Non-matching Subset",
assertArray: harness.AssertArray{
Match: &metav1.PartialObjectMetadata{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"key": "wrongValue",
},
},
},
},
obj: &metav1.PartialObjectMetadata{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"key": "value",
"foo": "bar",
},
},
},
expectedBool: false,
},
{
name: "Empty Annotations in Match",
assertArray: harness.AssertArray{
Match: &metav1.PartialObjectMetadata{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{},
},
},
},
obj: &metav1.PartialObjectMetadata{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"key": "value",
"foo": "bar",
},
},
},
expectedBool: true,
},
{
name: "Using Labels for Matching",
assertArray: harness.AssertArray{
Match: &metav1.PartialObjectMetadata{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"app": "test",
},
},
},
},
obj: &metav1.PartialObjectMetadata{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"app": "test",
"env": "prod",
},
},
},
expectedBool: true,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := metaTypeMatches(test.assertArray, test.obj)
assert.Equal(t, test.expectedBool, result)
})
}
}

0 comments on commit b8e511a

Please sign in to comment.