github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/api/meta/restmapper_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  	"reflect"
    21  	"strings"
    22  	"testing"
    23  
    24  	"github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime/schema"
    25  )
    26  
    27  func TestRESTMapperVersionAndKindForResource(t *testing.T) {
    28  	testGroup := "test.group"
    29  	testVersion := "test"
    30  	testGroupVersion := schema.GroupVersion{Group: testGroup, Version: testVersion}
    31  
    32  	testCases := []struct {
    33  		Resource               schema.GroupVersionResource
    34  		GroupVersionToRegister schema.GroupVersion
    35  		ExpectedGVK            schema.GroupVersionKind
    36  		Err                    bool
    37  	}{
    38  		{Resource: schema.GroupVersionResource{Resource: "internalobjec"}, Err: true},
    39  		{Resource: schema.GroupVersionResource{Resource: "internalObjec"}, Err: true},
    40  
    41  		{Resource: schema.GroupVersionResource{Resource: "internalobject"}, ExpectedGVK: testGroupVersion.WithKind("InternalObject")},
    42  		{Resource: schema.GroupVersionResource{Resource: "internalobjects"}, ExpectedGVK: testGroupVersion.WithKind("InternalObject")},
    43  	}
    44  	for i, testCase := range testCases {
    45  		mapper := NewDefaultRESTMapper([]schema.GroupVersion{testGroupVersion})
    46  		if len(testCase.ExpectedGVK.Kind) != 0 {
    47  			mapper.Add(testCase.ExpectedGVK, RESTScopeNamespace)
    48  		}
    49  		actualGVK, err := mapper.KindFor(testCase.Resource)
    50  
    51  		hasErr := err != nil
    52  		if hasErr != testCase.Err {
    53  			t.Errorf("%d: unexpected error behavior %t: %v", i, testCase.Err, err)
    54  			continue
    55  		}
    56  		if err != nil {
    57  			continue
    58  		}
    59  
    60  		if actualGVK != testCase.ExpectedGVK {
    61  			t.Errorf("%d: unexpected version and kind: e=%s a=%s", i, testCase.ExpectedGVK, actualGVK)
    62  		}
    63  	}
    64  }
    65  
    66  func TestRESTMapperGroupForResource(t *testing.T) {
    67  	testCases := []struct {
    68  		Resource         schema.GroupVersionResource
    69  		GroupVersionKind schema.GroupVersionKind
    70  		Err              bool
    71  	}{
    72  		{Resource: schema.GroupVersionResource{Resource: "myObject"}, GroupVersionKind: schema.GroupVersionKind{Group: "testapi", Version: "test", Kind: "MyObject"}},
    73  		{Resource: schema.GroupVersionResource{Resource: "myobject"}, GroupVersionKind: schema.GroupVersionKind{Group: "testapi2", Version: "test", Kind: "MyObject"}},
    74  		{Resource: schema.GroupVersionResource{Resource: "myObje"}, Err: true, GroupVersionKind: schema.GroupVersionKind{Group: "testapi", Version: "test", Kind: "MyObject"}},
    75  		{Resource: schema.GroupVersionResource{Resource: "myobje"}, Err: true, GroupVersionKind: schema.GroupVersionKind{Group: "testapi", Version: "test", Kind: "MyObject"}},
    76  	}
    77  	for i, testCase := range testCases {
    78  		mapper := NewDefaultRESTMapper([]schema.GroupVersion{testCase.GroupVersionKind.GroupVersion()})
    79  		mapper.Add(testCase.GroupVersionKind, RESTScopeNamespace)
    80  
    81  		actualGVK, err := mapper.KindFor(testCase.Resource)
    82  		if testCase.Err {
    83  			if err == nil {
    84  				t.Errorf("%d: expected error", i)
    85  			}
    86  		} else if err != nil {
    87  			t.Errorf("%d: unexpected error: %v", i, err)
    88  		} else if actualGVK != testCase.GroupVersionKind {
    89  			t.Errorf("%d: expected group %q, got %q", i, testCase.GroupVersionKind, actualGVK)
    90  		}
    91  	}
    92  }
    93  
    94  func TestRESTMapperKindsFor(t *testing.T) {
    95  	testCases := []struct {
    96  		Name                     string
    97  		PreferredOrder           []schema.GroupVersion
    98  		KindsToRegister          []schema.GroupVersionKind
    99  		PartialResourceToRequest schema.GroupVersionResource
   100  
   101  		ExpectedKinds   []schema.GroupVersionKind
   102  		ExpectedKindErr string
   103  	}{
   104  		{
   105  			// exact matches are preferred
   106  			Name: "groups, with group exact",
   107  			PreferredOrder: []schema.GroupVersion{
   108  				{Group: "first-group-1", Version: "first-version"},
   109  				{Group: "first-group", Version: "first-version"},
   110  			},
   111  			KindsToRegister: []schema.GroupVersionKind{
   112  				{Group: "first-group-1", Version: "first-version", Kind: "my-kind"},
   113  				{Group: "first-group", Version: "first-version", Kind: "my-kind"},
   114  			},
   115  			PartialResourceToRequest: schema.GroupVersionResource{Group: "first-group", Resource: "my-kind"},
   116  
   117  			ExpectedKinds: []schema.GroupVersionKind{
   118  				{Group: "first-group", Version: "first-version", Kind: "my-kind"},
   119  			},
   120  		},
   121  
   122  		{
   123  			// group prefixes work
   124  			Name: "groups, with group prefix",
   125  			PreferredOrder: []schema.GroupVersion{
   126  				{Group: "second-group", Version: "first-version"},
   127  				{Group: "first-group", Version: "first-version"},
   128  			},
   129  			KindsToRegister: []schema.GroupVersionKind{
   130  				{Group: "first-group", Version: "first-version", Kind: "my-kind"},
   131  				{Group: "second-group", Version: "first-version", Kind: "my-kind"},
   132  			},
   133  			PartialResourceToRequest: schema.GroupVersionResource{Group: "first", Resource: "my-kind"},
   134  
   135  			ExpectedKinds: []schema.GroupVersionKind{
   136  				{Group: "first-group", Version: "first-version", Kind: "my-kind"},
   137  			},
   138  		},
   139  
   140  		{
   141  			// group prefixes can be ambiguous
   142  			Name: "groups, with ambiguous group prefix",
   143  			PreferredOrder: []schema.GroupVersion{
   144  				{Group: "first-group-1", Version: "first-version"},
   145  				{Group: "first-group", Version: "first-version"},
   146  			},
   147  			KindsToRegister: []schema.GroupVersionKind{
   148  				{Group: "first-group-1", Version: "first-version", Kind: "my-kind"},
   149  				{Group: "first-group", Version: "first-version", Kind: "my-kind"},
   150  			},
   151  			PartialResourceToRequest: schema.GroupVersionResource{Group: "first", Resource: "my-kind"},
   152  
   153  			ExpectedKinds: []schema.GroupVersionKind{
   154  				{Group: "first-group-1", Version: "first-version", Kind: "my-kind"},
   155  				{Group: "first-group", Version: "first-version", Kind: "my-kind"},
   156  			},
   157  			ExpectedKindErr: " matches multiple kinds ",
   158  		},
   159  
   160  		{
   161  			Name: "ambiguous groups, with preference order",
   162  			PreferredOrder: []schema.GroupVersion{
   163  				{Group: "second-group", Version: "first-version"},
   164  				{Group: "first-group", Version: "first-version"},
   165  			},
   166  			KindsToRegister: []schema.GroupVersionKind{
   167  				{Group: "first-group", Version: "first-version", Kind: "my-kind"},
   168  				{Group: "first-group", Version: "first-version", Kind: "your-kind"},
   169  				{Group: "second-group", Version: "first-version", Kind: "my-kind"},
   170  				{Group: "second-group", Version: "first-version", Kind: "your-kind"},
   171  			},
   172  			PartialResourceToRequest: schema.GroupVersionResource{Resource: "my-kinds"},
   173  
   174  			ExpectedKinds: []schema.GroupVersionKind{
   175  				{Group: "second-group", Version: "first-version", Kind: "my-kind"},
   176  				{Group: "first-group", Version: "first-version", Kind: "my-kind"},
   177  			},
   178  			ExpectedKindErr: " matches multiple kinds ",
   179  		},
   180  
   181  		{
   182  			Name: "ambiguous groups, with explicit group match",
   183  			PreferredOrder: []schema.GroupVersion{
   184  				{Group: "second-group", Version: "first-version"},
   185  				{Group: "first-group", Version: "first-version"},
   186  			},
   187  			KindsToRegister: []schema.GroupVersionKind{
   188  				{Group: "first-group", Version: "first-version", Kind: "my-kind"},
   189  				{Group: "first-group", Version: "first-version", Kind: "your-kind"},
   190  				{Group: "second-group", Version: "first-version", Kind: "my-kind"},
   191  				{Group: "second-group", Version: "first-version", Kind: "your-kind"},
   192  			},
   193  			PartialResourceToRequest: schema.GroupVersionResource{Group: "first-group", Resource: "my-kinds"},
   194  
   195  			ExpectedKinds: []schema.GroupVersionKind{
   196  				{Group: "first-group", Version: "first-version", Kind: "my-kind"},
   197  			},
   198  		},
   199  
   200  		{
   201  			Name: "ambiguous groups, with ambiguous version match",
   202  			PreferredOrder: []schema.GroupVersion{
   203  				{Group: "first-group", Version: "first-version"},
   204  				{Group: "second-group", Version: "first-version"},
   205  			},
   206  			KindsToRegister: []schema.GroupVersionKind{
   207  				{Group: "first-group", Version: "first-version", Kind: "my-kind"},
   208  				{Group: "first-group", Version: "first-version", Kind: "your-kind"},
   209  				{Group: "second-group", Version: "first-version", Kind: "my-kind"},
   210  				{Group: "second-group", Version: "first-version", Kind: "your-kind"},
   211  			},
   212  			PartialResourceToRequest: schema.GroupVersionResource{Version: "first-version", Resource: "my-kinds"},
   213  
   214  			ExpectedKinds: []schema.GroupVersionKind{
   215  				{Group: "first-group", Version: "first-version", Kind: "my-kind"},
   216  				{Group: "second-group", Version: "first-version", Kind: "my-kind"},
   217  			},
   218  			ExpectedKindErr: " matches multiple kinds ",
   219  		},
   220  	}
   221  	for _, testCase := range testCases {
   222  		tcName := testCase.Name
   223  		mapper := NewDefaultRESTMapper(testCase.PreferredOrder)
   224  		for _, kind := range testCase.KindsToRegister {
   225  			mapper.Add(kind, RESTScopeNamespace)
   226  		}
   227  
   228  		actualKinds, err := mapper.KindsFor(testCase.PartialResourceToRequest)
   229  		if err != nil {
   230  			t.Errorf("%s: unexpected error: %v", tcName, err)
   231  			continue
   232  		}
   233  		if !reflect.DeepEqual(testCase.ExpectedKinds, actualKinds) {
   234  			t.Errorf("%s: expected %v, got %v", tcName, testCase.ExpectedKinds, actualKinds)
   235  		}
   236  
   237  		singleKind, err := mapper.KindFor(testCase.PartialResourceToRequest)
   238  		if err == nil && len(testCase.ExpectedKindErr) != 0 {
   239  			t.Errorf("%s: expected error: %v", tcName, testCase.ExpectedKindErr)
   240  			continue
   241  		}
   242  		if err != nil {
   243  			if len(testCase.ExpectedKindErr) == 0 {
   244  				t.Errorf("%s: unexpected error: %v", tcName, err)
   245  				continue
   246  			} else {
   247  				if !strings.Contains(err.Error(), testCase.ExpectedKindErr) {
   248  					t.Errorf("%s: expected %v, got %v", tcName, testCase.ExpectedKindErr, err)
   249  					continue
   250  				}
   251  			}
   252  
   253  		} else {
   254  			if testCase.ExpectedKinds[0] != singleKind {
   255  				t.Errorf("%s: expected %v, got %v", tcName, testCase.ExpectedKinds[0], singleKind)
   256  			}
   257  
   258  		}
   259  	}
   260  }
   261  
   262  func TestRESTMapperResourcesFor(t *testing.T) {
   263  	testCases := []struct {
   264  		Name                             string
   265  		PreferredOrder                   []schema.GroupVersion
   266  		KindsToRegister                  []schema.GroupVersionKind
   267  		PluralPartialResourceToRequest   schema.GroupVersionResource
   268  		SingularPartialResourceToRequest schema.GroupVersionResource
   269  
   270  		ExpectedResources   []schema.GroupVersionResource
   271  		ExpectedResourceErr string
   272  	}{
   273  		{
   274  			// exact matches are preferred
   275  			Name: "groups, with group exact",
   276  			PreferredOrder: []schema.GroupVersion{
   277  				{Group: "first-group-1", Version: "first-version"},
   278  				{Group: "first-group", Version: "first-version"},
   279  			},
   280  			KindsToRegister: []schema.GroupVersionKind{
   281  				{Group: "first-group-1", Version: "first-version", Kind: "my-kind"},
   282  				{Group: "first-group", Version: "first-version", Kind: "my-kind"},
   283  			},
   284  			PluralPartialResourceToRequest:   schema.GroupVersionResource{Group: "first-group", Resource: "my-kinds"},
   285  			SingularPartialResourceToRequest: schema.GroupVersionResource{Group: "first-group", Resource: "my-kind"},
   286  
   287  			ExpectedResources: []schema.GroupVersionResource{
   288  				{Group: "first-group", Version: "first-version", Resource: "my-kinds"},
   289  			},
   290  		},
   291  
   292  		{
   293  			// group prefixes work
   294  			Name: "groups, with group prefix",
   295  			PreferredOrder: []schema.GroupVersion{
   296  				{Group: "second-group", Version: "first-version"},
   297  				{Group: "first-group", Version: "first-version"},
   298  			},
   299  			KindsToRegister: []schema.GroupVersionKind{
   300  				{Group: "first-group", Version: "first-version", Kind: "my-kind"},
   301  				{Group: "second-group", Version: "first-version", Kind: "my-kind"},
   302  			},
   303  			PluralPartialResourceToRequest:   schema.GroupVersionResource{Group: "first", Resource: "my-kinds"},
   304  			SingularPartialResourceToRequest: schema.GroupVersionResource{Group: "first", Resource: "my-kind"},
   305  
   306  			ExpectedResources: []schema.GroupVersionResource{
   307  				{Group: "first-group", Version: "first-version", Resource: "my-kinds"},
   308  			},
   309  		},
   310  
   311  		{
   312  			// group prefixes can be ambiguous
   313  			Name: "groups, with ambiguous group prefix",
   314  			PreferredOrder: []schema.GroupVersion{
   315  				{Group: "first-group-1", Version: "first-version"},
   316  				{Group: "first-group", Version: "first-version"},
   317  			},
   318  			KindsToRegister: []schema.GroupVersionKind{
   319  				{Group: "first-group-1", Version: "first-version", Kind: "my-kind"},
   320  				{Group: "first-group", Version: "first-version", Kind: "my-kind"},
   321  			},
   322  			PluralPartialResourceToRequest:   schema.GroupVersionResource{Group: "first", Resource: "my-kinds"},
   323  			SingularPartialResourceToRequest: schema.GroupVersionResource{Group: "first", Resource: "my-kind"},
   324  
   325  			ExpectedResources: []schema.GroupVersionResource{
   326  				{Group: "first-group-1", Version: "first-version", Resource: "my-kinds"},
   327  				{Group: "first-group", Version: "first-version", Resource: "my-kinds"},
   328  			},
   329  			ExpectedResourceErr: " matches multiple resources ",
   330  		},
   331  
   332  		{
   333  			Name: "ambiguous groups, with preference order",
   334  			PreferredOrder: []schema.GroupVersion{
   335  				{Group: "second-group", Version: "first-version"},
   336  				{Group: "first-group", Version: "first-version"},
   337  			},
   338  			KindsToRegister: []schema.GroupVersionKind{
   339  				{Group: "first-group", Version: "first-version", Kind: "my-kind"},
   340  				{Group: "first-group", Version: "first-version", Kind: "your-kind"},
   341  				{Group: "second-group", Version: "first-version", Kind: "my-kind"},
   342  				{Group: "second-group", Version: "first-version", Kind: "your-kind"},
   343  			},
   344  			PluralPartialResourceToRequest:   schema.GroupVersionResource{Resource: "my-kinds"},
   345  			SingularPartialResourceToRequest: schema.GroupVersionResource{Resource: "my-kind"},
   346  
   347  			ExpectedResources: []schema.GroupVersionResource{
   348  				{Group: "second-group", Version: "first-version", Resource: "my-kinds"},
   349  				{Group: "first-group", Version: "first-version", Resource: "my-kinds"},
   350  			},
   351  			ExpectedResourceErr: " matches multiple resources ",
   352  		},
   353  
   354  		{
   355  			Name: "ambiguous groups, with explicit group match",
   356  			PreferredOrder: []schema.GroupVersion{
   357  				{Group: "second-group", Version: "first-version"},
   358  				{Group: "first-group", Version: "first-version"},
   359  			},
   360  			KindsToRegister: []schema.GroupVersionKind{
   361  				{Group: "first-group", Version: "first-version", Kind: "my-kind"},
   362  				{Group: "first-group", Version: "first-version", Kind: "your-kind"},
   363  				{Group: "second-group", Version: "first-version", Kind: "my-kind"},
   364  				{Group: "second-group", Version: "first-version", Kind: "your-kind"},
   365  			},
   366  			PluralPartialResourceToRequest:   schema.GroupVersionResource{Group: "first-group", Resource: "my-kinds"},
   367  			SingularPartialResourceToRequest: schema.GroupVersionResource{Group: "first-group", Resource: "my-kind"},
   368  
   369  			ExpectedResources: []schema.GroupVersionResource{
   370  				{Group: "first-group", Version: "first-version", Resource: "my-kinds"},
   371  			},
   372  		},
   373  
   374  		{
   375  			Name: "ambiguous groups, with ambiguous version match",
   376  			PreferredOrder: []schema.GroupVersion{
   377  				{Group: "first-group", Version: "first-version"},
   378  				{Group: "second-group", Version: "first-version"},
   379  			},
   380  			KindsToRegister: []schema.GroupVersionKind{
   381  				{Group: "first-group", Version: "first-version", Kind: "my-kind"},
   382  				{Group: "first-group", Version: "first-version", Kind: "your-kind"},
   383  				{Group: "second-group", Version: "first-version", Kind: "my-kind"},
   384  				{Group: "second-group", Version: "first-version", Kind: "your-kind"},
   385  			},
   386  			PluralPartialResourceToRequest:   schema.GroupVersionResource{Version: "first-version", Resource: "my-kinds"},
   387  			SingularPartialResourceToRequest: schema.GroupVersionResource{Version: "first-version", Resource: "my-kind"},
   388  
   389  			ExpectedResources: []schema.GroupVersionResource{
   390  				{Group: "first-group", Version: "first-version", Resource: "my-kinds"},
   391  				{Group: "second-group", Version: "first-version", Resource: "my-kinds"},
   392  			},
   393  			ExpectedResourceErr: " matches multiple resources ",
   394  		},
   395  	}
   396  	for _, testCase := range testCases {
   397  		tcName := testCase.Name
   398  
   399  		for _, partialResource := range []schema.GroupVersionResource{testCase.PluralPartialResourceToRequest, testCase.SingularPartialResourceToRequest} {
   400  			mapper := NewDefaultRESTMapper(testCase.PreferredOrder)
   401  			for _, kind := range testCase.KindsToRegister {
   402  				mapper.Add(kind, RESTScopeNamespace)
   403  			}
   404  
   405  			actualResources, err := mapper.ResourcesFor(partialResource)
   406  			if err != nil {
   407  				t.Errorf("%s: unexpected error: %v", tcName, err)
   408  				continue
   409  			}
   410  			if !reflect.DeepEqual(testCase.ExpectedResources, actualResources) {
   411  				t.Errorf("%s: expected %v, got %v", tcName, testCase.ExpectedResources, actualResources)
   412  			}
   413  
   414  			singleResource, err := mapper.ResourceFor(partialResource)
   415  			if err == nil && len(testCase.ExpectedResourceErr) != 0 {
   416  				t.Errorf("%s: expected error: %v", tcName, testCase.ExpectedResourceErr)
   417  				continue
   418  			}
   419  			if err != nil {
   420  				if len(testCase.ExpectedResourceErr) == 0 {
   421  					t.Errorf("%s: unexpected error: %v", tcName, err)
   422  					continue
   423  				} else {
   424  					if !strings.Contains(err.Error(), testCase.ExpectedResourceErr) {
   425  						t.Errorf("%s: expected %v, got %v", tcName, testCase.ExpectedResourceErr, err)
   426  						continue
   427  					}
   428  				}
   429  
   430  			} else {
   431  				if testCase.ExpectedResources[0] != singleResource {
   432  					t.Errorf("%s: expected %v, got %v", tcName, testCase.ExpectedResources[0], singleResource)
   433  				}
   434  
   435  			}
   436  		}
   437  	}
   438  }
   439  
   440  func TestKindToResource(t *testing.T) {
   441  	testCases := []struct {
   442  		Kind             string
   443  		Plural, Singular string
   444  	}{
   445  		{Kind: "Pod", Plural: "pods", Singular: "pod"},
   446  
   447  		{Kind: "ReplicationController", Plural: "replicationcontrollers", Singular: "replicationcontroller"},
   448  
   449  		// Add "ies" when ending with "y"
   450  		{Kind: "ImageRepository", Plural: "imagerepositories", Singular: "imagerepository"},
   451  		// Add "es" when ending with "s"
   452  		{Kind: "miss", Plural: "misses", Singular: "miss"},
   453  		// Add "s" otherwise
   454  		{Kind: "lowercase", Plural: "lowercases", Singular: "lowercase"},
   455  	}
   456  	for i, testCase := range testCases {
   457  		version := schema.GroupVersion{}
   458  
   459  		plural, singular := UnsafeGuessKindToResource(version.WithKind(testCase.Kind))
   460  		if singular != version.WithResource(testCase.Singular) || plural != version.WithResource(testCase.Plural) {
   461  			t.Errorf("%d: unexpected plural and singular: %v %v", i, plural, singular)
   462  		}
   463  	}
   464  }
   465  
   466  func TestRESTMapperResourceSingularizer(t *testing.T) {
   467  	testGroupVersion := schema.GroupVersion{Group: "tgroup", Version: "test"}
   468  
   469  	testCases := []struct {
   470  		Kind     string
   471  		Plural   string
   472  		Singular string
   473  	}{
   474  		{Kind: "Pod", Plural: "pods", Singular: "pod"},
   475  		{Kind: "ReplicationController", Plural: "replicationcontrollers", Singular: "replicationcontroller"},
   476  		{Kind: "ImageRepository", Plural: "imagerepositories", Singular: "imagerepository"},
   477  		{Kind: "Status", Plural: "statuses", Singular: "status"},
   478  
   479  		{Kind: "lowercase", Plural: "lowercases", Singular: "lowercase"},
   480  		// TODO this test is broken.  This updates to reflect actual behavior.  Kinds are expected to be singular
   481  		// old (incorrect), comment: Don't add extra s if the original object is already plural
   482  		{Kind: "lowercases", Plural: "lowercaseses", Singular: "lowercases"},
   483  	}
   484  	for i, testCase := range testCases {
   485  		mapper := NewDefaultRESTMapper([]schema.GroupVersion{testGroupVersion})
   486  		// create singular/plural mapping
   487  		mapper.Add(testGroupVersion.WithKind(testCase.Kind), RESTScopeNamespace)
   488  
   489  		singular, err := mapper.ResourceSingularizer(testCase.Plural)
   490  		if err != nil {
   491  			t.Errorf("%d: unexpected error: %v", i, err)
   492  		}
   493  		if singular != testCase.Singular {
   494  			t.Errorf("%d: mismatched singular: got %v, expected %v", i, singular, testCase.Singular)
   495  		}
   496  	}
   497  }
   498  
   499  func TestRESTMapperRESTMapping(t *testing.T) {
   500  	testGroup := "tgroup"
   501  	testGroupVersion := schema.GroupVersion{Group: testGroup, Version: "test"}
   502  	internalGroupVersion := schema.GroupVersion{Group: testGroup, Version: "test"}
   503  
   504  	testCases := []struct {
   505  		Kind             string
   506  		APIGroupVersions []schema.GroupVersion
   507  		DefaultVersions  []schema.GroupVersion
   508  
   509  		Resource             schema.GroupVersionResource
   510  		ExpectedGroupVersion *schema.GroupVersion
   511  		Err                  bool
   512  	}{
   513  		{Kind: "Unknown", Err: true},
   514  		{Kind: "InternalObject", Err: true},
   515  
   516  		{DefaultVersions: []schema.GroupVersion{testGroupVersion}, Kind: "Unknown", Err: true},
   517  
   518  		{DefaultVersions: []schema.GroupVersion{testGroupVersion}, Kind: "InternalObject", APIGroupVersions: []schema.GroupVersion{{Group: testGroup, Version: "test"}}, Resource: testGroupVersion.WithResource("internalobjects")},
   519  		{DefaultVersions: []schema.GroupVersion{testGroupVersion}, Kind: "InternalObject", APIGroupVersions: []schema.GroupVersion{{Group: testGroup, Version: "test"}}, Resource: testGroupVersion.WithResource("internalobjects")},
   520  
   521  		{DefaultVersions: []schema.GroupVersion{testGroupVersion}, Kind: "InternalObject", APIGroupVersions: []schema.GroupVersion{{Group: testGroup, Version: "test"}}, Resource: testGroupVersion.WithResource("internalobjects")},
   522  
   523  		{DefaultVersions: []schema.GroupVersion{testGroupVersion}, Kind: "InternalObject", APIGroupVersions: []schema.GroupVersion{}, Resource: internalGroupVersion.WithResource("internalobjects"), ExpectedGroupVersion: &schema.GroupVersion{Group: testGroup, Version: "test"}},
   524  
   525  		{DefaultVersions: []schema.GroupVersion{testGroupVersion}, Kind: "InternalObject", APIGroupVersions: []schema.GroupVersion{{Group: testGroup, Version: "test"}}, Resource: testGroupVersion.WithResource("internalobjects")},
   526  
   527  		// TODO: add test for a resource that exists in one version but not another
   528  	}
   529  	for i, testCase := range testCases {
   530  		mapper := NewDefaultRESTMapper(testCase.DefaultVersions)
   531  		mapper.Add(internalGroupVersion.WithKind("InternalObject"), RESTScopeNamespace)
   532  
   533  		preferredVersions := []string{}
   534  		for _, gv := range testCase.APIGroupVersions {
   535  			preferredVersions = append(preferredVersions, gv.Version)
   536  		}
   537  		gk := schema.GroupKind{Group: testGroup, Kind: testCase.Kind}
   538  
   539  		mapping, err := mapper.RESTMapping(gk, preferredVersions...)
   540  		hasErr := err != nil
   541  		if hasErr != testCase.Err {
   542  			t.Errorf("%d: unexpected error behavior %t: %v", i, testCase.Err, err)
   543  		}
   544  		if hasErr {
   545  			continue
   546  		}
   547  		if mapping.Resource != testCase.Resource {
   548  			t.Errorf("%d: unexpected resource: %#v", i, mapping)
   549  		}
   550  
   551  		groupVersion := testCase.ExpectedGroupVersion
   552  		if groupVersion == nil {
   553  			groupVersion = &testCase.APIGroupVersions[0]
   554  		}
   555  		if mapping.GroupVersionKind.GroupVersion() != *groupVersion {
   556  			t.Errorf("%d: unexpected version: %#v", i, mapping)
   557  		}
   558  
   559  	}
   560  }
   561  
   562  func TestRESTMapperRESTMappingSelectsVersion(t *testing.T) {
   563  	expectedGroupVersion1 := schema.GroupVersion{Group: "tgroup", Version: "test1"}
   564  	expectedGroupVersion2 := schema.GroupVersion{Group: "tgroup", Version: "test2"}
   565  	expectedGroupVersion3 := schema.GroupVersion{Group: "tgroup", Version: "test3"}
   566  	internalObjectGK := schema.GroupKind{Group: "tgroup", Kind: "InternalObject"}
   567  	otherObjectGK := schema.GroupKind{Group: "tgroup", Kind: "OtherObject"}
   568  
   569  	mapper := NewDefaultRESTMapper([]schema.GroupVersion{expectedGroupVersion1, expectedGroupVersion2})
   570  	mapper.Add(expectedGroupVersion1.WithKind("InternalObject"), RESTScopeNamespace)
   571  	mapper.Add(expectedGroupVersion2.WithKind("OtherObject"), RESTScopeNamespace)
   572  
   573  	// pick default matching object kind based on search order
   574  	mapping, err := mapper.RESTMapping(otherObjectGK)
   575  	if err != nil {
   576  		t.Fatalf("unexpected error: %v", err)
   577  	}
   578  	if mapping.Resource != expectedGroupVersion2.WithResource("otherobjects") || mapping.GroupVersionKind.GroupVersion() != expectedGroupVersion2 {
   579  		t.Errorf("unexpected mapping: %#v", mapping)
   580  	}
   581  
   582  	mapping, err = mapper.RESTMapping(internalObjectGK)
   583  	if err != nil {
   584  		t.Fatalf("unexpected error: %v", err)
   585  	}
   586  	if mapping.Resource != expectedGroupVersion1.WithResource("internalobjects") || mapping.GroupVersionKind.GroupVersion() != expectedGroupVersion1 {
   587  		t.Errorf("unexpected mapping: %#v", mapping)
   588  	}
   589  
   590  	// mismatch of version
   591  	_, err = mapper.RESTMapping(internalObjectGK, expectedGroupVersion2.Version)
   592  	if err == nil {
   593  		t.Errorf("unexpected non-error")
   594  	}
   595  	_, err = mapper.RESTMapping(otherObjectGK, expectedGroupVersion1.Version)
   596  	if err == nil {
   597  		t.Errorf("unexpected non-error")
   598  	}
   599  
   600  	// not in the search versions
   601  	_, err = mapper.RESTMapping(otherObjectGK, expectedGroupVersion3.Version)
   602  	if err == nil {
   603  		t.Errorf("unexpected non-error")
   604  	}
   605  
   606  	// explicit search order
   607  	_, err = mapper.RESTMapping(otherObjectGK, expectedGroupVersion3.Version, expectedGroupVersion1.Version)
   608  	if err == nil {
   609  		t.Errorf("unexpected non-error")
   610  	}
   611  
   612  	mapping, err = mapper.RESTMapping(otherObjectGK, expectedGroupVersion3.Version, expectedGroupVersion2.Version)
   613  	if err != nil {
   614  		t.Fatalf("unexpected error: %v", err)
   615  	}
   616  	if mapping.Resource != expectedGroupVersion2.WithResource("otherobjects") || mapping.GroupVersionKind.GroupVersion() != expectedGroupVersion2 {
   617  		t.Errorf("unexpected mapping: %#v", mapping)
   618  	}
   619  }
   620  
   621  func TestRESTMapperRESTMappings(t *testing.T) {
   622  	testGroup := "tgroup"
   623  	testGroupVersion := schema.GroupVersion{Group: testGroup, Version: "v1"}
   624  
   625  	testCases := []struct {
   626  		Kind                string
   627  		APIGroupVersions    []schema.GroupVersion
   628  		DefaultVersions     []schema.GroupVersion
   629  		AddGroupVersionKind []schema.GroupVersionKind
   630  
   631  		ExpectedRESTMappings []*RESTMapping
   632  		Err                  bool
   633  	}{
   634  		{Kind: "Unknown", Err: true},
   635  		{Kind: "InternalObject", Err: true},
   636  
   637  		{DefaultVersions: []schema.GroupVersion{testGroupVersion}, Kind: "Unknown", Err: true},
   638  
   639  		// ask for specific version - not available - thus error
   640  		{DefaultVersions: []schema.GroupVersion{testGroupVersion}, Kind: "InternalObject", APIGroupVersions: []schema.GroupVersion{{Group: testGroup, Version: "v2"}}, Err: true},
   641  
   642  		// ask for specific version - available - check ExpectedRESTMappings
   643  		{
   644  			DefaultVersions:      []schema.GroupVersion{testGroupVersion},
   645  			Kind:                 "InternalObject",
   646  			APIGroupVersions:     []schema.GroupVersion{{Group: testGroup, Version: "v2"}},
   647  			AddGroupVersionKind:  []schema.GroupVersionKind{schema.GroupVersion{Group: testGroup, Version: "v2"}.WithKind("InternalObject")},
   648  			ExpectedRESTMappings: []*RESTMapping{{Resource: schema.GroupVersionResource{Group: testGroup, Version: "v2", Resource: "internalobjects"}, GroupVersionKind: schema.GroupVersionKind{Group: testGroup, Version: "v2", Kind: "InternalObject"}}},
   649  		},
   650  
   651  		// ask for specific versions - only one available - check ExpectedRESTMappings
   652  		{
   653  			DefaultVersions:      []schema.GroupVersion{testGroupVersion},
   654  			Kind:                 "InternalObject",
   655  			APIGroupVersions:     []schema.GroupVersion{{Group: testGroup, Version: "v3"}, {Group: testGroup, Version: "v2"}},
   656  			AddGroupVersionKind:  []schema.GroupVersionKind{schema.GroupVersion{Group: testGroup, Version: "v2"}.WithKind("InternalObject")},
   657  			ExpectedRESTMappings: []*RESTMapping{{Resource: schema.GroupVersionResource{Group: testGroup, Version: "v2", Resource: "internalobjects"}, GroupVersionKind: schema.GroupVersionKind{Group: testGroup, Version: "v2", Kind: "InternalObject"}}},
   658  		},
   659  
   660  		// do not ask for specific version - search through default versions - check ExpectedRESTMappings
   661  		{
   662  			DefaultVersions:     []schema.GroupVersion{testGroupVersion, {Group: testGroup, Version: "v2"}},
   663  			Kind:                "InternalObject",
   664  			AddGroupVersionKind: []schema.GroupVersionKind{schema.GroupVersion{Group: testGroup, Version: "v1"}.WithKind("InternalObject"), schema.GroupVersion{Group: testGroup, Version: "v2"}.WithKind("InternalObject")},
   665  			ExpectedRESTMappings: []*RESTMapping{
   666  				{
   667  					Resource:         schema.GroupVersionResource{Group: testGroup, Version: "v1", Resource: "internalobjects"},
   668  					GroupVersionKind: schema.GroupVersionKind{Group: testGroup, Version: "v1", Kind: "InternalObject"},
   669  				},
   670  				{
   671  					Resource:         schema.GroupVersionResource{Group: testGroup, Version: "v2", Resource: "internalobjects"},
   672  					GroupVersionKind: schema.GroupVersionKind{Group: testGroup, Version: "v2", Kind: "InternalObject"},
   673  				},
   674  			},
   675  		},
   676  	}
   677  
   678  	for i, testCase := range testCases {
   679  		mapper := NewDefaultRESTMapper(testCase.DefaultVersions)
   680  		for _, gvk := range testCase.AddGroupVersionKind {
   681  			mapper.Add(gvk, RESTScopeNamespace)
   682  		}
   683  
   684  		preferredVersions := []string{}
   685  		for _, gv := range testCase.APIGroupVersions {
   686  			preferredVersions = append(preferredVersions, gv.Version)
   687  		}
   688  		gk := schema.GroupKind{Group: testGroup, Kind: testCase.Kind}
   689  
   690  		mappings, err := mapper.RESTMappings(gk, preferredVersions...)
   691  		hasErr := err != nil
   692  		if hasErr != testCase.Err {
   693  			t.Errorf("%d: unexpected error behavior %t: %v", i, testCase.Err, err)
   694  		}
   695  		if hasErr {
   696  			continue
   697  		}
   698  		if len(mappings) != len(testCase.ExpectedRESTMappings) {
   699  			t.Errorf("%d: unexpected number = %d of rest mappings was returned, expected = %d", i, len(mappings), len(testCase.ExpectedRESTMappings))
   700  		}
   701  		for j, mapping := range mappings {
   702  			exp := testCase.ExpectedRESTMappings[j]
   703  			if mapping.Resource != exp.Resource {
   704  				t.Errorf("%d - %d: unexpected resource: %#v", i, j, mapping)
   705  			}
   706  			if mapping.GroupVersionKind != exp.GroupVersionKind {
   707  				t.Errorf("%d - %d: unexpected GroupVersionKind: %#v", i, j, mapping)
   708  			}
   709  		}
   710  	}
   711  }
   712  
   713  func TestRESTMapperReportsErrorOnBadVersion(t *testing.T) {
   714  	expectedGroupVersion1 := schema.GroupVersion{Group: "tgroup", Version: "test1"}
   715  	expectedGroupVersion2 := schema.GroupVersion{Group: "tgroup", Version: "test2"}
   716  	internalObjectGK := schema.GroupKind{Group: "tgroup", Kind: "InternalObject"}
   717  
   718  	mapper := NewDefaultRESTMapper([]schema.GroupVersion{expectedGroupVersion1, expectedGroupVersion2})
   719  	mapper.Add(expectedGroupVersion1.WithKind("InternalObject"), RESTScopeNamespace)
   720  	_, err := mapper.RESTMapping(internalObjectGK, "test3")
   721  	if err == nil {
   722  		t.Errorf("unexpected non-error")
   723  	}
   724  }