forked from AdRoll/baker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_config_test.go
167 lines (133 loc) · 3.66 KB
/
user_config_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
package baker_test
import (
"reflect"
"strings"
"testing"
"github.com/AdRoll/baker"
"github.com/AdRoll/baker/input/inputtest"
"github.com/AdRoll/baker/output/outputtest"
)
func fillComponentsAndLoadConfig(t *testing.T, toml string, user ...baker.UserDesc) (*baker.Config, error) {
t.Helper()
const base = `
[fields]
names=["f0", "f1", "f2", "f3"]
[input]
name="random"
[output]
name="recorder"
`
comp := baker.Components{
Inputs: []baker.InputDesc{inputtest.RandomDesc},
Outputs: []baker.OutputDesc{outputtest.RecorderDesc},
User: user,
}
return baker.NewConfigFromToml(strings.NewReader(base+toml), comp)
}
func TestUserConfigSimple(t *testing.T) {
// This test checks that a single user configuration is correctly decoded.
const toml = `
[[user]]
name="MyConfiG"
[user.config]
field1 = 1
field2 = "hello!"`
type myConfig struct {
Field1 int
Field2 string
}
mycfg := myConfig{}
userCfg := baker.UserDesc{Name: "myconfig", Config: &mycfg}
_, err := fillComponentsAndLoadConfig(t, toml, userCfg)
if err != nil {
t.Fatal(err)
}
want := myConfig{Field1: 1, Field2: "hello!"}
if mycfg != want {
t.Errorf("got %#v, want %#v", mycfg, want)
}
}
func TestUserConfigMultiple(t *testing.T) {
// This test checks that we can provide multiple user configurations.
const toml = `
# This is user config configA
[[user]]
name="configA"
[user.config]
field1 = 23
# This is user config configB
[[user]]
name="configB"
# with a comment
[user.config]
field1 = ["a", "b", "c", "d"]`
type configA struct{ Field1 int }
cfga := configA{}
ucfga := baker.UserDesc{Name: "configa", Config: &cfga}
type configB struct{ Field1 []string }
cfgb := configB{}
ucfgb := baker.UserDesc{Name: "configb", Config: &cfgb}
_, err := fillComponentsAndLoadConfig(t, toml, ucfgb, ucfga)
if err != nil {
t.Fatal(err)
}
wanta := configA{Field1: 23}
if cfga != wanta {
t.Errorf("configa: got %#v, want %#v", cfga, wanta)
}
wantb := configB{Field1: []string{"a", "b", "c", "d"}}
if !reflect.DeepEqual(cfgb, wantb) {
t.Errorf("configb: got %#v, want %#v", cfgb, wantb)
}
}
func TestUserConfigExtraConfigInTOML(t *testing.T) {
// This test checks that each user configuration in TOML must be defined
// of NewConfigFromToml fails.
const toml = `
# This is defined in baker.UserDesc
[[user]]
name="configA"
[user.config]
field1 = 23
# This is not defined in baker.UserDesc
[[user]]
name="configB"
# with a comment
[user.config]
field1 = ["a", "b", "c", "d"]`
type configA struct{ Field1 int }
cfga := configA{}
ucfga := baker.UserDesc{Name: "configa", Config: &cfga}
_, err := fillComponentsAndLoadConfig(t, toml, ucfga)
if err == nil {
t.Errorf(`want an error since configB is not defined as a baker.UserDesc, got nil`)
}
}
func TestUserConfigExtraConfigDefinition(t *testing.T) {
// This test checks that NewConfigFromToml succeeds if some user
// configurations have been defined but do not exist in TOML.
const toml = `
# This is defined in baker.UserDesc
[[user]]
name="configA"
[user.config]
field1 = 23`
type configA struct{ Field1 int }
cfga := configA{}
ucfga := baker.UserDesc{Name: "configa", Config: &cfga}
type configB struct{ Field1 []string }
cfgb := configB{}
ucfgb := baker.UserDesc{Name: "configb", Config: &cfgb}
_, err := fillComponentsAndLoadConfig(t, toml, ucfgb, ucfga)
if err != nil {
t.Fatal(err)
}
wanta := configA{Field1: 23}
if cfga != wanta {
t.Errorf("configa: got %#v, want %#v", cfga, wanta)
}
wantb := configB{} // zero value
if !reflect.DeepEqual(cfgb, wantb) {
t.Errorf("configb: got %#v, want %#v", cfgb, wantb)
}
}