@@ -1749,54 +1749,75 @@ func (s) TestAllowedGRPCServices_UnmarshalJSON(t *testing.T) {
17491749 tests := []struct {
17501750 name string
17511751 json string
1752- // The identity of the built channel credentials must match the first
1753- // supported channel-creds entry from the bootstrap JSON.
1754- wantSelectedChannelCredsType string
1755- wantSelectedChannelCredsConfig json.RawMessage
1756- wantSideCallCreds int
1752+ // want carries the expected target and credential identities;
1753+ // comparisons use the Equal methods via cmp.Diff.
1754+ want AllowedGRPCServices
17571755 }{
17581756 {
1759- name : "insecure_channel_creds" ,
1760- json : `{"dns:///sharding-service:443": {"channel_creds": [{"type": "insecure"}]}}` ,
1761- wantSelectedChannelCredsType : "insecure" ,
1762- wantSideCallCreds : 0 ,
1757+ name : "insecure_channel_creds" ,
1758+ json : `{"dns:///sharding-service:443": {"channel_creds": [{"type": "insecure"}]}}` ,
1759+ want : AllowedGRPCServices {target : {
1760+ targetURI : target ,
1761+ sideChannelCreds : xdscreds .NewChannelCreds (nil , xdscreds.Identity {Type : "insecure" }, nil ),
1762+ }},
17631763 },
17641764 {
1765- name : "with_call_creds" ,
1766- json : `{"dns:///sharding-service:443": {"channel_creds": [{"type": "insecure"}], "call_creds": [{"type": "jwt_token_file", "config": {"jwt_token_file": "/var/run/secrets/tokens/istio-token"}}]}}` ,
1767- wantSelectedChannelCredsType : "insecure" ,
1768- // One call credential is built for the supported call-creds
1769- // config.
1770- wantSideCallCreds : 1 ,
1765+ name : "with_call_creds" ,
1766+ json : `{"dns:///sharding-service:443": {"channel_creds": [{"type": "insecure"}], "call_creds": [{"type": "jwt_token_file", "config": {"jwt_token_file": "/var/run/secrets/tokens/istio-token"}}]}}` ,
1767+ want : AllowedGRPCServices {target : {
1768+ targetURI : target ,
1769+ sideChannelCreds : xdscreds .NewChannelCreds (nil , xdscreds.Identity {Type : "insecure" }, nil ),
1770+ sideCallCreds : []* xdscreds.CallCreds {xdscreds .NewCallCreds (nil , xdscreds.Identity {
1771+ Type : "jwt_token_file" ,
1772+ Data : json .RawMessage (`{"jwt_token_file": "/var/run/secrets/tokens/istio-token"}` ),
1773+ }, nil )},
1774+ }},
17711775 },
17721776 {
1773- name : "unsupported_call_creds_skipped" ,
1774- json : `{"dns:///sharding-service:443": {"channel_creds": [{"type": "insecure"}], "call_creds": [{"type": "unsupported_call_creds_type"}]}}` ,
1775- wantSelectedChannelCredsType : "insecure" ,
1776- // Unsupported call-creds types are skipped without error, so
1777- // no call credentials are built.
1778- wantSideCallCreds : 0 ,
1777+ // Unsupported call-creds types are skipped without error, so no
1778+ // call credentials are built.
1779+ name : "unsupported_call_creds_skipped" ,
1780+ json : `{"dns:///sharding-service:443": {"channel_creds": [{"type": "insecure"}], "call_creds": [{"type": "unsupported_call_creds_type"}]}}` ,
1781+ want : AllowedGRPCServices {target : {
1782+ targetURI : target ,
1783+ sideChannelCreds : xdscreds .NewChannelCreds (nil , xdscreds.Identity {Type : "insecure" }, nil ),
1784+ }},
17791785 },
17801786 {
1781- name : "multiple_supported_call_creds" ,
1782- json : `{"dns:///sharding-service:443": {"channel_creds": [{"type": "insecure"}], "call_creds": [{"type": "jwt_token_file", "config": {"jwt_token_file": "/tokens/token-one"}}, {"type": "jwt_token_file", "config": {"jwt_token_file": "/tokens/token-two"}}]}}` ,
1783- wantSelectedChannelCredsType : "insecure" ,
17841787 // One call credential is built for each supported call-creds
1785- // config.
1786- wantSideCallCreds : 2 ,
1788+ // config, preserving order.
1789+ name : "multiple_supported_call_creds" ,
1790+ json : `{"dns:///sharding-service:443": {"channel_creds": [{"type": "insecure"}], "call_creds": [{"type": "jwt_token_file", "config": {"jwt_token_file": "/tokens/token-one"}}, {"type": "jwt_token_file", "config": {"jwt_token_file": "/tokens/token-two"}}]}}` ,
1791+ want : AllowedGRPCServices {target : {
1792+ targetURI : target ,
1793+ sideChannelCreds : xdscreds .NewChannelCreds (nil , xdscreds.Identity {Type : "insecure" }, nil ),
1794+ sideCallCreds : []* xdscreds.CallCreds {
1795+ xdscreds .NewCallCreds (nil , xdscreds.Identity {
1796+ Type : "jwt_token_file" ,
1797+ Data : json .RawMessage (`{"jwt_token_file": "/tokens/token-one"}` ),
1798+ }, nil ),
1799+ xdscreds .NewCallCreds (nil , xdscreds.Identity {
1800+ Type : "jwt_token_file" ,
1801+ Data : json .RawMessage (`{"jwt_token_file": "/tokens/token-two"}` ),
1802+ }, nil ),
1803+ },
1804+ }},
17871805 },
17881806 {
1789- name : "tls_channel_creds" ,
1790- json : `{"dns:///sharding-service:443": {"channel_creds": [{"type": "tls", "config": {}}]}}` ,
1791- wantSelectedChannelCredsType : "tls" ,
1792- wantSelectedChannelCredsConfig : json .RawMessage ("{}" ),
1793- wantSideCallCreds : 0 ,
1807+ name : "tls_channel_creds" ,
1808+ json : `{"dns:///sharding-service:443": {"channel_creds": [{"type": "tls", "config": {}}]}}` ,
1809+ want : AllowedGRPCServices {target : {
1810+ targetURI : target ,
1811+ sideChannelCreds : xdscreds .NewChannelCreds (nil , xdscreds.Identity {Type : "tls" , Data : json .RawMessage ("{}" )}, nil ),
1812+ }},
17941813 },
17951814 {
1796- name : "skips_unsupported_channel_creds" ,
1797- json : `{"dns:///sharding-service:443": {"channel_creds": [{"type": "unsupported_cred_type"}, {"type": "insecure"}]}}` ,
1798- wantSelectedChannelCredsType : "insecure" ,
1799- wantSideCallCreds : 0 ,
1815+ name : "skips_unsupported_channel_creds" ,
1816+ json : `{"dns:///sharding-service:443": {"channel_creds": [{"type": "unsupported_cred_type"}, {"type": "insecure"}]}}` ,
1817+ want : AllowedGRPCServices {target : {
1818+ targetURI : target ,
1819+ sideChannelCreds : xdscreds .NewChannelCreds (nil , xdscreds.Identity {Type : "insecure" }, nil ),
1820+ }},
18001821 },
18011822 }
18021823
@@ -1806,24 +1827,15 @@ func (s) TestAllowedGRPCServices_UnmarshalJSON(t *testing.T) {
18061827 if err := json .Unmarshal ([]byte (test .json ), & got ); err != nil {
18071828 t .Fatalf ("AllowedGRPCServices unmarshal failed: %v" , err )
18081829 }
1809- svc , ok := got [target ]
1810- if ! ok {
1811- t .Fatalf ("AllowedGRPCServices missing key %q" , target )
1812- }
1813- if svc .TargetURI () != target {
1814- t .Errorf ("TargetURI() = %q, want %q" , svc .TargetURI (), target )
1830+ if diff := cmp .Diff (test .want , got ); diff != "" {
1831+ t .Errorf ("AllowedGRPCServices unmarshal returned unexpected diff (-want +got):\n %s" , diff )
18151832 }
1816- chanCreds , callCreds := svc .SideChannelCredentials ()
1833+ // Equal compares credentials by identity only, so it cannot tell
1834+ // a built bundle from a nil one; verify the bundle was built.
1835+ chanCreds , _ := got [target ].SideChannelCredentials ()
18171836 if chanCreds == nil || chanCreds .Bundle () == nil {
18181837 t .Error ("SideChannelCredentials() returned no built channel credentials" )
18191838 }
1820- wantIdentity := xdscreds.Identity {Type : test .wantSelectedChannelCredsType , Data : test .wantSelectedChannelCredsConfig }
1821- if wantChanCreds := xdscreds .NewChannelCreds (nil , wantIdentity , nil ); ! chanCreds .Equal (wantChanCreds ) {
1822- t .Errorf ("SideChannelCredentials() channel credentials = %+v, want identity %+v" , chanCreds , wantIdentity )
1823- }
1824- if got := len (callCreds ); got != test .wantSideCallCreds {
1825- t .Errorf ("len(SideChannelCredentials() call creds) = %d, want %d" , got , test .wantSideCallCreds )
1826- }
18271839 })
18281840 }
18291841}
0 commit comments