-
Notifications
You must be signed in to change notification settings - Fork 136
/
assert.go
426 lines (364 loc) · 14.3 KB
/
assert.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// Copyright 2020 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
package yttlibrary
import (
"fmt"
"carvel.dev/ytt/pkg/orderedmap"
"carvel.dev/ytt/pkg/template/core"
"github.com/k14s/starlark-go/starlark"
"github.com/k14s/starlark-go/starlarkstruct"
"github.com/k14s/starlark-go/syntax"
)
// NewAssertModule constructs a new instance of AssertModule, respecting the "validations" experiment flag.
func NewAssertModule() AssertModule {
return AssertModule{}
}
// AssertModule contains the definition of the @ytt:assert module.
type AssertModule struct{}
// AsModule produces the corresponding Starlark module definition suitable for use in running a Starlark program.
func (m AssertModule) AsModule() starlark.StringDict {
members := starlark.StringDict{}
members["equals"] = starlark.NewBuiltin("assert.equals", core.ErrWrapper(m.Equals))
members["fail"] = starlark.NewBuiltin("assert.fail", core.ErrWrapper(m.Fail))
members["try_to"] = starlark.NewBuiltin("assert.try_to", core.ErrWrapper(m.TryTo))
members["min"] = starlark.NewBuiltin("assert.min", core.ErrWrapper(m.Min))
members["min_len"] = starlark.NewBuiltin("assert.min_len", core.ErrWrapper(m.MinLen))
members["max"] = starlark.NewBuiltin("assert.max", core.ErrWrapper(m.Max))
members["max_len"] = starlark.NewBuiltin("assert.max_len", core.ErrWrapper(m.MaxLen))
members["not_null"] = starlark.NewBuiltin("assert.not_null", core.ErrWrapper(m.NotNull))
members["one_not_null"] = starlark.NewBuiltin("assert.one_not_null", core.ErrWrapper(m.OneNotNull))
members["one_of"] = starlark.NewBuiltin("assert.one_of", core.ErrWrapper(m.OneOf))
return starlark.StringDict{
"assert": &starlarkstruct.Module{
Name: "assert",
Members: members,
},
}
}
// Equals compares two values for equality
func (m AssertModule) Equals(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, _ []starlark.Tuple) (starlark.Value, error) {
if args.Len() != 2 {
return starlark.None, fmt.Errorf("got %d arguments, want %d", args.Len(), 2)
}
expected := args.Index(0)
if _, notOk := expected.(starlark.Callable); notOk {
return starlark.None, fmt.Errorf("expected argument not to be a function, but was %T", expected)
}
actual := args.Index(1)
if _, notOk := actual.(starlark.Callable); notOk {
return starlark.None, fmt.Errorf("expected argument not to be a function, but was %T", actual)
}
expectedString, err := m.asString(expected)
if err != nil {
return starlark.None, err
}
actualString, err := m.asString(actual)
if err != nil {
return starlark.None, err
}
if expectedString != actualString {
return starlark.None, fmt.Errorf("Not equal:\n"+
"(expected type: %s)\n%s\n\n(was type: %s)\n%s", expected.Type(), expectedString, actual.Type(), actualString)
}
return starlark.None, nil
}
func (m AssertModule) asString(value starlark.Value) (string, error) {
starlarkValue, err := core.NewStarlarkValue(value).AsGoValue()
if err != nil {
return "", err
}
yamlString, err := yamlModule{}.Encode(starlarkValue)
if err != nil {
return "", err
}
return yamlString, nil
}
// Fail is a core.StarlarkFunc that forces a Starlark failure.
func (m AssertModule) Fail(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, _ []starlark.Tuple) (starlark.Value, error) {
if args.Len() != 1 {
return starlark.None, fmt.Errorf("got %d arguments, want %d", args.Len(), 1)
}
val, err := core.NewStarlarkValue(args.Index(0)).AsString()
if err != nil {
return starlark.None, err
}
return starlark.None, fmt.Errorf("fail: %s", val)
}
// TryTo is a core.StarlarkFunc that attempts to invoke the passed in starlark.Callable, converting any error into
// an error message.
func (m AssertModule) TryTo(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, _ []starlark.Tuple) (starlark.Value, error) {
if args.Len() != 1 {
return starlark.None, fmt.Errorf("got %d arguments, want %d", args.Len(), 1)
}
lambda := args.Index(0)
if _, ok := lambda.(starlark.Callable); !ok {
return starlark.None, fmt.Errorf("expected argument to be a function, but was %T", lambda)
}
retVal, err := starlark.Call(thread, lambda, nil, nil)
if err != nil {
return starlark.Tuple{starlark.None, starlark.String(err.Error())}, nil
}
return starlark.Tuple{retVal, starlark.None}, nil
}
// Assertion encapsulates a rule (a predicate) that can be accessed in a Starlark expression (via the "check" attribute)
// or in Go (via CheckFunc()).
type Assertion struct {
check starlark.Callable
*core.StarlarkStruct
}
// Type reports the name of this type in the Starlark type system.
func (a *Assertion) Type() string { return "@ytt:assert.assertion" }
// CheckFunc returns the function that — given a value — makes this assertion on it.
func (a *Assertion) CheckFunc() starlark.Callable {
return a.check
}
// ConversionHint helps the user get unstuck if they accidentally left an Assertion as a value in a YAML being
// encoded.
func (a *Assertion) ConversionHint() string {
return a.Type() + " does not encode (did you mean to call check()?)"
}
// NewAssertionFromSource creates an Assertion whose "check" attribute is the lambda expression defined in "checkSrc".
func NewAssertionFromSource(funcName, checkSrc string, env starlark.StringDict) *Assertion {
expr, err := syntax.ParseExpr(funcName, checkSrc, syntax.BlockScanner)
if err != nil {
panic(fmt.Sprintf("Failed to parse internal expression (%s) :%s", checkSrc, err))
}
thread := &starlark.Thread{Name: "ytt-internal"}
evalExpr, err := starlark.EvalExpr(thread, expr, env)
if err != nil {
panic(fmt.Sprintf("Failed to evaluate internal expression (%s) given env=%s", checkSrc, env))
}
a := &Assertion{check: evalExpr.(*starlark.Function)}
m := orderedmap.NewMap()
m.Set("check", a.check)
a.StarlarkStruct = core.NewStarlarkStruct(m)
return a
}
// NewAssertionFromStarlarkFunc creates an Assertion whose "check" attribute is "checkFunc".
func NewAssertionFromStarlarkFunc(funcName string, checkFunc core.StarlarkFunc) *Assertion {
a := &Assertion{check: starlark.NewBuiltin(funcName, checkFunc)}
m := orderedmap.NewMap()
m.Set("check", a.check)
a.StarlarkStruct = core.NewStarlarkStruct(m)
return a
}
// NewAssertMaxLen produces an Assertion that a given sequence is at most "maximum" in length.
//
// see also: https://github.com/google/starlark-go/blob/master/doc/spec.md#len
func NewAssertMaxLen(maximum starlark.Int) *Assertion {
return NewAssertionFromSource(
"assert.max_len",
`lambda sequence: True if len(sequence) <= maximum else fail ("length = {}".format(len(sequence)))`,
starlark.StringDict{"maximum": maximum},
)
}
// MaxLen is a core.StarlarkFunc wrapping NewAssertMaxLen()
func (m AssertModule) MaxLen(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, _ []starlark.Tuple) (starlark.Value, error) {
if args.Len() != 1 {
return starlark.None, fmt.Errorf("got %d arguments, want %d", args.Len(), 1)
}
max, err := starlark.NumberToInt(args[0])
if err != nil {
return nil, fmt.Errorf("expected value to be an number, but was %s", args[0].Type())
}
return NewAssertMaxLen(max), nil
}
// NewAssertMinLen produces an Assertion that a given sequence is at least "minimum" in length.
//
// see also: https://github.com/google/starlark-go/blob/master/doc/spec.md#len
func NewAssertMinLen(minimum starlark.Int) *Assertion {
return NewAssertionFromSource(
"assert.min_len",
`lambda sequence: True if len(sequence) >= minimum else fail ("length = {}".format(len(sequence)))`,
starlark.StringDict{"minimum": minimum},
)
}
// MinLen is a core.StarlarkFunc wrapping NewAssertMinLen()
func (m AssertModule) MinLen(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, _ []starlark.Tuple) (starlark.Value, error) {
if args.Len() != 1 {
return starlark.None, fmt.Errorf("got %d arguments, want %d", args.Len(), 1)
}
min, err := starlark.NumberToInt(args[0])
if err != nil {
return nil, fmt.Errorf("expected value to be an number, but was %s", args[0].Type())
}
return NewAssertMinLen(min), nil
}
// NewAssertMin produces an Assertion that a given value is at least "minimum".
//
// see also:https://github.com/google/starlark-go/blob/master/doc/spec.md#comparisons
func NewAssertMin(min starlark.Value) *Assertion {
return NewAssertionFromSource(
"assert.min",
`lambda val: yaml.decode(yaml.encode(val)) >= yaml.decode(yaml.encode(min)) or fail("value < {}".format(yaml.decode(yaml.encode(min))))`,
starlark.StringDict{"min": min, "yaml": YAMLAPI["yaml"]},
)
}
// Min is a core.StarlarkFunc wrapping NewAssertMin()
func (m AssertModule) Min(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, _ []starlark.Tuple) (starlark.Value, error) {
if args.Len() != 1 {
return starlark.None, fmt.Errorf("got %d arguments, want %d", args.Len(), 1)
}
minFunc := NewAssertMin(args[0])
return minFunc, nil
}
// NewAssertMax produces an Assertion that a given value is at most "maximum".
//
// see also:https://github.com/google/starlark-go/blob/master/doc/spec.md#comparisons
func NewAssertMax(max starlark.Value) *Assertion {
return NewAssertionFromSource(
"assert.max",
`lambda val: yaml.decode(yaml.encode(val)) <= yaml.decode(yaml.encode(max)) or fail("value > {}".format(yaml.decode(yaml.encode(max))))`,
starlark.StringDict{"max": max, "yaml": YAMLAPI["yaml"]},
)
}
// Max is a core.StarlarkFunc wrapping NewAssertMax()
func (m AssertModule) Max(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, _ []starlark.Tuple) (starlark.Value, error) {
if args.Len() != 1 {
return starlark.None, fmt.Errorf("got %d arguments, want %d", args.Len(), 1)
}
maxFunc := NewAssertMax(args[0])
return maxFunc, nil
}
// NewAssertNotNull produces an Assertion that a given value is not null.
func NewAssertNotNull() *Assertion {
return NewAssertionFromSource(
"assert.not_null",
`lambda value: value != None or fail("value is null")`,
starlark.StringDict{},
)
}
// NotNull is a core.StarlarkFunc wrapping NewAssertNotNull()
func (m AssertModule) NotNull(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, _ []starlark.Tuple) (starlark.Value, error) {
if args.Len() > 1 {
return starlark.None, fmt.Errorf("got %d arguments, want at most %d", args.Len(), 1)
}
result := NewAssertNotNull()
if args.Len() == 0 {
return result, nil
}
// support shorthand syntax: assert.not_null(value)
return starlark.Call(thread, result.CheckFunc(), args, []starlark.Tuple{})
}
// NewAssertOneNotNull produces an Assertion that a given value is a map having exactly one item with a non-null value.
func NewAssertOneNotNull(keys starlark.Sequence) *Assertion {
return NewAssertionFromStarlarkFunc("assert.one_not_null", AssertModule{}.oneNotNullCheck(keys))
}
// OneNotNull is a core.StarlarkFunc wrapping NewAssertOneNotNull()
func (m AssertModule) OneNotNull(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, _ []starlark.Tuple) (starlark.Value, error) {
if args.Len() > 1 {
return starlark.None, fmt.Errorf("got %d arguments, want %d", args.Len(), 1)
}
if args.Len() == 0 {
return NewAssertOneNotNull(nil), nil
}
if b, ok := args[0].(starlark.Bool); ok {
if b.Truth() {
return NewAssertOneNotNull(nil), nil
}
return nil, fmt.Errorf("one_not_null() cannot be False")
}
seq, ok := args[0].(starlark.Sequence)
if !ok {
return nil, fmt.Errorf("expected a sequence of keys, but was a '%s'", args[0].Type())
}
return NewAssertOneNotNull(seq), nil
}
func (m AssertModule) oneNotNullCheck(keys starlark.Sequence) core.StarlarkFunc {
return func(thread *starlark.Thread, f *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
if args.Len() != 1 {
return starlark.None, fmt.Errorf("check: got %d arguments, want %d", args.Len(), 1)
}
val, err := m.yamlEncodeDecode(args[0])
if err != nil {
return nil, err
}
dict, ok := val.(*starlark.Dict)
if !ok {
return nil, fmt.Errorf("check: value must be a map or dict, but was '%s'", val.Type())
}
var keysToCheck []starlark.Value
if keys == nil {
keysToCheck = append(keysToCheck, dict.Keys()...)
} else {
var key starlark.Value
keys := keys.Iterate()
for keys.Next(&key) {
v, err := m.yamlEncodeDecode(key)
if err != nil {
return nil, err
}
keysToCheck = append(keysToCheck, v)
}
}
var nulls, notNulls []starlark.Value
for _, key := range keysToCheck {
value, found, err := dict.Get(key)
if err != nil {
return nil, fmt.Errorf("check: unexpected error while looking up key %s in dict %s", key, dict)
}
if !found {
// allow schema to catch this (see also https://github.com/carvel-dev/ytt/issues/722)
nulls = append(nulls, key)
}
if value == starlark.None {
nulls = append(nulls, key)
} else {
notNulls = append(notNulls, key)
}
}
switch len(notNulls) {
case 0:
if len(dict.Keys()) == 0 {
return nil, fmt.Errorf("check: value is empty")
}
return nil, fmt.Errorf("check: all values are null")
case 1:
return starlark.True, nil
default:
return nil, fmt.Errorf("check: %s are not null", starlark.NewList(notNulls).String())
}
}
}
// NewAssertOneOf produces an Assertion that a given value is one of a pre-defined set.
//
// see also:https://github.com/google/starlark-go/blob/master/doc/spec.md#membership-tests
func NewAssertOneOf(enum starlark.Sequence) *Assertion {
return NewAssertionFromSource(
"assert.one_of",
`lambda val: yaml.decode(yaml.encode(val)) in yaml.decode(yaml.encode(enum)) or fail("not one of allowed values")`,
starlark.StringDict{"enum": enum, "yaml": YAMLAPI["yaml"]},
)
}
// OneOf is a core.StarlarkFunc wrapping NewAssertOneOf()
func (m AssertModule) OneOf(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, _ []starlark.Tuple) (starlark.Value, error) {
if args.Len() == 0 {
return starlark.None, fmt.Errorf("got %d arguments, want at least %d", args.Len(), 1)
}
enum := args[0]
if args.Len() > 1 {
enum = args
}
seq, ok := enum.(starlark.Sequence)
if !ok {
return nil, fmt.Errorf("expected a sequence, but was a '%s'", enum.Type())
}
maxFunc := NewAssertOneOf(seq)
return maxFunc, nil
}
func (m AssertModule) yamlEncodeDecode(val starlark.Value) (starlark.Value, error) {
yaml := yamlModule{}
value, err := core.NewStarlarkValue(val).AsGoValue()
if err != nil {
return nil, err
}
encode, err := yaml.Encode(value)
if err != nil {
return nil, err
}
val, err = yaml.Decode(encode)
if err != nil {
return nil, err
}
return val, nil
}