forked from obsidiandynamics/goharvest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
270 lines (245 loc) · 7.56 KB
/
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
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
package goharvest
import (
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/obsidiandynamics/goneli"
"github.com/obsidiandynamics/libstdgo/check"
"github.com/obsidiandynamics/libstdgo/scribe"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
)
func TestDefaultKafkaConsumerProvider(t *testing.T) {
c := Config{}
c.SetDefaults()
cons, err := c.KafkaConsumerProvider(&KafkaConfigMap{})
assert.Nil(t, cons)
if assert.NotNil(t, err) {
assert.Contains(t, err.Error(), "Required property")
}
}
func TestDefaultKafkaProducerProvider(t *testing.T) {
c := Config{}
c.SetDefaults()
prod, err := c.KafkaProducerProvider(&KafkaConfigMap{"foo": "bar"})
assert.Nil(t, prod)
if assert.NotNil(t, err) {
assert.Contains(t, err.Error(), "No such configuration property")
}
}
func TestDefaultNeliProvider(t *testing.T) {
c := Config{}
c.SetDefaults()
consMock := &consMock{}
consMock.fillDefaults()
prodMock := &prodMock{}
prodMock.fillDefaults()
neli, err := c.NeliProvider(goneli.Config{
KafkaConsumerProvider: convertKafkaConsumerProvider(mockKafkaConsumerProvider(consMock)),
KafkaProducerProvider: convertKafkaProducerProvider(mockKafkaProducerProvider(prodMock)),
}, goneli.NopBarrier())
assert.NotNil(t, neli)
assert.Nil(t, err)
assert.Nil(t, neli.Close())
}
func TestLimitsString(t *testing.T) {
lim := Limits{}
lim.SetDefaults()
assert.Contains(t, lim.String(), "Limits[")
}
func TestLimitsFromYaml(t *testing.T) {
const y = `
ioErrorBackoff: 10ms
pollDuration: 20ms
minPollInterval: 30ms
`
lim := Limits{}
err := yaml.UnmarshalStrict([]byte(y), &lim)
assert.Nil(t, err)
assert.Equal(t, 10*time.Millisecond, *lim.IOErrorBackoff)
assert.Equal(t, 20*time.Millisecond, *lim.PollDuration)
assert.Equal(t, 30*time.Millisecond, *lim.MinPollInterval)
lim.SetDefaults()
// Check that the defaults weren't overridden.
def := Limits{}
def.SetDefaults()
assert.Equal(t, *def.MarkBackoff, *lim.MarkBackoff)
}
func TestGetString(t *testing.T) {
assert.Equal(t, "some-default", getString("some-default", func() (string, error) { return "", check.ErrSimulated }))
assert.Equal(t, "some-string", getString("some-default", func() (string, error) { return "some-string", nil }))
}
func TestValidateLimits(t *testing.T) {
lim := Limits{}
lim.SetDefaults()
assert.Nil(t, lim.Validate())
lim = Limits{
IOErrorBackoff: Duration(-1),
PollDuration: Duration(time.Millisecond),
}
lim.SetDefaults()
if err := lim.Validate(); assert.NotNil(t, err) {
assert.Equal(t, "IOErrorBackoff: must be no less than 0.", lim.Validate().Error())
}
lim = Limits{
DrainInterval: Duration(0),
}
lim.SetDefaults()
if err := lim.Validate(); assert.NotNil(t, err) {
assert.Equal(t, "DrainInterval: cannot be blank.", lim.Validate().Error())
}
lim = Limits{
DrainInterval: Duration(1 * time.Nanosecond),
}
lim.SetDefaults()
if err := lim.Validate(); assert.NotNil(t, err) {
assert.Equal(t, "DrainInterval: must be no less than 1ms.", lim.Validate().Error())
}
}
func TestConfigString(t *testing.T) {
cfg := Config{}
cfg.SetDefaults()
assert.Contains(t, cfg.String(), "Config[")
}
func TestValidateConfig_valid(t *testing.T) {
cfg := Config{
BaseKafkaConfig: KafkaConfigMap{},
ProducerKafkaConfig: KafkaConfigMap{},
LeaderTopic: "leader-topic",
LeaderGroupID: "leader-group-d",
DataSource: "data-source",
OutboxTable: "outbox-table",
KafkaConsumerProvider: StandardKafkaConsumerProvider(),
KafkaProducerProvider: StandardKafkaProducerProvider(),
DatabaseBindingProvider: StandardPostgresBindingProvider(),
Scribe: scribe.New(scribe.StandardBinding()),
Name: "name",
}
cfg.SetDefaults()
assert.Nil(t, cfg.Validate())
}
func TestValidateConfig_invalidLimits(t *testing.T) {
cfg := Config{
BaseKafkaConfig: KafkaConfigMap{},
ProducerKafkaConfig: KafkaConfigMap{},
LeaderTopic: "leader-topic",
LeaderGroupID: "leader-group-id",
DataSource: "data-source",
OutboxTable: "outbox-table",
Limits: Limits{
SendConcurrency: Int(-1),
},
KafkaConsumerProvider: StandardKafkaConsumerProvider(),
KafkaProducerProvider: StandardKafkaProducerProvider(),
DatabaseBindingProvider: StandardPostgresBindingProvider(),
Scribe: scribe.New(scribe.StandardBinding()),
Name: "name",
}
cfg.SetDefaults()
assert.NotNil(t, cfg.Validate())
}
func TestValidateConfig_default(t *testing.T) {
cfg := Config{}
cfg.SetDefaults()
assert.Nil(t, cfg.Validate())
}
func TestDefaultDrainTimeout(t *testing.T) {
cfg := Config{
Limits: Limits{
HeartbeatTimeout: Duration(40 * time.Second),
},
}
cfg.SetDefaults()
assert.Equal(t, 20*time.Second, *cfg.Limits.MaxPollInterval)
assert.Equal(t, 20*time.Second, *cfg.Limits.DrainInterval)
cfg = Config{
Limits: Limits{
HeartbeatTimeout: Duration(40 * time.Second),
QueueTimeout: Duration(15 * time.Second),
},
}
cfg.SetDefaults()
assert.Equal(t, 20*time.Second, *cfg.Limits.MaxPollInterval)
assert.Equal(t, 15*time.Second, *cfg.Limits.DrainInterval)
}
func TestUnmarshal_fullyPopulated(t *testing.T) {
const y = `
baseKafkaConfig:
bootstrap.servers: localhost:9093
producerKafkaConfig:
compression.type: lz4
leaderTopic: leader-topic
leaderGroupID: leader-group-id
dataSource: data-source
outboxTable: outbox-table
limits:
ioErrorBackoff: 10ms
pollDuration: 20ms
minPollInterval: 30ms
maxPollInterval: 35ms
heartbeatTimeout: 15ms
drainInterval: 32ms
queueTimeout: 40ms
markBackoff: 50ms
maxInFlightRecords: 60
sendConcurrency: 70
sendBuffer: 80
minMetricsInterval: 90ms
name: test-name
`
cfg, err := Unmarshal([]byte(y))
require.Nil(t, err)
if !assert.Nil(t, cfg.Validate()) {
t.Errorf("Validation error: %s", cfg.Validate().Error())
}
exp := Config{
BaseKafkaConfig: KafkaConfigMap{
"bootstrap.servers": "localhost:9093",
},
ProducerKafkaConfig: KafkaConfigMap{
"compression.type": "lz4",
},
LeaderTopic: "leader-topic",
LeaderGroupID: "leader-group-id",
DataSource: "data-source",
OutboxTable: "outbox-table",
Limits: Limits{
IOErrorBackoff: Duration(10 * time.Millisecond),
PollDuration: Duration(20 * time.Millisecond),
MinPollInterval: Duration(30 * time.Millisecond),
MaxPollInterval: Duration(35 * time.Millisecond),
HeartbeatTimeout: Duration(15 * time.Millisecond),
DrainInterval: Duration(32 * time.Millisecond),
QueueTimeout: Duration(40 * time.Millisecond),
MarkBackoff: Duration(50 * time.Millisecond),
MaxInFlightRecords: Int(60),
SendConcurrency: Int(70),
SendBuffer: Int(80),
MinMetricsInterval: Duration(90 * time.Millisecond),
},
Name: "test-name",
}
exp.SetDefaults()
ignoreFields := cmpopts.IgnoreFields(
Config{},
"KafkaConsumerProvider", "KafkaProducerProvider", "DatabaseBindingProvider", "NeliProvider", "Scribe",
)
assert.True(t, cmp.Equal(exp, cfg, ignoreFields), "Diff: %v", cmp.Diff(exp, cfg, ignoreFields))
}
func TestUnmarshal_empty(t *testing.T) {
const y = ``
cfg, err := Unmarshal([]byte(y))
assert.Nil(t, err)
if !assert.Nil(t, cfg.Validate()) {
t.Errorf("Validation error: %s", cfg.Validate().Error())
}
exp := Config{}
exp.SetDefaults()
ignoreFields := cmpopts.IgnoreFields(
Config{},
"KafkaConsumerProvider", "KafkaProducerProvider", "DatabaseBindingProvider", "NeliProvider", "Scribe", "Name",
)
assert.True(t, cmp.Equal(exp, cfg, ignoreFields), "Diff: %v", cmp.Diff(exp, cfg, ignoreFields))
}