@ -45,6 +45,7 @@
# include "src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_balancer_addresses.h"
# include "src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_balancer_addresses.h"
# include "src/core/ext/filters/client_channel/resolver/fake/fake_resolver.h"
# include "src/core/ext/filters/client_channel/resolver/fake/fake_resolver.h"
# include "src/core/lib/address_utils/parse_address.h"
# include "src/core/lib/address_utils/parse_address.h"
# include "src/core/lib/channel/channel_args.h"
# include "src/core/lib/gpr/env.h"
# include "src/core/lib/gpr/env.h"
# include "src/core/lib/gprpp/ref_counted_ptr.h"
# include "src/core/lib/gprpp/ref_counted_ptr.h"
# include "src/core/lib/iomgr/sockaddr.h"
# include "src/core/lib/iomgr/sockaddr.h"
@ -102,12 +103,22 @@ using BalancerService = CountedService<LoadBalancer::Service>;
const char g_kCallCredsMdKey [ ] = " Balancer should not ... " ;
const char g_kCallCredsMdKey [ ] = " Balancer should not ... " ;
const char g_kCallCredsMdValue [ ] = " ... receive me " ;
const char g_kCallCredsMdValue [ ] = " ... receive me " ;
// A test user agent string sent by the client only to the grpclb loadbalancer.
// The backend should not see this user-agent string.
constexpr char kGrpclbSpecificUserAgentString [ ] = " grpc-grpclb-test-user-agent " ;
class BackendServiceImpl : public BackendService {
class BackendServiceImpl : public BackendService {
public :
public :
BackendServiceImpl ( ) { }
BackendServiceImpl ( ) { }
Status Echo ( ServerContext * context , const EchoRequest * request ,
Status Echo ( ServerContext * context , const EchoRequest * request ,
EchoResponse * response ) override {
EchoResponse * response ) override {
// The backend should not see a test user agent configured at the client
// using GRPC_ARG_GRPCLB_CHANNEL_ARGS.
auto it = context - > client_metadata ( ) . find ( " user-agent " ) ;
if ( it ! = context - > client_metadata ( ) . end ( ) ) {
EXPECT_FALSE ( it - > second . starts_with ( kGrpclbSpecificUserAgentString ) ) ;
}
// Backend should receive the call credentials metadata.
// Backend should receive the call credentials metadata.
auto call_credentials_entry =
auto call_credentials_entry =
context - > client_metadata ( ) . find ( g_kCallCredsMdKey ) ;
context - > client_metadata ( ) . find ( g_kCallCredsMdKey ) ;
@ -198,6 +209,15 @@ class BalancerServiceImpl : public BalancerService {
if ( serverlist_done_ ) goto done ;
if ( serverlist_done_ ) goto done ;
}
}
{
{
// The loadbalancer should see a test user agent because it was
// specifically configured at the client using
// GRPC_ARG_GRPCLB_CHANNEL_ARGS
auto it = context - > client_metadata ( ) . find ( " user-agent " ) ;
EXPECT_TRUE ( it ! = context - > client_metadata ( ) . end ( ) ) ;
if ( it ! = context - > client_metadata ( ) . end ( ) ) {
EXPECT_THAT ( std : : string ( it - > second . data ( ) , it - > second . length ( ) ) ,
: : testing : : StartsWith ( kGrpclbSpecificUserAgentString ) ) ;
}
// Balancer shouldn't receive the call credentials metadata.
// Balancer shouldn't receive the call credentials metadata.
EXPECT_EQ ( context - > client_metadata ( ) . find ( g_kCallCredsMdKey ) ,
EXPECT_EQ ( context - > client_metadata ( ) . find ( g_kCallCredsMdKey ) ,
context - > client_metadata ( ) . end ( ) ) ;
context - > client_metadata ( ) . end ( ) ) ;
@ -406,17 +426,45 @@ class GrpclbEnd2endTest : public ::testing::Test {
void ResetStub ( int fallback_timeout = 0 ,
void ResetStub ( int fallback_timeout = 0 ,
const std : : string & expected_targets = " " ,
const std : : string & expected_targets = " " ,
int subchannel_cache_delay_ms = 0 ) {
int subchannel_cache_delay_ms = 0 ) {
// Send a separate user agent string for the grpclb load balancer alone.
grpc_core : : ChannelArgs grpclb_channel_args ;
// Set a special user agent string for the grpclb load balancer. It
// will be verified at the load balancer.
grpclb_channel_args = grpclb_channel_args . Set (
GRPC_ARG_PRIMARY_USER_AGENT_STRING , kGrpclbSpecificUserAgentString ) ;
ChannelArguments args ;
ChannelArguments args ;
if ( fallback_timeout > 0 ) args . SetGrpclbFallbackTimeout ( fallback_timeout ) ;
if ( fallback_timeout > 0 ) args . SetGrpclbFallbackTimeout ( fallback_timeout ) ;
args . SetPointer ( GRPC_ARG_FAKE_RESOLVER_RESPONSE_GENERATOR ,
args . SetPointer ( GRPC_ARG_FAKE_RESOLVER_RESPONSE_GENERATOR ,
response_generator_ . get ( ) ) ;
response_generator_ . get ( ) ) ;
if ( ! expected_targets . empty ( ) ) {
if ( ! expected_targets . empty ( ) ) {
args . SetString ( GRPC_ARG_FAKE_SECURITY_EXPECTED_TARGETS , expected_targets ) ;
args . SetString ( GRPC_ARG_FAKE_SECURITY_EXPECTED_TARGETS , expected_targets ) ;
grpclb_channel_args = grpclb_channel_args . Set (
GRPC_ARG_FAKE_SECURITY_EXPECTED_TARGETS , expected_targets ) ;
}
}
if ( subchannel_cache_delay_ms > 0 ) {
if ( subchannel_cache_delay_ms > 0 ) {
args . SetInt ( GRPC_ARG_GRPCLB_SUBCHANNEL_CACHE_INTERVAL_MS ,
args . SetInt ( GRPC_ARG_GRPCLB_SUBCHANNEL_CACHE_INTERVAL_MS ,
subchannel_cache_delay_ms * grpc_test_slowdown_factor ( ) ) ;
subchannel_cache_delay_ms * grpc_test_slowdown_factor ( ) ) ;
}
}
static const grpc_arg_pointer_vtable channel_args_vtable = {
// copy
[ ] ( void * p ) - > void * {
return grpc_channel_args_copy ( static_cast < grpc_channel_args * > ( p ) ) ;
} ,
// destroy
[ ] ( void * p ) {
grpc_channel_args_destroy ( static_cast < grpc_channel_args * > ( p ) ) ;
} ,
// compare
[ ] ( void * p1 , void * p2 ) {
return grpc_channel_args_compare ( static_cast < grpc_channel_args * > ( p1 ) ,
static_cast < grpc_channel_args * > ( p2 ) ) ;
} ,
} ;
// Specify channel args for the channel to the load balancer.
args . SetPointerWithVtable (
GRPC_ARG_EXPERIMENTAL_GRPCLB_CHANNEL_ARGS ,
const_cast < grpc_channel_args * > ( grpclb_channel_args . ToC ( ) . get ( ) ) ,
& channel_args_vtable ) ;
std : : ostringstream uri ;
std : : ostringstream uri ;
uri < < " fake:/// " < < kApplicationTargetName_ ;
uri < < " fake:/// " < < kApplicationTargetName_ ;
// TODO(dgq): templatize tests to run everything using both secure and
// TODO(dgq): templatize tests to run everything using both secure and