Skip to content

Commit

Permalink
chore: incorporate review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Kumar Mallikarjuna <kumarmallikarjuna.work@gmail.com>
  • Loading branch information
kumar-mallikarjuna committed Dec 3, 2024
1 parent a85752a commit 212b993
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 59 deletions.
19 changes: 1 addition & 18 deletions pkg/apis/testharness/v1beta1/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"strings"

"github.com/google/cel-go/cel"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -18,7 +17,7 @@ func (t *TestResourceRef) BuildResourceReference() (namespacedName types.Namespa
Version: apiVersionSplit[len(apiVersionSplit)-1],
Kind: t.Kind,
}
if len(t.APIVersion) > 1 {
if len(apiVersionSplit) > 1 {
gvk.Group = apiVersionSplit[0]
}
referencedResource.SetGroupVersionKind(gvk)
Expand All @@ -38,8 +37,6 @@ func (t *TestResourceRef) Validate() error {
return fmt.Errorf("apiVersion '%v' not of the format (<group>/)<version>", t.APIVersion)
case t.Kind == "":
return errors.New("kind not specified")
case t.Namespace == "":
return errors.New("namespace not specified")
case t.Name == "":
return errors.New("name not specified")
case t.Ref == "":
Expand All @@ -59,17 +56,3 @@ func (t *TestResourceRef) String() string {
t.Ref,
)
}

func (t *Assertion) BuildProgram(env *cel.Env) (cel.Program, error) {
ast, issues := env.Compile(t.CELExpression)
if issues != nil && issues.Err() != nil {
return nil, fmt.Errorf("type-check error: %s", issues.Err())
}

prg, err := env.Program(ast)
if err != nil {
return nil, fmt.Errorf("program construction error: %w", err)
}

return prg, nil
}
16 changes: 15 additions & 1 deletion pkg/test/utils/expression.go → pkg/expressions/cel.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package utils
package expressions

import (
"fmt"
Expand All @@ -8,6 +8,20 @@ import (
"github.com/kudobuilder/kuttl/pkg/apis/testharness/v1beta1"
)

func BuildProgram(expr string, env *cel.Env) (cel.Program, error) {
ast, issues := env.Compile(expr)
if issues != nil && issues.Err() != nil {
return nil, fmt.Errorf("type-check error: %s", issues.Err())
}

prg, err := env.Program(ast)
if err != nil {
return nil, fmt.Errorf("program construction error: %w", err)
}

return prg, nil
}

func BuildEnv(resourceRefs []v1beta1.TestResourceRef) (*cel.Env, error) {
env, err := cel.NewEnv()
if err != nil {
Expand Down
17 changes: 15 additions & 2 deletions pkg/test/case.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ func (t *Case) Run(test *testing.T, ts *report.Testsuite) {
continue
}

cl, err = testutils.NewClient(testStep.Kubeconfig, testStep.Context)(false)
cl, err = newClient(testStep.Kubeconfig, testStep.Context)(false)
if err != nil {
setupReport.Failure = report.NewFailure(err.Error(), nil)
ts.AddTestcase(setupReport)
Expand All @@ -364,7 +364,7 @@ func (t *Case) Run(test *testing.T, ts *report.Testsuite) {
tc := report.NewCase("step " + testStep.String())
testStep.Client = t.Client
if testStep.Kubeconfig != "" {
testStep.Client = testutils.NewClient(testStep.Kubeconfig, testStep.Context)
testStep.Client = newClient(testStep.Kubeconfig, testStep.Context)
}
testStep.DiscoveryClient = t.DiscoveryClient
if testStep.Kubeconfig != "" {
Expand Down Expand Up @@ -532,6 +532,19 @@ func (t *Case) LoadTestSteps() error {
return nil
}

func newClient(kubeconfig, context string) func(bool) (client.Client, error) {
return func(bool) (client.Client, error) {
config, err := k8s.BuildConfigWithContext(kubeconfig, context)
if err != nil {
return nil, err
}

return testutils.NewRetryClient(config, client.Options{
Scheme: testutils.Scheme(),
})
}
}

func newDiscoveryClient(kubeconfig, context string) func() (discovery.DiscoveryInterface, error) {
return func() (discovery.DiscoveryInterface, error) {
config, err := k8s.BuildConfigWithContext(kubeconfig, context)
Expand Down
21 changes: 18 additions & 3 deletions pkg/test/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

harness "github.com/kudobuilder/kuttl/pkg/apis/testharness/v1beta1"
"github.com/kudobuilder/kuttl/pkg/env"
"github.com/kudobuilder/kuttl/pkg/expressions"
kfile "github.com/kudobuilder/kuttl/pkg/file"
"github.com/kudobuilder/kuttl/pkg/http"
testutils "github.com/kudobuilder/kuttl/pkg/test/utils"
Expand Down Expand Up @@ -426,7 +427,21 @@ func (s *Step) CheckAssertExpressions(
return []error{err}
}

return testutils.RunAssertExpressions(ctx, client, s.Programs, resourceRefs, assertAny, assertAll)
variables := make(map[string]interface{})
for _, resourceRef := range resourceRefs {
namespacedName, referencedResource := resourceRef.BuildResourceReference()
if err := client.Get(
ctx,
namespacedName,
referencedResource,
); err != nil {
return []error{fmt.Errorf("failed to get referenced resource '%v': %w", namespacedName, err)}
}

variables[resourceRef.Ref] = referencedResource.Object
}

return testutils.RunAssertExpressions(s.Programs, variables, assertAny, assertAll)
}

// Check checks if the resources defined in Asserts and Errors are in the correct state.
Expand Down Expand Up @@ -567,7 +582,7 @@ func (s *Step) LoadYAML(file string) error {
assertions = append(assertions, s.Assert.AssertAny...)
assertions = append(assertions, s.Assert.AssertAll...)

env, err := testutils.BuildEnv(s.Assert.ResourceRefs)
env, err := expressions.BuildEnv(s.Assert.ResourceRefs)
if err != nil {
return fmt.Errorf("failed to load TestAssert object from %s: %w", file, err)
}
Expand All @@ -577,7 +592,7 @@ func (s *Step) LoadYAML(file string) error {
}

for _, assertion := range assertions {
if prg, err := assertion.BuildProgram(env); err != nil {
if prg, err := expressions.BuildProgram(assertion.CELExpression, env); err != nil {
errs = append(errs, err)
} else {
s.Programs[assertion.CELExpression] = prg
Expand Down
38 changes: 4 additions & 34 deletions pkg/test/utils/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ import (
"github.com/kudobuilder/kuttl/pkg/apis"
harness "github.com/kudobuilder/kuttl/pkg/apis/testharness/v1beta1"
"github.com/kudobuilder/kuttl/pkg/env"
"github.com/kudobuilder/kuttl/pkg/k8s"
)

// ensure that we only add to the scheme once.
Expand Down Expand Up @@ -1114,7 +1113,7 @@ func RunCommand(ctx context.Context, namespace string, cmd harness.Command, cwd

kuttlENV := make(map[string]string)
kuttlENV["NAMESPACE"] = namespace
kuttlENV["KUBECONFIG"] = KubeconfigPath(actualDir, kubeconfigOverride)
kuttlENV["KUBECONFIG"] = kubeconfigPath(actualDir, kubeconfigOverride)
kuttlENV["PATH"] = fmt.Sprintf("%s/bin/:%s", actualDir, os.Getenv("PATH"))

// by default testsuite timeout is the command timeout
Expand Down Expand Up @@ -1183,7 +1182,7 @@ func RunCommand(ctx context.Context, namespace string, cmd harness.Command, cwd
return nil, nil
}

func KubeconfigPath(actualDir, override string) string {
func kubeconfigPath(actualDir, override string) string {
if override != "" {
if filepath.IsAbs(override) {
return override
Expand Down Expand Up @@ -1221,32 +1220,16 @@ 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,
cl client.Client,
programs map[string]cel.Program,
resourceRefs []harness.TestResourceRef,
variables map[string]interface{},
assertAny,
assertAll []*harness.Assertion,
) []error {
errs := []error{}
var errs []error
if len(assertAny) == 0 && len(assertAll) == 0 {
return errs
}

variables := make(map[string]interface{})
for _, resourceRef := range resourceRefs {
namespacedName, referencedResource := resourceRef.BuildResourceReference()
if err := cl.Get(
ctx,
namespacedName,
referencedResource,
); err != nil {
return []error{fmt.Errorf("failed to get referenced resource '%v': %w", namespacedName, err)}
}

variables[resourceRef.Ref] = referencedResource.Object
}

var anyExpressionsEvaluation, allExpressionsEvaluation []error
for _, expr := range assertAny {
prg, ok := programs[expr.CELExpression]
Expand Down Expand Up @@ -1393,16 +1376,3 @@ func Kubeconfig(cfg *rest.Config, w io.Writer) error {
},
}, w)
}

func NewClient(kubeconfig, context string) func(bool) (client.Client, error) {
return func(bool) (client.Client, error) {
config, err := k8s.BuildConfigWithContext(kubeconfig, context)
if err != nil {
return nil, err
}

return NewRetryClient(config, client.Options{
Scheme: Scheme(),
})
}
}
2 changes: 1 addition & 1 deletion pkg/test/utils/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func TestKubeconfigPath(t *testing.T) {
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
result := KubeconfigPath(tt.path, tt.override)
result := kubeconfigPath(tt.path, tt.override)
assert.Equal(t, tt.expected, result)
})
}
Expand Down

0 comments on commit 212b993

Please sign in to comment.