Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 0 additions & 149 deletions internal/tiger/cmd/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,155 +676,6 @@ func TestIsConnectionRejected(t *testing.T) {
}
}

func TestBuildConnectionString(t *testing.T) {
testCases := []struct {
name string
service api.Service
pooled bool
role string
expectedString string
expectError bool
}{
{
name: "Basic connection string",
service: api.Service{
Endpoint: &api.Endpoint{
Host: util.Ptr("test-host.tigerdata.com"),
Port: util.Ptr(5432),
},
},
pooled: false,
role: "tsdbadmin",
expectedString: "postgresql://[email protected]: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),
},
},
pooled: false,
role: "readonly",
expectedString: "postgresql://[email protected]: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
},
},
pooled: false,
role: "tsdbadmin",
expectedString: "postgresql://[email protected]: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),
},
},
},
pooled: true,
role: "tsdbadmin",
expectedString: "postgresql://[email protected]: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
},
pooled: true,
role: "tsdbadmin",
expectedString: "postgresql://[email protected]:5432/tsdb?sslmode=require",
expectError: false,
},
{
name: "Error when no endpoint available",
service: api.Service{
Endpoint: nil,
},
pooled: false,
role: "tsdbadmin",
expectError: true,
},
{
name: "Error when no host available",
service: api.Service{
Endpoint: &api.Endpoint{
Host: nil,
Port: util.Ptr(5432),
},
},
pooled: false,
role: "tsdbadmin",
expectError: true,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Create a test command to capture stderr output
cmd := &cobra.Command{}
errBuf := new(bytes.Buffer)
cmd.SetErr(errBuf)

result, err := password.GetConnectionDetails(tc.service, password.ConnectionDetailsOptions{
Pooled: tc.pooled,
Role: tc.role,
})

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.String() != tc.expectedString {
t.Errorf("Expected connection string %q, got %q", tc.expectedString, result)
}

// Check for warning message
if tc.pooled && tc.service.ConnectionPooler == nil && result.IsPooler {
t.Errorf("Expected direct connection when pooler unavailable, but got pooler connection")
}

if !tc.pooled && result.IsPooler {
t.Errorf("Expected direct connection, but got pooler connection")
}

stderrOutput := errBuf.String()
if stderrOutput != "" {
t.Errorf("Expected no warning, but got: %q", stderrOutput)
}
})
}
}

func TestDBTestConnection_TimeoutParsing(t *testing.T) {
testCases := []struct {
name string
Expand Down
11 changes: 6 additions & 5 deletions internal/tiger/password/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,21 @@ func GetConnectionDetails(service api.Service, opts ConnectionDetailsOptions) (*
endpoint = service.Endpoint
}

if endpoint.Host == nil {
if endpoint.Host == nil || *endpoint.Host == "" {
return nil, fmt.Errorf("endpoint host not available")
}

if endpoint.Port == nil || *endpoint.Port == 0 {
return nil, fmt.Errorf("endpoint port not available")
}

details := &ConnectionDetails{
Role: opts.Role,
Host: *endpoint.Host,
Port: 5432, // Default PostgreSQL port
Port: *endpoint.Port,
Database: "tsdb", // Database is always "tsdb" for TimescaleDB/PostgreSQL services
IsPooler: isPooler,
}
if endpoint.Port != nil {
details.Port = *endpoint.Port
}

if opts.WithPassword {
if opts.InitialPassword != "" {
Expand Down
104 changes: 59 additions & 45 deletions internal/tiger/password/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ import (

func TestBuildConnectionString_Basic(t *testing.T) {
testCases := []struct {
name string
service api.Service
opts ConnectionDetailsOptions
expectedString string
expectError bool
name string
service api.Service
opts ConnectionDetailsOptions
expectedString string
expectedIsPooler bool
expectError bool
}{
{
name: "Basic connection string without password",
Expand Down Expand Up @@ -47,17 +48,23 @@ func TestBuildConnectionString_Basic(t *testing.T) {
expectedString: "postgresql://[email protected]:5432/tsdb?sslmode=require",
},
{
name: "Connection string with default port",
name: "Direct connection when pooler is available",
service: api.Service{
Endpoint: &api.Endpoint{
Host: util.Ptr("test-host.tigerdata.com"),
Port: nil, // Should use default 5432
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: ConnectionDetailsOptions{
Role: "tsdbadmin",
},
expectedString: "postgresql://tsdbadmin@test-host.tigerdata.com:5432/tsdb?sslmode=require",
expectedString: "postgresql://tsdbadmin@direct-host.tigerdata.com:5432/tsdb?sslmode=require",
},
{
name: "Pooled connection string",
Expand All @@ -77,7 +84,8 @@ func TestBuildConnectionString_Basic(t *testing.T) {
Pooled: true,
Role: "tsdbadmin",
},
expectedString: "postgresql://[email protected]:6432/tsdb?sslmode=require",
expectedString: "postgresql://[email protected]:6432/tsdb?sslmode=require",
expectedIsPooler: true,
},
{
name: "Pooled connection fallback to direct when pooler unavailable",
Expand Down Expand Up @@ -117,6 +125,45 @@ func TestBuildConnectionString_Basic(t *testing.T) {
},
expectError: true,
},
{
name: "Error when host is empty",
service: api.Service{
Endpoint: &api.Endpoint{
Host: util.Ptr(""),
Port: util.Ptr(5432),
},
},
opts: ConnectionDetailsOptions{
Role: "tsdbadmin",
},
expectError: true,
},
{
name: "Error when no port available",
service: api.Service{
Endpoint: &api.Endpoint{
Host: util.Ptr("test-host.tigerdata.com"),
Port: nil,
},
},
opts: ConnectionDetailsOptions{
Role: "tsdbadmin",
},
expectError: true,
},
{
name: "Error when port is zero",
service: api.Service{
Endpoint: &api.Endpoint{
Host: util.Ptr("test-host.tigerdata.com"),
Port: util.Ptr(0),
},
},
opts: ConnectionDetailsOptions{
Role: "tsdbadmin",
},
expectError: true,
},
}

for _, tc := range testCases {
Expand All @@ -139,9 +186,8 @@ func TestBuildConnectionString_Basic(t *testing.T) {
t.Errorf("Expected connection string %q, got %q", tc.expectedString, result.String())
}

// Check for warning message
if tc.opts.Pooled && tc.service.ConnectionPooler == nil && result.IsPooler {
t.Errorf("Expected IsPooler to be false when pooler is unavailable, but got true")
if result.IsPooler != tc.expectedIsPooler {
t.Errorf("Expected IsPooler to be %v, got %v", tc.expectedIsPooler, result.IsPooler)
}
})
}
Expand Down Expand Up @@ -368,35 +414,3 @@ func TestBuildConnectionString_WithPassword_InvalidServiceEndpoint(t *testing.T)
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
}

// Request pooled connection when pooler is not available
details, err := GetConnectionDetails(service, ConnectionDetailsOptions{
Pooled: true,
Role: "tsdbadmin",
})
connectionString := details.String()

if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

// Should return direct connection string
expectedString := "postgresql://[email protected]:5432/tsdb?sslmode=require"
if connectionString != expectedString {
t.Errorf("Expected connection string %q, got %q", expectedString, connectionString)
}

if details.IsPooler {
t.Errorf("Expected IsPooler to be false when pooler is unavailable, but got true")
}
}