github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/api/meta/multirestmapper_test.go (about)

     1  /*
     2  Copyright 2014 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package meta
    18  
    19  import (
    20  	"errors"
    21  	"reflect"
    22  	"testing"
    23  
    24  	"github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime/schema"
    25  )
    26  
    27  func TestMultiRESTMapperResourceFor(t *testing.T) {
    28  	tcs := []struct {
    29  		name string
    30  
    31  		mapper MultiRESTMapper
    32  		input  schema.GroupVersionResource
    33  		result schema.GroupVersionResource
    34  		err    error
    35  	}{
    36  		{
    37  			name:   "empty",
    38  			mapper: MultiRESTMapper{},
    39  			input:  schema.GroupVersionResource{Resource: "foo"},
    40  			result: schema.GroupVersionResource{},
    41  			err:    &NoResourceMatchError{PartialResource: schema.GroupVersionResource{Resource: "foo"}},
    42  		},
    43  		{
    44  			name:   "ignore not found",
    45  			mapper: MultiRESTMapper{fixedRESTMapper{err: &NoResourceMatchError{PartialResource: schema.GroupVersionResource{Resource: "IGNORE_THIS"}}}},
    46  			input:  schema.GroupVersionResource{Resource: "foo"},
    47  			result: schema.GroupVersionResource{},
    48  			err:    &NoResourceMatchError{PartialResource: schema.GroupVersionResource{Resource: "foo"}},
    49  		},
    50  		{
    51  			name:   "accept first failure",
    52  			mapper: MultiRESTMapper{fixedRESTMapper{err: errors.New("fail on this")}, fixedRESTMapper{resourcesFor: []schema.GroupVersionResource{{Resource: "unused"}}}},
    53  			input:  schema.GroupVersionResource{Resource: "foo"},
    54  			result: schema.GroupVersionResource{},
    55  			err:    errors.New("fail on this"),
    56  		},
    57  	}
    58  
    59  	for _, tc := range tcs {
    60  		actualResult, actualErr := tc.mapper.ResourceFor(tc.input)
    61  		if e, a := tc.result, actualResult; e != a {
    62  			t.Errorf("%s: expected %v, got %v", tc.name, e, a)
    63  		}
    64  		switch {
    65  		case tc.err == nil && actualErr == nil:
    66  		case tc.err == nil:
    67  			t.Errorf("%s: unexpected error: %v", tc.name, actualErr)
    68  		case actualErr == nil:
    69  			t.Errorf("%s: expected error: %v got nil", tc.name, tc.err)
    70  		case tc.err.Error() != actualErr.Error():
    71  			t.Errorf("%s: expected %v, got %v", tc.name, tc.err, actualErr)
    72  		}
    73  	}
    74  }
    75  
    76  func TestMultiRESTMapperResourcesFor(t *testing.T) {
    77  	tcs := []struct {
    78  		name string
    79  
    80  		mapper MultiRESTMapper
    81  		input  schema.GroupVersionResource
    82  		result []schema.GroupVersionResource
    83  		err    error
    84  	}{
    85  		{
    86  			name:   "empty",
    87  			mapper: MultiRESTMapper{},
    88  			input:  schema.GroupVersionResource{Resource: "foo"},
    89  			result: nil,
    90  			err:    &NoResourceMatchError{PartialResource: schema.GroupVersionResource{Resource: "foo"}},
    91  		},
    92  		{
    93  			name:   "ignore not found",
    94  			mapper: MultiRESTMapper{fixedRESTMapper{err: &NoResourceMatchError{PartialResource: schema.GroupVersionResource{Resource: "IGNORE_THIS"}}}},
    95  			input:  schema.GroupVersionResource{Resource: "foo"},
    96  			result: nil,
    97  			err:    &NoResourceMatchError{PartialResource: schema.GroupVersionResource{Resource: "foo"}},
    98  		},
    99  		{
   100  			name:   "accept first failure",
   101  			mapper: MultiRESTMapper{fixedRESTMapper{err: errors.New("fail on this")}, fixedRESTMapper{resourcesFor: []schema.GroupVersionResource{{Resource: "unused"}}}},
   102  			input:  schema.GroupVersionResource{Resource: "foo"},
   103  			result: nil,
   104  			err:    errors.New("fail on this"),
   105  		},
   106  		{
   107  			name: "union and dedup",
   108  			mapper: MultiRESTMapper{
   109  				fixedRESTMapper{resourcesFor: []schema.GroupVersionResource{{Resource: "dupe"}, {Resource: "first"}}},
   110  				fixedRESTMapper{resourcesFor: []schema.GroupVersionResource{{Resource: "dupe"}, {Resource: "second"}}},
   111  			},
   112  			input:  schema.GroupVersionResource{Resource: "foo"},
   113  			result: []schema.GroupVersionResource{{Resource: "dupe"}, {Resource: "first"}, {Resource: "second"}},
   114  		},
   115  		{
   116  			name: "skip not and continue",
   117  			mapper: MultiRESTMapper{
   118  				fixedRESTMapper{err: &NoResourceMatchError{PartialResource: schema.GroupVersionResource{Resource: "IGNORE_THIS"}}},
   119  				fixedRESTMapper{resourcesFor: []schema.GroupVersionResource{{Resource: "first"}, {Resource: "second"}}},
   120  			},
   121  			input:  schema.GroupVersionResource{Resource: "foo"},
   122  			result: []schema.GroupVersionResource{{Resource: "first"}, {Resource: "second"}},
   123  		},
   124  	}
   125  
   126  	for _, tc := range tcs {
   127  		actualResult, actualErr := tc.mapper.ResourcesFor(tc.input)
   128  		if e, a := tc.result, actualResult; !reflect.DeepEqual(e, a) {
   129  			t.Errorf("%s: expected %v, got %v", tc.name, e, a)
   130  		}
   131  		switch {
   132  		case tc.err == nil && actualErr == nil:
   133  		case tc.err == nil:
   134  			t.Errorf("%s: unexpected error: %v", tc.name, actualErr)
   135  		case actualErr == nil:
   136  			t.Errorf("%s: expected error: %v got nil", tc.name, tc.err)
   137  		case tc.err.Error() != actualErr.Error():
   138  			t.Errorf("%s: expected %v, got %v", tc.name, tc.err, actualErr)
   139  		}
   140  	}
   141  }
   142  
   143  func TestMultiRESTMapperKindsFor(t *testing.T) {
   144  	tcs := []struct {
   145  		name string
   146  
   147  		mapper MultiRESTMapper
   148  		input  schema.GroupVersionResource
   149  		result []schema.GroupVersionKind
   150  		err    error
   151  	}{
   152  		{
   153  			name:   "empty",
   154  			mapper: MultiRESTMapper{},
   155  			input:  schema.GroupVersionResource{Resource: "foo"},
   156  			result: nil,
   157  			err:    &NoResourceMatchError{PartialResource: schema.GroupVersionResource{Resource: "foo"}},
   158  		},
   159  		{
   160  			name:   "ignore not found",
   161  			mapper: MultiRESTMapper{fixedRESTMapper{err: &NoResourceMatchError{PartialResource: schema.GroupVersionResource{Resource: "IGNORE_THIS"}}}},
   162  			input:  schema.GroupVersionResource{Resource: "foo"},
   163  			result: nil,
   164  			err:    &NoResourceMatchError{PartialResource: schema.GroupVersionResource{Resource: "foo"}},
   165  		},
   166  		{
   167  			name:   "accept first failure",
   168  			mapper: MultiRESTMapper{fixedRESTMapper{err: errors.New("fail on this")}, fixedRESTMapper{kindsFor: []schema.GroupVersionKind{{Kind: "unused"}}}},
   169  			input:  schema.GroupVersionResource{Resource: "foo"},
   170  			result: nil,
   171  			err:    errors.New("fail on this"),
   172  		},
   173  		{
   174  			name: "union and dedup",
   175  			mapper: MultiRESTMapper{
   176  				fixedRESTMapper{kindsFor: []schema.GroupVersionKind{{Kind: "dupe"}, {Kind: "first"}}},
   177  				fixedRESTMapper{kindsFor: []schema.GroupVersionKind{{Kind: "dupe"}, {Kind: "second"}}},
   178  			},
   179  			input:  schema.GroupVersionResource{Resource: "foo"},
   180  			result: []schema.GroupVersionKind{{Kind: "dupe"}, {Kind: "first"}, {Kind: "second"}},
   181  		},
   182  		{
   183  			name: "skip not and continue",
   184  			mapper: MultiRESTMapper{
   185  				fixedRESTMapper{err: &NoResourceMatchError{PartialResource: schema.GroupVersionResource{Resource: "IGNORE_THIS"}}},
   186  				fixedRESTMapper{kindsFor: []schema.GroupVersionKind{{Kind: "first"}, {Kind: "second"}}},
   187  			},
   188  			input:  schema.GroupVersionResource{Resource: "foo"},
   189  			result: []schema.GroupVersionKind{{Kind: "first"}, {Kind: "second"}},
   190  		},
   191  	}
   192  
   193  	for _, tc := range tcs {
   194  		actualResult, actualErr := tc.mapper.KindsFor(tc.input)
   195  		if e, a := tc.result, actualResult; !reflect.DeepEqual(e, a) {
   196  			t.Errorf("%s: expected %v, got %v", tc.name, e, a)
   197  		}
   198  		switch {
   199  		case tc.err == nil && actualErr == nil:
   200  		case tc.err == nil:
   201  			t.Errorf("%s: unexpected error: %v", tc.name, actualErr)
   202  		case actualErr == nil:
   203  			t.Errorf("%s: expected error: %v got nil", tc.name, tc.err)
   204  		case tc.err.Error() != actualErr.Error():
   205  			t.Errorf("%s: expected %v, got %v", tc.name, tc.err, actualErr)
   206  		}
   207  	}
   208  }
   209  
   210  func TestMultiRESTMapperKindFor(t *testing.T) {
   211  	tcs := []struct {
   212  		name string
   213  
   214  		mapper MultiRESTMapper
   215  		input  schema.GroupVersionResource
   216  		result schema.GroupVersionKind
   217  		err    error
   218  	}{
   219  		{
   220  			name:   "empty",
   221  			mapper: MultiRESTMapper{},
   222  			input:  schema.GroupVersionResource{Resource: "foo"},
   223  			result: schema.GroupVersionKind{},
   224  			err:    &NoResourceMatchError{PartialResource: schema.GroupVersionResource{Resource: "foo"}},
   225  		},
   226  		{
   227  			name:   "ignore not found",
   228  			mapper: MultiRESTMapper{fixedRESTMapper{err: &NoResourceMatchError{PartialResource: schema.GroupVersionResource{Resource: "IGNORE_THIS"}}}},
   229  			input:  schema.GroupVersionResource{Resource: "foo"},
   230  			result: schema.GroupVersionKind{},
   231  			err:    &NoResourceMatchError{PartialResource: schema.GroupVersionResource{Resource: "foo"}},
   232  		},
   233  		{
   234  			name:   "accept first failure",
   235  			mapper: MultiRESTMapper{fixedRESTMapper{err: errors.New("fail on this")}, fixedRESTMapper{kindsFor: []schema.GroupVersionKind{{Kind: "unused"}}}},
   236  			input:  schema.GroupVersionResource{Resource: "foo"},
   237  			result: schema.GroupVersionKind{},
   238  			err:    errors.New("fail on this"),
   239  		},
   240  	}
   241  
   242  	for _, tc := range tcs {
   243  		actualResult, actualErr := tc.mapper.KindFor(tc.input)
   244  		if e, a := tc.result, actualResult; e != a {
   245  			t.Errorf("%s: expected %v, got %v", tc.name, e, a)
   246  		}
   247  		switch {
   248  		case tc.err == nil && actualErr == nil:
   249  		case tc.err == nil:
   250  			t.Errorf("%s: unexpected error: %v", tc.name, actualErr)
   251  		case actualErr == nil:
   252  			t.Errorf("%s: expected error: %v got nil", tc.name, tc.err)
   253  		case tc.err.Error() != actualErr.Error():
   254  			t.Errorf("%s: expected %v, got %v", tc.name, tc.err, actualErr)
   255  		}
   256  	}
   257  }
   258  
   259  func TestMultiRESTMapperRESTMappings(t *testing.T) {
   260  	mapping1, mapping2 := &RESTMapping{}, &RESTMapping{}
   261  	tcs := []struct {
   262  		name string
   263  
   264  		mapper    MultiRESTMapper
   265  		groupKind schema.GroupKind
   266  		versions  []string
   267  		result    []*RESTMapping
   268  		err       error
   269  	}{
   270  		{
   271  			name:      "empty with no versions",
   272  			mapper:    MultiRESTMapper{},
   273  			groupKind: schema.GroupKind{Kind: "Foo"},
   274  			result:    nil,
   275  			err:       &NoKindMatchError{GroupKind: schema.GroupKind{Kind: "Foo"}},
   276  		},
   277  		{
   278  			name:      "empty with one version",
   279  			mapper:    MultiRESTMapper{},
   280  			groupKind: schema.GroupKind{Kind: "Foo"},
   281  			versions:  []string{"v1beta"},
   282  			result:    nil,
   283  			err:       &NoKindMatchError{GroupKind: schema.GroupKind{Kind: "Foo"}, SearchedVersions: []string{"v1beta"}},
   284  		},
   285  		{
   286  			name:      "empty with multi(two) vesions",
   287  			mapper:    MultiRESTMapper{},
   288  			groupKind: schema.GroupKind{Kind: "Foo"},
   289  			versions:  []string{"v1beta", "v2"},
   290  			result:    nil,
   291  			err:       &NoKindMatchError{GroupKind: schema.GroupKind{Kind: "Foo"}, SearchedVersions: []string{"v1beta", "v2"}},
   292  		},
   293  		{
   294  			name:      "ignore not found with kind not exist",
   295  			mapper:    MultiRESTMapper{fixedRESTMapper{err: &NoKindMatchError{GroupKind: schema.GroupKind{Kind: "IGNORE_THIS"}}}},
   296  			groupKind: schema.GroupKind{Kind: "Foo"},
   297  			versions:  nil,
   298  			result:    nil,
   299  			err:       &NoKindMatchError{GroupKind: schema.GroupKind{Kind: "Foo"}},
   300  		},
   301  		{
   302  			name:      "ignore not found with version not exist",
   303  			mapper:    MultiRESTMapper{fixedRESTMapper{err: &NoKindMatchError{GroupKind: schema.GroupKind{Kind: "Foo"}, SearchedVersions: []string{"v1"}}}},
   304  			groupKind: schema.GroupKind{Kind: "Foo"},
   305  			versions:  []string{"v1beta"},
   306  			result:    nil,
   307  			err:       &NoKindMatchError{GroupKind: schema.GroupKind{Kind: "Foo"}, SearchedVersions: []string{"v1beta"}},
   308  		},
   309  		{
   310  			name:      "ignore not found with multi versions not exist",
   311  			mapper:    MultiRESTMapper{fixedRESTMapper{err: &NoKindMatchError{GroupKind: schema.GroupKind{Kind: "Foo"}, SearchedVersions: []string{"v1"}}}},
   312  			groupKind: schema.GroupKind{Kind: "Foo"},
   313  			versions:  []string{"v1beta", "v2"},
   314  			result:    nil,
   315  			err:       &NoKindMatchError{GroupKind: schema.GroupKind{Kind: "Foo"}, SearchedVersions: []string{"v1beta", "v2"}},
   316  		},
   317  		{
   318  			name:      "accept first failure",
   319  			mapper:    MultiRESTMapper{fixedRESTMapper{err: errors.New("fail on this")}, fixedRESTMapper{mappings: []*RESTMapping{mapping1}}},
   320  			groupKind: schema.GroupKind{Kind: "Foo"},
   321  			versions:  []string{"v1beta"},
   322  			result:    nil,
   323  			err:       errors.New("fail on this"),
   324  		},
   325  		{
   326  			name:      "return both",
   327  			mapper:    MultiRESTMapper{fixedRESTMapper{mappings: []*RESTMapping{mapping1}}, fixedRESTMapper{mappings: []*RESTMapping{mapping2}}},
   328  			groupKind: schema.GroupKind{Kind: "Foo"},
   329  			versions:  []string{"v1beta"},
   330  			result:    []*RESTMapping{mapping1, mapping2},
   331  		},
   332  	}
   333  
   334  	for _, tc := range tcs {
   335  		actualResult, actualErr := tc.mapper.RESTMappings(tc.groupKind, tc.versions...)
   336  		if e, a := tc.result, actualResult; !reflect.DeepEqual(e, a) {
   337  			t.Errorf("%s: expected %v, got %v", tc.name, e, a)
   338  		}
   339  		switch {
   340  		case tc.err == nil && actualErr == nil:
   341  		case tc.err == nil:
   342  			t.Errorf("%s: unexpected error: %v", tc.name, actualErr)
   343  		case actualErr == nil:
   344  			t.Errorf("%s: expected error: %v got nil", tc.name, tc.err)
   345  		case tc.err.Error() != actualErr.Error():
   346  			t.Errorf("%s: expected %v, got %v", tc.name, tc.err, actualErr)
   347  		}
   348  	}
   349  }
   350  
   351  type fixedRESTMapper struct {
   352  	resourcesFor []schema.GroupVersionResource
   353  	kindsFor     []schema.GroupVersionKind
   354  	resourceFor  schema.GroupVersionResource
   355  	kindFor      schema.GroupVersionKind
   356  	mappings     []*RESTMapping
   357  
   358  	err error
   359  }
   360  
   361  func (m fixedRESTMapper) ResourceSingularizer(resource string) (singular string, err error) {
   362  	return "", m.err
   363  }
   364  
   365  func (m fixedRESTMapper) ResourcesFor(resource schema.GroupVersionResource) ([]schema.GroupVersionResource, error) {
   366  	return m.resourcesFor, m.err
   367  }
   368  
   369  func (m fixedRESTMapper) KindsFor(resource schema.GroupVersionResource) (gvk []schema.GroupVersionKind, err error) {
   370  	return m.kindsFor, m.err
   371  }
   372  
   373  func (m fixedRESTMapper) ResourceFor(resource schema.GroupVersionResource) (schema.GroupVersionResource, error) {
   374  	return m.resourceFor, m.err
   375  }
   376  
   377  func (m fixedRESTMapper) KindFor(resource schema.GroupVersionResource) (schema.GroupVersionKind, error) {
   378  	return m.kindFor, m.err
   379  }
   380  
   381  func (m fixedRESTMapper) RESTMapping(gk schema.GroupKind, versions ...string) (mapping *RESTMapping, err error) {
   382  	return nil, m.err
   383  }
   384  
   385  func (m fixedRESTMapper) RESTMappings(gk schema.GroupKind, versions ...string) (mappings []*RESTMapping, err error) {
   386  	return m.mappings, m.err
   387  }
   388  
   389  func (m fixedRESTMapper) ResourceIsValid(resource schema.GroupVersionResource) bool {
   390  	return false
   391  }