-
Notifications
You must be signed in to change notification settings - Fork 4.6k
delegatingresolver: add default port to addresses #8613
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
4186a66
2156251
07add50
abc1773
0702e0c
2f50b78
aee0066
3d93bc1
80a3e98
c19e88d
82076ed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,10 +23,12 @@ import ( | |
"errors" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"google.golang.org/grpc/internal/envconfig" | ||
"google.golang.org/grpc/internal/grpctest" | ||
"google.golang.org/grpc/internal/proxyattributes" | ||
"google.golang.org/grpc/internal/resolver/delegatingresolver" | ||
|
@@ -246,54 +248,130 @@ func (s) TestDelegatingResolverwithDNSAndProxyWithTargetResolution(t *testing.T) | |
} | ||
} | ||
|
||
// Tests the scenario where a proxy is configured, the target URI contains the | ||
// "dns" scheme, and target resolution is disabled(default behavior). The test | ||
// verifies that the addresses returned by the delegating resolver include the | ||
// proxy resolver's addresses, with the unresolved target URI as an attribute | ||
// of the proxy address. | ||
func (s) TestDelegatingResolverwithDNSAndProxyWithNoTargetResolution(t *testing.T) { | ||
// Tests the creation of a delegating resolver when a proxy is configured. It | ||
// verifies successful creation for valid targets and ensures the final address | ||
// is from the proxy resolver and contains the original, correctly-formatted | ||
// target address as an attribute. | ||
func (s) TestDelegatingResolverwithDNSAndProxyWithNoTargetResolutionHappyPaths(t *testing.T) { | ||
const ( | ||
targetTestAddr = "test.com" | ||
envProxyAddr = "proxytest.com" | ||
resolvedProxyTestAddr1 = "11.11.11.11:7687" | ||
) | ||
overrideTestHTTPSProxy(t, envProxyAddr) | ||
|
||
targetResolver := manual.NewBuilderWithScheme("dns") | ||
target := targetResolver.Scheme() + ":///" + targetTestAddr | ||
// Set up a manual DNS resolver to control the proxy address resolution. | ||
proxyResolver, proxyResolverBuilt := setupDNS(t) | ||
|
||
tcc, stateCh, _ := createTestResolverClientConn(t) | ||
if _, err := delegatingresolver.New(resolver.Target{URL: *testutils.MustParseURL(target)}, tcc, resolver.BuildOptions{}, targetResolver, false); err != nil { | ||
t.Fatalf("Failed to create delegating resolver: %v", err) | ||
tests := []struct { | ||
name string | ||
target string | ||
wantConnectAddress string | ||
defaultPortEnvVar bool | ||
}{ | ||
{ | ||
name: "no port ", | ||
target: "test.com", | ||
wantConnectAddress: "test.com:443", | ||
defaultPortEnvVar: true, | ||
}, | ||
{ | ||
name: "complete target", | ||
target: "test.com:8080", | ||
wantConnectAddress: "test.com:8080", | ||
defaultPortEnvVar: true, | ||
}, | ||
{ | ||
name: "no port with default port env variable diabled", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: s/diabled/disabled |
||
target: "test.com", | ||
wantConnectAddress: "test.com", | ||
defaultPortEnvVar: false, | ||
}, | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) | ||
defer cancel() | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
testutils.SetEnvConfig(t, &envconfig.EnableDefaultPortForProxyTarget, test.defaultPortEnvVar) | ||
overrideTestHTTPSProxy(t, envProxyAddr) | ||
|
||
// Wait for the proxy resolver to be built before calling UpdateState. | ||
mustBuildResolver(ctx, t, proxyResolverBuilt) | ||
proxyResolver.UpdateState(resolver.State{ | ||
Addresses: []resolver.Address{ | ||
{Addr: resolvedProxyTestAddr1}, | ||
}, | ||
}) | ||
targetResolver := manual.NewBuilderWithScheme("dns") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't you have to unregister the original |
||
target := targetResolver.Scheme() + ":///" + test.target | ||
// Set up a manual DNS resolver to control the proxy address resolution. | ||
proxyResolver, proxyResolverBuilt := setupDNS(t) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I see that this helper does re-register the original There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is what we had earlier too..in the tests. |
||
|
||
wantState := resolver.State{ | ||
Addresses: []resolver.Address{proxyAddressWithTargetAttribute(resolvedProxyTestAddr1, targetTestAddr)}, | ||
Endpoints: []resolver.Endpoint{{Addresses: []resolver.Address{proxyAddressWithTargetAttribute(resolvedProxyTestAddr1, targetTestAddr)}}}, | ||
tcc, stateCh, _ := createTestResolverClientConn(t) | ||
if _, err := delegatingresolver.New(resolver.Target{URL: *testutils.MustParseURL(target)}, tcc, resolver.BuildOptions{}, targetResolver, false); err != nil { | ||
t.Fatalf("Delegating resolver creation failed unexpectedly with error: %v", err) | ||
} | ||
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) | ||
defer cancel() | ||
|
||
// Wait for the proxy resolver to be built before calling UpdateState. | ||
mustBuildResolver(ctx, t, proxyResolverBuilt) | ||
proxyResolver.UpdateState(resolver.State{ | ||
Addresses: []resolver.Address{ | ||
{Addr: resolvedProxyTestAddr1}, | ||
}, | ||
}) | ||
|
||
wantState := resolver.State{ | ||
Addresses: []resolver.Address{proxyAddressWithTargetAttribute(resolvedProxyTestAddr1, test.wantConnectAddress)}, | ||
Endpoints: []resolver.Endpoint{{Addresses: []resolver.Address{proxyAddressWithTargetAttribute(resolvedProxyTestAddr1, test.wantConnectAddress)}}}, | ||
} | ||
|
||
var gotState resolver.State | ||
select { | ||
case gotState = <-stateCh: | ||
case <-ctx.Done(): | ||
t.Fatal("Context timed out when waiting for a state update from the delegating resolver") | ||
} | ||
|
||
if diff := cmp.Diff(gotState, wantState); diff != "" { | ||
t.Fatalf("Unexpected state from delegating resolver. Diff (-got +want):\n%v", diff) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
var gotState resolver.State | ||
select { | ||
case gotState = <-stateCh: | ||
case <-ctx.Done(): | ||
t.Fatal("Context timed out when waiting for a state update from the delegating resolver") | ||
// Tests the creation of a delegating resolver when a proxy is configured. It | ||
// verifies correct error handling for invalid targets. | ||
func (s) TestDelegatingResolverwithDNSAndProxyWithNoTargetResolutionWithErrorTargets(t *testing.T) { | ||
const ( | ||
envProxyAddr = "proxytest.com" | ||
resolvedProxyTestAddr1 = "11.11.11.11:7687" | ||
) | ||
tests := []struct { | ||
name string | ||
target string | ||
wantErrorSubstring string | ||
defaultPortEnvVar bool | ||
}{ | ||
|
||
{ | ||
name: "colon after host but no port", | ||
target: "test.com:", | ||
wantErrorSubstring: "missing port after port-separator colon", | ||
defaultPortEnvVar: true, | ||
}, | ||
{ | ||
name: "empty target", | ||
target: "", | ||
wantErrorSubstring: "missing address", | ||
defaultPortEnvVar: true, | ||
}, | ||
} | ||
|
||
if diff := cmp.Diff(gotState, wantState); diff != "" { | ||
t.Fatalf("Unexpected state from delegating resolver. Diff (-got +want):\n%v", diff) | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
testutils.SetEnvConfig(t, &envconfig.EnableDefaultPortForProxyTarget, test.defaultPortEnvVar) | ||
overrideTestHTTPSProxy(t, envProxyAddr) | ||
|
||
targetResolver := manual.NewBuilderWithScheme("dns") | ||
target := targetResolver.Scheme() + ":///" + test.target | ||
|
||
tcc, _, _ := createTestResolverClientConn(t) | ||
_, err := delegatingresolver.New(resolver.Target{URL: *testutils.MustParseURL(target)}, tcc, resolver.BuildOptions{}, targetResolver, false) | ||
if err == nil { | ||
t.Fatalf("Delegating resolver created, want error containing %q", test.wantErrorSubstring) | ||
} | ||
if !strings.Contains(err.Error(), test.wantErrorSubstring) { | ||
t.Fatalf("Delegating resolver failed with error %v, want error containing %v", err.Error(), test.wantErrorSubstring) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if you have the appetite to change this to follow the more idiomatic way in Go, which is to check for errors (instead of checking for the absence of them).
Something like this seems much more readable to me and more idiomatic: