k8s.io/apiserver@v0.31.1/pkg/admission/testing/helpers.go (about) 1 /* 2 Copyright 2019 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 testing 18 19 import ( 20 "context" 21 "reflect" 22 "testing" 23 24 "github.com/google/go-cmp/cmp" 25 apiequality "k8s.io/apimachinery/pkg/api/equality" 26 "k8s.io/apimachinery/pkg/runtime" 27 "k8s.io/apiserver/pkg/admission" 28 ) 29 30 // WithReinvocationTesting wraps a mutating admission handler and reinvokes it each time Admit is 31 // called. It checks the admission output object and reports a test error if the admission handler 32 // performs non-idempotent mutatations to the object. 33 func WithReinvocationTesting(t *testing.T, admission admission.MutationInterface) admission.MutationInterface { 34 return &reinvoker{t, admission} 35 } 36 37 type reinvoker struct { 38 t *testing.T 39 admission admission.MutationInterface 40 } 41 42 // Admit reinvokes the admission handler and reports a test error if the admission handler performs 43 // non-idempotent mutatations to the admission object. 44 func (r *reinvoker) Admit(ctx context.Context, a admission.Attributes, o admission.ObjectInterfaces) error { 45 r.t.Helper() 46 outputs := []runtime.Object{} 47 for i := 0; i < 2; i++ { 48 err := r.admission.Admit(ctx, a, o) 49 if err != nil { 50 return err 51 } 52 if a.GetObject() != nil { 53 // keep a copy of the output for subsequent idempotency checking 54 outputs = append(outputs, a.GetObject().DeepCopyObject()) 55 // replace a.GetObject() with a copy of itself to make sure admission is safe to reinvoke with a round-tripped copy (no pointer comparisons are done) 56 if deepCopyInto, ok := reflect.TypeOf(a.GetObject()).MethodByName("DeepCopyInto"); ok { 57 deepCopyInto.Func.Call([]reflect.Value{ 58 reflect.ValueOf(a.GetObject().DeepCopyObject()), 59 reflect.ValueOf(a.GetObject()), 60 }) 61 } 62 } 63 } 64 for i := 1; i < len(outputs); i++ { 65 if !apiequality.Semantic.DeepEqual(outputs[0], outputs[i]) { 66 r.t.Errorf("expected mutating admission plugin to be idempontent, but got different results on reinvocation, diff:\n%s", cmp.Diff(outputs[0], outputs[i])) 67 } 68 } 69 return nil 70 } 71 72 // Handles will return true if any of the admission andler handlers handle the given operation. 73 func (r *reinvoker) Handles(operation admission.Operation) bool { 74 r.t.Helper() 75 return r.admission.Handles(operation) 76 }