github.com/redhat-appstudio/release-service@v0.0.0-20240507143925-083712697924/api/v1alpha1/webhooks/releaseplan/webhook_test.go (about) 1 // 2 // Copyright 2022 Red Hat, Inc. 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 package releaseplan 17 18 import ( 19 . "github.com/onsi/ginkgo/v2" 20 . "github.com/onsi/gomega" 21 "github.com/redhat-appstudio/release-service/api/v1alpha1" 22 23 "k8s.io/apimachinery/pkg/api/errors" 24 "k8s.io/apimachinery/pkg/types" 25 26 "github.com/redhat-appstudio/release-service/metadata" 27 28 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 29 //+kubebuilder:scaffold:imports 30 ) 31 32 var _ = Describe("ReleasePlan webhook", func() { 33 var releasePlan *v1alpha1.ReleasePlan 34 35 BeforeEach(func() { 36 releasePlan = &v1alpha1.ReleasePlan{ 37 TypeMeta: metav1.TypeMeta{ 38 APIVersion: "appstudio.redhat.com/v1alpha1", 39 Kind: "ReleasePlan", 40 }, 41 ObjectMeta: metav1.ObjectMeta{ 42 Name: "releaseplan", 43 Namespace: "default", 44 }, 45 Spec: v1alpha1.ReleasePlanSpec{ 46 Application: "application", 47 Target: "default", 48 }, 49 } 50 }) 51 52 AfterEach(func() { 53 err := k8sClient.Delete(ctx, releasePlan) 54 Expect(err == nil || errors.IsNotFound(err)).To(BeTrue()) 55 }) 56 57 When("a ReleasePlan is created without the auto-release label", func() { 58 It("should get the label added with its value set to true", func() { 59 Expect(k8sClient.Create(ctx, releasePlan)).Should(Succeed()) 60 Eventually(func() bool { 61 err := k8sClient.Get(ctx, types.NamespacedName{ 62 Name: releasePlan.Name, 63 Namespace: releasePlan.Namespace, 64 }, releasePlan) 65 66 labelValue, ok := releasePlan.GetLabels()[metadata.AutoReleaseLabel] 67 68 return err == nil && ok && labelValue == "true" 69 }, timeout).Should(BeTrue()) 70 }) 71 }) 72 73 When("a ReleasePlan is created with an invalid auto-release label value", func() { 74 It("should get rejected until the value is valid", func() { 75 releasePlan.Labels = map[string]string{metadata.AutoReleaseLabel: "foo"} 76 err := k8sClient.Create(ctx, releasePlan) 77 Expect(err).To(HaveOccurred()) 78 Expect(err.Error()).To(ContainSubstring("'%s' label can only be set to true or false", metadata.AutoReleaseLabel)) 79 }) 80 }) 81 82 When("a ReleasePlan is created with a valid auto-release label value", func() { 83 It("shouldn't be modified", func() { 84 // Using value "true" 85 localReleasePlan := releasePlan.DeepCopy() 86 localReleasePlan.Labels = map[string]string{metadata.AutoReleaseLabel: "true"} 87 Expect(k8sClient.Create(ctx, localReleasePlan)).Should(Succeed()) 88 Eventually(func() bool { 89 err := k8sClient.Get(ctx, types.NamespacedName{ 90 Name: localReleasePlan.Name, 91 Namespace: localReleasePlan.Namespace, 92 }, localReleasePlan) 93 94 labelValue, ok := localReleasePlan.GetLabels()[metadata.AutoReleaseLabel] 95 96 return err == nil && ok && labelValue == "true" 97 }, timeout).Should(BeTrue()) 98 99 Expect(k8sClient.Delete(ctx, localReleasePlan)).To(Succeed()) 100 101 // Using value "false" 102 localReleasePlan = releasePlan.DeepCopy() 103 localReleasePlan.Labels = map[string]string{metadata.AutoReleaseLabel: "false"} 104 Expect(k8sClient.Create(ctx, localReleasePlan)).Should(Succeed()) 105 Eventually(func() bool { 106 err := k8sClient.Get(ctx, types.NamespacedName{ 107 Name: localReleasePlan.Name, 108 Namespace: localReleasePlan.Namespace, 109 }, localReleasePlan) 110 111 labelValue, ok := localReleasePlan.GetLabels()[metadata.AutoReleaseLabel] 112 113 return err == nil && ok && labelValue == "false" 114 }, timeout).Should(BeTrue()) 115 }) 116 }) 117 118 When("a ReleasePlan is updated using an invalid auto-release label value", func() { 119 It("shouldn't be modified", func() { 120 Expect(k8sClient.Create(ctx, releasePlan)).Should(Succeed()) 121 releasePlan.GetLabels()[metadata.AutoReleaseLabel] = "foo" 122 err := k8sClient.Update(ctx, releasePlan) 123 Expect(err).To(HaveOccurred()) 124 Expect(err.Error()).To(ContainSubstring("'%s' label can only be set to true or false", metadata.AutoReleaseLabel)) 125 }) 126 }) 127 128 When("ValidateDelete method is called", func() { 129 It("should return nil", func() { 130 releasePlan := &v1alpha1.ReleasePlan{} 131 Expect(webhook.ValidateDelete(ctx, releasePlan)).To(BeNil()) 132 }) 133 }) 134 })