sigs.k8s.io/kueue@v0.6.2/pkg/controller/admissionchecks/provisioning/admissioncheck_reconciler_test.go (about) 1 /* 2 Copyright 2023 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 provisioning 18 19 import ( 20 "testing" 21 22 "github.com/google/go-cmp/cmp" 23 apimeta "k8s.io/apimachinery/pkg/api/meta" 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 "k8s.io/apimachinery/pkg/types" 26 "sigs.k8s.io/controller-runtime/pkg/reconcile" 27 28 kueue "sigs.k8s.io/kueue/apis/kueue/v1beta1" 29 utiltesting "sigs.k8s.io/kueue/pkg/util/testing" 30 ) 31 32 func TestReconcileAdmissionCheck(t *testing.T) { 33 cases := map[string]struct { 34 configs []kueue.ProvisioningRequestConfig 35 check *kueue.AdmissionCheck 36 wantCondition *metav1.Condition 37 }{ 38 "unrelated check": { 39 check: utiltesting.MakeAdmissionCheck("check1"). 40 ControllerName("other-controller"). 41 Obj(), 42 }, 43 "no parameters specified": { 44 check: utiltesting.MakeAdmissionCheck("check1"). 45 ControllerName(ControllerName). 46 Obj(), 47 wantCondition: &metav1.Condition{ 48 Type: kueue.AdmissionCheckActive, 49 Status: metav1.ConditionFalse, 50 Reason: "BadParametersRef", 51 Message: "missing parameters reference", 52 }, 53 }, 54 "bad ref group": { 55 check: utiltesting.MakeAdmissionCheck("check1"). 56 Parameters("bad.group", ConfigKind, "config1"). 57 ControllerName(ControllerName). 58 Obj(), 59 wantCondition: &metav1.Condition{ 60 Type: kueue.AdmissionCheckActive, 61 Status: metav1.ConditionFalse, 62 Reason: "BadParametersRef", 63 Message: "wrong group \"bad.group\", expecting \"kueue.x-k8s.io\": bad parameters reference", 64 }, 65 }, 66 "bad ref kind": { 67 check: utiltesting.MakeAdmissionCheck("check1"). 68 Parameters(kueue.GroupVersion.Group, "BadKind", "config1"). 69 ControllerName(ControllerName). 70 Obj(), 71 wantCondition: &metav1.Condition{ 72 Type: kueue.AdmissionCheckActive, 73 Status: metav1.ConditionFalse, 74 Reason: "BadParametersRef", 75 Message: "wrong kind \"BadKind\", expecting \"ProvisioningRequestConfig\": bad parameters reference", 76 }, 77 }, 78 "config missing": { 79 check: utiltesting.MakeAdmissionCheck("check1"). 80 Parameters(kueue.GroupVersion.Group, ConfigKind, "config1"). 81 ControllerName(ControllerName). 82 Obj(), 83 wantCondition: &metav1.Condition{ 84 Type: kueue.AdmissionCheckActive, 85 Status: metav1.ConditionFalse, 86 Reason: "BadParametersRef", 87 Message: "provisioningrequestconfigs.kueue.x-k8s.io \"config1\" not found", 88 }, 89 }, 90 "config found": { 91 check: utiltesting.MakeAdmissionCheck("check1"). 92 Parameters(kueue.GroupVersion.Group, ConfigKind, "config1"). 93 ControllerName(ControllerName). 94 Obj(), 95 configs: []kueue.ProvisioningRequestConfig{ 96 { 97 ObjectMeta: metav1.ObjectMeta{ 98 Name: "config1", 99 }, 100 }, 101 }, 102 wantCondition: &metav1.Condition{ 103 Type: kueue.AdmissionCheckActive, 104 Status: metav1.ConditionTrue, 105 Reason: "Active", 106 Message: "The admission check is active", 107 }, 108 }, 109 } 110 111 for name, tc := range cases { 112 t.Run(name, func(t *testing.T) { 113 builder, ctx := getClientBuilder() 114 115 builder = builder.WithObjects(tc.check) 116 builder = builder.WithStatusSubresource(tc.check) 117 118 builder = builder.WithLists(&kueue.ProvisioningRequestConfigList{Items: tc.configs}) 119 120 k8sclient := builder.Build() 121 122 helper, err := newProvisioningConfigHelper(k8sclient) 123 if err != nil { 124 t.Errorf("unable to create the config helper: %s", err) 125 return 126 } 127 reconciler := acReconciler{ 128 client: k8sclient, 129 helper: helper, 130 } 131 132 req := reconcile.Request{ 133 NamespacedName: types.NamespacedName{ 134 Name: tc.check.Name, 135 }, 136 } 137 _, gotReconcileError := reconciler.Reconcile(ctx, req) 138 if gotReconcileError != nil { 139 t.Errorf("unexpected reconcile error: %s", gotReconcileError) 140 } 141 142 gotAc := &kueue.AdmissionCheck{} 143 if err := k8sclient.Get(ctx, types.NamespacedName{Name: tc.check.Name}, gotAc); err != nil { 144 t.Errorf("unexpected error getting check %q", tc.check.Name) 145 146 } 147 148 gotCondition := apimeta.FindStatusCondition(gotAc.Status.Conditions, kueue.AdmissionCheckActive) 149 if diff := cmp.Diff(tc.wantCondition, gotCondition, acCmpOptions...); diff != "" { 150 t.Errorf("unexpected check %q (-want/+got):\n%s", tc.check.Name, diff) 151 } 152 }) 153 } 154 }