-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathconnection_test.go
More file actions
452 lines (403 loc) · 13 KB
/
connection_test.go
File metadata and controls
452 lines (403 loc) · 13 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
package password
import (
"bytes"
"fmt"
"strings"
"testing"
"github.com/spf13/viper"
"github.com/timescale/tiger-cli/internal/tiger/api"
"github.com/timescale/tiger-cli/internal/tiger/config"
"github.com/timescale/tiger-cli/internal/tiger/util"
)
func TestBuildConnectionString_Basic(t *testing.T) {
testCases := []struct {
name string
service api.Service
opts ConnectionStringOptions
expectedString string
expectError bool
expectWarning bool
}{
{
name: "Basic connection string without password",
service: api.Service{
Endpoint: &api.Endpoint{
Host: util.Ptr("test-host.tigerdata.com"),
Port: util.Ptr(5432),
},
},
opts: ConnectionStringOptions{
Pooled: false,
Role: "tsdbadmin",
PasswordMode: PasswordExclude,
},
expectedString: "postgresql://tsdbadmin@test-host.tigerdata.com:5432/tsdb?sslmode=require",
expectError: false,
},
{
name: "Connection string with custom role",
service: api.Service{
Endpoint: &api.Endpoint{
Host: util.Ptr("test-host.tigerdata.com"),
Port: util.Ptr(5432),
},
},
opts: ConnectionStringOptions{
Pooled: false,
Role: "readonly",
PasswordMode: PasswordExclude,
},
expectedString: "postgresql://readonly@test-host.tigerdata.com:5432/tsdb?sslmode=require",
expectError: false,
},
{
name: "Connection string with default port",
service: api.Service{
Endpoint: &api.Endpoint{
Host: util.Ptr("test-host.tigerdata.com"),
Port: nil, // Should use default 5432
},
},
opts: ConnectionStringOptions{
Pooled: false,
Role: "tsdbadmin",
PasswordMode: PasswordExclude,
},
expectedString: "postgresql://tsdbadmin@test-host.tigerdata.com:5432/tsdb?sslmode=require",
expectError: false,
},
{
name: "Pooled connection string",
service: api.Service{
Endpoint: &api.Endpoint{
Host: util.Ptr("direct-host.tigerdata.com"),
Port: util.Ptr(5432),
},
ConnectionPooler: &api.ConnectionPooler{
Endpoint: &api.Endpoint{
Host: util.Ptr("pooler-host.tigerdata.com"),
Port: util.Ptr(6432),
},
},
},
opts: ConnectionStringOptions{
Pooled: true,
Role: "tsdbadmin",
PasswordMode: PasswordExclude,
},
expectedString: "postgresql://tsdbadmin@pooler-host.tigerdata.com:6432/tsdb?sslmode=require",
expectError: false,
},
{
name: "Pooled connection fallback to direct when pooler unavailable",
service: api.Service{
Endpoint: &api.Endpoint{
Host: util.Ptr("direct-host.tigerdata.com"),
Port: util.Ptr(5432),
},
ConnectionPooler: nil, // No pooler available
},
opts: ConnectionStringOptions{
Pooled: true,
Role: "tsdbadmin",
PasswordMode: PasswordExclude,
WarnWriter: new(bytes.Buffer), // Enable warnings
},
expectedString: "postgresql://tsdbadmin@direct-host.tigerdata.com:5432/tsdb?sslmode=require",
expectError: false,
expectWarning: true, // Should warn about pooler not available
},
{
name: "Error when no endpoint available",
service: api.Service{
Endpoint: nil,
},
opts: ConnectionStringOptions{
Pooled: false,
Role: "tsdbadmin",
PasswordMode: PasswordExclude,
},
expectError: true,
},
{
name: "Error when no host available",
service: api.Service{
Endpoint: &api.Endpoint{
Host: nil,
Port: util.Ptr(5432),
},
},
opts: ConnectionStringOptions{
Pooled: false,
Role: "tsdbadmin",
PasswordMode: PasswordExclude,
},
expectError: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// If expecting a warning, create a buffer for WarnWriter
var warnBuf *bytes.Buffer
if tc.expectWarning && tc.opts.WarnWriter == nil {
warnBuf = new(bytes.Buffer)
tc.opts.WarnWriter = warnBuf
} else if !tc.expectWarning && tc.opts.WarnWriter != nil {
warnBuf = tc.opts.WarnWriter.(*bytes.Buffer)
}
result, err := BuildConnectionString(tc.service, tc.opts)
if tc.expectError {
if err == nil {
t.Errorf("Expected error but got none")
}
return
}
if err != nil {
t.Errorf("Unexpected error: %v", err)
return
}
if result != tc.expectedString {
t.Errorf("Expected connection string %q, got %q", tc.expectedString, result)
}
// Check for warning message
if warnBuf != nil {
stderrOutput := warnBuf.String()
if tc.expectWarning {
if !strings.Contains(stderrOutput, "Warning: Connection pooler not available") {
t.Errorf("Expected warning about pooler not available, but got: %q", stderrOutput)
}
} else {
if stderrOutput != "" {
t.Errorf("Expected no warning, but got: %q", stderrOutput)
}
}
}
})
}
}
func TestBuildConnectionString_WithPassword_KeyringStorage(t *testing.T) {
// Use a unique service name for this test to avoid conflicts
config.SetTestServiceName(t)
// Set keyring as the password storage method for this test
originalStorage := viper.GetString("password_storage")
viper.Set("password_storage", "keyring")
defer viper.Set("password_storage", originalStorage)
// Create a test service
serviceID := "test-password-service"
projectID := "test-password-project"
host := "test-host.com"
port := 5432
service := api.Service{
ServiceId: &serviceID,
ProjectId: &projectID,
Endpoint: &api.Endpoint{
Host: &host,
Port: &port,
},
}
// Store a test password in keyring
testPassword := "test-password-keyring-123"
storage := GetPasswordStorage()
err := storage.Save(service, testPassword)
if err != nil {
t.Fatalf("Failed to save test password: %v", err)
}
defer storage.Remove(service) // Clean up after test
// Call BuildConnectionString with withPassword=true
result, err := BuildConnectionString(service, ConnectionStringOptions{
Pooled: false,
Role: "tsdbadmin",
PasswordMode: PasswordRequired,
})
if err != nil {
t.Fatalf("BuildConnectionString failed: %v", err)
}
// Verify that the password is included in the result
expectedResult := fmt.Sprintf("postgresql://tsdbadmin:%s@%s:%d/tsdb?sslmode=require", testPassword, host, port)
if result != expectedResult {
t.Errorf("Expected connection string with password '%s', got '%s'", expectedResult, result)
}
// Verify the password is actually in the connection string
if !strings.Contains(result, testPassword) {
t.Errorf("Password '%s' not found in connection string: %s", testPassword, result)
}
}
func TestBuildConnectionString_WithPassword_PgpassStorage(t *testing.T) {
// Set pgpass as the password storage method for this test
originalStorage := viper.GetString("password_storage")
viper.Set("password_storage", "pgpass")
defer viper.Set("password_storage", originalStorage)
// Create a test service with endpoint information (required for pgpass)
serviceID := "test-pgpass-service"
projectID := "test-pgpass-project"
host := "test-pgpass-host.com"
port := 5432
service := api.Service{
ServiceId: &serviceID,
ProjectId: &projectID,
Endpoint: &api.Endpoint{
Host: &host,
Port: &port,
},
}
// Store a test password in pgpass
testPassword := "test-password-pgpass-456"
storage := GetPasswordStorage()
err := storage.Save(service, testPassword)
if err != nil {
t.Fatalf("Failed to save test password: %v", err)
}
defer storage.Remove(service) // Clean up after test
// Call BuildConnectionString with withPassword=true
result, err := BuildConnectionString(service, ConnectionStringOptions{
Pooled: false,
Role: "tsdbadmin",
PasswordMode: PasswordRequired,
})
if err != nil {
t.Fatalf("BuildConnectionString failed: %v", err)
}
// Verify that the password is included in the result
expectedResult := fmt.Sprintf("postgresql://tsdbadmin:%s@%s:%d/tsdb?sslmode=require", testPassword, host, port)
if result != expectedResult {
t.Errorf("Expected connection string with password '%s', got '%s'", expectedResult, result)
}
// Verify the password is actually in the connection string
if !strings.Contains(result, testPassword) {
t.Errorf("Password '%s' not found in connection string: %s", testPassword, result)
}
}
func TestBuildConnectionString_WithPassword_NoStorage(t *testing.T) {
// Set no storage as the password storage method for this test
originalStorage := viper.GetString("password_storage")
viper.Set("password_storage", "none")
defer viper.Set("password_storage", originalStorage)
// Create a test service
serviceID := "test-nostorage-service"
projectID := "test-nostorage-project"
host := "test-host.com"
port := 5432
service := api.Service{
ServiceId: &serviceID,
ProjectId: &projectID,
Endpoint: &api.Endpoint{
Host: &host,
Port: &port,
},
}
// Call BuildConnectionString with withPassword=true - should fail
_, err := BuildConnectionString(service, ConnectionStringOptions{
Pooled: false,
Role: "tsdbadmin",
PasswordMode: PasswordRequired,
})
if err == nil {
t.Fatal("Expected error when password storage is disabled, but got none")
}
// Verify we get the expected error message
expectedError := "password storage is disabled (--password-storage=none)"
if !strings.Contains(err.Error(), expectedError) {
t.Errorf("Expected error message to contain '%s', got: %v", expectedError, err)
}
}
func TestBuildConnectionString_WithPassword_NoPasswordAvailable(t *testing.T) {
// Use a unique service name for this test to avoid conflicts
config.SetTestServiceName(t)
// Set keyring as the password storage method for this test
originalStorage := viper.GetString("password_storage")
viper.Set("password_storage", "keyring")
defer viper.Set("password_storage", originalStorage)
// Create a test service (but don't store any password for it)
serviceID := "test-nopassword-service"
projectID := "test-nopassword-project"
host := "test-host.com"
port := 5432
service := api.Service{
ServiceId: &serviceID,
ProjectId: &projectID,
Endpoint: &api.Endpoint{
Host: &host,
Port: &port,
},
}
// Call BuildConnectionString with withPassword=true - should fail
_, err := BuildConnectionString(service, ConnectionStringOptions{
Pooled: false,
Role: "tsdbadmin",
PasswordMode: PasswordRequired,
})
if err == nil {
t.Fatal("Expected error when no password is available, but got none")
}
// Verify we get the expected error message
expectedError := "no password found in keyring for this service"
if !strings.Contains(err.Error(), expectedError) {
t.Errorf("Expected error message to contain '%s', got: %v", expectedError, err)
}
}
func TestBuildConnectionString_WithPassword_InvalidServiceEndpoint(t *testing.T) {
// Use a unique service name for this test to avoid conflicts
config.SetTestServiceName(t)
// Set keyring as the password storage method for this test
originalStorage := viper.GetString("password_storage")
viper.Set("password_storage", "keyring")
defer viper.Set("password_storage", originalStorage)
// Create a test service without endpoint (invalid)
serviceID := "test-invalid-service"
projectID := "test-invalid-project"
service := api.Service{
ServiceId: &serviceID,
ProjectId: &projectID,
Endpoint: nil, // Invalid - no endpoint
}
// Call BuildConnectionString with withPassword=true - should fail
_, err := BuildConnectionString(service, ConnectionStringOptions{
Pooled: false,
Role: "tsdbadmin",
PasswordMode: PasswordRequired,
})
if err == nil {
t.Fatal("Expected error for invalid service endpoint, but got none")
}
// Verify we get an endpoint error
expectedError := "service endpoint not available"
if !strings.Contains(err.Error(), expectedError) {
t.Errorf("Expected error message to contain '%s', got: %v", expectedError, err)
}
}
func TestBuildConnectionString_PoolerWarning(t *testing.T) {
// Service without connection pooler
service := api.Service{
Endpoint: &api.Endpoint{
Host: util.Ptr("test-host.tigerdata.com"),
Port: util.Ptr(5432),
},
ConnectionPooler: nil, // No pooler available
}
// Create a buffer to capture warnings
warnBuf := new(bytes.Buffer)
// Request pooled connection when pooler is not available
connectionString, err := BuildConnectionString(service, ConnectionStringOptions{
Pooled: true,
Role: "tsdbadmin",
PasswordMode: PasswordExclude,
WarnWriter: warnBuf,
})
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
// Should return direct connection string
expectedString := "postgresql://tsdbadmin@test-host.tigerdata.com:5432/tsdb?sslmode=require"
if connectionString != expectedString {
t.Errorf("Expected connection string %q, got %q", expectedString, connectionString)
}
// Should have warning message
stderrOutput := warnBuf.String()
if !strings.Contains(stderrOutput, "Warning: Connection pooler not available") {
t.Errorf("Expected warning about pooler not available, but got: %q", stderrOutput)
}
// Verify the warning mentions using direct connection
if !strings.Contains(stderrOutput, "using direct connection") {
t.Errorf("Expected warning to mention direct connection fallback, but got: %q", stderrOutput)
}
}