@@ -14,28 +14,33 @@ See the License for the specific language governing permissions and
14
14
limitations under the License.
15
15
*/
16
16
17
- package v1beta1
17
+ package webhooks
18
18
19
19
import (
20
20
"testing"
21
21
22
22
. "github.com/onsi/gomega"
23
23
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24
+ ctrl "sigs.k8s.io/controller-runtime"
24
25
25
- utildefaulting "sigs.k8s.io/cluster-api/util/defaulting"
26
+ addonsv1 "sigs.k8s.io/cluster-api/exp/addons/api/v1beta1"
27
+ "sigs.k8s.io/cluster-api/internal/webhooks/util"
26
28
)
27
29
30
+ var ctx = ctrl .SetupSignalHandler ()
31
+
28
32
func TestClusterResourcesetDefault (t * testing.T ) {
29
33
g := NewWithT (t )
30
- clusterResourceSet := & ClusterResourceSet {}
34
+ clusterResourceSet := & addonsv1. ClusterResourceSet {}
31
35
defaultingValidationCRS := clusterResourceSet .DeepCopy ()
32
36
defaultingValidationCRS .Spec .ClusterSelector = metav1.LabelSelector {
33
37
MatchLabels : map [string ]string {"foo" : "bar" },
34
38
}
35
- t .Run ("for ClusterResourceSet" , utildefaulting .DefaultValidateTest (defaultingValidationCRS ))
36
- clusterResourceSet .Default ()
39
+ webhook := ClusterResourceSet {}
40
+ t .Run ("for ClusterResourceSet" , util .CustomDefaultValidateTest (ctx , defaultingValidationCRS , & webhook ))
41
+ g .Expect (webhook .Default (ctx , clusterResourceSet )).To (Succeed ())
37
42
38
- g .Expect (clusterResourceSet .Spec .Strategy ).To (Equal (string (ClusterResourceSetStrategyApplyOnce )))
43
+ g .Expect (clusterResourceSet .Spec .Strategy ).To (Equal (string (addonsv1 . ClusterResourceSetStrategyApplyOnce )))
39
44
}
40
45
41
46
func TestClusterResourceSetLabelSelectorAsSelectorValidation (t * testing.T ) {
@@ -59,25 +64,26 @@ func TestClusterResourceSetLabelSelectorAsSelectorValidation(t *testing.T) {
59
64
for _ , tt := range tests {
60
65
t .Run (tt .name , func (t * testing.T ) {
61
66
g := NewWithT (t )
62
- clusterResourceSet := & ClusterResourceSet {
63
- Spec : ClusterResourceSetSpec {
67
+ clusterResourceSet := & addonsv1. ClusterResourceSet {
68
+ Spec : addonsv1. ClusterResourceSetSpec {
64
69
ClusterSelector : metav1.LabelSelector {
65
70
MatchLabels : tt .selectors ,
66
71
},
67
72
},
68
73
}
74
+ webhook := ClusterResourceSet {}
69
75
if tt .expectErr {
70
- warnings , err := clusterResourceSet .ValidateCreate ()
76
+ warnings , err := webhook .ValidateCreate (ctx , clusterResourceSet )
71
77
g .Expect (err ).To (HaveOccurred ())
72
78
g .Expect (warnings ).To (BeEmpty ())
73
- warnings , err = clusterResourceSet .ValidateUpdate (clusterResourceSet )
79
+ warnings , err = webhook .ValidateUpdate (ctx , clusterResourceSet , clusterResourceSet )
74
80
g .Expect (err ).To (HaveOccurred ())
75
81
g .Expect (warnings ).To (BeEmpty ())
76
82
} else {
77
- warnings , err := clusterResourceSet .ValidateCreate ()
83
+ warnings , err := webhook .ValidateCreate (ctx , clusterResourceSet )
78
84
g .Expect (err ).ToNot (HaveOccurred ())
79
85
g .Expect (warnings ).To (BeEmpty ())
80
- warnings , err = clusterResourceSet .ValidateUpdate (clusterResourceSet )
86
+ warnings , err = webhook .ValidateUpdate (ctx , clusterResourceSet , clusterResourceSet )
81
87
g .Expect (err ).ToNot (HaveOccurred ())
82
88
g .Expect (warnings ).To (BeEmpty ())
83
89
}
@@ -94,13 +100,13 @@ func TestClusterResourceSetStrategyImmutable(t *testing.T) {
94
100
}{
95
101
{
96
102
name : "when the Strategy has not changed" ,
97
- oldStrategy : string (ClusterResourceSetStrategyApplyOnce ),
98
- newStrategy : string (ClusterResourceSetStrategyApplyOnce ),
103
+ oldStrategy : string (addonsv1 . ClusterResourceSetStrategyApplyOnce ),
104
+ newStrategy : string (addonsv1 . ClusterResourceSetStrategyApplyOnce ),
99
105
expectErr : false ,
100
106
},
101
107
{
102
108
name : "when the Strategy has changed" ,
103
- oldStrategy : string (ClusterResourceSetStrategyApplyOnce ),
109
+ oldStrategy : string (addonsv1 . ClusterResourceSetStrategyApplyOnce ),
104
110
newStrategy : "" ,
105
111
expectErr : true ,
106
112
},
@@ -110,8 +116,8 @@ func TestClusterResourceSetStrategyImmutable(t *testing.T) {
110
116
t .Run (tt .name , func (t * testing.T ) {
111
117
g := NewWithT (t )
112
118
113
- newClusterResourceSet := & ClusterResourceSet {
114
- Spec : ClusterResourceSetSpec {
119
+ newClusterResourceSet := & addonsv1. ClusterResourceSet {
120
+ Spec : addonsv1. ClusterResourceSetSpec {
115
121
ClusterSelector : metav1.LabelSelector {
116
122
MatchLabels : map [string ]string {
117
123
"test" : "test" ,
@@ -121,8 +127,8 @@ func TestClusterResourceSetStrategyImmutable(t *testing.T) {
121
127
},
122
128
}
123
129
124
- oldClusterResourceSet := & ClusterResourceSet {
125
- Spec : ClusterResourceSetSpec {
130
+ oldClusterResourceSet := & addonsv1. ClusterResourceSet {
131
+ Spec : addonsv1. ClusterResourceSetSpec {
126
132
ClusterSelector : metav1.LabelSelector {
127
133
MatchLabels : map [string ]string {
128
134
"test" : "test" ,
@@ -131,8 +137,9 @@ func TestClusterResourceSetStrategyImmutable(t *testing.T) {
131
137
Strategy : tt .oldStrategy ,
132
138
},
133
139
}
140
+ webhook := ClusterResourceSet {}
134
141
135
- warnings , err := newClusterResourceSet .ValidateUpdate (oldClusterResourceSet )
142
+ warnings , err := webhook .ValidateUpdate (ctx , oldClusterResourceSet , newClusterResourceSet )
136
143
if tt .expectErr {
137
144
g .Expect (err ).To (HaveOccurred ())
138
145
g .Expect (warnings ).To (BeEmpty ())
@@ -169,23 +176,24 @@ func TestClusterResourceSetClusterSelectorImmutable(t *testing.T) {
169
176
t .Run (tt .name , func (t * testing.T ) {
170
177
g := NewWithT (t )
171
178
172
- newClusterResourceSet := & ClusterResourceSet {
173
- Spec : ClusterResourceSetSpec {
179
+ newClusterResourceSet := & addonsv1. ClusterResourceSet {
180
+ Spec : addonsv1. ClusterResourceSetSpec {
174
181
ClusterSelector : metav1.LabelSelector {
175
182
MatchLabels : tt .newClusterSelector ,
176
183
},
177
184
},
178
185
}
179
186
180
- oldClusterResourceSet := & ClusterResourceSet {
181
- Spec : ClusterResourceSetSpec {
187
+ oldClusterResourceSet := & addonsv1. ClusterResourceSet {
188
+ Spec : addonsv1. ClusterResourceSetSpec {
182
189
ClusterSelector : metav1.LabelSelector {
183
190
MatchLabels : tt .oldClusterSelector ,
184
191
},
185
192
},
186
193
}
194
+ webhook := ClusterResourceSet {}
187
195
188
- warnings , err := newClusterResourceSet .ValidateUpdate (oldClusterResourceSet )
196
+ warnings , err := webhook .ValidateUpdate (ctx , oldClusterResourceSet , newClusterResourceSet )
189
197
if tt .expectErr {
190
198
g .Expect (err ).To (HaveOccurred ())
191
199
g .Expect (warnings ).To (BeEmpty ())
@@ -199,8 +207,9 @@ func TestClusterResourceSetClusterSelectorImmutable(t *testing.T) {
199
207
200
208
func TestClusterResourceSetSelectorNotEmptyValidation (t * testing.T ) {
201
209
g := NewWithT (t )
202
- clusterResourceSet := & ClusterResourceSet {}
203
- err := clusterResourceSet .validate (nil )
210
+ clusterResourceSet := & addonsv1.ClusterResourceSet {}
211
+ webhook := ClusterResourceSet {}
212
+ err := webhook .validate (nil , clusterResourceSet )
204
213
g .Expect (err ).To (HaveOccurred ())
205
214
g .Expect (err .Error ()).To (ContainSubstring ("selector must not be empty" ))
206
215
}
0 commit comments