forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 1
/
templates.go
341 lines (292 loc) · 11.7 KB
/
templates.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Copyright 2018 Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package data
import (
"context"
"errors"
"fmt"
"strings"
"time"
"github.com/gogo/protobuf/proto"
"github.com/gogo/protobuf/types"
istio_mixer_v1_template "istio.io/api/mixer/adapter/model/v1beta1"
policy "istio.io/api/policy/v1beta1"
"istio.io/istio/mixer/pkg/adapter"
"istio.io/istio/mixer/pkg/attribute"
"istio.io/istio/mixer/pkg/lang/ast"
"istio.io/istio/mixer/pkg/lang/compiled"
"istio.io/istio/mixer/pkg/runtime/lang"
"istio.io/istio/mixer/pkg/template"
)
// BuildTemplates builds a standard set of testing templates. The supplied settings is used to override behavior.
func BuildTemplates(l *Logger, settings ...FakeTemplateSettings) map[string]*template.Info {
m := make(map[string]FakeTemplateSettings)
for _, setting := range settings {
m[setting.Name] = setting
}
var t = map[string]*template.Info{
"tcheck": createFakeTemplate("tcheck", m["tcheck"], l, istio_mixer_v1_template.TEMPLATE_VARIETY_CHECK),
"tcheckoutput": createFakeTemplate("tcheckoutput", m["tcheckoutput"], l, istio_mixer_v1_template.TEMPLATE_VARIETY_CHECK_WITH_OUTPUT),
"treport": createFakeTemplate("treport", m["treport"], l, istio_mixer_v1_template.TEMPLATE_VARIETY_REPORT),
"tquota": createFakeTemplate("tquota", m["tquota"], l, istio_mixer_v1_template.TEMPLATE_VARIETY_QUOTA),
"tapa": createFakeTemplate("tapa", m["tapa"], l, istio_mixer_v1_template.TEMPLATE_VARIETY_ATTRIBUTE_GENERATOR),
// This is another template with check.
"thalt": createFakeTemplate("thalt", m["thalt"], l, istio_mixer_v1_template.TEMPLATE_VARIETY_CHECK),
}
return t
}
func createFakeTemplate(name string, s FakeTemplateSettings, l *Logger, variety istio_mixer_v1_template.TemplateVariety) *template.Info {
callCount := 0
return &template.Info{
Name: name,
Variety: variety,
CtrCfg: &types.Struct{},
AttributeManifests: []*policy.AttributeManifest{
{
Attributes: map[string]*policy.AttributeManifest_AttributeInfo{
"prefix.generated.string": {
ValueType: policy.STRING,
},
},
},
// output template attribute manifest
{
Attributes: map[string]*policy.AttributeManifest_AttributeInfo{
"value": {
ValueType: policy.STRING,
},
},
},
},
InferType: func(p proto.Message, evalFn template.TypeEvalFn) (proto.Message, error) {
l.WriteFormat(name, "InferType => p: '%+v'", p)
if s.ErrorAtInferType {
l.WriteFormat(name, "InferType <= (FAIL)")
return nil, fmt.Errorf("infer type error, as requested")
}
_, _ = evalFn("source.name")
l.WriteFormat(name, "InferType <= (SUCCESS)")
return &types.Struct{}, nil
},
BuilderSupportsTemplate: func(hndlrBuilder adapter.HandlerBuilder) bool {
l.Write(name, "BuilderSupportsTemplate =>")
l.WriteFormat(name, "BuilderSupportsTemplate <= %v", !s.BuilderDoesNotSupportTemplate)
return !s.BuilderDoesNotSupportTemplate
},
HandlerSupportsTemplate: func(hndlr adapter.Handler) bool {
l.Write(name, "HandlerSupportsTemplate =>")
l.WriteFormat(name, "HandlerSupportsTemplate <= %v", !s.HandlerDoesNotSupportTemplate)
return !s.HandlerDoesNotSupportTemplate
},
SetType: func(types map[string]proto.Message, builder adapter.HandlerBuilder) {
l.WriteFormat(name, "SetType => types: '%+v'", types)
l.Write(name, "SetType <=")
},
DispatchCheck: func(ctx context.Context, handler adapter.Handler, instance interface{}, out *attribute.MutableBag,
outPrefix string) (adapter.CheckResult, error) {
l.WriteFormat(name, "DispatchCheck => context exists: '%+v'", ctx != nil)
l.WriteFormat(name, "DispatchCheck => handler exists: '%+v'", handler != nil)
l.WriteFormat(name, "DispatchCheck => instance: '%+v'", instance)
signalCallAndWait(s)
if s.PanicOnDispatchCheck {
l.Write(name, "DispatchCheck <= (PANIC)")
panic(s.PanicData)
}
if s.ErrorOnDispatchCheck {
l.Write(name, "DispatchCheck <= (ERROR)")
return adapter.CheckResult{}, errors.New("error at dispatch check, as expected")
}
result := adapter.CheckResult{
ValidUseCount: 123,
ValidDuration: 123 * time.Second,
}
if callCount < len(s.CheckResults) {
result = s.CheckResults[callCount]
}
callCount++
l.Write(name, "DispatchCheck <= (SUCCESS)")
if variety == istio_mixer_v1_template.TEMPLATE_VARIETY_CHECK_WITH_OUTPUT {
l.Write(name, "DispatchCheck => output: {value: '1337'}")
if out != nil {
out.Set(outPrefix+"value", "1337")
}
return result, nil
}
return result, nil
},
DispatchReport: func(ctx context.Context, handler adapter.Handler, instances []interface{}) error {
l.WriteFormat(name, "DispatchReport => context exists: '%+v'", ctx != nil)
l.WriteFormat(name, "DispatchReport => handler exists: '%+v'", handler != nil)
l.WriteFormat(name, "DispatchReport => instances: '%+v'", instances)
signalCallAndWait(s)
if s.PanicOnDispatchReport {
l.Write(name, "DispatchReport <= (PANIC)")
panic(s.PanicData)
}
if s.ErrorOnDispatchReport {
l.Write(name, "DispatchReport <= (ERROR)")
return errors.New("error at dispatch report, as expected")
}
l.Write(name, "DispatchReport <= (SUCCESS)")
return nil
},
DispatchQuota: func(ctx context.Context, handler adapter.Handler, instance interface{}, args adapter.QuotaArgs) (adapter.QuotaResult, error) {
l.WriteFormat(name, "DispatchQuota => context exists: '%+v'", ctx != nil)
l.WriteFormat(name, "DispatchQuota => handler exists: '%+v'", handler != nil)
l.WriteFormat(name, "DispatchQuota => instance: '%+v' qArgs:{dedup:'%v', amount:'%v', best:'%v'}",
instance, args.DeduplicationID, args.QuotaAmount, args.BestEffort)
signalCallAndWait(s)
if s.PanicOnDispatchQuota {
l.Write(name, "DispatchQuota <= (PANIC)")
panic(s.PanicData)
}
if s.ErrorOnDispatchQuota {
l.Write(name, "DispatchQuota <= (ERROR)")
return adapter.QuotaResult{}, errors.New("error at dispatch quota, as expected")
}
result := adapter.QuotaResult{}
if callCount < len(s.QuotaResults) {
result = s.QuotaResults[callCount]
}
callCount++
l.Write(name, "DispatchQuota <= (SUCCESS)")
return result, nil
},
DispatchGenAttrs: func(ctx context.Context, handler adapter.Handler, instance interface{},
attrs attribute.Bag, mapper template.OutputMapperFn) (*attribute.MutableBag, error) {
l.WriteFormat(name, "DispatchGenAttrs => instance: '%+v'", instance)
l.WriteFormat(name, "DispatchGenAttrs => attrs: '%+v'", attrs.String())
l.WriteFormat(name, "DispatchGenAttrs => mapper(exists): '%+v'", mapper != nil)
signalCallAndWait(s)
if s.PanicOnDispatchGenAttrs {
l.Write(name, "DispatchGenAttrs <= (PANIC)")
panic(s.PanicData)
}
if s.ErrorOnDispatchGenAttrs {
l.Write(name, "DispatchGenAttrs <= (ERROR)")
return nil, errors.New("error at dispatch quota, as expected")
}
outputAttrs := map[string]interface{}{}
if s.OutputAttrs != nil {
outputAttrs = s.OutputAttrs
}
l.Write(name, "DispatchGenAttrs <= (SUCCESS)")
return attribute.GetMutableBagForTesting(outputAttrs), nil
},
CreateInstanceBuilder: func(instanceName string, instanceParam proto.Message, builder lang.Compiler) (template.InstanceBuilderFn, error) {
l.WriteFormat(name, "CreateInstanceBuilder => instanceName: '%+s'", instanceName)
l.WriteFormat(name, "CreateInstanceBuilder => instanceParam: '%s'", instanceParam)
if s.ErrorAtCreateInstanceBuilder {
l.WriteFormat(name, "CreateInstanceBuilder <= (FAIL)")
return nil, errors.New("error at create instance builder")
}
l.WriteFormat(name, "CreateInstanceBuilder <= (SUCCESS)")
ip := instanceParam.(*types.Struct)
exprs := make(map[string]compiled.Expression)
for k, v := range ip.Fields {
if k == "attribute_bindings" {
continue
}
exp, _, err := builder.Compile(v.GetStringValue())
if err != nil {
l.WriteFormat(name, "InstanceBuilderFn() <= (UNEXPECTED ERROR) (%v) %v", v.GetStringValue(), err)
return nil, fmt.Errorf("error compiling expression: %v=%v => %v", k, v, err)
}
exprs[k] = exp
}
return func(bag attribute.Bag) (interface{}, error) {
l.WriteFormat(name, "InstanceBuilderFn() => name: '%s', bag: '%v'", name, bag.String())
if s.ErrorAtCreateInstance {
l.Write(name, "InstanceBuilderFn() <= (ERROR)")
return nil, errors.New("error at create instance")
}
l.Write(name, "InstanceBuilderFn() <= (SUCCESS)")
instance := &types.Struct{
Fields: make(map[string]*types.Value),
}
for k, exp := range exprs {
v, err := exp.Evaluate(bag)
if err != nil {
return nil, err
}
instance.Fields[k] = &types.Value{
Kind: &types.Value_StringValue{StringValue: fmt.Sprintf("%v", v)},
}
}
return instance, nil
}, nil
},
CreateOutputExpressions: func(
instanceParam proto.Message,
finder ast.AttributeDescriptorFinder,
expb lang.Compiler) (map[string]compiled.Expression, error) {
l.WriteFormat(name, "CreateOutputExpressions => param: '%+v'", instanceParam)
l.WriteFormat(name, "CreateOutputExpressions => finder exists: '%+v'", finder != nil)
l.WriteFormat(name, "CreateOutputExpressions => expb exists: '%+v'", expb != nil)
if s.ErrorAtCreateOutputExpressions {
l.WriteFormat(name, "CreateOutputExpressions <= (FAIL)")
return nil, errors.New("error ar create output expressions")
}
ip := instanceParam.(*types.Struct)
exprs := make(map[string]compiled.Expression)
if bindings, ok := ip.Fields["attribute_bindings"]; ok {
for k, v := range bindings.GetStructValue().Fields {
str := strings.Replace(v.GetStringValue(), "$out", "prefix", -1)
exp, _, err := expb.Compile(str)
if err != nil {
l.WriteFormat(name, "CreateOutputExpressions() <= (UNEXPECTED ERROR) (%v) %v", str, err)
return nil, fmt.Errorf("error compiling expression: %v=%v => %v", k, v, err)
}
exprs[k] = exp
}
}
l.Write(name, "CreateOutputExpressions <= (SUCCESS)")
return exprs, nil
},
}
}
func signalCallAndWait(s FakeTemplateSettings) {
if s.ReceivedCallChannel != nil {
s.ReceivedCallChannel <- struct{}{}
}
if s.CommenceSignalChannel != nil {
<-s.CommenceSignalChannel
}
}
// FakeTemplateSettings describes the behavior of a fake template.
type FakeTemplateSettings struct {
Name string
ErrorAtCreateInstance bool
ErrorAtCreateInstanceBuilder bool
ErrorAtCreateOutputExpressions bool
ErrorAtInferType bool
BuilderDoesNotSupportTemplate bool
HandlerDoesNotSupportTemplate bool
PanicOnDispatchCheck bool
ErrorOnDispatchCheck bool
PanicOnDispatchReport bool
ErrorOnDispatchReport bool
PanicOnDispatchQuota bool
ErrorOnDispatchQuota bool
PanicOnDispatchGenAttrs bool
ErrorOnDispatchGenAttrs bool
PanicData interface{}
QuotaResults []adapter.QuotaResult
CheckResults []adapter.CheckResult
OutputAttrs map[string]interface{}
// template will signal the receipt of an incoming dispatch on this channel
ReceivedCallChannel chan struct{}
// template will wait on this channel before completing the dispatch
CommenceSignalChannel chan struct{}
}