-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathplugin_test.go
More file actions
290 lines (236 loc) · 7.97 KB
/
Copy pathplugin_test.go
File metadata and controls
290 lines (236 loc) · 7.97 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
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
package config
import (
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// rpcConfig is the smallest valid configuration: a version and one section.
const rpcConfig = `version: "3"
rpc:
listen: tcp://127.0.0.1:6391
`
func TestInitRequiresPath(t *testing.T) {
p := &Plugin{}
require.ErrorContains(t, p.Init(), "path should be set")
}
func TestInitMissingFile(t *testing.T) {
p := &Plugin{Path: filepath.Join(t.TempDir(), ".rr.yaml")}
require.ErrorContains(t, p.Init(), "no such file")
}
// TestInitFromInlineConfig covers the short circuit taken when the configuration
// arrives as bytes: the file, the flags and the version check are all skipped,
// and the version stays empty instead of falling back to the default.
func TestInitFromInlineConfig(t *testing.T) {
p := &Plugin{
Type: "yaml",
ReadInCfg: []byte(`rpc:
listen: tcp://127.0.0.1:6391
`),
Flags: []string{"rpc.listen=tcp://127.0.0.1:6392"},
}
require.NoError(t, p.Init())
assert.Equal(t, "tcp://127.0.0.1:6391", p.Get("rpc.listen"))
assert.Empty(t, p.RRVersion())
}
func TestInitInlineConfigInvalidYAML(t *testing.T) {
p := &Plugin{Type: "yaml", ReadInCfg: []byte("rpc: [broken\n")}
require.ErrorContains(t, p.Init(), "yaml")
}
func TestInitRejectsMissingVersion(t *testing.T) {
p := &Plugin{Path: writeYAML(t, "rpc:\n listen: tcp://127.0.0.1:6391\n")}
require.ErrorContains(t, p.Init(), "rr configuration file should contain a version")
}
func TestInitRejectsNonStringVersion(t *testing.T) {
p := &Plugin{Path: writeYAML(t, "version: 3\n")}
err := p.Init()
require.ErrorContains(t, err, "version should be a string")
assert.ErrorContains(t, err, "actual type is: int")
}
// TestInitAcceptsPreviousVersion covers the deprecated 2.7 schema: it is still
// accepted, and the schema version is independent of the version the plugin
// reports, which keeps the value it was handed.
func TestInitAcceptsPreviousVersion(t *testing.T) {
p := &Plugin{
Path: writeYAML(t, `version: "2.7"
rpc:
listen: tcp://127.0.0.1:6391
`),
Version: "2025.1.1",
}
require.NoError(t, p.Init())
assert.Equal(t, "2025.1.1", p.RRVersion())
assert.Equal(t, "2.7", p.Get("version"))
}
func TestRRVersionDefaulting(t *testing.T) {
tests := []struct {
name string
version string
want string
}{
{name: "empty falls back to the default", version: "", want: "3"},
{name: "local falls back to the default", version: "local", want: "3"},
{name: "a real version is kept", version: "2025.1.1", want: "2025.1.1"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &Plugin{Path: writeYAML(t, rpcConfig), Version: tt.version}
require.NoError(t, p.Init())
assert.Equal(t, tt.want, p.RRVersion())
})
}
}
func TestFlagsOverrideFileValues(t *testing.T) {
t.Run("flag replaces a file value", func(t *testing.T) {
p := &Plugin{
Path: writeYAML(t, rpcConfig),
Flags: []string{"rpc.listen=tcp://127.0.0.1:6392"},
}
require.NoError(t, p.Init())
assert.Equal(t, "tcp://127.0.0.1:6392", p.Get("rpc.listen"))
})
t.Run("flag creates a key the file lacks", func(t *testing.T) {
p := &Plugin{
Path: writeYAML(t, rpcConfig),
Flags: []string{"logs.level=debug"},
}
require.NoError(t, p.Init())
assert.Equal(t, "debug", p.Get("logs.level"))
})
t.Run("flag value expands an env default", func(t *testing.T) {
p := &Plugin{
Path: writeYAML(t, rpcConfig),
Flags: []string{"rpc.listen=tcp://${CONFIG_TEST_UNSET_HOST:-127.0.0.1:6393}"},
}
require.NoError(t, p.Init())
assert.Equal(t, "tcp://127.0.0.1:6393", p.Get("rpc.listen"))
})
// Flags are applied before the version check, so a flag can supply the version
// a file does not carry.
t.Run("flag supplies a missing version", func(t *testing.T) {
p := &Plugin{
Path: writeYAML(t, "rpc:\n listen: tcp://127.0.0.1:6391\n"),
Flags: []string{"version=3"},
}
require.NoError(t, p.Init())
assert.Equal(t, "3", p.Get("version"))
})
}
func TestFlagErrorsAbortInit(t *testing.T) {
tests := []struct {
name string
flag string
want string
}{
{name: "no separator", flag: "rpc.listen", want: "invalid flag"},
{name: "empty key", flag: "=value", want: "key should not be empty"},
{name: "empty value", flag: "rpc.listen=", want: "value should not be empty"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &Plugin{Path: writeYAML(t, rpcConfig), Flags: []string{tt.flag}}
require.ErrorContains(t, p.Init(), tt.want)
})
}
}
func TestParseFlag(t *testing.T) {
tests := []struct {
name string
flag string
wantKey string
wantValue string
}{
{name: "plain pair", flag: "a=b", wantKey: "a", wantValue: "b"},
{name: "surrounding spaces are trimmed", flag: " a = b ", wantKey: "a", wantValue: "b"},
{name: "value keeps the second separator", flag: "a=b=c", wantKey: "a", wantValue: "b=c"},
{name: "double quotes are stripped", flag: `a="quoted value"`, wantKey: "a", wantValue: "quoted value"},
{name: "single quotes are stripped", flag: "a='quoted value'", wantKey: "a", wantValue: "quoted value"},
{name: "backticks are stripped", flag: "a=`quoted value`", wantKey: "a", wantValue: "quoted value"},
{name: "escaped quote is unescaped", flag: `a="say \"hi\" now"`, wantKey: "a", wantValue: `say "hi" now`},
// Init expands the value before storing it, parseFlag hands it over as is.
{name: "env reference is kept verbatim", flag: "a=${B:-c}", wantKey: "a", wantValue: "${B:-c}"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
key, value, err := parseFlag(tt.flag)
require.NoError(t, err)
assert.Equal(t, tt.wantKey, key)
assert.Equal(t, tt.wantValue, value)
})
}
}
func TestOverwriteReplacesValues(t *testing.T) {
p := initFromYAML(t, rpcConfig)
require.NoError(t, p.Overwrite(map[string]any{
"rpc.listen": "tcp://127.0.0.1:6392",
"logs.level": "debug",
}))
assert.Equal(t, "tcp://127.0.0.1:6392", p.Get("rpc.listen"))
assert.Equal(t, "debug", p.Get("logs.level"))
}
func TestPluginAccessors(t *testing.T) {
p := initFromYAML(t, rpcConfig)
assert.Equal(t, "config", p.Name())
assert.True(t, p.Has("rpc.listen"))
assert.False(t, p.Has("http.address"))
assert.Nil(t, p.Get("http.address"))
}
func TestGracefulTimeout(t *testing.T) {
p := &Plugin{Timeout: time.Second * 10}
assert.Equal(t, time.Second*10, p.GracefulTimeout())
}
func TestUnmarshalKey(t *testing.T) {
p := initFromYAML(t, rpcConfig)
out := struct {
Listen string `mapstructure:"listen"`
}{}
require.NoError(t, p.UnmarshalKey("rpc", &out))
assert.Equal(t, "tcp://127.0.0.1:6391", out.Listen)
mistyped := struct {
Listen int `mapstructure:"listen"`
}{}
err := p.UnmarshalKey("rpc", &mistyped)
require.ErrorContains(t, err, "config_plugin_unmarshal_key")
assert.ErrorContains(t, err, "cannot parse value as 'int'")
}
func TestUnmarshal(t *testing.T) {
p := initFromYAML(t, rpcConfig)
out := struct {
RPC struct {
Listen string `mapstructure:"listen"`
} `mapstructure:"rpc"`
}{}
require.NoError(t, p.Unmarshal(&out))
assert.Equal(t, "tcp://127.0.0.1:6391", out.RPC.Listen)
mistyped := struct {
RPC struct {
Listen int `mapstructure:"listen"`
} `mapstructure:"rpc"`
}{}
err := p.Unmarshal(&mistyped)
require.ErrorContains(t, err, "config_plugin_unmarshal")
assert.ErrorContains(t, err, "cannot parse value as 'int'")
}
func TestEnvVarsExpandedInFileValues(t *testing.T) {
t.Setenv("CONFIG_TEST_LISTEN", "tcp://127.0.0.1:6394")
p := initFromYAML(t, `version: "3"
rpc:
listen: ${CONFIG_TEST_LISTEN}
logs:
level: ${CONFIG_TEST_LEVEL:-error}
`)
assert.Equal(t, "tcp://127.0.0.1:6394", p.Get("rpc.listen"))
assert.Equal(t, "error", p.Get("logs.level"))
}
func TestEnvVarsExpandedInLists(t *testing.T) {
t.Setenv("CONFIG_TEST_ADDR_1", "localhost:2999")
t.Setenv("CONFIG_TEST_ADDR_2", "localhost:2998")
p := initFromYAML(t, `version: "3"
redis:
addrs:
- ${CONFIG_TEST_ADDR_1}
- ${CONFIG_TEST_ADDR_2}
`)
assert.Equal(t, []string{"localhost:2999", "localhost:2998"}, p.Get("redis.addrs"))
}