diff --git a/main.go b/main.go index def5d36bc..d8c8701a5 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "fmt" "math" "math/rand" + "net" "os" "os/signal" "path/filepath" @@ -348,7 +349,7 @@ func controllerRestURL() string { if hostname == "" { hostname = config.ServerCertName } - return "https://" + hostname + ":" + port + return "https://" + net.JoinHostPort(hostname, port) } func main() { diff --git a/main_test.go b/main_test.go index 0610a43ef..121e29c00 100644 --- a/main_test.go +++ b/main_test.go @@ -31,6 +31,46 @@ func TestMain(m *testing.M) { os.Exit(m.Run()) } +func TestControllerRestURL(t *testing.T) { + testCases := []struct { + name string + host string + port string + expected string + }{ + { + name: "IPv6 address", + host: "2001:db8::1", + port: "34571", + expected: "https://[2001:db8::1]:34571", + }, + { + name: "IPv4 address", + host: "192.0.2.10", + port: "34571", + expected: "https://192.0.2.10:34571", + }, + { + name: "DNS name", + host: "trident-csi", + port: "34571", + expected: "https://trident-csi:34571", + }, + { + name: "defaults", + expected: "https://trident-csi:34571", + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + t.Setenv("TRIDENT_CSI_SERVICE_HOST", tc.host) + t.Setenv("TRIDENT_CSI_SERVICE_PORT", tc.port) + assert.Equal(t, tc.expected, controllerRestURL()) + }) + } +} + func TestMain_processCommandLineArgs_QPS(t *testing.T) { type parameters struct { k8sApiQPS float64