xdsclient: handle empty authority in new style resource names (#5488)

This commit is contained in:
Easwar Swaminathan 2022-07-12 14:12:07 -07:00 committed by GitHub
parent c402378755
commit 5e15eac0c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 31 deletions

View File

@ -119,7 +119,7 @@ func (n *Name) String() string {
path := n.Type path := n.Type
if n.ID != "" { if n.ID != "" {
path = path + "/" + n.ID path = "/" + path + "/" + n.ID
} }
tempURL := &url.URL{ tempURL := &url.URL{

View File

@ -18,9 +18,10 @@
package xdsresource package xdsresource
import ( import (
"reflect"
"testing" "testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"google.golang.org/grpc/internal/envconfig" "google.golang.org/grpc/internal/envconfig"
) )
@ -30,42 +31,63 @@ func TestParseName(t *testing.T) {
env bool // Whether federation env is set to true. env bool // Whether federation env is set to true.
in string in string
want *Name want *Name
wantStr string
}{ }{
{ {
name: "env off", name: "env off",
env: false, env: false,
in: "xdstp://auth/type/id", in: "xdstp://auth/type/id",
want: &Name{ID: "xdstp://auth/type/id"}, want: &Name{ID: "xdstp://auth/type/id"},
wantStr: "xdstp://auth/type/id",
}, },
{ {
name: "old style name", name: "old style name",
env: true, env: true,
in: "test-resource", in: "test-resource",
want: &Name{ID: "test-resource"}, want: &Name{ID: "test-resource"},
wantStr: "test-resource",
}, },
{ {
name: "invalid not url", name: "invalid not url",
env: true, env: true,
in: "a:/b/c", in: "a:/b/c",
want: &Name{ID: "a:/b/c"}, want: &Name{ID: "a:/b/c"},
wantStr: "a:/b/c",
}, },
{ {
name: "invalid no resource type", name: "invalid no resource type",
env: true, env: true,
in: "xdstp://auth/id", in: "xdstp://auth/id",
want: &Name{ID: "xdstp://auth/id"}, want: &Name{ID: "xdstp://auth/id"},
wantStr: "xdstp://auth/id",
},
{
name: "valid with no authority",
env: true,
in: "xdstp:///type/id",
want: &Name{Scheme: "xdstp", Authority: "", Type: "type", ID: "id"},
wantStr: "xdstp:///type/id",
}, },
{ {
name: "valid no ctx params", name: "valid no ctx params",
env: true, env: true,
in: "xdstp://auth/type/id", in: "xdstp://auth/type/id",
want: &Name{Scheme: "xdstp", Authority: "auth", Type: "type", ID: "id"}, want: &Name{Scheme: "xdstp", Authority: "auth", Type: "type", ID: "id"},
wantStr: "xdstp://auth/type/id",
}, },
{ {
name: "valid with ctx params", name: "valid with ctx params",
env: true, env: true,
in: "xdstp://auth/type/id?a=1&b=2", in: "xdstp://auth/type/id?a=1&b=2",
want: &Name{Scheme: "xdstp", Authority: "auth", Type: "type", ID: "id", ContextParams: map[string]string{"a": "1", "b": "2"}}, want: &Name{Scheme: "xdstp", Authority: "auth", Type: "type", ID: "id", ContextParams: map[string]string{"a": "1", "b": "2"}},
wantStr: "xdstp://auth/type/id?a=1&b=2",
},
{
name: "valid with ctx params sorted by keys",
env: true,
in: "xdstp://auth/type/id?b=2&a=1",
want: &Name{Scheme: "xdstp", Authority: "auth", Type: "type", ID: "id", ContextParams: map[string]string{"a": "1", "b": "2"}},
wantStr: "xdstp://auth/type/id?a=1&b=2",
}, },
} }
for _, tt := range tests { for _, tt := range tests {
@ -77,9 +99,13 @@ func TestParseName(t *testing.T) {
return func() { envconfig.XDSFederation = oldEnv } return func() { envconfig.XDSFederation = oldEnv }
}()() }()()
} }
if got := ParseName(tt.in); !reflect.DeepEqual(got, tt.want) { got := ParseName(tt.in)
if !cmp.Equal(got, tt.want, cmpopts.IgnoreFields(Name{}, "processingDirective")) {
t.Errorf("ParseName() = %#v, want %#v", got, tt.want) t.Errorf("ParseName() = %#v, want %#v", got, tt.want)
} }
if gotStr := got.String(); gotStr != tt.wantStr {
t.Errorf("Name.String() = %s, want %s", gotStr, tt.wantStr)
}
}) })
} }
} }