-
Notifications
You must be signed in to change notification settings - Fork 136
/
assert_annotation.go
109 lines (91 loc) · 3.02 KB
/
assert_annotation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package overlay
import (
"fmt"
"github.com/k14s/ytt/pkg/template"
tplcore "github.com/k14s/ytt/pkg/template/core"
"github.com/k14s/ytt/pkg/yamlmeta"
"go.starlark.net/starlark"
)
type AssertAnnotation struct {
newNode template.EvaluationNode
thread *starlark.Thread
via *starlark.Value
}
func NewAssertAnnotation(newNode template.EvaluationNode, thread *starlark.Thread) (AssertAnnotation, error) {
annotation := AssertAnnotation{
newNode: newNode,
thread: thread,
}
kwargs := template.NewAnnotations(newNode).Kwargs(AnnotationAssert)
for _, kwarg := range kwargs {
kwargName := string(kwarg[0].(starlark.String))
switch kwargName {
case "via":
annotation.via = &kwarg[1]
default:
return annotation, fmt.Errorf(
"Unknown '%s' annotation keyword argument '%s'", AnnotationAssert, kwargName)
}
}
return annotation, nil
}
func (a AssertAnnotation) Check(existingNode template.EvaluationNode) error {
// Make sure original nodes are not affected in any way
existingNode = existingNode.DeepCopyAsInterface().(template.EvaluationNode)
newNode := a.newNode.DeepCopyAsInterface().(template.EvaluationNode)
// TODO currently assumes that we can always get at least one value
existingVal := existingNode.GetValues()[0]
newVal := newNode.GetValues()[0]
if a.via == nil {
actualObj := yamlmeta.NewASTFromInterface(existingVal)
expectedObj := yamlmeta.NewASTFromInterface(newVal)
// TODO use generic equal function from our library?
equal, desc := Comparison{}.Compare(actualObj, expectedObj)
if !equal {
return fmt.Errorf("Expected objects to equal, but did not: %s", desc)
}
return nil
}
switch typedVal := (*a.via).(type) {
case starlark.Callable:
viaArgs := starlark.Tuple{
tplcore.NewGoValue(existingVal, false).AsStarlarkValue(),
tplcore.NewGoValue(newVal, false).AsStarlarkValue(),
}
result, err := starlark.Call(a.thread, *a.via, viaArgs, []starlark.Tuple{})
if err != nil {
return err
}
switch typedResult := result.(type) {
case nil, starlark.NoneType:
// Assume if via didnt error then it's successful
return nil
case starlark.Bool:
if !bool(typedResult) {
return fmt.Errorf("Expected via invocation to return true, but was false")
}
return nil
default:
result := tplcore.NewStarlarkValue(result).AsInterface()
// Extract result tuple(bool, string) to determine success
if typedResult, ok := result.([]interface{}); ok {
if len(typedResult) == 2 {
resultSuccess, ok1 := typedResult[0].(bool)
resultMsg, ok2 := typedResult[1].(string)
if ok1 && ok2 {
if !resultSuccess {
return fmt.Errorf("Expected via invocation to return true, "+
"but was false with message: %s", resultMsg)
}
return nil
}
}
}
return fmt.Errorf("Expected via invocation to return NoneType, " +
"Bool or Tuple(Bool,String), but returned neither of those")
}
default:
return fmt.Errorf("Expected '%s' annotation keyword argument 'via'"+
" to be function, but was %T", AnnotationAssert, typedVal)
}
}