This repository was archived by the owner on Dec 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
273 lines (234 loc) · 6 KB
/
Copy pathmain_test.go
File metadata and controls
273 lines (234 loc) · 6 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
package main
import (
"net/http"
"os"
"path/filepath"
"testing"
"time"
"github.com/fsnotify/fsnotify"
"github.com/spicyneuron/llama-matchmaker/config"
)
type fakeWatcher struct {
added []string
closed bool
events chan fsnotify.Event
errs chan error
}
func newFakeWatcher() *fakeWatcher {
return &fakeWatcher{
events: make(chan fsnotify.Event, 2),
errs: make(chan error, 1),
}
}
func (f *fakeWatcher) Add(name string) error {
f.added = append(f.added, name)
return nil
}
func (f *fakeWatcher) Close() error {
if f.closed {
return nil
}
f.closed = true
close(f.events)
close(f.errs)
return nil
}
func (f *fakeWatcher) Events() <-chan fsnotify.Event {
return f.events
}
func (f *fakeWatcher) Errors() <-chan error {
return f.errs
}
func TestSetWatcherReplacesPrevious(t *testing.T) {
origFactory := watchFactory
origWatcher := configWatcher
defer func() {
watchFactory = origFactory
configWatcher = origWatcher
closeWatcher()
}()
first := newFakeWatcher()
second := newFakeWatcher()
created := 0
watchFactory = func() (fileWatcher, error) {
defer func() { created++ }()
if created == 0 {
return first, nil
}
return second, nil
}
if err := setWatcher([]string{"a", "b"}); err != nil {
t.Fatalf("setWatcher initial: %v", err)
}
if len(first.added) != 2 {
t.Fatalf("expected first watcher to add 2 paths, got %d", len(first.added))
}
if err := setWatcher([]string{"c"}); err != nil {
t.Fatalf("setWatcher replace: %v", err)
}
if !first.closed {
t.Fatalf("expected first watcher to be closed when replaced")
}
if len(second.added) != 1 || second.added[0] != "c" {
t.Fatalf("expected second watcher to add new path, got %v", second.added)
}
closeWatcher()
if !second.closed {
t.Fatalf("expected second watcher to be closed on cleanup")
}
// Should be safe to call twice
closeWatcher()
}
func TestReloadConfigUpdatesWatcherAndTriggersOnNewFile(t *testing.T) {
tmpDir := t.TempDir()
includePath := filepath.Join(tmpDir, "include.yml")
certPath := filepath.Join(tmpDir, "cert-new.pem")
keyPath := filepath.Join(tmpDir, "key-new.pem")
configPath := filepath.Join(tmpDir, "config.yml")
if err := writeFile(includePath, `
- methods: GET
paths: /.*
on_request:
- merge:
injected: true
`); err != nil {
t.Fatalf("failed to write include: %v", err)
}
if err := writeFile(configPath, `
proxy:
listen: "localhost:0"
target: "http://example.com"
ssl_cert: "`+filepath.Base(certPath)+`"
ssl_key: "`+filepath.Base(keyPath)+`"
routes:
- include: "`+filepath.Base(includePath)+`"
`); err != nil {
t.Fatalf("failed to write config: %v", err)
}
origFactory := watchFactory
origWatcher := configWatcher
origPaths := configPaths
origOverrides := overrides
origStart := startAllProxiesFn
origStop := stopAllProxiesFn
origReload := reloadConfigFn
defer func() {
watchFactory = origFactory
configWatcher = origWatcher
configPaths = origPaths
overrides = origOverrides
startAllProxiesFn = origStart
stopAllProxiesFn = origStop
reloadConfigFn = origReload
reloadMutex.Lock()
if reloadTimer != nil {
reloadTimer.Stop()
}
reloadMutex.Unlock()
closeWatcher()
}()
var watchers []*fakeWatcher
watchFactory = func() (fileWatcher, error) {
fw := newFakeWatcher()
watchers = append(watchers, fw)
return fw, nil
}
startAllProxiesFn = func(*config.Config) error { return nil }
stopAllProxiesFn = func() {}
trigger := make(chan struct{})
reloadConfigFn = func() {
select {
case <-trigger:
default:
close(trigger)
}
}
configPaths = []string{configPath}
overrides = config.CliOverrides{}
reloadConfig()
if len(watchers) != 1 {
t.Fatalf("expected one watcher to be created, got %d", len(watchers))
}
added := watchers[0].added
assertContains := func(path string) {
for _, p := range added {
if filepath.Base(p) == filepath.Base(path) {
return
}
}
t.Fatalf("expected watcher to include %s, got %v", path, added)
}
assertContains(configPath)
assertContains(includePath)
assertContains(certPath)
assertContains(keyPath)
// Simulate change on newly watched include file
watchers[0].events <- fsnotify.Event{Name: includePath, Op: fsnotify.Write}
select {
case <-trigger:
case <-time.After(2 * time.Second):
t.Fatal("expected reloadConfigFn to be invoked after file change")
}
}
func TestCreateServerTimeouts(t *testing.T) {
tests := []struct {
name string
timeout time.Duration
wantIdle time.Duration
wantRead time.Duration
wantWrite time.Duration
}{
{
name: "zero timeout",
timeout: 0,
wantIdle: 0,
wantRead: 0,
wantWrite: 0,
},
{
name: "with timeout",
timeout: 30 * time.Second,
wantIdle: 30 * time.Second,
wantRead: 0,
wantWrite: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := config.ProxyConfig{
Listen: "localhost:8080",
Target: "http://localhost:3000",
Timeout: tt.timeout,
}
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
server := CreateServer(cfg, handler)
if server.Addr != cfg.Listen {
t.Errorf("Server.Addr = %s, want %s", server.Addr, cfg.Listen)
}
if server.IdleTimeout != tt.wantIdle {
t.Errorf("Server.IdleTimeout = %v, want %v", server.IdleTimeout, tt.wantIdle)
}
if server.ReadTimeout != tt.wantRead {
t.Errorf("Server.ReadTimeout = %v, want %v (must be 0 for streaming)", server.ReadTimeout, tt.wantRead)
}
if server.WriteTimeout != tt.wantWrite {
t.Errorf("Server.WriteTimeout = %v, want %v (must be 0 for streaming)", server.WriteTimeout, tt.wantWrite)
}
})
}
}
func TestCreateServerWithoutTLSConfig(t *testing.T) {
cfg := config.ProxyConfig{
Listen: "localhost:8080",
Target: "http://localhost:3000",
}
server := CreateServer(cfg, http.NewServeMux())
if server.TLSConfig != nil {
t.Fatalf("Expected TLSConfig to be nil when no cert/key provided")
}
}
func writeFile(path, content string) error {
return os.WriteFile(path, []byte(content), 0644)
}