-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathmodule_test.go
336 lines (310 loc) · 8.44 KB
/
module_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
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
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: Apache-2.0
package tscaddy
import (
"errors"
"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_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_Listen(t *testing.T) {
must.Do(caddy.Run(new(caddy.Config)))
ctx := caddy.ActiveContext()
node, err := getNode(ctx, "testhost")
if err != nil {
t.Fatal("failed to get server", err)
}
ln, err := node.Listen("tcp", ":80")
if err != nil {
t.Fatal("failed to listen", err)
}
count, exists := nodes.References("testhost")
if !exists && count != 1 {
t.Fatal("reference doesn't exist")
}
ln.Close()
count, exists = nodes.References("testhost")
if exists && count != 0 {
t.Fatal("reference exists when it shouldn't")
}
err = node.Close()
if !errors.Is(err, net.ErrClosed) {
t.Fatal("unexpected error", err)
}
}