Skip to content

Commit

Permalink
tests(e2e): allow passing options when decoding files
Browse files Browse the repository at this point in the history
Signed-off-by: Philippe Scorsolini <p.scorsolini@gmail.com>
(cherry picked from commit 3a401f8)
  • Loading branch information
phisco committed Apr 5, 2024
1 parent 61dacbf commit b66da63
Showing 1 changed file with 21 additions and 28 deletions.
49 changes: 21 additions & 28 deletions test/e2e/funcs/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ func DeploymentBecomesAvailableWithin(d time.Duration, namespace, name string) f

// ResourcesCreatedWithin fails a test if the supplied resources are not found
// to exist within the supplied duration.
func ResourcesCreatedWithin(d time.Duration, dir, pattern string) features.Func {
func ResourcesCreatedWithin(d time.Duration, dir, pattern string, options ...decoder.DecodeOption) features.Func {
return func(ctx context.Context, t *testing.T, c *envconf.Config) context.Context {

rs, err := decoder.DecodeAllFiles(ctx, os.DirFS(dir), pattern)
rs, err := decoder.DecodeAllFiles(ctx, os.DirFS(dir), pattern, options...)
if err != nil {
t.Error(err)
return ctx
Expand Down Expand Up @@ -169,10 +169,10 @@ func ResourceCreatedWithin(d time.Duration, o k8s.Object) features.Func {

// ResourcesDeletedWithin fails a test if the supplied resources are not deleted
// within the supplied duration.
func ResourcesDeletedWithin(d time.Duration, dir, pattern string) features.Func {
func ResourcesDeletedWithin(d time.Duration, dir, pattern string, options ...decoder.DecodeOption) features.Func {
return func(ctx context.Context, t *testing.T, c *envconf.Config) context.Context {

rs, err := decoder.DecodeAllFiles(ctx, os.DirFS(dir), pattern)
rs, err := decoder.DecodeAllFiles(ctx, os.DirFS(dir), pattern, options...)
if err != nil {
t.Error(err)
return ctx
Expand Down Expand Up @@ -313,10 +313,10 @@ var NotFound = notFound{}
// ResourcesHaveFieldValueWithin fails a test if the supplied resources do not
// have the supplied value at the supplied field path within the supplied
// duration. The supplied 'want' value must cmp.Equal the actual value.
func ResourcesHaveFieldValueWithin(d time.Duration, dir, pattern, path string, want any) features.Func {
func ResourcesHaveFieldValueWithin(d time.Duration, dir, pattern, path string, want any, options ...decoder.DecodeOption) features.Func {
return func(ctx context.Context, t *testing.T, c *envconf.Config) context.Context {

rs, err := decoder.DecodeAllFiles(ctx, os.DirFS(dir), pattern)
rs, err := decoder.DecodeAllFiles(ctx, os.DirFS(dir), pattern, options...)
if err != nil {
t.Error(err)
return ctx
Expand Down Expand Up @@ -433,7 +433,7 @@ type claimCtxKey struct{}

// ApplyClaim applies the claim stored in the given folder and file
// and stores it in the test context for later retrival if needed
func ApplyClaim(manager, dir, cm string) features.Func {
func ApplyClaim(manager, dir, cm string, options ...decoder.DecodeOption) features.Func {
return func(ctx context.Context, t *testing.T, c *envconf.Config) context.Context {
dfs := os.DirFS(dir)

Expand All @@ -444,6 +444,7 @@ func ApplyClaim(manager, dir, cm string) features.Func {
}

objs, err := decoder.DecodeAllFiles(ctx, dfs, cm)
objs, err := decoder.DecodeAllFiles(ctx, dfs, cm, options...)

Check failure on line 447 in test/e2e/funcs/feature.go

View workflow job for this annotation

GitHub Actions / codeql

no new variables on left side of :=

Check failure on line 447 in test/e2e/funcs/feature.go

View workflow job for this annotation

GitHub Actions / codeql

no new variables on left side of :=

Check failure on line 447 in test/e2e/funcs/feature.go

View workflow job for this annotation

GitHub Actions / e2e-tests (base)

no new variables on left side of :=

Check failure on line 447 in test/e2e/funcs/feature.go

View workflow job for this annotation

GitHub Actions / lint

no new variables on left side of := (typecheck)

Check failure on line 447 in test/e2e/funcs/feature.go

View workflow job for this annotation

GitHub Actions / lint

no new variables on left side of :=) (typecheck)

Check failure on line 447 in test/e2e/funcs/feature.go

View workflow job for this annotation

GitHub Actions / lint

no new variables on left side of :=) (typecheck)

Check failure on line 447 in test/e2e/funcs/feature.go

View workflow job for this annotation

GitHub Actions / e2e-tests (environment-configs)

no new variables on left side of :=

Check failure on line 447 in test/e2e/funcs/feature.go

View workflow job for this annotation

GitHub Actions / e2e-tests (usage)

no new variables on left side of :=

Check failure on line 447 in test/e2e/funcs/feature.go

View workflow job for this annotation

GitHub Actions / e2e-tests (ssa-claims)

no new variables on left side of :=

Check failure on line 447 in test/e2e/funcs/feature.go

View workflow job for this annotation

GitHub Actions / publish-artifacts

no new variables on left side of :=

Check failure on line 447 in test/e2e/funcs/feature.go

View workflow job for this annotation

GitHub Actions / publish-artifacts

no new variables on left side of :=
if err != nil {
t.Error(err)
return ctx
Expand All @@ -469,27 +470,19 @@ func ApplyClaim(manager, dir, cm string) features.Func {
// SetAnnotationMutateOption returns a DecodeOption that sets the supplied
// annotation on the decoded object.
func SetAnnotationMutateOption(key, value string) decoder.DecodeOption {
return decoder.MutateOption(func(o k8s.Object) error {
a := o.GetAnnotations()
if a == nil {
a = map[string]string{}
}
a[key] = value
o.SetAnnotations(a)
return nil
})
return decoder.MutateAnnotations(map[string]string{key: value})
}

// ResourcesFailToApply applies all manifests under the supplied directory that
// match the supplied glob pattern (e.g. *.yaml). It uses server-side apply -
// fields are managed by the supplied field manager. It fails the test if any
// supplied resource _can_ be applied successfully - use it to test that the API
// server should reject a resource.
func ResourcesFailToApply(manager, dir, pattern string) features.Func {
func ResourcesFailToApply(manager, dir, pattern string, options ...decoder.DecodeOption) features.Func {
return func(ctx context.Context, t *testing.T, c *envconf.Config) context.Context {
dfs := os.DirFS(dir)

if err := decoder.DecodeEachFile(ctx, dfs, pattern, ApplyHandler(c.Client().Resources(), manager)); err == nil {
if err := decoder.DecodeEachFile(ctx, dfs, pattern, ApplyHandler(c.Client().Resources(), manager), options...); err == nil {
// TODO(negz): Ideally we'd say which one.
t.Error("Resource applied successfully, but should have failed")
return ctx
Expand Down Expand Up @@ -525,11 +518,11 @@ func ApplyHandler(r *resources.Resources, manager string, osh ...onSuccessHandle
// DeleteResources deletes (from the environment) all resources defined by the
// manifests under the supplied directory that match the supplied glob pattern
// (e.g. *.yaml).
func DeleteResources(dir, pattern string) features.Func {
func DeleteResources(dir, pattern string, options ...decoder.DecodeOption) features.Func {
return func(ctx context.Context, t *testing.T, c *envconf.Config) context.Context {
dfs := os.DirFS(dir)

if err := decoder.DecodeEachFile(ctx, dfs, pattern, decoder.DeleteHandler(c.Client().Resources())); err != nil {
if err := decoder.DecodeEachFile(ctx, dfs, pattern, decoder.DeleteHandler(c.Client().Resources()), options...); err != nil {
t.Fatal(err)
return ctx
}
Expand Down Expand Up @@ -620,11 +613,11 @@ func CompositeUnderTestMustNotChangeWithin(d time.Duration) features.Func {

// CompositeResourceMustMatchWithin assert that a composite referred by the given file
// must be matched by the given function within the given timeout
func CompositeResourceMustMatchWithin(d time.Duration, dir, claimFile string, match func(xr *composite.Unstructured) bool) features.Func {
func CompositeResourceMustMatchWithin(d time.Duration, dir, claimFile string, match func(xr *composite.Unstructured) bool, options ...decoder.DecodeOption) features.Func {
return func(ctx context.Context, t *testing.T, c *envconf.Config) context.Context {
cm := &claim.Unstructured{}

if err := decoder.DecodeFile(os.DirFS(dir), claimFile, cm); err != nil {
if err := decoder.DecodeFile(os.DirFS(dir), claimFile, cm, options...); err != nil {
t.Error(err)
return ctx
}
Expand Down Expand Up @@ -670,11 +663,11 @@ func CompositeResourceMustMatchWithin(d time.Duration, dir, claimFile string, ma
// CompositeResourceHasFieldValueWithin asserts that the XR referred to by the
// claim in the given file has the specified value at the specified path within
// the specified time.
func CompositeResourceHasFieldValueWithin(d time.Duration, dir, claimFile, path string, want any) features.Func {
func CompositeResourceHasFieldValueWithin(d time.Duration, dir, claimFile, path string, want any, options ...decoder.DecodeOption) features.Func {
return func(ctx context.Context, t *testing.T, c *envconf.Config) context.Context {
cm := &claim.Unstructured{}

if err := decoder.DecodeFile(os.DirFS(dir), claimFile, cm); err != nil {
if err := decoder.DecodeFile(os.DirFS(dir), claimFile, cm, options...); err != nil {
t.Error(err)
return ctx
}
Expand Down Expand Up @@ -739,10 +732,10 @@ func CompositeResourceHasFieldValueWithin(d time.Duration, dir, claimFile, path
// ComposedResourcesHaveFieldValueWithin fails a test if the composed
// resources created by the claim does not have the supplied value at the
// supplied path within the supplied duration.
func ComposedResourcesHaveFieldValueWithin(d time.Duration, dir, file, path string, want any, filter func(object k8s.Object) bool) features.Func { //nolint:gocyclo // Not too much over.
func ComposedResourcesHaveFieldValueWithin(d time.Duration, dir, file, path string, want any, filter func(object k8s.Object) bool, options ...decoder.DecodeOption) features.Func { //nolint:gocyclo // Not too much over.
return func(ctx context.Context, t *testing.T, c *envconf.Config) context.Context {
cm := &claim.Unstructured{}
if err := decoder.DecodeFile(os.DirFS(dir), file, cm); err != nil {
if err := decoder.DecodeFile(os.DirFS(dir), file, cm, options...); err != nil {
t.Error(err)
return ctx
}
Expand Down Expand Up @@ -965,11 +958,11 @@ func LogResources(list k8s.ObjectList, listOptions ...resources.ListOption) feat
// defined by the manifests under the supplied directory that match the supplied
// glob pattern (e.g. *.yaml) and verifies that they are blocked by the usage
// webhook.
func DeletionBlockedByUsageWebhook(dir, pattern string) features.Func {
func DeletionBlockedByUsageWebhook(dir, pattern string, options ...decoder.DecodeOption) features.Func {
return func(ctx context.Context, t *testing.T, c *envconf.Config) context.Context {
dfs := os.DirFS(dir)

err := decoder.DecodeEachFile(ctx, dfs, pattern, decoder.DeleteHandler(c.Client().Resources()))
err := decoder.DecodeEachFile(ctx, dfs, pattern, decoder.DeleteHandler(c.Client().Resources()), options...)
if err == nil {
t.Fatal("expected the usage webhook to deny the request but deletion succeeded")
return ctx
Expand Down

0 comments on commit b66da63

Please sign in to comment.