knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/kmeta/ownerrefable_accessor_test.go (about) 1 /* 2 Copyright 2019 The Knative 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 kmeta 18 19 import ( 20 "testing" 21 22 "github.com/google/go-cmp/cmp" 23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 24 "k8s.io/apimachinery/pkg/runtime/schema" 25 . "knative.dev/pkg/testing" 26 ) 27 28 type GoodObject struct { 29 Resource 30 } 31 32 func (f *GoodObject) GetGroupVersionKind() schema.GroupVersionKind { 33 return schema.GroupVersionKind{ 34 Group: "example.knative.dev", 35 Version: "v1alpha1", 36 Kind: "GoodObject", 37 } 38 } 39 40 func TestOwnerRefableAccessor(t *testing.T) { 41 var _ OwnerRefableAccessor = (*GoodObject)(nil) 42 var _ OwnerRefable = (*GoodObject)(nil) 43 var _ Accessor = (*GoodObject)(nil) 44 } 45 46 func TestNewControllerRef_OwnerRefableAccessor(t *testing.T) { 47 goodObject := &GoodObject{ 48 Resource: Resource{ 49 TypeMeta: metav1.TypeMeta{}, 50 ObjectMeta: metav1.ObjectMeta{ 51 Name: "foo", 52 UID: "42", 53 }, 54 }, 55 } 56 57 blockOwnerDeletion := true 58 isController := true 59 want := &metav1.OwnerReference{ 60 APIVersion: "example.knative.dev/v1alpha1", 61 Kind: "GoodObject", 62 Name: "foo", 63 UID: "42", 64 BlockOwnerDeletion: &blockOwnerDeletion, 65 Controller: &isController, 66 } 67 68 got := NewControllerRef(goodObject) 69 if diff := cmp.Diff(want, got); diff != "" { 70 t.Error("Unexpected OwnerReference (-want +got):", diff) 71 } 72 }