-
Notifications
You must be signed in to change notification settings - Fork 353
/
Copy pathwatch_test.go
238 lines (203 loc) · 5.47 KB
/
watch_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
package eskipfile
import (
"net/http"
"net/url"
"os"
"testing"
"time"
"github.com/zalando/skipper/filters/builtin"
"github.com/zalando/skipper/logging/loggingtest"
"github.com/zalando/skipper/routing"
)
const testWatchFile = "fixtures/watch-test.eskip"
const testWatchFileContent = `
foo: Path("/foo") -> setPath("/") -> "https://foo.example.org";
bar: Path("/bar") -> setPath("/") -> "https://bar.example.org";
baz: Path("/baz") -> setPath("/") -> "https://baz.example.org";
`
const testWatchFileInvalidContent = `
invalid eskip
`
const testWatchFileUpdatedContent = `
foo: Path("/foo") -> setPath("/") -> "https://foo.example.org";
baz: Path("/baz") -> setPath("/") -> "https://baz-new.example.org";
`
type watchTest struct {
testing *testing.T
log *loggingtest.Logger
file *WatchClient
routing *routing.Routing
}
func deleteFile(t *testing.T) {
err := os.Remove(testWatchFile)
if err != nil {
t.Logf("Ignoring %v", err)
}
}
func createFileWith(content string, t *testing.T) {
tmpfile, err := os.Create(testWatchFile + ".tmp")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())
_, err = tmpfile.WriteString(content)
if err != nil {
t.Fatal(err)
}
err = os.Rename(tmpfile.Name(), testWatchFile)
if err != nil {
t.Fatal(err)
}
}
func createFile(t *testing.T) {
createFileWith(testWatchFileContent, t)
}
func invalidFile(t *testing.T) {
createFileWith(testWatchFileInvalidContent, t)
}
func updateFile(t *testing.T) {
createFileWith(testWatchFileUpdatedContent, t)
}
func initWatchTest(t *testing.T) *watchTest {
l := loggingtest.New()
f := Watch(testWatchFile)
return &watchTest{
testing: t,
log: l,
file: f,
routing: routing.New(routing.Options{
Log: l,
FilterRegistry: builtin.MakeRegistry(),
DataClients: []routing.DataClient{f},
PollTimeout: 15 * time.Millisecond,
}),
}
}
func (t *watchTest) testFail(path string) {
if r, _ := t.routing.Route(&http.Request{URL: &url.URL{Path: path}}); r != nil {
t.testing.Log("got: ", r.Id)
t.testing.Log("expected: nil")
t.testing.Fatalf("unexpected route received for: %v", path)
}
}
func (t *watchTest) testSuccess(id, path, backend string) {
r, _ := t.routing.Route(&http.Request{URL: &url.URL{Path: path}})
if r == nil {
t.testing.Fatalf("failed to load route for: %v", path)
return
}
if r.Id != id || r.Backend != backend {
t.testing.Log("got: ", r.Id, backend)
t.testing.Log("expected:", id, r.Backend)
t.testing.Fatal("unexpected route received")
}
}
func (t *watchTest) timeoutInitial() {
defer t.log.Reset()
err := t.log.WaitFor("route settings applied", 90*time.Millisecond)
if err == nil {
t.testing.Fatal("expected timeout, got route settings applied")
} else if err != loggingtest.ErrWaitTimeout {
t.testing.Fatalf("unexpected error: %v", err)
}
}
func (t *watchTest) timeoutAndSucceedInitial() {
defer t.log.Reset()
if err := t.log.WaitFor("route settings applied", 90*time.Millisecond); err == nil {
t.testing.Fatal("unexpected change detected")
}
t.testSuccess("foo", "/foo", "https://foo.example.org")
t.testSuccess("bar", "/bar", "https://bar.example.org")
t.testSuccess("baz", "/baz", "https://baz.example.org")
}
func (t *watchTest) waitAndFailInitial() {
defer t.log.Reset()
if err := t.log.WaitFor("route settings applied", 90*time.Millisecond); err != nil {
t.testing.Fatal(err)
}
t.testFail("/foo")
t.testFail("/bar")
t.testFail("/baz")
}
func (t *watchTest) waitAndSucceedInitial() {
defer t.log.Reset()
if err := t.log.WaitFor("route settings applied", 90*time.Millisecond); err != nil {
t.testing.Fatal(err)
}
t.testSuccess("foo", "/foo", "https://foo.example.org")
t.testSuccess("bar", "/bar", "https://bar.example.org")
t.testSuccess("baz", "/baz", "https://baz.example.org")
}
func (t *watchTest) waitAndSucceedUpdated() {
defer t.log.Reset()
if err := t.log.WaitFor("route settings applied", 90*time.Millisecond); err != nil {
t.testing.Fatal(err)
}
t.testSuccess("foo", "/foo", "https://foo.example.org")
t.testFail("/bar")
t.testSuccess("baz", "/baz", "https://baz-new.example.org")
}
func (t *watchTest) close() {
t.log.Close()
t.file.Close()
t.routing.Close()
}
func TestWatchInitialFails(t *testing.T) {
test := initWatchTest(t)
defer test.close()
test.timeoutInitial()
}
func TestWatchInitialRecovers(t *testing.T) {
test := initWatchTest(t)
defer test.close()
test.timeoutInitial()
createFile(t)
defer deleteFile(t)
test.waitAndSucceedInitial()
}
func TestWatchUpdateFails(t *testing.T) {
createFile(t)
defer deleteFile(t)
test := initWatchTest(t)
defer test.close()
test.waitAndSucceedInitial()
invalidFile(t)
test.timeoutAndSucceedInitial()
}
func TestWatchUpdateRecover(t *testing.T) {
createFile(t)
defer deleteFile(t)
test := initWatchTest(t)
defer test.close()
test.waitAndSucceedInitial()
invalidFile(t)
test.timeoutAndSucceedInitial()
updateFile(t)
test.waitAndSucceedUpdated()
}
func TestInitialAndUnchanged(t *testing.T) {
createFile(t)
defer deleteFile(t)
test := initWatchTest(t)
defer test.close()
test.waitAndSucceedInitial()
test.timeoutAndSucceedInitial()
}
func TestInitialAndDeleteFile(t *testing.T) {
createFile(t)
defer deleteFile(t)
test := initWatchTest(t)
defer test.close()
test.waitAndSucceedInitial()
deleteFile(t)
test.waitAndFailInitial()
}
func TestWatchUpdate(t *testing.T) {
createFile(t)
defer deleteFile(t)
test := initWatchTest(t)
defer test.close()
test.waitAndSucceedInitial()
updateFile(t)
test.waitAndSucceedUpdated()
}