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