-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_fetch_test.go
More file actions
460 lines (444 loc) · 15.3 KB
/
Copy pathconfig_fetch_test.go
File metadata and controls
460 lines (444 loc) · 15.3 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
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
package domainfront
import (
"context"
"encoding/json"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func refreshClient(t *testing.T, urls ...string) *Client {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
t.Cleanup(cancel)
c := &Client{ctx: ctx, log: slog.Default(), httpClient: http.DefaultClient, pool: newFrontPool(10), gate: newPauseGate(), configURLs: urls}
require.NoError(t, c.applyConfig(seedConfig("seed")))
return c
}
func receiveConfigHeader(t *testing.T, headers <-chan http.Header) http.Header {
t.Helper()
select {
case h := <-headers:
return h
case <-time.After(time.Second):
t.Fatal("config request did not arrive")
return nil
}
}
func TestConditionalConfigPreservesReadyPool(t *testing.T) {
for _, mode := range []string{"etag", "last-modified", "identical-200"} {
t.Run(mode, func(t *testing.T) {
payload := gzConfig(t, "cached")
headers := make(chan http.Header, 4)
var requests atomic.Int32
modified := "Mon, 14 Sep 2026 00:00:00 GMT"
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
headers <- r.Header.Clone()
n := requests.Add(1)
w.Header().Set("Last-Modified", modified)
if mode != "last-modified" {
w.Header().Set("ETag", `W/"version-one"`)
}
if n > 1 && mode != "identical-200" {
w.WriteHeader(http.StatusNotModified)
return
}
_, _ = w.Write(payload)
}))
defer server.Close()
c := refreshClient(t, server.URL)
c.configCachePath = filepath.Join(t.TempDir(), "fronted.yaml.gz")
c.fetchAndApplyConfig()
initial := receiveConfigHeader(t, headers)
assert.Empty(t, initial.Get("If-None-Match"))
assert.Empty(t, initial.Get("If-Modified-Since"))
require.True(t, hasProvider(c, "cached"))
ready := seedReadyFront(t, c)
c.gate.pause()
oldTime := time.Unix(100000, 0)
require.NoError(t, os.Chtimes(c.configCachePath, oldTime, oldTime))
done := make(chan struct{})
go func() { c.fetchAndApplyConfig(); close(done) }()
select {
case <-done:
case <-time.After(time.Second):
t.Fatal("unchanged refresh waited for resume")
}
next := receiveConfigHeader(t, headers)
if mode == "last-modified" {
assert.Empty(t, next.Get("If-None-Match"))
assert.Equal(t, modified, next.Get("If-Modified-Since"))
} else {
assert.Equal(t, `W/"version-one"`, next.Get("If-None-Match"))
assert.Empty(t, next.Get("If-Modified-Since"), "ETag takes precedence")
}
assert.Same(t, ready, c.pool.candidates()[0])
assert.Equal(t, 1, c.pool.readyCount())
info, err := os.Stat(c.configCachePath)
require.NoError(t, err)
assert.Equal(t, oldTime, info.ModTime(), "unchanged payload must not be rewritten")
})
}
}
func TestConfig304DoesNotCancelChangedSource(t *testing.T) {
old := gzConfig(t, "old")
updated := gzConfig(t, "updated")
var phase atomic.Int32
mirrorReturned := make(chan struct{}, 1)
release := make(chan struct{})
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("ETag", `"`+r.URL.Path+`"`)
if phase.Load() == 0 {
_, _ = w.Write(old)
return
}
if r.URL.Path == "/mirror" {
assert.Equal(t, `"/mirror"`, r.Header.Get("If-None-Match"))
w.WriteHeader(http.StatusNotModified)
w.(http.Flusher).Flush()
mirrorReturned <- struct{}{}
return
}
assert.Equal(t, `"/primary"`, r.Header.Get("If-None-Match"))
select {
case <-release:
case <-r.Context().Done():
return
}
w.Header().Set("ETag", `"new-primary"`)
_, _ = w.Write(updated)
}))
defer server.Close()
c := refreshClient(t, server.URL+"/primary")
c.fetchAndApplyConfig()
c.configURLs = []string{server.URL + "/mirror"}
c.fetchAndApplyConfig()
c.configURLs = []string{server.URL + "/mirror", server.URL + "/primary"}
phase.Store(1)
done := make(chan struct{})
go func() { c.fetchAndApplyConfig(); close(done) }()
select {
case <-mirrorReturned:
case <-time.After(time.Second):
t.Fatal("mirror did not respond")
}
select {
case <-done:
t.Fatal("304 canceled the changed source")
case <-time.After(30 * time.Millisecond):
}
close(release)
select {
case <-done:
case <-time.After(time.Second):
t.Fatal("updated config did not apply")
}
assert.True(t, hasProvider(c, "updated"))
assert.NotContains(t, c.configValidators, server.URL+"/mirror", "old-payload validators must be discarded")
assert.Equal(t, `"new-primary"`, c.configValidators[server.URL+"/primary"].ETag)
}
func TestConfigValidatorsStayScopedToURL(t *testing.T) {
payload := gzConfig(t, "shared")
headers := make(chan http.Header, 8)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
headers <- r.Header.Clone()
tag := `"` + r.URL.Path + `"`
w.Header().Set("ETag", tag)
if r.Header.Get("If-None-Match") == tag {
w.WriteHeader(http.StatusNotModified)
return
}
_, _ = w.Write(payload)
}))
defer server.Close()
c := refreshClient(t, server.URL+"/one")
c.fetchAndApplyConfig()
assert.Empty(t, receiveConfigHeader(t, headers).Get("If-None-Match"))
c.configURLs = []string{server.URL + "/two"}
c.fetchAndApplyConfig()
assert.Empty(t, receiveConfigHeader(t, headers).Get("If-None-Match"), "do not reuse another URL's ETag")
c.configURLs = []string{server.URL + "/one", server.URL + "/two"}
c.fetchAndApplyConfig()
tags := []string{receiveConfigHeader(t, headers).Get("If-None-Match"), receiveConfigHeader(t, headers).Get("If-None-Match")}
assert.ElementsMatch(t, []string{`"/one"`, `"/two"`}, tags)
}
func TestConfigValidatorsFollowSuccessfulResponses(t *testing.T) {
good := gzConfig(t, "good")
invalidCA, err := CompressConfig([]byte("trustedcas:\n - commonname: invalid\n cert: broken\nproviders:\n bad:\n masquerades: []\n"))
require.NoError(t, err)
for _, failure := range []string{"bad-gzip", "bad-cert", "http-error"} {
t.Run(failure, func(t *testing.T) {
var requests atomic.Int32
headers := make(chan http.Header, 4)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
headers <- r.Header.Clone()
if requests.Add(1) == 1 {
w.Header().Set("ETag", `"good"`)
_, _ = w.Write(good)
return
}
w.Header().Set("ETag", `"bad"`)
switch failure {
case "bad-gzip":
_, _ = w.Write([]byte("broken"))
case "bad-cert":
_, _ = w.Write(invalidCA)
default:
w.WriteHeader(http.StatusInternalServerError)
}
}))
defer server.Close()
c := refreshClient(t, server.URL)
c.fetchAndApplyConfig()
receiveConfigHeader(t, headers)
ready := seedReadyFront(t, c)
c.fetchAndApplyConfig()
assert.Equal(t, `"good"`, receiveConfigHeader(t, headers).Get("If-None-Match"))
assert.True(t, hasProvider(c, "good"))
assert.False(t, hasProvider(c, "bad"), "failed apply must not replace active provider state")
assert.Same(t, ready, c.pool.candidates()[0])
assert.Equal(t, `"good"`, c.configValidators[server.URL].ETag)
})
}
}
func TestConfigMetadataRequiresMatchingUsablePayload(t *testing.T) {
payload := gzConfig(t, "persisted")
invalid, err := CompressConfig([]byte("providers: {}\n"))
require.NoError(t, err)
for _, mode := range []string{"valid", "no-metadata", "bad-metadata", "wrong-hash", "changed-payload", "no-payload", "invalid-payload", "oversized-metadata", "bad-last-modified", "valid-last-modified"} {
t.Run(mode, func(t *testing.T) {
headers := make(chan http.Header, 1)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
headers <- r.Header.Clone()
w.WriteHeader(http.StatusNotModified)
}))
defer server.Close()
path := filepath.Join(t.TempDir(), "fronted.yaml.gz")
data := payload
if mode == "changed-payload" {
data = gzConfig(t, "changed")
}
if mode == "invalid-payload" {
data = invalid
}
if mode != "no-payload" {
require.NoError(t, os.WriteFile(path, data, 0600))
}
hash := configDigest(payload)
if mode == "wrong-hash" {
hash = "not-the-payload"
}
if mode == "invalid-payload" {
hash = configDigest(data)
}
validator := configValidator{ETag: `"persisted"`}
if mode == "bad-last-modified" {
validator = configValidator{LastModified: "not an HTTP date"}
}
if mode == "valid-last-modified" {
validator = configValidator{LastModified: "Mon, 14 Sep 2026 00:00:00 GMT"}
}
metadata, err := json.Marshal(configMetadata{SHA256: hash, Validators: map[string]configValidator{server.URL: validator}})
require.NoError(t, err)
if mode == "bad-metadata" {
metadata = []byte("invalid json")
}
if mode == "oversized-metadata" {
metadata = append(metadata, []byte(strings.Repeat(" ", 64<<10))...)
}
if mode != "no-metadata" {
require.NoError(t, os.WriteFile(path+".http.json", metadata, 0600))
}
c, err := New(context.Background(), seedConfig("seed"), WithConfigCacheFile(path), WithConfigURL(server.URL), WithDialer(noDialer{}))
require.NoError(t, err)
h := receiveConfigHeader(t, headers)
c.Close()
if mode == "valid" {
assert.Equal(t, `"persisted"`, h.Get("If-None-Match"))
} else {
assert.Empty(t, h.Get("If-None-Match"))
}
if mode == "valid-last-modified" {
assert.Equal(t, "Mon, 14 Sep 2026 00:00:00 GMT", h.Get("If-Modified-Since"))
} else {
assert.Empty(t, h.Get("If-Modified-Since"))
}
if mode == "no-payload" || mode == "invalid-payload" {
assert.True(t, hasProvider(c, "seed"))
}
})
}
}
func TestConfigValidatorsPersistAcrossRestart(t *testing.T) {
payload := gzConfig(t, "cached")
headers := make(chan http.Header, 4)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
headers <- r.Header.Clone()
w.Header().Set("ETag", `W/"persisted"`)
if r.Header.Get("If-None-Match") != "" {
w.WriteHeader(http.StatusNotModified)
return
}
_, _ = w.Write(payload)
}))
defer server.Close()
c := refreshClient(t, server.URL)
c.configCachePath = filepath.Join(t.TempDir(), "fronted.yaml.gz")
c.fetchAndApplyConfig()
receiveConfigHeader(t, headers)
restarted, err := New(context.Background(), seedConfig("seed"), WithConfigCacheFile(c.configCachePath), WithConfigURL(server.URL), WithDialer(noDialer{}))
require.NoError(t, err)
defer restarted.Close()
assert.Equal(t, `W/"persisted"`, receiveConfigHeader(t, headers).Get("If-None-Match"))
assert.True(t, hasProvider(restarted, "cached"))
}
func TestUnsolicited304IsIgnored(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("ETag", `"unknown"`)
w.WriteHeader(http.StatusNotModified)
}))
defer server.Close()
c := refreshClient(t, server.URL)
c.fetchAndApplyConfig()
assert.True(t, hasProvider(c, "seed"))
assert.Empty(t, c.configHash)
assert.Empty(t, c.configValidators)
}
func TestConfigRefreshCancellation(t *testing.T) {
entered := make(chan struct{}, 1)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
entered <- struct{}{}
<-r.Context().Done()
}))
defer server.Close()
c := refreshClient(t, server.URL)
ctx, cancel := context.WithCancel(c.ctx)
defer cancel()
c.ctx = ctx
done := make(chan struct{})
go func() { c.fetchAndApplyConfig(); close(done) }()
select {
case <-entered:
case <-time.After(time.Second):
t.Fatal("request did not start")
}
cancel()
select {
case <-done:
case <-time.After(time.Second):
t.Fatal("canceled refresh did not stop")
}
assert.Empty(t, c.configValidators)
}
func TestConfigResponseRefreshesOrClearsValidators(t *testing.T) {
payload := gzConfig(t, "cached")
var requests atomic.Int32
headers := make(chan http.Header, 4)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
headers <- r.Header.Clone()
switch requests.Add(1) {
case 1:
w.Header().Set("ETag", `"first"`)
case 2:
w.Header().Set("ETag", `"second"`)
w.WriteHeader(http.StatusNotModified)
return
}
_, _ = w.Write(payload)
}))
defer server.Close()
c := refreshClient(t, server.URL)
for _, want := range []string{"", `"first"`, `"second"`, ""} {
c.fetchAndApplyConfig()
assert.Equal(t, want, receiveConfigHeader(t, headers).Get("If-None-Match"))
}
assert.Empty(t, c.configValidators)
}
func BenchmarkConfigRefresh(b *testing.B) {
payload, err := os.ReadFile("fronted.yaml.gz")
if err != nil {
b.Fatal(err)
}
for _, mode := range []string{"full-refresh", "identical-200", "not-modified-304"} {
b.Run(mode, func(b *testing.B) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("ETag", `"benchmark"`)
if mode == "not-modified-304" && r.Header.Get("If-None-Match") != "" {
w.WriteHeader(http.StatusNotModified)
return
}
_, _ = w.Write(payload)
}))
defer server.Close()
c := &Client{ctx: context.Background(), log: slog.New(slog.NewTextHandler(io.Discard, nil)), httpClient: server.Client(), pool: newFrontPool(10), gate: newPauseGate(), configURLs: []string{server.URL}}
c.fetchAndApplyConfig()
if c.configHash == "" {
b.Fatal("initial fetch failed")
}
b.ReportAllocs()
for b.Loop() {
if mode == "full-refresh" {
// Forget the active payload to measure parsing and rebuilding every time.
c.configHash = ""
c.configValidators = nil
}
c.fetchAndApplyConfig()
}
})
}
}
func TestConfigMetadataRejectsPartialValidatorRestore(t *testing.T) {
payload := gzConfig(t, "persisted")
headers := make(chan http.Header, 2)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
headers <- r.Header.Clone()
w.WriteHeader(http.StatusNotModified)
}))
defer server.Close()
path := filepath.Join(t.TempDir(), "fronted.yaml.gz")
require.NoError(t, os.WriteFile(path, payload, 0600))
metadata, err := json.Marshal(configMetadata{
SHA256: configDigest(payload),
Validators: map[string]configValidator{
server.URL + "/good": {ETag: `"good"`, LastModified: "Mon, 14 Sep 2026 00:00:00 GMT"},
server.URL + "/bad": {ETag: `"bad"`, LastModified: "corrupt date"},
},
})
require.NoError(t, err)
require.NoError(t, os.WriteFile(path+".http.json", metadata, 0600))
c, err := New(context.Background(), seedConfig("seed"), WithConfigCacheFile(path), WithConfigURL(server.URL+"/good", server.URL+"/bad"), WithDialer(noDialer{}))
require.NoError(t, err)
defer c.Close()
for range 2 {
h := receiveConfigHeader(t, headers)
assert.Empty(t, h.Get("If-None-Match"))
assert.Empty(t, h.Get("If-Modified-Since"))
}
}
func TestReceiveConfigResponseDrainsQueueAfterDeadline(t *testing.T) {
ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(-time.Second))
defer cancel()
require.ErrorIs(t, ctx.Err(), context.DeadlineExceeded)
for range 100 {
results := make(chan *configResponse, 2)
results <- nil
expected := &configResponse{url: "https://example.com/config", data: []byte("completed response")}
results <- expected
failed, ok := receiveConfigResponse(ctx, results)
require.True(t, ok)
require.Nil(t, failed)
got, ok := receiveConfigResponse(ctx, results)
require.True(t, ok, "a ready deadline must not discard queued responses")
require.Same(t, expected, got)
_, ok = receiveConfigResponse(ctx, results)
require.False(t, ok, "stop waiting once the deadline has passed and the queue is empty")
}
}