From a85752a5e81f59b6a392cb70a861f2df5a7c94ff Mon Sep 17 00:00:00 2001 From: Kumar Mallikarjuna Date: Tue, 3 Dec 2024 13:25:22 +0530 Subject: [PATCH] chore: make linter happy and initialize Programs only if assertions are present Signed-off-by: Kumar Mallikarjuna --- pkg/apis/testharness/v1beta1/expression.go | 11 ++++++----- pkg/test/step.go | 8 +++++--- pkg/test/utils/kubernetes.go | 1 - 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/pkg/apis/testharness/v1beta1/expression.go b/pkg/apis/testharness/v1beta1/expression.go index 6c265099..0739de2f 100644 --- a/pkg/apis/testharness/v1beta1/expression.go +++ b/pkg/apis/testharness/v1beta1/expression.go @@ -33,15 +33,16 @@ func (t *TestResourceRef) BuildResourceReference() (namespacedName types.Namespa func (t *TestResourceRef) Validate() error { apiVersionSplit := strings.Split(t.APIVersion, "/") - if t.APIVersion == "" || (len(apiVersionSplit) != 1 && len(apiVersionSplit) != 2) { + switch { + case t.APIVersion == "" || (len(apiVersionSplit) != 1 && len(apiVersionSplit) != 2): return fmt.Errorf("apiVersion '%v' not of the format (/)", t.APIVersion) - } else if t.Kind == "" { + case t.Kind == "": return errors.New("kind not specified") - } else if t.Namespace == "" { + case t.Namespace == "": return errors.New("namespace not specified") - } else if t.Name == "" { + case t.Name == "": return errors.New("name not specified") - } else if t.Ref == "" { + case t.Ref == "": return errors.New("ref not specified") } diff --git a/pkg/test/step.go b/pkg/test/step.go index 9557ef83..5043f7bd 100644 --- a/pkg/test/step.go +++ b/pkg/test/step.go @@ -426,7 +426,7 @@ func (s *Step) CheckAssertExpressions( return []error{err} } - return testutils.RunAssertExpressions(ctx, s.Logger, client, s.Programs, resourceRefs, assertAny, assertAll) + return testutils.RunAssertExpressions(ctx, client, s.Programs, resourceRefs, assertAny, assertAll) } // Check checks if the resources defined in Asserts and Errors are in the correct state. @@ -533,8 +533,6 @@ func (s *Step) String() string { // if seen, mark a test immediately failed. // - All other YAML files are considered resources to create. func (s *Step) LoadYAML(file string) error { - s.Programs = make(map[string]cel.Program) - skipFile, objects, err := s.loadOrSkipFile(file) if skipFile || err != nil { return err @@ -574,6 +572,10 @@ func (s *Step) LoadYAML(file string) error { return fmt.Errorf("failed to load TestAssert object from %s: %w", file, err) } + if len(assertions) > 0 { + s.Programs = make(map[string]cel.Program) + } + for _, assertion := range assertions { if prg, err := assertion.BuildProgram(env); err != nil { errs = append(errs, err) diff --git a/pkg/test/utils/kubernetes.go b/pkg/test/utils/kubernetes.go index 23d59da4..4f2d0a24 100644 --- a/pkg/test/utils/kubernetes.go +++ b/pkg/test/utils/kubernetes.go @@ -1222,7 +1222,6 @@ func RunAssertCommands(ctx context.Context, logger Logger, namespace string, com // RunAssertExpressions evaluates a set of CEL expressions specified as AnyAllExpressions func RunAssertExpressions( ctx context.Context, - logger Logger, cl client.Client, programs map[string]cel.Program, resourceRefs []harness.TestResourceRef,