github.com/redhat-appstudio/release-service@v0.0.0-20240507045911-a8558ef3422a/api/v1alpha1/webhooks/release/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 release 17 18 import ( 19 "context" 20 21 toolkit "github.com/konflux-ci/operator-toolkit/loader" 22 "github.com/redhat-appstudio/release-service/api/v1alpha1" 23 "github.com/redhat-appstudio/release-service/loader" 24 "k8s.io/apimachinery/pkg/api/errors" 25 "k8s.io/apimachinery/pkg/runtime/schema" 26 27 . "github.com/onsi/ginkgo/v2" 28 . "github.com/onsi/gomega" 29 30 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 31 //+kubebuilder:scaffold:imports 32 ) 33 34 var _ = Describe("Release validation webhook", func() { 35 var ( 36 createResources func() 37 38 release *v1alpha1.Release 39 releasePlan *v1alpha1.ReleasePlan 40 ) 41 42 When("Default method is called", func() { 43 var mockedWebhook *Webhook 44 45 BeforeEach(func() { 46 createResources() 47 48 mockedWebhook = &Webhook{ 49 client: k8sClient, 50 loader: loader.NewMockLoader(), 51 } 52 }) 53 54 It("should set GracePeriodDays to ReleasePlan's value and return nil", func() { 55 mockedCtx := toolkit.GetMockedContext(ctx, []toolkit.MockData{ 56 { 57 ContextKey: loader.ReleasePlanContextKey, 58 Resource: releasePlan, 59 }, 60 }) 61 62 Expect(mockedWebhook.Default(mockedCtx, release)).To(BeNil()) 63 Expect(release.Spec.GracePeriodDays).To(Equal(releasePlan.Spec.ReleaseGracePeriodDays)) 64 }) 65 66 It("should return nil and keep the default value of a go `int` for GracePeriodDays when the specified ReleasePlan does not exist", func() { 67 mockedCtx := toolkit.GetMockedContext(ctx, []toolkit.MockData{ 68 { 69 ContextKey: loader.ReleasePlanContextKey, 70 Err: errors.NewNotFound(schema.GroupResource{}, ""), 71 }, 72 }) 73 74 Expect(mockedWebhook.Default(mockedCtx, release)).To(BeNil()) 75 Expect(release.Spec.GracePeriodDays).To(Equal(0)) 76 }) 77 }) 78 79 When("When ValidateUpdate is called", func() { 80 It("should error out when updating the resource", func() { 81 updatedRelease := release.DeepCopy() 82 updatedRelease.Spec.Snapshot = "another-snapshot" 83 84 _, err := webhook.ValidateUpdate(ctx, release, updatedRelease) 85 Expect(err).Should(HaveOccurred()) 86 Expect(err.Error()).Should(ContainSubstring("release resources spec cannot be updated")) 87 }) 88 89 It("should not error out when updating the resource metadata", func() { 90 ctx := context.Background() 91 92 updatedRelease := release.DeepCopy() 93 updatedRelease.ObjectMeta.Annotations = map[string]string{ 94 "foo": "bar", 95 } 96 97 _, err := webhook.ValidateUpdate(ctx, release, updatedRelease) 98 Expect(err).NotTo(HaveOccurred()) 99 }) 100 }) 101 102 When("ValidateDelete method is called", func() { 103 It("should return nil", func() { 104 _, err := webhook.ValidateDelete(ctx, &v1alpha1.Release{}) 105 Expect(err).NotTo(HaveOccurred()) 106 }) 107 }) 108 109 createResources = func() { 110 release = &v1alpha1.Release{ 111 TypeMeta: metav1.TypeMeta{ 112 APIVersion: "appstudio.redhat.com/v1alpha1", 113 Kind: "Release", 114 }, 115 ObjectMeta: metav1.ObjectMeta{ 116 GenerateName: "test-release-", 117 Namespace: "default", 118 }, 119 Spec: v1alpha1.ReleaseSpec{ 120 Snapshot: "test-snapshot", 121 ReleasePlan: "test-releaseplan", 122 }, 123 } 124 125 releasePlan = &v1alpha1.ReleasePlan{ 126 TypeMeta: metav1.TypeMeta{ 127 APIVersion: "appstudio.redhat.com/v1alpha1", 128 Kind: "ReleasePlan", 129 }, 130 ObjectMeta: metav1.ObjectMeta{ 131 Name: "test-releaseplan", 132 Namespace: "default", 133 }, 134 Spec: v1alpha1.ReleasePlanSpec{ 135 Application: "test-application", 136 Target: "default", 137 ReleaseGracePeriodDays: 7, 138 }, 139 } 140 } 141 })