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

store assert/vaildate rules in Node's metas #654

Merged
merged 3 commits into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 48 additions & 64 deletions pkg/workspace/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ package workspace

import (
"fmt"

"github.com/k14s/starlark-go/starlark"
"github.com/vmware-tanzu/carvel-ytt/pkg/filepos"
"github.com/vmware-tanzu/carvel-ytt/pkg/template"
"github.com/vmware-tanzu/carvel-ytt/pkg/yamlmeta"
"github.com/vmware-tanzu/carvel-ytt/pkg/yamltemplate"
)

// Declare @assert/... annotation names
const (
AnnotationAssertValidate template.AnnotationName = "assert/validate"
)

// TODO: update go docs
// ProcessAndRunValidations takes a root Node, and threadName, and traverses the tree checking for assert annotations.
// Validations are processed and executed using the value of the annotated node as the parameter to the assertions.
//
Expand All @@ -26,14 +26,18 @@ func ProcessAndRunValidations(n yamlmeta.Node, threadName string) (AssertCheck,
if n == nil {
return AssertCheck{}, nil
}
err := yamlmeta.Walk(n, &assertGetter{})
if err != nil {
return AssertCheck{}, err
}

assertionChecker := newAssertChecker(threadName)
err := yamlmeta.Walk(n, assertionChecker)
assertionRunner := newAssertRunner(threadName)
err = yamlmeta.Walk(n, assertionRunner)
if err != nil {
return AssertCheck{}, err
}

return assertionChecker.chk, nil
return assertionRunner.chk, nil
}

// AssertCheck holds the resulting violations from executing Validations on a node.
Expand All @@ -59,21 +63,10 @@ func (ac *AssertCheck) HasViolations() bool {
return len(ac.Violations) > 0
}

type assertChecker struct {
thread *starlark.Thread
chk AssertCheck
}
type assertGetter struct {}

func newAssertChecker(threadName string) *assertChecker {
return &assertChecker{thread: &starlark.Thread{Name: threadName}, chk: AssertCheck{[]error{}}}
}

// Visit if `node` is annotated with `@assert/validate` (AnnotationAssertValidate).
// Checks, validates, and runs the validation Rules, any violations from running the assertions are collected.
//
// This visitor returns and error if any assertion is not well-formed,
// otherwise, returns nil.
func (a *assertChecker) Visit(node yamlmeta.Node) error {
// TODO: update go docs
func (a *assertGetter) Visit(node yamlmeta.Node) error {
gcheadle-vmware marked this conversation as resolved.
Show resolved Hide resolved
nodeAnnotations := template.NewAnnotations(node)
if !nodeAnnotations.Has(AnnotationAssertValidate) {
return nil
Expand All @@ -86,19 +79,16 @@ func (a *assertChecker) Visit(node yamlmeta.Node) error {
if syntaxErr != nil {
return syntaxErr
}
for _, rule := range rules {
err := rule.Validate(node, a.thread)
if err != nil {
a.chk.Violations = append(a.chk.Violations, err)
}
}
//store rules in node's meta
// TODO: not overriding any metas previously set
node.SetMeta("validations", rules)
}

return nil
}

func newRulesFromAssertValidateAnnotation(annotation template.NodeAnnotation) ([]Rule, error) {
var rules []Rule
func newRulesFromAssertValidateAnnotation(annotation template.NodeAnnotation) ([]yamlmeta.Rule, error) {
var rules []yamlmeta.Rule

if len(annotation.Kwargs) != 0 {
return nil, fmt.Errorf("Invalid @%s annotation - expected @%s to have 2-tuple as argument(s), but found keyword argument (by %s)", AnnotationAssertValidate, AnnotationAssertValidate, annotation.Position.AsCompactString())
Expand All @@ -122,58 +112,52 @@ func newRulesFromAssertValidateAnnotation(annotation template.NodeAnnotation) ([
if !ok {
return nil, fmt.Errorf("Invalid @%s annotation - expected second item in the 2-tuple to be an assertion function, but was %s (at %s)", AnnotationAssertValidate, ruleTuple[1].Type(), annotation.Position.AsCompactString())
}
rules = append(rules, Rule{
msg: message.GoString(),
assertion: lambda,
position: annotation.Position,
rules = append(rules, yamlmeta.Rule{
Msg: message.GoString(),
Assertion: lambda,
Position: annotation.Position,
})
}

return rules, nil
}

// A Rule represents an argument to an @assert/validate annotation;
// it contains a string description of what constitutes a valid value,
// and a function that asserts the rule against an actual value.
// One @assert/validate annotation can have multiple Rules.
type Rule struct {
msg string
assertion starlark.Callable
position *filepos.Position

type assertRunner struct {
thread *starlark.Thread
chk AssertCheck
}

func newAssertRunner(threadName string) *assertRunner {
return &assertRunner{thread: &starlark.Thread{Name: threadName}, chk: AssertCheck{[]error{}}}
}

// Validate runs the assertion from the Rule with the node's value as arguments.
// TODO: update go docs
// Visit if `node` is annotated with `@assert/validate` (AnnotationAssertValidate).
// Checks, validates, and runs the validation Rules, any violations from running the assertions are collected.
//
// Returns an error if the assertion returns False (not-None), or assert.fail()s.
// Otherwise, returns nil.
func (r Rule) Validate(node yamlmeta.Node, thread *starlark.Thread) error {
var key string
var nodeValue starlark.Value
switch typedNode := node.(type) {
case *yamlmeta.DocumentSet, *yamlmeta.Array, *yamlmeta.Map:
panic(fmt.Sprintf("@%s annotation at %s - not supported on %s at %s", AnnotationAssertValidate, r.position.AsCompactString(), yamlmeta.TypeName(node), node.GetPosition().AsCompactString()))
case *yamlmeta.MapItem:
key = fmt.Sprintf("%q", typedNode.Key)
nodeValue = yamltemplate.NewGoValueWithYAML(typedNode.Value).AsStarlarkValue()
case *yamlmeta.ArrayItem:
key = yamlmeta.TypeName(typedNode)
nodeValue = yamltemplate.NewGoValueWithYAML(typedNode.Value).AsStarlarkValue()
case *yamlmeta.Document:
key = yamlmeta.TypeName(typedNode)
nodeValue = yamltemplate.NewGoValueWithYAML(typedNode.Value).AsStarlarkValue()
// This visitor returns and error if any assertion is not well-formed,
// otherwise, returns nil.
func (a *assertRunner) Visit(node yamlmeta.Node) error {

//get rules in node's meta
metas := node.GetMeta("validations")
if metas == nil {
return nil
}

result, err := starlark.Call(thread, r.assertion, starlark.Tuple{nodeValue}, []starlark.Tuple{})
if err != nil {
return fmt.Errorf("%s (%s) requires %q; %s (by %s)", key, node.GetPosition().AsCompactString(), r.msg, err.Error(), r.position.AsCompactString())
rules, ok := metas.([]yamlmeta.Rule)
if !ok {
return nil
}

// in order to pass, the assertion must return True or None
if _, ok := result.(starlark.NoneType); !ok {
if !result.Truth() {
return fmt.Errorf("%s (%s) requires %q (by %s)", key, node.GetPosition().AsCompactString(), r.msg, r.position.AsCompactString())
for _, rule := range rules {
err := rule.Validate(node, a.thread)
if err != nil {
a.chk.Violations = append(a.chk.Violations, err)
}
}

return nil
}

6 changes: 6 additions & 0 deletions pkg/yamlmeta/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/vmware-tanzu/carvel-ytt/pkg/filepos"
"github.com/vmware-tanzu/carvel-ytt/pkg/orderedmap"
tplcore "github.com/vmware-tanzu/carvel-ytt/pkg/template/core"
"github.com/vmware-tanzu/carvel-ytt/pkg/yamlmeta/internal/yaml.v2"
)

Expand All @@ -27,6 +28,11 @@ func NewGoFromAST(val interface{}) interface{} {
return convertToGo(val)
}

//TODO: how else can we provide the RIGHT type of value
func NewGoValueFromAST(val interface{}) tplcore.GoValue {
return tplcore.NewGoValue(convertToGo(val))
}

func convertToLowYAML(val interface{}) interface{} {
switch typedVal := val.(type) {
case map[interface{}]interface{}:
Expand Down
55 changes: 55 additions & 0 deletions pkg/yamlmeta/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package yamlmeta

import (
"fmt"

"github.com/k14s/starlark-go/starlark"
"github.com/vmware-tanzu/carvel-ytt/pkg/filepos"
)

// A Rule represents an argument to an @assert/validate annotation;
// it contains a string description of what constitutes a valid value,
// and a function that asserts the rule against an actual value.
// One @assert/validate annotation can have multiple Rules.
type Rule struct {
Msg string
Assertion starlark.Callable
Position *filepos.Position
}

// Validate runs the assertion from the Rule with the node's value as arguments.
//
// Returns an error if the assertion returns False (not-None), or assert.fail()s.
// Otherwise, returns nil.
func (r Rule) Validate(node Node, thread *starlark.Thread) error {
var key string
var nodeValue starlark.Value
switch typedNode := node.(type) {
case *DocumentSet, *Array, *Map:
// TODO: is this the correct place for as specific of an error
panic(fmt.Sprintf("@%s annotation at %s - not supported on %s at %s", "assert/validate", r.Position.AsCompactString(), TypeName(node), node.GetPosition().AsCompactString()))
case *MapItem:
key = fmt.Sprintf("%q", typedNode.Key)
nodeValue = NewGoValueFromAST(typedNode.Value).AsStarlarkValue()
case *ArrayItem:
key = TypeName(typedNode)
nodeValue = NewGoValueFromAST(typedNode.Value).AsStarlarkValue()
case *Document:
key = TypeName(typedNode)
nodeValue = NewGoValueFromAST(typedNode.Value).AsStarlarkValue()
}

result, err := starlark.Call(thread, r.Assertion, starlark.Tuple{nodeValue}, []starlark.Tuple{})
if err != nil {
return fmt.Errorf("%s (%s) requires %q; %s (by %s)", key, node.GetPosition().AsCompactString(), r.Msg, err.Error(), r.Position.AsCompactString())
}

// in order to pass, the assertion must return True or None
if _, ok := result.(starlark.NoneType); !ok {
if !result.Truth() {
return fmt.Errorf("%s (%s) requires %q (by %s)", key, node.GetPosition().AsCompactString(), r.Msg, r.Position.AsCompactString())
}
}

return nil
}