k8s.io/client-go@v0.31.1/tools/reference/ref_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 reference 18 19 import ( 20 "testing" 21 22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 23 "k8s.io/apimachinery/pkg/runtime" 24 "k8s.io/apimachinery/pkg/runtime/schema" 25 ) 26 27 type TestRuntimeObj struct { 28 metav1.TypeMeta 29 metav1.ObjectMeta 30 } 31 32 func (o *TestRuntimeObj) DeepCopyObject() runtime.Object { 33 panic("die") 34 } 35 36 func TestGetReferenceRefVersion(t *testing.T) { 37 tests := []struct { 38 name string 39 input *TestRuntimeObj 40 groupVersion schema.GroupVersion 41 expectedRefVersion string 42 }{ 43 { 44 name: "v1 GV from scheme", 45 input: &TestRuntimeObj{ 46 ObjectMeta: metav1.ObjectMeta{}, 47 }, 48 groupVersion: schema.GroupVersion{Group: "", Version: "v1"}, 49 expectedRefVersion: "v1", 50 }, 51 { 52 name: "foo.group/v3 GV from scheme", 53 input: &TestRuntimeObj{ 54 ObjectMeta: metav1.ObjectMeta{}, 55 }, 56 groupVersion: schema.GroupVersion{Group: "foo.group", Version: "v3"}, 57 expectedRefVersion: "foo.group/v3", 58 }, 59 } 60 61 for _, test := range tests { 62 t.Run(test.name, func(t *testing.T) { 63 scheme := runtime.NewScheme() 64 scheme.AddKnownTypes(test.groupVersion, &TestRuntimeObj{}) 65 ref, err := GetReference(scheme, test.input) 66 if err != nil { 67 t.Fatal(err) 68 } 69 if test.expectedRefVersion != ref.APIVersion { 70 t.Errorf("expected %q, got %q", test.expectedRefVersion, ref.APIVersion) 71 } 72 }) 73 } 74 }