Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[KEP-0009] feat: add expression based assertions #576

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

kumar-mallikarjuna
Copy link
Contributor

What this PR does / why we need it:
This PR adds CEL-expression based assertions to TestAsserts. See https://github.com/kudobuilder/kuttl/blob/main/keps/0009-expression-based-assertions.md for more details.

Fixes #562

Copy link
Member

@porridge porridge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, but needs a few changes.

pkg/apis/testharness/v1beta1/test_types.go Outdated Show resolved Hide resolved
pkg/apis/testharness/v1beta1/test_types.go Outdated Show resolved Hide resolved
pkg/test/utils/kubernetes.go Outdated Show resolved Hide resolved
@@ -1321,3 +1398,29 @@ func Kubeconfig(cfg *rest.Config, w io.Writer) error {
},
}, w)
}

func constructGVK(apiVersion, kind string) schema.GroupVersionKind {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nice helper method, but it would be even more powerful it you

  • changed it from a bare func to a method of TestResourceRef
  • made it also internally create an *Unstructured:
      	gvk := constructGVK(resourceRef.ApiVersion, resourceRef.Kind)
      	referencedResource := &unstructured.Unstructured{}
      	referencedResource.SetGroupVersionKind(gvk)
    
  • and called it NewUnstructured or similar.

Thinking about it, it might make sense to have it also return a NamespacedName and an error in case crucial fields (like name and kind) are missing. On one hand, if we don't do any validation here, then the client would probably catch such garbage later. But on the other hand, we could use such validation function earlier, just after loading the assert files, in order to give user feedback on critical errors ASAP.

When you're at it, please also add a couple of test cases for it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NamespacedName/*unstructured.Unstructured would be required when we evaluate the expressions. If the goal is also to validate, we should have two separate methods:

func (t *TestResourceRef) BuildResourceReference() (namespacedName types.NamespacedName, referencedResource *unstructured.Unstructured)

func (t *TestResourceRef) Validate() (error)

pkg/test/utils/kubernetes.go Outdated Show resolved Hide resolved
variables[resourceRef.Id] = referencedResource.Object
}

env, err := cel.NewEnv()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling cel.NewEnv() may not be enough, you probably want to enable a couple of options/libs.

This PR adds CEL-expression based assertions to `TestAsserts`. See https://github.com/kudobuilder/kuttl/blob/main/keps/0009-expression-based-assertions.md for more details.

Signed-off-by: Kumar Mallikarjuna <kumarmallikarjuna.work@gmail.com>
Signed-off-by: Kumar Mallikarjuna <kumarmallikarjuna.work@gmail.com>
Signed-off-by: Kumar Mallikarjuna <kumarmallikarjuna.work@gmail.com>
Signed-off-by: Kumar Mallikarjuna <kumarmallikarjuna.work@gmail.com>
Signed-off-by: Kumar Mallikarjuna <kumarmallikarjuna.work@gmail.com>
Signed-off-by: Kumar Mallikarjuna <kumarmallikarjuna.work@gmail.com>
Signed-off-by: Kumar Mallikarjuna <kumarmallikarjuna.work@gmail.com>
Signed-off-by: Kumar Mallikarjuna <kumarmallikarjuna.work@gmail.com>
…re present

Signed-off-by: Kumar Mallikarjuna <kumarmallikarjuna.work@gmail.com>
pkg/apis/testharness/v1beta1/expression.go Outdated Show resolved Hide resolved
pkg/apis/testharness/v1beta1/expression.go Outdated Show resolved Hide resolved
pkg/apis/testharness/v1beta1/expression.go Outdated Show resolved Hide resolved
pkg/test/step.go Outdated Show resolved Hide resolved
pkg/test/utils/kubernetes.go Outdated Show resolved Hide resolved
pkg/test/utils/kubernetes.go Outdated Show resolved Hide resolved
pkg/test/utils/expression.go Outdated Show resolved Hide resolved

variables := make(map[string]interface{})
for _, resourceRef := range resourceRefs {
namespacedName, referencedResource := resourceRef.BuildResourceReference()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking more about the namespace, this should probably default to the current test's namespace if not specified in the assert file 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about cluster-scoped resources? 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to check. I think some clients ignore the namespace if it's specified when fetching cluster-scoped resources, so while ugly and confusing, we could keep it for simplicity. If this client is more picky, then we'd have to look into discovery to distinguish cluster-scoped from namespaced ones 🤔

pkg/test/utils/kubernetes.go Outdated Show resolved Hide resolved
Signed-off-by: Kumar Mallikarjuna <kumarmallikarjuna.work@gmail.com>
Signed-off-by: Kumar Mallikarjuna <kumarmallikarjuna.work@gmail.com>
Signed-off-by: Kumar Mallikarjuna <kumarmallikarjuna.work@gmail.com>
Signed-off-by: Kumar Mallikarjuna <kumarmallikarjuna.work@gmail.com>
@kumar-mallikarjuna
Copy link
Contributor Author

Thanks for the quick review @porridge . I'll add the tests tomorrow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add CEL expression support
3 participants