From a62c81720ffab46d0d7aa360334178c5896855c3 Mon Sep 17 00:00:00 2001 From: Shubham Gupta Date: Wed, 27 Sep 2023 21:20:18 +0530 Subject: [PATCH] improve-factory Signed-off-by: Shubham Gupta --- pkg/apis/testharness/v1beta1/test_types.go | 4 +++ pkg/test/step.go | 30 ++++++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/pkg/apis/testharness/v1beta1/test_types.go b/pkg/apis/testharness/v1beta1/test_types.go index 04669cf0..15f104bf 100644 --- a/pkg/apis/testharness/v1beta1/test_types.go +++ b/pkg/apis/testharness/v1beta1/test_types.go @@ -166,6 +166,10 @@ 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"` } diff --git a/pkg/test/step.go b/pkg/test/step.go index ca5de1d0..b845de13 100644 --- a/pkg/test/step.go +++ b/pkg/test/step.go @@ -11,6 +11,7 @@ import ( "testing" "time" + wildcard "github.com/IGLOU-EU/go-wildcard" k8serrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" @@ -426,13 +427,38 @@ func (s *Step) CheckResourceAbsent(expected runtime.Object, namespace string) er return fmt.Errorf("resource %s %s (and %d other resources) matched error assertion", unexpectedObjects[0].GroupVersionKind(), unexpectedObjects[0].GetName(), len(unexpectedObjects)-1) } +// pathMatches checks if the given path matches the pattern. +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 + } + + if opt.Namespace != "" && opt.Namespace != obj.GetNamespace() { + return false + } + + if opt.Kind != "" && opt.Kind != obj.GetObjectKind().GroupVersionKind().Kind { + return false + } + + if opt.ApiVersion != "" && opt.ApiVersion != obj.GetObjectKind().GroupVersionKind().GroupVersion().String() { + return false + } + + return true +} + // Build StrategyFactory for IsSubset 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 { + if a.options != nil && len(a.options.AssertArray) > 0 && metadataMatches(a.options, a.object) { for _, assertArr := range a.options.AssertArray { - if assertArr.Path == path { + if pathMatches(assertArr.Path, path) { switch assertArr.Strategy { case harness.StrategyExact: return testutils.StrategyExact(path, strategyFactory)