-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathcontext_inferrer_test.go
179 lines (168 loc) · 5.78 KB
/
context_inferrer_test.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package ottl
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var defaultDummyPriorityContextInferrerCandidate = &priorityContextInferrerCandidate{
hasFunctionName: func(_ string) bool {
return true
},
hasEnumSymbol: func(_ *EnumSymbol) bool {
return true
},
getLowerContexts: func(_ string) []string {
return nil
},
}
func newDummyPriorityContextInferrerCandidate(hasFunctionName, hasEnumSymbol bool, lowerContexts []string) *priorityContextInferrerCandidate {
return &priorityContextInferrerCandidate{
hasFunctionName: func(_ string) bool {
return hasFunctionName
},
hasEnumSymbol: func(_ *EnumSymbol) bool {
return hasEnumSymbol
},
getLowerContexts: func(_ string) []string {
return lowerContexts
},
}
}
func Test_NewPriorityContextInferrer_Infer(t *testing.T) {
tests := []struct {
name string
priority []string
candidates map[string]*priorityContextInferrerCandidate
statements []string
expected string
}{
{
name: "with priority and statement context",
priority: []string{"spanevent", "span", "resource"},
candidates: map[string]*priorityContextInferrerCandidate{
"spanevent": defaultDummyPriorityContextInferrerCandidate,
},
statements: []string{"set(span.foo, resource.value) where spanevent.bar == true"},
expected: "spanevent",
},
{
name: "with multiple statements and contexts",
priority: []string{"spanevent", "span", "resource"},
candidates: map[string]*priorityContextInferrerCandidate{
"spanevent": defaultDummyPriorityContextInferrerCandidate,
},
statements: []string{
"set(resource.foo, resource.value) where span.bar == true",
"set(resource.foo, resource.value) where spanevent.bar == true",
},
expected: "spanevent",
},
{
name: "with no statements context",
priority: []string{"log", "resource"},
candidates: map[string]*priorityContextInferrerCandidate{},
statements: []string{"set(foo, true) where bar == true"},
expected: "",
},
{
name: "with empty priority list",
candidates: map[string]*priorityContextInferrerCandidate{
"foo": defaultDummyPriorityContextInferrerCandidate,
},
statements: []string{"set(foo.name, true) where bar.name == true"},
expected: "foo",
},
{
name: "with non-prioritized statement context",
priority: []string{"foo", "bar"},
candidates: map[string]*priorityContextInferrerCandidate{
"span": defaultDummyPriorityContextInferrerCandidate,
},
statements: []string{"set(span.foo, true) where span.bar == true"},
expected: "span",
},
{
name: "inferred path context with missing function",
priority: []string{"foo", "datapoint", "metric"},
statements: []string{`set(metric.is_foo, true) where metric.name == "foo"`},
candidates: map[string]*priorityContextInferrerCandidate{
"metric": newDummyPriorityContextInferrerCandidate(false, true, []string{"foo", "datapoint"}),
"foo": newDummyPriorityContextInferrerCandidate(false, true, []string{}),
"datapoint": newDummyPriorityContextInferrerCandidate(true, true, []string{}),
},
expected: "datapoint",
},
{
name: "inferred path context with missing function and no qualified lower context",
priority: []string{"datapoint", "metric"},
statements: []string{`set(metric.is_foo, true) where metric.name == "foo"`},
candidates: map[string]*priorityContextInferrerCandidate{
"metric": newDummyPriorityContextInferrerCandidate(false, false, []string{"datapoint"}),
"datapoint": newDummyPriorityContextInferrerCandidate(false, false, []string{}),
},
expected: "",
},
{
name: "inferred path context with missing function and no lower context",
priority: []string{"datapoint", "metric"},
statements: []string{`set(metric.is_foo, true) where metric.name == "foo"`},
candidates: map[string]*priorityContextInferrerCandidate{
"metric": newDummyPriorityContextInferrerCandidate(false, true, []string{}),
},
expected: "",
},
{
name: "inferred path context with missing enum",
priority: []string{"foo", "bar"},
statements: []string{`set(foo.name, FOO) where IsFoo() == true`},
candidates: map[string]*priorityContextInferrerCandidate{
"foo": newDummyPriorityContextInferrerCandidate(true, false, []string{"foo", "bar"}),
"bar": newDummyPriorityContextInferrerCandidate(true, true, []string{}),
},
expected: "bar",
},
{
name: "unknown context candidate inferred from paths",
priority: []string{"unknown"},
statements: []string{`set(unknown.count, 0)`},
candidates: map[string]*priorityContextInferrerCandidate{},
expected: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
inferrer := newPriorityContextInferrer(
tt.candidates,
withContextInferrerPriorities(tt.priority),
)
inferredContext, err := inferrer.infer(tt.statements)
require.NoError(t, err)
assert.Equal(t, tt.expected, inferredContext)
})
}
}
func Test_NewPriorityContextInferrer_InvalidStatement(t *testing.T) {
inferrer := newPriorityContextInferrer(map[string]*priorityContextInferrerCandidate{})
statements := []string{"set(foo.field,"}
_, err := inferrer.infer(statements)
require.ErrorContains(t, err, "unexpected token")
}
func Test_DefaultPriorityContextInferrer(t *testing.T) {
expectedPriority := []string{
"log",
"datapoint",
"metric",
"spanevent",
"span",
"resource",
"scope",
"instrumentation_scope",
}
inferrer := newPriorityContextInferrer(map[string]*priorityContextInferrerCandidate{}).(*priorityContextInferrer)
require.NotNil(t, inferrer)
for pri, ctx := range expectedPriority {
require.Equal(t, pri, inferrer.contextPriority[ctx])
}
}