...

Source file src/google.golang.org/grpc/xds/internal/xdsclient/xdsresource/name_test.go

Documentation: google.golang.org/grpc/xds/internal/xdsclient/xdsresource

     1  /*
     2   *
     3   * Copyright 2021 gRPC authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package xdsresource
    19  
    20  import (
    21  	"testing"
    22  
    23  	"github.com/google/go-cmp/cmp"
    24  	"github.com/google/go-cmp/cmp/cmpopts"
    25  )
    26  
    27  func TestParseName(t *testing.T) {
    28  	tests := []struct {
    29  		name    string
    30  		in      string
    31  		want    *Name
    32  		wantStr string
    33  	}{
    34  		{
    35  			name:    "old style name",
    36  			in:      "test-resource",
    37  			want:    &Name{ID: "test-resource"},
    38  			wantStr: "test-resource",
    39  		},
    40  		{
    41  			name:    "invalid not url",
    42  			in:      "a:/b/c",
    43  			want:    &Name{ID: "a:/b/c"},
    44  			wantStr: "a:/b/c",
    45  		},
    46  		{
    47  			name:    "invalid no resource type",
    48  			in:      "xdstp://auth/id",
    49  			want:    &Name{ID: "xdstp://auth/id"},
    50  			wantStr: "xdstp://auth/id",
    51  		},
    52  		{
    53  			name:    "valid with no authority",
    54  			in:      "xdstp:///type/id",
    55  			want:    &Name{Scheme: "xdstp", Authority: "", Type: "type", ID: "id"},
    56  			wantStr: "xdstp:///type/id",
    57  		},
    58  		{
    59  			name:    "valid no ctx params",
    60  			in:      "xdstp://auth/type/id",
    61  			want:    &Name{Scheme: "xdstp", Authority: "auth", Type: "type", ID: "id"},
    62  			wantStr: "xdstp://auth/type/id",
    63  		},
    64  		{
    65  			name:    "valid with ctx params",
    66  			in:      "xdstp://auth/type/id?a=1&b=2",
    67  			want:    &Name{Scheme: "xdstp", Authority: "auth", Type: "type", ID: "id", ContextParams: map[string]string{"a": "1", "b": "2"}},
    68  			wantStr: "xdstp://auth/type/id?a=1&b=2",
    69  		},
    70  		{
    71  			name:    "valid with ctx params sorted by keys",
    72  			in:      "xdstp://auth/type/id?b=2&a=1",
    73  			want:    &Name{Scheme: "xdstp", Authority: "auth", Type: "type", ID: "id", ContextParams: map[string]string{"a": "1", "b": "2"}},
    74  			wantStr: "xdstp://auth/type/id?a=1&b=2",
    75  		},
    76  	}
    77  	for _, tt := range tests {
    78  		t.Run(tt.name, func(t *testing.T) {
    79  			got := ParseName(tt.in)
    80  			if !cmp.Equal(got, tt.want, cmpopts.IgnoreFields(Name{}, "processingDirective")) {
    81  				t.Errorf("ParseName() = %#v, want %#v", got, tt.want)
    82  			}
    83  			if gotStr := got.String(); gotStr != tt.wantStr {
    84  				t.Errorf("Name.String() = %s, want %s", gotStr, tt.wantStr)
    85  			}
    86  		})
    87  	}
    88  }
    89  
    90  // TestNameStringCtxParamsOrder covers the case that if two names differ only in
    91  // context parameter __order__, the parsed name.String() has the same value.
    92  func TestNameStringCtxParamsOrder(t *testing.T) {
    93  	const (
    94  		a = "xdstp://auth/type/id?a=1&b=2"
    95  		b = "xdstp://auth/type/id?b=2&a=1"
    96  	)
    97  	aParsed := ParseName(a).String()
    98  	bParsed := ParseName(b).String()
    99  
   100  	if aParsed != bParsed {
   101  		t.Fatalf("aParsed.String() = %q, bParsed.String() = %q, want them to be the same", aParsed, bParsed)
   102  	}
   103  }
   104  

View as plain text