-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathconfig_test.go
More file actions
382 lines (369 loc) · 12.2 KB
/
Copy pathconfig_test.go
File metadata and controls
382 lines (369 loc) · 12.2 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
// Copyright 2025 Element Creations Ltd.
// Copyright 2025 New Vector Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
// Please see LICENSE files in the repository root for full details.
package main
import (
"maps"
"os"
"reflect"
"testing"
"time"
)
func TestReadKeySecret(t *testing.T) {
testCases := []struct {
name string
env map[string]string
expectedKey string
expectedSecret string
}{
{
name: "Read from env",
env: map[string]string{
"LIVEKIT_KEY": "from_env_pheethiewixohp9eecheeGhuayeeph4l",
"LIVEKIT_SECRET": "from_env_ahb8eiwae0viey7gee4ieNgahgeeQuie",
},
expectedKey: "from_env_pheethiewixohp9eecheeGhuayeeph4l",
expectedSecret: "from_env_ahb8eiwae0viey7gee4ieNgahgeeQuie",
},
{
name: "Read from livekit keysecret",
env: map[string]string{
"LIVEKIT_KEY_FILE": "./tests/keysecret.yaml",
},
expectedKey: "keysecret_iethuB2LeLiNuishiaKeephei9jaatio",
expectedSecret: "keysecret_xefaingo4oos6ohla9phiMieBu3ohJi2",
},
{
name: "Read from file",
env: map[string]string{
"LIVEKIT_KEY_FROM_FILE": "./tests/key",
"LIVEKIT_SECRET_FROM_FILE": "./tests/secret",
},
expectedKey: "from_file_oquusheiheiw4Iegah8te3Vienguus5a",
expectedSecret: "from_file_vohmahH3eeyieghohSh3kee8feuPhaim",
},
{
name: "Read from file key only",
env: map[string]string{
"LIVEKIT_KEY_FROM_FILE": "./tests/key",
"LIVEKIT_SECRET": "from_env_ahb8eiwae0viey7gee4ieNgahgeeQuie",
},
expectedKey: "from_file_oquusheiheiw4Iegah8te3Vienguus5a",
expectedSecret: "from_env_ahb8eiwae0viey7gee4ieNgahgeeQuie",
},
{
name: "Read from file secret only",
env: map[string]string{
"LIVEKIT_SECRET_FROM_FILE": "./tests/secret",
"LIVEKIT_KEY": "from_env_qui8aiTopiekiechah9oocbeimeew2O",
},
expectedKey: "from_env_qui8aiTopiekiechah9oocbeimeew2O",
expectedSecret: "from_file_vohmahH3eeyieghohSh3kee8feuPhaim",
},
{
name: "Empty if secret no env",
env: map[string]string{},
expectedKey: "",
expectedSecret: "",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
for k, v := range tc.env {
if err := os.Setenv(k, v); err != nil {
t.Errorf("Failed to set environment variable %s: %v", k, err)
}
}
key, secret := readKeySecret()
if secret != tc.expectedSecret || key != tc.expectedKey {
t.Errorf("Expected secret and key to be %s and %s but got %s and %s",
tc.expectedSecret,
tc.expectedKey,
secret,
key)
}
for k := range tc.env {
if err := os.Unsetenv(k); err != nil {
t.Errorf("Failed to unset environment variable %s: %v", k, err)
}
}
})
}
}
func TestReadCsApiUrlOverrides(t *testing.T) {
testCases := []struct {
name string
env string
expectedMap map[string]CsApiUrl
expectedErr bool
}{
{
name: "Empty",
env: "",
expectedMap: map[string]CsApiUrl{},
expectedErr: false,
},
{
name: "DNS name",
env: "example.com=https://matrix-client.example.com",
expectedMap: map[string]CsApiUrl{"example.com": "https://matrix-client.example.com"},
expectedErr: false,
},
{
name: "DNS name with port",
env: "example.com=https://matrix-client.example.com:1234",
expectedMap: map[string]CsApiUrl{"example.com": "https://matrix-client.example.com:1234"},
expectedErr: false,
},
{
name: "IPv4",
env: "192.168.1.100=https://matrix-client.example.com",
expectedMap: map[string]CsApiUrl{"192.168.1.100": "https://matrix-client.example.com"},
expectedErr: false,
},
{
name: "IPv4 with port",
env: "192.168.1.100:1234=https://matrix-client.example.com",
expectedMap: map[string]CsApiUrl{"192.168.1.100:1234": "https://matrix-client.example.com"},
expectedErr: false,
},
{
name: "IPv6",
env: "[2001:0db8:85a3:0000:0000:8a2e:0370:7334]=https://matrix-client.example.com",
expectedMap: map[string]CsApiUrl{"[2001:0db8:85a3:0000:0000:8a2e:0370:7334]": "https://matrix-client.example.com"},
expectedErr: false,
},
{
name: "IPv6 with port",
env: "[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:1234=https://matrix-client.example.com",
expectedMap: map[string]CsApiUrl{"[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:1234": "https://matrix-client.example.com"},
expectedErr: false,
},
{
name: "Invalid value at the start",
env: "example.com",
expectedMap: nil,
expectedErr: true,
},
{
name: "Invalid value at the end",
env: "example.com=https://matrix-client.example.com,example.com",
expectedMap: nil,
expectedErr: true,
},
{
name: "Empty key",
env: "=https://matrix-client.example.com",
expectedMap: nil,
expectedErr: true,
},
{
name: "Empty value",
env: "example.com=",
expectedMap: nil,
expectedErr: true,
},
{
name: "Two DNS names",
env: "example.com=https://matrix-client.example.com,example.org=https://matrix-client.example.org",
expectedMap: map[string]CsApiUrl{"example.com": "https://matrix-client.example.com", "example.org": "https://matrix-client.example.org"},
expectedErr: false,
},
{
name: "Two DNS names with whitespace",
env: " example.com = https://matrix-client.example.com , example.org = https://matrix-client.example.org ",
expectedMap: map[string]CsApiUrl{"example.com": "https://matrix-client.example.com", "example.org": "https://matrix-client.example.org"},
expectedErr: false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actualMap, actualErr := readCsApiUrlOverrides(tc.env)
if !maps.Equal(tc.expectedMap, actualMap) || tc.expectedErr && actualErr == nil {
t.Errorf("Expected map: %v, error: %v, got map: %v, error: %v",
tc.expectedMap,
tc.expectedErr,
actualMap,
actualErr)
}
})
}
}
func TestParseConfig(t *testing.T) {
testCases := []struct {
name string
env map[string]string
wantConfig *Config
wantErrMsg string
}{
{
name: "Minimal valid config (explicit wildcard)",
env: map[string]string{
"LIVEKIT_KEY": "test_key",
"LIVEKIT_SECRET": "test_secret",
"LIVEKIT_URL": "wss://test.livekit.cloud",
"LIVEKIT_FULL_ACCESS_HOMESERVERS": "*",
},
wantConfig: &Config{
Key: "test_key",
Secret: "test_secret",
LkUrl: "wss://test.livekit.cloud",
SkipVerifyTLS: false,
FullAccessHomeservers: []string{"*"},
LkJwtBind: ":8080",
CsApiUrlOverrides: map[string]CsApiUrl{},
},
},
{
name: "Full config with all options",
env: map[string]string{
"LIVEKIT_KEY": "test_key",
"LIVEKIT_SECRET": "test_secret",
"LIVEKIT_URL": "wss://test.livekit.cloud",
"LIVEKIT_FULL_ACCESS_HOMESERVERS": "example.com, test.com",
"LIVEKIT_JWT_BIND": ":9090",
"LIVEKIT_INSECURE_SKIP_VERIFY_TLS": "YES_I_KNOW_WHAT_I_AM_DOING",
"LIVEKIT_SANITY_CHECK_INTERVAL_SECONDS": "30",
"LIVEKIT_CS_API_URL_OVERRIDES": "matrix.com=https://matrix-client.matrix.com",
"LIVEKIT_REDIS_URL": "localhost:6379",
},
wantConfig: &Config{
Key: "test_key",
Secret: "test_secret",
LkUrl: "wss://test.livekit.cloud",
SkipVerifyTLS: true,
FullAccessHomeservers: []string{"example.com", "test.com"},
LkJwtBind: ":9090",
SanityCheckInterval: 30 * time.Second,
CsApiUrlOverrides: map[string]CsApiUrl{"matrix.com": "https://matrix-client.matrix.com"},
RedisURL: "localhost:6379",
},
},
{
name: "Legacy port configuration",
env: map[string]string{
"LIVEKIT_KEY": "test_key",
"LIVEKIT_SECRET": "test_secret",
"LIVEKIT_URL": "wss://test.livekit.cloud",
"LIVEKIT_JWT_PORT": "9090",
"LIVEKIT_FULL_ACCESS_HOMESERVERS": "*",
},
wantConfig: &Config{
Key: "test_key",
Secret: "test_secret",
LkUrl: "wss://test.livekit.cloud",
SkipVerifyTLS: false,
FullAccessHomeservers: []string{"*"},
LkJwtBind: ":9090",
CsApiUrlOverrides: map[string]CsApiUrl{},
},
},
{
name: "Missing LIVEKIT_FULL_ACCESS_HOMESERVERS",
env: map[string]string{
"LIVEKIT_KEY": "test_key",
"LIVEKIT_SECRET": "test_secret",
"LIVEKIT_URL": "wss://test.livekit.cloud",
},
wantErrMsg: "LIVEKIT_FULL_ACCESS_HOMESERVERS environment variable must be set to the homeserver(s) you intend to serve — see README for guidance",
},
{
name: "Missing required config",
env: map[string]string{
"LIVEKIT_KEY": "test_key",
},
wantErrMsg: "LIVEKIT_KEY* / LIVEKIT_SECRET* and LIVEKIT_URL must be set",
},
{
name: "Conflicting bind configuration",
env: map[string]string{
"LIVEKIT_KEY": "test_key",
"LIVEKIT_SECRET": "test_secret",
"LIVEKIT_URL": "wss://test.livekit.cloud",
"LIVEKIT_JWT_BIND": ":9090",
"LIVEKIT_JWT_PORT": "8080",
"LIVEKIT_FULL_ACCESS_HOMESERVERS": "*",
},
wantErrMsg: "LIVEKIT_JWT_BIND and LIVEKIT_JWT_PORT must not be set together",
},
{
name: "Sanity check interval invalid",
env: map[string]string{
"LIVEKIT_KEY": "test_key",
"LIVEKIT_SECRET": "test_secret",
"LIVEKIT_URL": "wss://test.livekit.cloud",
"LIVEKIT_SANITY_CHECK_INTERVAL_SECONDS": "not-a-number",
"LIVEKIT_FULL_ACCESS_HOMESERVERS": "*",
},
wantErrMsg: `LIVEKIT_SANITY_CHECK_INTERVAL_SECONDS must be a non-negative integer, got "not-a-number"`,
},
{
name: "Sanity check interval zero disables",
env: map[string]string{
"LIVEKIT_KEY": "test_key",
"LIVEKIT_SECRET": "test_secret",
"LIVEKIT_URL": "wss://test.livekit.cloud",
"LIVEKIT_SANITY_CHECK_INTERVAL_SECONDS": "0",
"LIVEKIT_FULL_ACCESS_HOMESERVERS": "*",
},
wantConfig: &Config{
Key: "test_key",
Secret: "test_secret",
LkUrl: "wss://test.livekit.cloud",
FullAccessHomeservers: []string{"*"},
LkJwtBind: ":8080",
SanityCheckInterval: 0,
CsApiUrlOverrides: map[string]CsApiUrl{},
},
},
{
name: "Sanity check interval negative rejected",
env: map[string]string{
"LIVEKIT_KEY": "test_key",
"LIVEKIT_SECRET": "test_secret",
"LIVEKIT_URL": "wss://test.livekit.cloud",
"LIVEKIT_SANITY_CHECK_INTERVAL_SECONDS": "-1",
"LIVEKIT_FULL_ACCESS_HOMESERVERS": "*",
},
wantErrMsg: `LIVEKIT_SANITY_CHECK_INTERVAL_SECONDS must be a non-negative integer, got "-1"`,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
for k, v := range tc.env {
if err := os.Setenv(k, v); err != nil {
t.Fatalf("Failed to set environment variable %s: %v", k, err)
}
}
defer func() {
for k := range tc.env {
if err := os.Unsetenv(k); err != nil {
t.Errorf("Failed to unset environment variable %s: %v", k, err)
}
}
}()
got, err := parseConfig()
if tc.wantErrMsg != "" {
if err == nil {
t.Errorf("parseConfig() error = nil, wantErr %q", tc.wantErrMsg)
return
}
if err.Error() != tc.wantErrMsg {
t.Errorf("parseConfig() error = %q, wantErr %q", err.Error(), tc.wantErrMsg)
}
return
}
if err != nil {
t.Errorf("parseConfig() unexpected error: %v", err)
return
}
// If any of the defaults change, the wantConfigs might need updating.
if !reflect.DeepEqual(got, tc.wantConfig) {
t.Errorf("Expected config: %v, got: %v", tc.wantConfig, got)
return
}
})
}
}