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

Commit

Permalink
improve-factory
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 27, 2023
1 parent 69b0cb8 commit a62c817
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
4 changes: 4 additions & 0 deletions pkg/apis/testharness/v1beta1/test_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ type Assert struct {
}

type Options struct {
Kind string `json:"kind,omitempty"`
ApiVersion string `json:"apiVersion,omitempty"`

Check failure on line 170 in pkg/apis/testharness/v1beta1/test_types.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: struct field ApiVersion should be APIVersion (revive)
Name string `json:"name,omitempty"`
Namespace string `json:"namespace,omitempty"`
AssertArray []AssertArray `json:"arrays,omitempty"`
}

Expand Down
30 changes: 28 additions & 2 deletions pkg/test/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit a62c817

Please sign in to comment.