Repository navigation
Expand file tree
/
Copy pathcel_tracker_test.go
More file actions
147 lines (123 loc) · 5.1 KB
/
Copy pathcel_tracker_test.go
File metadata and controls
147 lines (123 loc) · 5.1 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package gomplate
import (
"context"
"sync"
"cel.dev/cel-go/cel"
"cel.dev/cel-go/common/types/ref"
commonsContext "github.com/flanksource/commons/context"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("CELTracker", func() {
newTrackedContext := func(tracker *CELTracker) commonsContext.Context {
return WithCELTracker(commonsContext.NewContext(context.Background()), tracker)
}
It("tracks native evaluation state only when requested", func() {
tracker := NewCELTracker()
template := Template{Expression: "a == b && c > d"}
values := map[string]any{"a": 1, "b": 2, "c": 3, "d": 4}
result, err := RunExpression(values, template)
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(false))
Expect(tracker.Snapshot()).To(Equal(CELTraceSnapshot{}))
result, err = RunExpressionContext(newTrackedContext(tracker), values, template)
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(false))
snapshot := tracker.Snapshot()
Expect(snapshot.AST).NotTo(BeNil())
Expect(snapshot.Details).NotTo(BeNil())
Expect(snapshot.Details.State().IDs()).NotTo(BeEmpty())
Expect(snapshot.Output.Value()).To(Equal(false))
})
It("retains partial native state when evaluation returns an error", func() {
tracker := NewCELTracker()
expression := "numerator / denominator == expected"
_, err := RunExpressionContext(newTrackedContext(tracker), map[string]any{
"numerator": 8,
"denominator": 0,
"expected": 2,
}, Template{Expression: expression})
Expect(err).To(HaveOccurred())
snapshot := tracker.Snapshot()
Expect(snapshot.AST).NotTo(BeNil())
Expect(snapshot.AST.Source().Content()).To(Equal(expression))
Expect(snapshot.Details).NotTo(BeNil())
Expect(snapshot.Details.State().IDs()).NotTo(BeEmpty())
Expect(snapshot.Output).NotTo(BeNil())
})
It("preserves multiline source locations", func() {
tracker := NewCELTracker()
expression := "a == b &&\n c > d"
_, err := RunExpressionContext(newTrackedContext(tracker), map[string]any{
"a": 1,
"b": 1,
"c": 3,
"d": 4,
}, Template{Expression: expression})
Expect(err).NotTo(HaveOccurred())
snapshot := tracker.Snapshot()
Expect(snapshot.AST.Source().Content()).To(Equal(expression))
ids := snapshot.Details.State().IDs()
valueLines := make([]int, 0, len(ids))
for _, id := range ids {
valueLines = append(valueLines, snapshot.AST.NativeRep().SourceInfo().GetStartLocation(id).Line())
}
Expect(valueLines).To(ContainElement(2))
})
It("tracks optimized list, optional, and regex evaluation", func() {
tracker := NewCELTracker()
expression := `([1, 2] + [3, 4]).size() == 4 && optional.of(name).orValue("") == "Ada" && name.matches("^A.*")`
result, err := RunExpressionContext(newTrackedContext(tracker), map[string]any{"name": "Ada"}, Template{Expression: expression})
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(true))
Expect(tracker.Snapshot()).To(SatisfyAll(
WithTransform(func(snapshot CELTraceSnapshot) *cel.Ast { return snapshot.AST }, Not(BeNil())),
WithTransform(func(snapshot CELTraceSnapshot) *cel.EvalDetails { return snapshot.Details }, Not(BeNil())),
WithTransform(func(snapshot CELTraceSnapshot) any { return snapshot.Output.Value() }, Equal(true)),
))
})
It("rejects concurrent reuse and can be reused after evaluation", func() {
tracker := NewCELTracker()
started := make(chan struct{})
release := make(chan struct{})
var startOnce sync.Once
blockingFunction := cel.Function("block",
cel.Overload("block_int", []*cel.Type{cel.IntType}, cel.IntType,
cel.UnaryBinding(func(value ref.Val) ref.Val {
startOnce.Do(func() { close(started) })
<-release
return value
})))
template := Template{Expression: "block(value) == 1", CelEnvs: []cel.EnvOption{blockingFunction}}
firstDone := make(chan error, 1)
go func() {
_, err := RunExpressionContext(newTrackedContext(tracker), map[string]any{"value": 1}, template)
firstDone <- err
}()
<-started
_, err := RunExpressionContext(newTrackedContext(tracker), map[string]any{"value": 1}, template)
Expect(err).To(MatchError(ContainSubstring("CEL tracker is already in use")))
close(release)
Expect(<-firstDone).NotTo(HaveOccurred())
_, err = RunExpressionContext(newTrackedContext(tracker), map[string]any{"value": 1}, template)
Expect(err).NotTo(HaveOccurred())
})
It("leaves the normal program cache unchanged", func() {
celExpressionCache.Flush()
template := Template{Expression: "value == 1"}
values := map[string]any{"value": 1}
_, err := RunExpression(values, template)
Expect(err).NotTo(HaveOccurred())
Expect(celExpressionCache.ItemCount()).To(Equal(1))
tracker := NewCELTracker()
_, err = RunExpressionContext(newTrackedContext(tracker), values, template)
Expect(err).NotTo(HaveOccurred())
Expect(celExpressionCache.ItemCount()).To(Equal(1))
})
It("does not publish a snapshot for compile errors", func() {
tracker := NewCELTracker()
_, err := RunExpressionContext(newTrackedContext(tracker), nil, Template{Expression: "missing("})
Expect(err).To(HaveOccurred())
Expect(tracker.Snapshot()).To(Equal(CELTraceSnapshot{}))
})
})