-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsemantics_test.go
More file actions
203 lines (189 loc) · 6.59 KB
/
Copy pathsemantics_test.go
File metadata and controls
203 lines (189 loc) · 6.59 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
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
package dyfields_test
import (
"encoding/json"
"sync"
"testing"
df "github.com/BrobridgeOrg/dyfields"
)
// Visibility, defaults and removal have to reach a fixed point before anything
// is deleted, no matter what order the fields were declared in.
func TestSettleReachesAFixedPointBeforeDeleting(t *testing.T) {
b := df.NewBuilder()
b.AddField(df.String("tuning").VisibleWhen(df.Equals("mode", "adv")))
b.AddField(df.Enum("mode").Options(df.Opt("basic", "b"), df.Opt("adv", "a")).Default("adv"))
c, err := b.Compile()
if err != nil {
t.Fatal(err)
}
out, _ := c.Validate(df.ValueDocument{Values: map[string]any{"tuning": "x"}})
if out.Values["tuning"] != "x" {
t.Errorf("order of declaration decided the outcome: %v", out.Values)
}
}
// Two levels out. Three nested lists, so that ^^. genuinely has to skip a
// scope rather than land on the root.
func TestGrandparentScopeReference(t *testing.T) {
inner := df.ObjectList("inner").MinItems(1)
inner.AddField(df.String("only_for_kafka").VisibleWhen(df.Equals("^^.kind", "kafka")))
mid := df.ObjectList("mid").MinItems(1)
mid.AddField(df.String("m"))
mid.AddField(inner)
outer := df.ObjectList("outer").MinItems(1)
outer.AddField(df.String("kind"))
outer.AddField(mid)
b := df.NewBuilder()
b.AddField(outer)
c, err := b.Compile()
if err != nil {
t.Fatalf("^^. did not compile: %v", err)
}
entry := func(kind string) any {
return map[string]any{"$id": "o" + kind, "kind": kind, "mid": []any{
map[string]any{"$id": "m1", "inner": []any{map[string]any{"$id": "i1"}}}}}
}
vis := c.Visible(df.ValueDocument{Values: map[string]any{
"outer": []any{entry("kafka"), entry("http")}}})
if !vis.Fields["outer[0].mid[0].inner[0].only_for_kafka"] {
t.Error("^^. should resolve to the grandparent entry")
}
if vis.Fields["outer[1].mid[0].inner[0].only_for_kafka"] {
t.Error("^^. leaked across entries")
}
}
// Validating an already-validated document must not change it again.
func TestValidateIsIdempotent(t *testing.T) {
c := pipelineCompiled(t)
once, errs := c.Validate(readDoc(t, "pipeline_before.json"))
if errs.Len() != 0 {
t.Fatal(errs)
}
twice, errs := c.Validate(once)
if errs.Len() != 0 {
t.Fatal(errs)
}
if canonical(t, once) != canonical(t, twice) {
t.Errorf("validation is not idempotent")
}
}
// An undeclared key inside an entry is still an undeclared key.
func TestUnknownKeyInsideAnEntry(t *testing.T) {
c := pipelineCompiled(t)
doc := readDoc(t, "pipeline_before.json")
sink := doc.Values["sinks"].([]any)[0].(map[string]any)
sink["mappings"].([]any)[0].(map[string]any)["typo_field"] = 1
_, errs := c.Validate(doc)
if !errs.Has(df.ErrUnknownField) {
t.Errorf("an undeclared key inside an entry was accepted: %v", errs)
}
}
// A patch adding an entry that holds a secret but carries no $id.
func TestNewEntryHoldingASecretNeedsAnID(t *testing.T) {
c := pipelineCompiled(t)
before := readDoc(t, "pipeline_before.json")
patch := df.Patch{Values: map[string]any{"sinks": []any{
map[string]any{"name": "s3_new", "type": "s3", "bucket": "b",
"mappings": []any{map[string]any{"$id": "mm", "source_field": "a", "target_field": "b"}}},
}}}
_, errs := c.Apply(before, patch)
if !errs.Has(df.ErrMissingItemID) {
t.Errorf("a secret-bearing entry was accepted without a $id: %v", errs)
}
}
// Deleting the last entry must trip min_items.
func TestDeletingBelowMinItems(t *testing.T) {
c := pipelineCompiled(t)
before := readDoc(t, "pipeline_before.json")
patch := df.Patch{Values: map[string]any{"sinks": []any{
map[string]any{"$id": "s_k1", "$deleted": true},
map[string]any{"$id": "s_h1", "$deleted": true},
}}}
_, errs := c.Apply(before, patch)
if !errs.Has(df.ErrMinItems) {
t.Errorf("emptying a list below min_items was accepted: %v", errs)
}
}
// An empty condition would silently read as "always true".
func TestCompileRejectsAnEmptyCondition(t *testing.T) {
s, err := df.Parse([]byte(`{"groups":[{"key":"g","fields":[
{"name":"a","type":"string","visible_when":{}}]}]}`))
if err != nil {
t.Fatal(err)
}
if _, err := df.Compile(s); err == nil {
t.Error("an empty condition compiled")
}
}
// A field name containing a dot makes the secrets path ambiguous, which is
// exactly why $id has a restricted charset.
func TestCompileRejectsADottedFieldName(t *testing.T) {
s, err := df.Parse([]byte(`{"groups":[{"key":"g","fields":[
{"name":"a.b","type":"secret"}]}]}`))
if err != nil {
t.Fatal(err)
}
if _, err := df.Compile(s); err == nil {
t.Error("a field name with a dot compiled")
}
}
// Compiled is documented as safe for concurrent use.
func TestCompiledIsSafeForConcurrentUse(t *testing.T) {
c := pipelineCompiled(t)
var wg sync.WaitGroup
for i := 0; i < 8; i++ {
wg.Add(1)
go func() {
defer wg.Done()
var doc df.ValueDocument
_ = json.Unmarshal(readFile(t, "pipeline_before.json"), &doc)
if _, errs := c.Validate(doc); errs.Len() != 0 {
t.Errorf("%v", errs)
}
}()
}
wg.Wait()
}
func TestCompileRejectsConstraintsTheTypeCannotHonour(t *testing.T) {
cases := map[string]string{
"length on a number": `{"name":"n","type":"integer","max_length":3}`,
"range on a string": `{"name":"s","type":"string","min":1}`,
"format on a boolean": `{"name":"b","type":"boolean","format":"email"}`,
"pattern on a boolean": `{"name":"b","type":"boolean","pattern":"^x$"}`,
}
for name, field := range cases {
t.Run(name, func(t *testing.T) {
// Silently ignoring these is how a schema ends up looking stricter
// than it is.
s, err := df.Parse([]byte(`{"groups":[{"key":"g","fields":[` + field + `]}]}`))
if err != nil {
t.Fatal(err)
}
if _, err := df.Compile(s); err == nil {
t.Error("expected the no-op constraint to be rejected")
}
})
}
}
func TestCompileRejectsSecretAndOptionlessItems(t *testing.T) {
for name, field := range map[string]string{
"a list of secrets": `{"name":"t","type":"list","items":{"type":"secret"}}`,
"enum with no options": `{"name":"t","type":"list","items":{"type":"enum"}}`,
"options on a string": `{"name":"t","type":"list","items":{"type":"string","options":[{"value":"a","label":"A"}]}}`,
} {
t.Run(name, func(t *testing.T) {
s, err := df.Parse([]byte(`{"groups":[{"key":"g","fields":[` + field + `]}]}`))
if err != nil {
t.Fatal(err)
}
if _, err := df.Compile(s); err == nil {
t.Error("expected the items constraint to be rejected")
}
})
}
}
func TestCompileRejectsDuplicateOptionValues(t *testing.T) {
b := df.NewBuilder()
b.AddField(df.Enum("mode").Options(df.Opt("a", "First"), df.Opt("a", "Second")))
if _, err := b.Compile(); err == nil {
t.Error("two options with the same value mean one of them can never be chosen")
}
}