k8s.io/client-go@v0.31.1/restmapper/category_expansion_test.go (about) 1 /* 2 Copyright 2017 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 restmapper 18 19 import ( 20 "reflect" 21 "testing" 22 23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 24 "k8s.io/apimachinery/pkg/runtime/schema" 25 ) 26 27 func TestCategoryExpansion(t *testing.T) { 28 tests := []struct { 29 name string 30 arg string 31 32 expected []schema.GroupResource 33 expectedOk bool 34 }{ 35 { 36 name: "no-replacement", 37 arg: "service", 38 expected: nil, 39 }, 40 { 41 name: "all-replacement", 42 arg: "all", 43 expected: []schema.GroupResource{ 44 {Resource: "one"}, 45 {Resource: "two"}, 46 {Resource: "three", Group: "alpha"}, 47 {Resource: "four", Group: "bravo"}, 48 }, 49 expectedOk: true, 50 }, 51 } 52 53 for _, test := range tests { 54 simpleCategoryExpander := SimpleCategoryExpander{ 55 Expansions: map[string][]schema.GroupResource{ 56 "all": { 57 {Group: "", Resource: "one"}, 58 {Group: "", Resource: "two"}, 59 {Group: "alpha", Resource: "three"}, 60 {Group: "bravo", Resource: "four"}, 61 }, 62 }, 63 } 64 65 actual, actualOk := simpleCategoryExpander.Expand(test.arg) 66 if e, a := test.expected, actual; !reflect.DeepEqual(e, a) { 67 t.Errorf("%s: expected %s, got %s", test.name, e, a) 68 } 69 if e, a := test.expectedOk, actualOk; e != a { 70 t.Errorf("%s: expected %v, got %v", test.name, e, a) 71 } 72 } 73 } 74 75 func TestDiscoveryCategoryExpander(t *testing.T) { 76 tests := []struct { 77 category string 78 serverResponse []*metav1.APIResourceList 79 expected []schema.GroupResource 80 }{ 81 { 82 category: "all", 83 serverResponse: []*metav1.APIResourceList{ 84 { 85 GroupVersion: "batch/v1", 86 APIResources: []metav1.APIResource{ 87 { 88 Name: "jobs", 89 ShortNames: []string{"jz"}, 90 Categories: []string{"all"}, 91 }, 92 }, 93 }, 94 }, 95 expected: []schema.GroupResource{ 96 { 97 Group: "batch", 98 Resource: "jobs", 99 }, 100 }, 101 }, 102 { 103 category: "all", 104 serverResponse: []*metav1.APIResourceList{ 105 { 106 GroupVersion: "batch/v1", 107 APIResources: []metav1.APIResource{ 108 { 109 Name: "jobs", 110 ShortNames: []string{"jz"}, 111 }, 112 }, 113 }, 114 }, 115 }, 116 { 117 category: "targaryens", 118 serverResponse: []*metav1.APIResourceList{ 119 { 120 GroupVersion: "batch/v1", 121 APIResources: []metav1.APIResource{ 122 { 123 Name: "jobs", 124 ShortNames: []string{"jz"}, 125 Categories: []string{"all"}, 126 }, 127 }, 128 }, 129 }, 130 }, 131 } 132 133 dc := &fakeDiscoveryClient{} 134 for _, test := range tests { 135 dc.serverResourcesHandler = func() ([]*metav1.APIResourceList, error) { 136 return test.serverResponse, nil 137 } 138 expander := NewDiscoveryCategoryExpander(dc) 139 expanded, _ := expander.Expand(test.category) 140 if !reflect.DeepEqual(expanded, test.expected) { 141 t.Errorf("expected %v, got %v", test.expected, expanded) 142 } 143 } 144 145 }