knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/reconciler/testing/util.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 testing includes utilities for testing controllers. 18 package testing 19 20 import ( 21 "regexp" 22 "testing" 23 24 corev1 "k8s.io/api/core/v1" 25 "k8s.io/apimachinery/pkg/runtime" 26 "k8s.io/client-go/tools/cache" 27 ) 28 29 // KeyOrDie returns the string key of the Kubernetes object or panics if a key 30 // cannot be generated. 31 func KeyOrDie(obj interface{}) string { 32 key, err := cache.MetaNamespaceKeyFunc(obj) 33 if err != nil { 34 panic(err) 35 } 36 return key 37 } 38 39 // ExpectNormalEventDelivery returns a hook function that can be passed to a 40 // Hooks.OnCreate() call to verify that an event of type Normal was created 41 // matching the given regular expression. For this expectation to be effective 42 // the test must also call Hooks.WaitForHooks(). 43 func ExpectNormalEventDelivery(t *testing.T, messageRegexp string) CreateHookFunc { 44 t.Helper() 45 wantRegexp, err := regexp.Compile(messageRegexp) 46 if err != nil { 47 t.Fatal("Invalid regular expression:", err) 48 } 49 return func(obj runtime.Object) HookResult { 50 t.Helper() 51 event := obj.(*corev1.Event) 52 if !wantRegexp.MatchString(event.Message) { 53 return HookIncomplete 54 } 55 t.Logf("Got an event message matching %q: %q", wantRegexp, event.Message) 56 if got, want := event.Type, corev1.EventTypeNormal; got != want { 57 t.Errorf("unexpected event Type: %q expected: %q", got, want) 58 } 59 return HookComplete 60 } 61 } 62 63 // ExpectWarningEventDelivery returns a hook function that can be passed to a 64 // Hooks.OnCreate() call to verify that an event of type Warning was created 65 // matching the given regular expression. For this expectation to be effective 66 // the test must also call Hooks.WaitForHooks(). 67 func ExpectWarningEventDelivery(t *testing.T, messageRegexp string) CreateHookFunc { 68 t.Helper() 69 wantRegexp, err := regexp.Compile(messageRegexp) 70 if err != nil { 71 t.Fatal("Invalid regular expression:", err) 72 } 73 return func(obj runtime.Object) HookResult { 74 t.Helper() 75 event := obj.(*corev1.Event) 76 if !wantRegexp.MatchString(event.Message) { 77 return HookIncomplete 78 } 79 t.Logf("Got an event message matching %q: %q", wantRegexp, event.Message) 80 if got, want := event.Type, corev1.EventTypeWarning; got != want { 81 t.Errorf("unexpected event Type: %q expected: %q", got, want) 82 } 83 return HookComplete 84 } 85 }