sigs.k8s.io/kueue@v0.6.2/test/integration/controller/core/admissioncheck_controller_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 core
    18  
    19  import (
    20  	"github.com/onsi/ginkgo/v2"
    21  	"github.com/onsi/gomega"
    22  	corev1 "k8s.io/api/core/v1"
    23  	apimeta "k8s.io/apimachinery/pkg/api/meta"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	"sigs.k8s.io/controller-runtime/pkg/client"
    26  
    27  	kueue "sigs.k8s.io/kueue/apis/kueue/v1beta1"
    28  	utiltesting "sigs.k8s.io/kueue/pkg/util/testing"
    29  	"sigs.k8s.io/kueue/test/integration/framework"
    30  	"sigs.k8s.io/kueue/test/util"
    31  )
    32  
    33  // +kubebuilder:docs-gen:collapse=Imports
    34  
    35  var _ = ginkgo.Describe("AdmissionCheck controller", ginkgo.Ordered, ginkgo.ContinueOnFailure, func() {
    36  	var ns *corev1.Namespace
    37  
    38  	ginkgo.BeforeAll(func() {
    39  		fwk = &framework.Framework{CRDPath: crdPath, WebhookPath: webhookPath}
    40  		cfg = fwk.Init()
    41  		ctx, k8sClient = fwk.RunManager(cfg, managerSetup)
    42  	})
    43  	ginkgo.AfterAll(func() {
    44  		fwk.Teardown()
    45  	})
    46  
    47  	ginkgo.BeforeEach(func() {
    48  		ns = &corev1.Namespace{
    49  			ObjectMeta: metav1.ObjectMeta{
    50  				GenerateName: "core-admissioncheck-",
    51  			},
    52  		}
    53  		gomega.Expect(k8sClient.Create(ctx, ns)).To(gomega.Succeed())
    54  	})
    55  
    56  	ginkgo.AfterEach(func() {
    57  		gomega.Expect(util.DeleteNamespace(ctx, k8sClient, ns)).To(gomega.Succeed())
    58  	})
    59  
    60  	ginkgo.When("one clusterQueue references admissionChecks", func() {
    61  		var admissionCheck *kueue.AdmissionCheck
    62  		var clusterQueue *kueue.ClusterQueue
    63  
    64  		ginkgo.BeforeEach(func() {
    65  			admissionCheck = utiltesting.MakeAdmissionCheck("check1").ControllerName("ac-controller").Obj()
    66  			clusterQueue = utiltesting.MakeClusterQueue("foo").
    67  				AdmissionChecks("check1").
    68  				Obj()
    69  
    70  			gomega.Expect(k8sClient.Create(ctx, admissionCheck)).To(gomega.Succeed())
    71  
    72  			ginkgo.By("Activating the admission check", func() {
    73  				util.SetAdmissionCheckActive(ctx, k8sClient, admissionCheck, metav1.ConditionTrue)
    74  			})
    75  
    76  			gomega.Expect(k8sClient.Create(ctx, clusterQueue)).To(gomega.Succeed())
    77  
    78  			ginkgo.By("Wait for the queue to become active", func() {
    79  				gomega.Eventually(func() bool {
    80  					var cq kueue.ClusterQueue
    81  					err := k8sClient.Get(ctx, client.ObjectKeyFromObject(clusterQueue), &cq)
    82  					if err != nil {
    83  						return false
    84  					}
    85  					return apimeta.IsStatusConditionTrue(cq.Status.Conditions, kueue.ClusterQueueActive)
    86  				}, util.Timeout, util.Interval).Should(gomega.BeTrue())
    87  			})
    88  		})
    89  
    90  		ginkgo.AfterEach(func() {
    91  			util.ExpectClusterQueueToBeDeleted(ctx, k8sClient, clusterQueue, true)
    92  			util.ExpectAdmissionCheckToBeDeleted(ctx, k8sClient, admissionCheck, true)
    93  		})
    94  
    95  		ginkgo.It("Should delete the admissionCheck when the corresponding clusterQueue no longer uses the admissionCheck", func() {
    96  			ginkgo.By("Try to delete admissionCheck")
    97  			gomega.Expect(util.DeleteAdmissionCheck(ctx, k8sClient, admissionCheck)).To(gomega.Succeed())
    98  			var ac kueue.AdmissionCheck
    99  			gomega.Eventually(func() []string {
   100  				gomega.Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(admissionCheck), &ac)).To(gomega.Succeed())
   101  				return ac.GetFinalizers()
   102  			}, util.Timeout, util.Interval).Should(gomega.BeComparableTo([]string{kueue.ResourceInUseFinalizerName}))
   103  			gomega.Expect(ac.GetDeletionTimestamp()).ShouldNot(gomega.BeNil())
   104  
   105  			ginkgo.By("Update clusterQueue's cohort")
   106  			var cq kueue.ClusterQueue
   107  			gomega.Eventually(func() error {
   108  				gomega.Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(clusterQueue), &cq)).To(gomega.Succeed())
   109  				cq.Spec.Cohort = "foo-cohort"
   110  				return k8sClient.Update(ctx, &cq)
   111  			}, util.Timeout, util.Interval).Should(gomega.Succeed())
   112  
   113  			gomega.Eventually(func() error {
   114  				return k8sClient.Get(ctx, client.ObjectKeyFromObject(admissionCheck), &ac)
   115  			}, util.Timeout, util.Interval).Should(gomega.Succeed())
   116  
   117  			ginkgo.By("Change clusterQueue's checks")
   118  			gomega.Eventually(func() error {
   119  				err := k8sClient.Get(ctx, client.ObjectKeyFromObject(clusterQueue), &cq)
   120  				if err != nil {
   121  					return err
   122  				}
   123  				cq.Spec.AdmissionChecks = []string{"check2"}
   124  				return k8sClient.Update(ctx, &cq)
   125  			}, util.Timeout, util.Interval).Should(gomega.Succeed())
   126  
   127  			gomega.Eventually(func() error {
   128  				return k8sClient.Get(ctx, client.ObjectKeyFromObject(admissionCheck), &ac)
   129  			}, util.Timeout, util.Interval).Should(utiltesting.BeNotFoundError())
   130  		})
   131  
   132  		ginkgo.It("Should delete the admissionCheck when the corresponding clusterQueue is deleted", func() {
   133  			gomega.Expect(util.DeleteAdmissionCheck(ctx, k8sClient, admissionCheck)).To(gomega.Succeed())
   134  
   135  			var rf kueue.AdmissionCheck
   136  			gomega.Eventually(func() []string {
   137  				gomega.Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(admissionCheck), &rf)).To(gomega.Succeed())
   138  				return rf.GetFinalizers()
   139  			}, util.Timeout, util.Interval).Should(gomega.BeComparableTo([]string{kueue.ResourceInUseFinalizerName}))
   140  			gomega.Expect(rf.GetDeletionTimestamp()).ShouldNot(gomega.BeNil())
   141  
   142  			gomega.Expect(util.DeleteClusterQueue(ctx, k8sClient, clusterQueue)).To(gomega.Succeed())
   143  			gomega.Eventually(func() error {
   144  				return k8sClient.Get(ctx, client.ObjectKeyFromObject(admissionCheck), &rf)
   145  			}, util.Timeout, util.Interval).Should(utiltesting.BeNotFoundError())
   146  		})
   147  	})
   148  })