@@ -23,6 +23,7 @@ import (
2323 "io"
2424 "log"
2525 "math/rand"
26+ "net"
2627 "os"
2728 "runtime"
2829 "strings"
@@ -722,9 +723,6 @@ func TestInitializeConfig(t *testing.T) {
722723 if ! strings .Contains (errorString , tt .wantErrorString ) {
723724 t .Errorf ("initializeConfig(%v) got error: %v, want contain %v" , tt .config , errorString , tt .wantErrorString )
724725 }
725- if tt .wantErrorString == "" {
726- tt .wantConfig .APIAddr = apiAddress
727- }
728726 if config != tt .wantConfig {
729727 t .Errorf ("initializeConfig(%v) got: %v, want %v" , tt .config , config , tt .wantConfig )
730728 }
@@ -982,7 +980,7 @@ func TestAgentWithServer(t *testing.T) {
982980 numProfiles : 2 ,
983981 DebugLogging : true ,
984982 DebugLoggingOutput : & logs ,
985- }); err != nil {
983+ }, option . WithoutAuthentication () ); err != nil {
986984 t .Fatalf ("Start(): %v" , err )
987985 }
988986
@@ -1011,3 +1009,187 @@ type testConnPool struct{ *grpc.ClientConn }
10111009
10121010func (p testConnPool ) Num () int { return 1 }
10131011func (p testConnPool ) Conn () * grpc.ClientConn { return p .ClientConn }
1012+
1013+ func TestEndpointResolution (t * testing.T ) {
1014+ origDialGRPC , origConfig , origOnGCE , origLogger := dialGRPC , config , onGCE , logger
1015+ t .Cleanup (func () {
1016+ dialGRPC , config , onGCE , logger = origDialGRPC , origConfig , origOnGCE , origLogger
1017+ })
1018+
1019+ // Sentinel error returned after inspecting the dialed target so start()
1020+ // exits cleanly without starting the background pollProfilerService loop.
1021+ errStopAfterDial := errors .New ("stop after dial" )
1022+
1023+ // Prevent any real network connections while still exercising real
1024+ // gtransport.DialPool option processing and endpoint resolution.
1025+ noOpDialer := func (context.Context , string ) (net.Conn , error ) {
1026+ return nil , errors .New ("network dialing disabled in unit test" )
1027+ }
1028+
1029+ tests := []struct {
1030+ name string
1031+ envUniverseDomain string
1032+ envUseMTLSEndpoint string
1033+ apiAddr string
1034+ opts []option.ClientOption
1035+ wantTarget string
1036+ }{
1037+ {
1038+ name : "default_gdu_endpoint" ,
1039+ wantTarget : "cloudprofiler.googleapis.com:443" ,
1040+ },
1041+ {
1042+ name : "mtls_always_gdu_endpoint" ,
1043+ envUseMTLSEndpoint : "always" ,
1044+ wantTarget : "cloudprofiler.mtls.googleapis.com:443" ,
1045+ },
1046+ {
1047+ name : "universe_domain_option" ,
1048+ opts : []option.ClientOption {option .WithUniverseDomain ("example-universe.test" )},
1049+ wantTarget : "cloudprofiler.example-universe.test:443" ,
1050+ },
1051+ {
1052+ name : "universe_domain_env" ,
1053+ envUniverseDomain : "example-universe.test" ,
1054+ wantTarget : "cloudprofiler.example-universe.test:443" ,
1055+ },
1056+ {
1057+ name : "universe_domain_option_overrides_env" ,
1058+ envUniverseDomain : "env-universe.test" ,
1059+ opts : []option.ClientOption {option .WithUniverseDomain ("opt-universe.test" )},
1060+ wantTarget : "cloudprofiler.opt-universe.test:443" ,
1061+ },
1062+ {
1063+ name : "empty_universe_domain_option_falls_back_to_env" ,
1064+ envUniverseDomain : "env-universe.test" ,
1065+ opts : []option.ClientOption {option .WithUniverseDomain ("" )},
1066+ wantTarget : "cloudprofiler.env-universe.test:443" ,
1067+ },
1068+ {
1069+ name : "empty_universe_domain_option_falls_back_to_default" ,
1070+ opts : []option.ClientOption {option .WithUniverseDomain ("" )},
1071+ wantTarget : "cloudprofiler.googleapis.com:443" ,
1072+ },
1073+ {
1074+ name : "config_api_addr" ,
1075+ apiAddr : "custom-config.example.com:443" ,
1076+ wantTarget : "custom-config.example.com:443" ,
1077+ },
1078+ {
1079+ name : "config_api_addr_overrides_mtls_always" ,
1080+ envUseMTLSEndpoint : "always" ,
1081+ apiAddr : "custom-config.example.com:443" ,
1082+ wantTarget : "custom-config.example.com:443" ,
1083+ },
1084+ {
1085+ name : "config_api_addr_overrides_universe_domain_env" ,
1086+ envUniverseDomain : "env-universe.test" ,
1087+ apiAddr : "custom-config.example.com:443" ,
1088+ wantTarget : "custom-config.example.com:443" ,
1089+ },
1090+ {
1091+ name : "config_api_addr_overrides_universe_domain_option" ,
1092+ apiAddr : "custom-config.example.com:443" ,
1093+ opts : []option.ClientOption {option .WithUniverseDomain ("opt-universe.test" )},
1094+ wantTarget : "custom-config.example.com:443" ,
1095+ },
1096+ {
1097+ name : "with_endpoint_option" ,
1098+ opts : []option.ClientOption {option .WithEndpoint ("opt-endpoint.example.com:443" )},
1099+ wantTarget : "opt-endpoint.example.com:443" ,
1100+ },
1101+ {
1102+ name : "with_endpoint_overrides_config_api_addr" ,
1103+ apiAddr : "custom-config.example.com:443" ,
1104+ opts : []option.ClientOption {option .WithEndpoint ("opt-endpoint.example.com:443" )},
1105+ wantTarget : "opt-endpoint.example.com:443" ,
1106+ },
1107+ {
1108+ name : "with_endpoint_overrides_config_api_addr_and_universe_domain" ,
1109+ envUniverseDomain : "env-universe.test" ,
1110+ apiAddr : "custom-config.example.com:443" ,
1111+ opts : []option.ClientOption {
1112+ option .WithUniverseDomain ("opt-universe.test" ),
1113+ option .WithEndpoint ("opt-endpoint.example.com:443" ),
1114+ },
1115+ wantTarget : "opt-endpoint.example.com:443" ,
1116+ },
1117+ {
1118+ name : "with_endpoint_before_with_universe_domain" ,
1119+ opts : []option.ClientOption {
1120+ option .WithEndpoint ("opt-endpoint.example.com:443" ),
1121+ option .WithUniverseDomain ("opt-universe.test" ),
1122+ },
1123+ wantTarget : "opt-endpoint.example.com:443" ,
1124+ },
1125+ {
1126+ name : "multiple_with_endpoint_last_wins" ,
1127+ apiAddr : "custom-config.example.com:443" ,
1128+ opts : []option.ClientOption {
1129+ option .WithEndpoint ("first.example.com:443" ),
1130+ option .WithEndpoint ("second.example.com:443" ),
1131+ },
1132+ wantTarget : "second.example.com:443" ,
1133+ },
1134+ {
1135+ name : "empty_with_endpoint_clears_config_api_addr" ,
1136+ envUniverseDomain : "env-universe.test" ,
1137+ apiAddr : "custom-config.example.com:443" ,
1138+ opts : []option.ClientOption {option .WithEndpoint ("" )},
1139+ wantTarget : "cloudprofiler.env-universe.test:443" ,
1140+ },
1141+ {
1142+ name : "endpoint_with_scheme_preserved_verbatim" ,
1143+ apiAddr : "dns:///127.0.0.1:443" ,
1144+ wantTarget : "dns:///127.0.0.1:443" ,
1145+ },
1146+ }
1147+
1148+ for _ , tc := range tests {
1149+ t .Run (tc .name , func (t * testing.T ) {
1150+ t .Setenv ("GOOGLE_CLOUD_UNIVERSE_DOMAIN" , tc .envUniverseDomain )
1151+ // Keep endpoint resolution hermetic on hosts with ambient mTLS/S2A settings.
1152+ t .Setenv ("GOOGLE_API_USE_CLIENT_CERTIFICATE" , "false" )
1153+ mtlsEndpoint := tc .envUseMTLSEndpoint
1154+ if mtlsEndpoint == "" {
1155+ mtlsEndpoint = "auto"
1156+ }
1157+ t .Setenv ("GOOGLE_API_USE_MTLS_ENDPOINT" , mtlsEndpoint )
1158+ t .Setenv ("GOOGLE_API_USE_MTLS" , "" )
1159+ t .Setenv ("EXPERIMENTAL_GOOGLE_API_USE_S2A" , "false" )
1160+
1161+ onGCE = func () bool { return false }
1162+
1163+ var gotTarget string
1164+ dialGRPC = func (ctx context.Context , opts ... option.ClientOption ) (gtransport.ConnPool , error ) {
1165+ connPool , err := gtransport .DialPool (ctx , opts ... )
1166+ if err != nil {
1167+ return nil , err
1168+ }
1169+ defer connPool .Close ()
1170+ gotTarget = connPool .Conn ().Target ()
1171+ return nil , errStopAfterDial
1172+ }
1173+
1174+ cfg := Config {
1175+ Service : testService ,
1176+ ProjectID : testProjectID ,
1177+ APIAddr : tc .apiAddr ,
1178+ DebugLoggingOutput : io .Discard ,
1179+ }
1180+ opts := append ([]option.ClientOption {
1181+ option .WithoutAuthentication (),
1182+ option .WithGRPCDialOption (grpc .WithContextDialer (noOpDialer )),
1183+ }, tc .opts ... )
1184+
1185+ err := start (cfg , opts ... )
1186+ if ! errors .Is (err , errStopAfterDial ) {
1187+ t .Fatalf ("start() unexpected error: %v" , err )
1188+ }
1189+ if gotTarget != tc .wantTarget {
1190+ t .Errorf ("start() resolved endpoint = %q, want %q" , gotTarget , tc .wantTarget )
1191+ }
1192+ })
1193+ }
1194+
1195+ }
0 commit comments