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

Commit

Permalink
fix changes
Browse files Browse the repository at this point in the history
Signed-off-by: Shubham Gupta <iamshubhamgupta2001@gmail.com>
  • Loading branch information
shubham-cmyk committed Sep 28, 2023
1 parent a62c817 commit 71dcc87
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
8 changes: 3 additions & 5 deletions pkg/apis/testharness/v1beta1/test_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,6 @@ type Assert struct {
}

type Options struct {
Kind string `json:"kind,omitempty"`
ApiVersion string `json:"apiVersion,omitempty"`
Name string `json:"name,omitempty"`
Namespace string `json:"namespace,omitempty"`
AssertArray []AssertArray `json:"arrays,omitempty"`
}

Expand All @@ -178,7 +174,9 @@ type AssertArray struct {
// Path indicates the location within the YAML file to extract data for verification.
Path string `json:"path"`
// Strategy defines how the extracted data should be compared against the Kubernetes resource.
Strategy Strategy `json:"strategy"`
Strategy Strategy `json:"strategy"`
Metadata metav1.ObjectMeta `json:"metadata,omitempty"`
metav1.TypeMeta
}

// UnmarshalJSON implements the json.Unmarshaller interface.
Expand Down
6 changes: 5 additions & 1 deletion pkg/apis/testharness/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 7 additions & 9 deletions pkg/test/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,20 +432,18 @@ func pathMatches(pattern, path string) bool {
return wildcard.Match(strings.TrimSuffix(pattern, "/"), path)
}

func metadataMatches(opt *harness.Options, obj client.Object) bool {
if opt.Name != "" && opt.Name != obj.GetName() {
return false
}
func metaTypeMatches(assertArray harness.AssertArray, obj client.Object) bool {
unstructuredObj := obj.(*unstructured.Unstructured).Object

if opt.Namespace != "" && opt.Namespace != obj.GetNamespace() {
if err := testutils.IsSubset(assertArray.Metadata, unstructuredObj["metadata"], "/", testutils.DefaultStrategyFactory()); err != nil {
return false
}

if opt.Kind != "" && opt.Kind != obj.GetObjectKind().GroupVersionKind().Kind {
if err := testutils.IsSubset(assertArray.TypeMeta.Kind, unstructuredObj["kind"], "/", testutils.DefaultStrategyFactory()); err != nil {
return false
}

if opt.ApiVersion != "" && opt.ApiVersion != obj.GetObjectKind().GroupVersionKind().GroupVersion().String() {
if err := testutils.IsSubset(assertArray.TypeMeta.APIVersion, unstructuredObj["apiVersion"], "/", testutils.DefaultStrategyFactory()); err != nil {
return false
}

Expand All @@ -456,9 +454,9 @@ func metadataMatches(opt *harness.Options, obj client.Object) bool {
func NewStrategyFactory(a asserts) func(path string) testutils.ArrayComparisonStrategy {
var strategyFactory func(path string) testutils.ArrayComparisonStrategy
recursiveStrategyFactory := func(path string) testutils.ArrayComparisonStrategy {
if a.options != nil && len(a.options.AssertArray) > 0 && metadataMatches(a.options, a.object) {
if a.options != nil && len(a.options.AssertArray) > 0 {
for _, assertArr := range a.options.AssertArray {
if pathMatches(assertArr.Path, path) {
if pathMatches(assertArr.Path, path) && metaTypeMatches(assertArr, a.object) {
switch assertArr.Strategy {
case harness.StrategyExact:
return testutils.StrategyExact(path, strategyFactory)
Expand Down
6 changes: 3 additions & 3 deletions pkg/test/utils/subset.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
type ArrayComparisonStrategyFactory = func(path string) ArrayComparisonStrategy
type ArrayComparisonStrategy = func(expectedData, actualData interface{}) error

func defaultStrategyFactory() ArrayComparisonStrategyFactory {
func DefaultStrategyFactory() ArrayComparisonStrategyFactory {
return alwaysExact
}

func alwaysExact(path string) ArrayComparisonStrategy {
return StrategyExact(path, defaultStrategyFactory())
return StrategyExact(path, DefaultStrategyFactory())
}

// SubsetError is an error type used by IsSubset for tracking the path in the struct.
Expand Down Expand Up @@ -61,7 +61,7 @@ func IsSubset(expected, actual interface{}, currentPath string, strategyFactory
switch reflect.TypeOf(expected).Kind() {
case reflect.Slice:
if strategyFactory == nil {
strategyFactory = defaultStrategyFactory()
strategyFactory = DefaultStrategyFactory()
}
strategy := strategyFactory(currentPath)

Expand Down

0 comments on commit 71dcc87

Please sign in to comment.