k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/registry/core/event/strategy_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 event 18 19 import ( 20 "context" 21 "reflect" 22 "testing" 23 24 "github.com/google/go-cmp/cmp" 25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 26 "k8s.io/apimachinery/pkg/fields" 27 apitesting "k8s.io/kubernetes/pkg/api/testing" 28 api "k8s.io/kubernetes/pkg/apis/core" 29 30 // ensure types are installed 31 _ "k8s.io/kubernetes/pkg/apis/core/install" 32 ) 33 34 func TestGetAttrs(t *testing.T) { 35 eventA := &api.Event{ 36 ObjectMeta: metav1.ObjectMeta{ 37 Name: "f0118", 38 Namespace: "default", 39 }, 40 InvolvedObject: api.ObjectReference{ 41 Kind: "Pod", 42 Name: "foo", 43 Namespace: "baz", 44 UID: "long uid string", 45 APIVersion: "v1", 46 ResourceVersion: "0", 47 FieldPath: "", 48 }, 49 Reason: "ForTesting", 50 Source: api.EventSource{Component: "test"}, 51 Type: api.EventTypeNormal, 52 } 53 field := ToSelectableFields(eventA) 54 expectA := fields.Set{ 55 "metadata.name": "f0118", 56 "metadata.namespace": "default", 57 "involvedObject.kind": "Pod", 58 "involvedObject.name": "foo", 59 "involvedObject.namespace": "baz", 60 "involvedObject.uid": "long uid string", 61 "involvedObject.apiVersion": "v1", 62 "involvedObject.resourceVersion": "0", 63 "involvedObject.fieldPath": "", 64 "reason": "ForTesting", 65 "reportingComponent": "", 66 "source": "test", 67 "type": api.EventTypeNormal, 68 } 69 if e, a := expectA, field; !reflect.DeepEqual(e, a) { 70 t.Errorf("diff: %s", cmp.Diff(e, a)) 71 } 72 73 eventB := &api.Event{ 74 ObjectMeta: metav1.ObjectMeta{ 75 Name: "f0118", 76 Namespace: "default", 77 }, 78 InvolvedObject: api.ObjectReference{ 79 Kind: "Pod", 80 Name: "foo", 81 Namespace: "baz", 82 UID: "long uid string", 83 APIVersion: "v1", 84 ResourceVersion: "0", 85 FieldPath: "", 86 }, 87 Reason: "ForTesting", 88 ReportingController: "test", 89 Type: api.EventTypeNormal, 90 } 91 field = ToSelectableFields(eventB) 92 expectB := fields.Set{ 93 "metadata.name": "f0118", 94 "metadata.namespace": "default", 95 "involvedObject.kind": "Pod", 96 "involvedObject.name": "foo", 97 "involvedObject.namespace": "baz", 98 "involvedObject.uid": "long uid string", 99 "involvedObject.apiVersion": "v1", 100 "involvedObject.resourceVersion": "0", 101 "involvedObject.fieldPath": "", 102 "reason": "ForTesting", 103 "reportingComponent": "test", 104 "source": "test", 105 "type": api.EventTypeNormal, 106 } 107 if e, a := expectB, field; !reflect.DeepEqual(e, a) { 108 t.Errorf("diff: %s", cmp.Diff(e, a)) 109 } 110 } 111 112 func TestSelectableFieldLabelConversions(t *testing.T) { 113 fset := ToSelectableFields(&api.Event{}) 114 apitesting.TestSelectableFieldLabelConversionsOfKind(t, 115 "v1", 116 "Event", 117 fset, 118 nil, 119 ) 120 } 121 122 func TestValidateUpdate(t *testing.T) { 123 makeEvent := func(name string) *api.Event { 124 return &api.Event{ 125 ObjectMeta: metav1.ObjectMeta{ 126 Name: name, 127 Namespace: "default", 128 ResourceVersion: "123", 129 }, 130 InvolvedObject: api.ObjectReference{ 131 Kind: "Pod", 132 Name: "foo", 133 Namespace: "default", 134 UID: "long uid string", 135 APIVersion: "v1", 136 ResourceVersion: "0", 137 FieldPath: "", 138 }, 139 Reason: "ForTesting", 140 Source: api.EventSource{Component: "test"}, 141 Type: api.EventTypeNormal, 142 } 143 } 144 eventA := makeEvent("eventA") 145 eventB := makeEvent("eventB") 146 errList := Strategy.ValidateUpdate(context.Background(), eventA, eventB) 147 if len(errList) == 0 { 148 t.Errorf("ValidateUpdate should fail on name change") 149 } 150 }