github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/runtime/local_scheme_test.go (about) 1 /* 2 Copyright 2018 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 runtime 18 19 import ( 20 "testing" 21 22 "reflect" 23 24 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime/schema" 25 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/util/diff" 26 ) 27 28 func TestPreferredVersionsAllGroups(t *testing.T) { 29 tests := []struct { 30 name string 31 versionPriority map[string][]string 32 observedVersions []schema.GroupVersion 33 expectedPrioritized map[string][]schema.GroupVersion 34 expectedPreferred map[schema.GroupVersion]bool 35 }{ 36 { 37 name: "observedOnly", 38 observedVersions: []schema.GroupVersion{ 39 {Group: "", Version: "v3"}, 40 {Group: "foo", Version: "v1"}, 41 {Group: "foo", Version: "v2"}, 42 {Group: "", Version: "v1"}, 43 }, 44 expectedPrioritized: map[string][]schema.GroupVersion{ 45 "": { 46 {Group: "", Version: "v3"}, 47 {Group: "", Version: "v1"}, 48 }, 49 "foo": { 50 {Group: "foo", Version: "v1"}, 51 {Group: "foo", Version: "v2"}, 52 }, 53 }, 54 expectedPreferred: map[schema.GroupVersion]bool{ 55 {Group: "", Version: "v3"}: true, 56 {Group: "foo", Version: "v1"}: true, 57 }, 58 }, 59 { 60 name: "specifiedOnly", 61 versionPriority: map[string][]string{ 62 "": {"v3", "v1"}, 63 "foo": {"v1", "v2"}, 64 }, 65 expectedPrioritized: map[string][]schema.GroupVersion{ 66 "": { 67 {Group: "", Version: "v3"}, 68 {Group: "", Version: "v1"}, 69 }, 70 "foo": { 71 {Group: "foo", Version: "v1"}, 72 {Group: "foo", Version: "v2"}, 73 }, 74 }, 75 expectedPreferred: map[schema.GroupVersion]bool{ 76 {Group: "", Version: "v3"}: true, 77 {Group: "foo", Version: "v1"}: true, 78 }, 79 }, 80 { 81 name: "both", 82 versionPriority: map[string][]string{ 83 "": {"v3", "v1"}, 84 "foo": {"v1", "v2"}, 85 }, 86 observedVersions: []schema.GroupVersion{ 87 {Group: "", Version: "v1"}, 88 {Group: "", Version: "v3"}, 89 {Group: "", Version: "v4"}, 90 {Group: "", Version: "v5"}, 91 {Group: "bar", Version: "v1"}, 92 {Group: "bar", Version: "v2"}, 93 }, 94 expectedPrioritized: map[string][]schema.GroupVersion{ 95 "": { 96 {Group: "", Version: "v3"}, 97 {Group: "", Version: "v1"}, 98 {Group: "", Version: "v4"}, 99 {Group: "", Version: "v5"}, 100 }, 101 "foo": { 102 {Group: "foo", Version: "v1"}, 103 {Group: "foo", Version: "v2"}, 104 }, 105 "bar": { 106 {Group: "bar", Version: "v1"}, 107 {Group: "bar", Version: "v2"}, 108 }, 109 }, 110 expectedPreferred: map[schema.GroupVersion]bool{ 111 {Group: "", Version: "v3"}: true, 112 {Group: "foo", Version: "v1"}: true, 113 {Group: "bar", Version: "v1"}: true, 114 }, 115 }, 116 } 117 118 for _, test := range tests { 119 t.Run(test.name, func(t *testing.T) { 120 scheme := NewScheme() 121 scheme.versionPriority = test.versionPriority 122 scheme.observedVersions = test.observedVersions 123 124 for group, expected := range test.expectedPrioritized { 125 actual := scheme.PrioritizedVersionsForGroup(group) 126 if !reflect.DeepEqual(expected, actual) { 127 t.Error(diff.ObjectDiff(expected, actual)) 128 } 129 } 130 131 prioritizedAll := scheme.PrioritizedVersionsAllGroups() 132 actualPrioritizedAll := map[string][]schema.GroupVersion{} 133 for _, actual := range prioritizedAll { 134 actualPrioritizedAll[actual.Group] = append(actualPrioritizedAll[actual.Group], actual) 135 } 136 if !reflect.DeepEqual(test.expectedPrioritized, actualPrioritizedAll) { 137 t.Error(diff.ObjectDiff(test.expectedPrioritized, actualPrioritizedAll)) 138 } 139 140 preferredAll := scheme.PreferredVersionAllGroups() 141 actualPreferredAll := map[schema.GroupVersion]bool{} 142 for _, actual := range preferredAll { 143 actualPreferredAll[actual] = true 144 } 145 if !reflect.DeepEqual(test.expectedPreferred, actualPreferredAll) { 146 t.Error(diff.ObjectDiff(test.expectedPreferred, actualPreferredAll)) 147 } 148 }) 149 } 150 }