forked from tailscale/caddy-tailscale
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule_test.go
More file actions
465 lines (427 loc) · 11.8 KB
/
module_test.go
File metadata and controls
465 lines (427 loc) · 11.8 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
461
462
463
464
465
// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: Apache-2.0
package tscaddy
import (
"io"
"net"
"os"
"path/filepath"
"testing"
"github.com/caddyserver/caddy/v2"
"tailscale.com/types/opt"
"tailscale.com/util/must"
)
func Test_GetAuthKey(t *testing.T) {
const host = "host"
tests := map[string]struct {
env map[string]string // env vars to set
defaultKey string // default key in caddy config
hostKey string // host key in caddy config
want string
}{
"default key from environment": {
env: map[string]string{"TS_AUTHKEY": "envkey"},
want: "envkey",
},
"default key from caddy": {
env: map[string]string{"TS_AUTHKEY": "envkey"},
defaultKey: "defaultkey",
want: "defaultkey",
},
"default key from caddy placeholder": {
env: map[string]string{
"TS_AUTHKEY": "envkey",
"MYKEY": "mykey",
},
defaultKey: "{env.MYKEY}",
want: "mykey",
},
"host key from environment": {
env: map[string]string{"TS_AUTHKEY_HOST": "envhostkey"},
want: "envhostkey",
},
"host key from caddy": {
env: map[string]string{"TS_AUTHKEY": "envkey"},
hostKey: "hostkey",
want: "hostkey",
},
"host key from caddy placeholder": {
env: map[string]string{"MYKEY": "mykey"},
hostKey: "{env.MYKEY}",
want: "mykey",
},
"empty key from empty env var": {
hostKey: "{env.DOES_NOT_EXIST}",
want: "",
},
"empty key from bad placeholder": {
hostKey: "{bad.placeholder}",
want: "",
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
app := &App{
DefaultAuthKey: tt.defaultKey,
Nodes: make(map[string]Node),
}
if err := app.Provision(caddy.Context{}); err != nil {
t.Fatal(err)
}
if tt.hostKey != "" {
app.Nodes[host] = Node{
AuthKey: tt.hostKey,
}
}
for k, v := range tt.env {
t.Setenv(k, v)
}
got, _ := getAuthKey(host, app)
if got != tt.want {
t.Errorf("GetAuthKey() = %v, want %v", got, tt.want)
}
})
}
}
func Test_GetControlURL(t *testing.T) {
const nodeName = "node"
tests := map[string]struct {
env map[string]string // env vars to set
defaultURL string // default control_url in caddy config
nodeURL string // node control_url in caddy config
want string
}{
"default empty URL": {
want: "",
},
"custom URL from app config": {
defaultURL: "http://custom.example.com",
want: "http://custom.example.com",
},
"custom URL from node config": {
defaultURL: "xxx",
nodeURL: "http://custom.example.com",
want: "http://custom.example.com",
},
"custom URL from env on app config": {
env: map[string]string{"CONTROL_URL": "http://env.example.com"},
defaultURL: "{env.CONTROL_URL}",
want: "http://env.example.com",
},
"custom URL from env on node config": {
env: map[string]string{"CONTROL_URL": "http://env.example.com"},
defaultURL: "xxx",
nodeURL: "{env.CONTROL_URL}",
want: "http://env.example.com",
},
}
for tn, tt := range tests {
t.Run(tn, func(t *testing.T) {
app := &App{
ControlURL: tt.defaultURL,
Nodes: make(map[string]Node),
}
if tt.nodeURL != "" {
app.Nodes[nodeName] = Node{
ControlURL: tt.nodeURL,
}
}
if err := app.Provision(caddy.Context{}); err != nil {
t.Fatal(err)
}
for k, v := range tt.env {
t.Setenv(k, v)
}
got, _ := getControlURL(nodeName, app)
if got != tt.want {
t.Errorf("GetControlURL() = %v, want %v", got, tt.want)
}
})
}
}
func Test_GetEphemeral(t *testing.T) {
app := &App{
Ephemeral: true,
Nodes: map[string]Node{
"empty": {},
"ephemeral": {Ephemeral: opt.NewBool(true)},
"not-ephemeral": {Ephemeral: opt.NewBool(false)},
},
}
if err := app.Provision(caddy.Context{}); err != nil {
t.Fatal(err)
}
// node without named config gets the app-level ephemeral setting
if got, want := getEphemeral("noconfig", app), true; got != want {
t.Errorf("GetEphemeral() = %v, want %v", got, want)
}
// with an empty config, it should return the app-level ephemeral setting
if got, want := getEphemeral("empty", app), true; got != want {
t.Errorf("GetEphemeral() = %v, want %v", got, want)
}
// explicit node-level true ephemeral setting
if got, want := getEphemeral("ephemeral", app), true; got != want {
t.Errorf("GetEphemeral() = %v, want %v", got, want)
}
// explicit node-level false ephemeral setting
if got, want := getEphemeral("not-ephemeral", app), false; got != want {
t.Errorf("GetEphemeral() = %v, want %v", got, want)
}
}
func Test_GetHostname(t *testing.T) {
const nodeName = "node"
tests := map[string]struct {
env map[string]string // env vars to set
hostname string // hostname value in caddy config
want string
}{
"default hostname from node name": {
want: nodeName,
},
"custom hostname from node config": {
hostname: "custom",
want: "custom",
},
"custom hostname with env vars": {
env: map[string]string{"REGION": "eu", "ENV": "prod"},
hostname: "custom-{env.REGION}-{env.ENV}",
want: "custom-eu-prod",
},
}
for tn, tt := range tests {
t.Run(tn, func(t *testing.T) {
app := &App{Nodes: map[string]Node{
nodeName: {Hostname: tt.hostname},
}}
if err := app.Provision(caddy.Context{}); err != nil {
t.Fatal(err)
}
for k, v := range tt.env {
t.Setenv(k, v)
}
got, _ := getHostname(nodeName, app)
if got != tt.want {
t.Errorf("GetHostname() = %v, want %v", got, tt.want)
}
})
}
}
func Test_GetPort(t *testing.T) {
app := &App{
Nodes: map[string]Node{
"empty": {},
"port": {Port: 3000},
},
}
if err := app.Provision(caddy.Context{}); err != nil {
t.Fatal(err)
}
got := getPort("noconfig", &App{})
if want := uint16(0); got != want {
t.Errorf("GetPort() = %v, want %v", got, want)
}
got = getPort("empty", app)
if want := uint16(0); got != want {
t.Errorf("GetPort() = %v, want %v", got, want)
}
got = getPort("port", app)
if want := uint16(3000); got != want {
t.Errorf("GetPort() = %v, want %v", got, want)
}
}
func Test_GetStateDir(t *testing.T) {
const nodeName = "node"
configDir := must.Get(os.UserConfigDir())
tests := map[string]struct {
env map[string]string // env vars to set
defaultDir string // default state_dir in caddy config
nodeDir string // node state_dir in caddy config
want string
}{
"default statedir from node name": {
want: filepath.Join(configDir, "tsnet-caddy-"+nodeName),
},
"custom hostname from app config": {
env: map[string]string{"TMPDIR": "/tmp/"},
defaultDir: "{env.TMPDIR}",
want: filepath.Join("/tmp/", nodeName),
},
"custom hostname from node config": {
env: map[string]string{"TMPDIR": "/tmp/"},
defaultDir: "/xxx/",
nodeDir: "{env.TMPDIR}",
want: "/tmp/",
},
}
for tn, tt := range tests {
t.Run(tn, func(t *testing.T) {
app := &App{
StateDir: tt.defaultDir,
Nodes: make(map[string]Node),
}
if tt.nodeDir != "" {
app.Nodes[nodeName] = Node{
StateDir: tt.nodeDir,
}
}
if err := app.Provision(caddy.Context{}); err != nil {
t.Fatal(err)
}
for k, v := range tt.env {
t.Setenv(k, v)
}
got, _ := getStateDir(nodeName, app)
if got != tt.want {
t.Errorf("GetStateDir() = %v, want %v", got, tt.want)
}
})
}
}
func Test_GetWebUI(t *testing.T) {
app := &App{
WebUI: true,
Nodes: map[string]Node{
"empty": {},
"webui": {WebUI: opt.NewBool(true)},
"no-webui": {WebUI: opt.NewBool(false)},
},
}
if err := app.Provision(caddy.Context{}); err != nil {
t.Fatal(err)
}
// node without named config gets the app-level webui setting
if got, want := getWebUI("noconfig", app), true; got != want {
t.Errorf("GetWebUI() = %v, want %v", got, want)
}
// with an empty config, it should return the app-level webui setting
if got, want := getWebUI("empty", app), true; got != want {
t.Errorf("GetWebUI() = %v, want %v", got, want)
}
// explicit node-level true webui setting
if got, want := getWebUI("webui", app), true; got != want {
t.Errorf("GetWebUI() = %v, want %v", got, want)
}
// explicit node-level false webui setting
if got, want := getWebUI("no-webui", app), false; got != want {
t.Errorf("GetWebUI() = %v, want %v", got, want)
}
}
func Test_GetTags(t *testing.T) {
tests := map[string]struct {
appTags []string
nodeTags []string
want []string
}{
"no tags": {
appTags: nil,
nodeTags: nil,
want: nil,
},
"app-level tags only": {
appTags: []string{"tag:test1", "tag:test2"},
nodeTags: nil,
want: []string{"tag:test1", "tag:test2"},
},
"node-level tags override app tags": {
appTags: []string{"tag:app1", "tag:app2"},
nodeTags: []string{"tag:node1", "tag:node2"},
want: []string{"tag:node1", "tag:node2"},
},
"empty node tags override app tags": {
appTags: []string{"tag:app1", "tag:app2"},
nodeTags: []string{},
want: []string{},
},
"single tag": {
appTags: []string{"tag:production"},
nodeTags: nil,
want: []string{"tag:production"},
},
"multiple node tags": {
appTags: nil,
nodeTags: []string{"tag:web", "tag:frontend", "tag:production"},
want: []string{"tag:web", "tag:frontend", "tag:production"},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
app := &App{
Tags: tt.appTags,
Nodes: make(map[string]Node),
}
if tt.nodeTags != nil {
app.Nodes["testnode"] = Node{
Tags: tt.nodeTags,
}
}
if err := app.Provision(caddy.Context{}); err != nil {
t.Fatal(err)
}
got := getTags("testnode", app)
if len(got) != len(tt.want) {
t.Errorf("getTags() = %v, want %v", got, tt.want)
return
}
for i := range got {
if got[i] != tt.want[i] {
t.Errorf("getTags() = %v, want %v", got, tt.want)
return
}
}
})
}
// Test node without config gets app-level tags
t.Run("node without config uses app tags", func(t *testing.T) {
app := &App{
Tags: []string{"tag:default1", "tag:default2"},
Nodes: make(map[string]Node),
}
if err := app.Provision(caddy.Context{}); err != nil {
t.Fatal(err)
}
got := getTags("unconfigured-node", app)
want := []string{"tag:default1", "tag:default2"}
if len(got) != len(want) {
t.Errorf("getTags() = %v, want %v", got, want)
return
}
for i := range got {
if got[i] != want[i] {
t.Errorf("getTags() = %v, want %v", got, want)
return
}
}
})
}
func Test_Listen(t *testing.T) {
must.Do(caddy.Run(new(caddy.Config)))
ctx := caddy.ActiveContext()
// Test the listener pooling system via getTCPListener
ln, err := getTCPListener(ctx, "tailscale", "testhost", "80", 0, net.ListenConfig{})
if err != nil {
t.Fatal("failed to get listener", err)
}
// Check that node reference exists
count, exists := nodes.References("testhost")
if !exists || count != 1 {
t.Fatalf("expected 1 node reference, got count=%d exists=%v", count, exists)
}
// Check that listener reference exists
listenerKey := "tailscale/testhost:tcp:80"
lcount, lexists := tailscaleListeners.References(listenerKey)
if !lexists || lcount != 1 {
t.Fatalf("expected 1 listener reference, got count=%d exists=%v", lcount, lexists)
}
// Close the listener
must.Do(ln.(io.Closer).Close())
// Check that listener reference is gone
lcount, lexists = tailscaleListeners.References(listenerKey)
if lexists && lcount != 0 {
t.Fatalf("expected 0 listener references after close, got count=%d exists=%v", lcount, lexists)
}
// Check that node reference is also gone (since no listeners remain)
count, exists = nodes.References("testhost")
if exists && count != 0 {
t.Fatalf("expected 0 node references after close, got count=%d exists=%v", count, exists)
}
}