1
1
/*
2
- Copyright 2023 The Kubernetes Authors.
2
+ Copyright 2024 The Kubernetes Authors.
3
3
4
4
Licensed under the Apache License, Version 2.0 (the "License");
5
5
you may not use this file except in compliance with the License.
@@ -18,149 +18,187 @@ package v1beta1
18
18
19
19
import (
20
20
"strings"
21
+ "testing"
21
22
22
- . "github.com/onsi/ginkgo/v2"
23
23
. "github.com/onsi/gomega"
24
24
infrav1 "sigs.k8s.io/cluster-api-provider-gcp/api/v1beta1"
25
25
)
26
26
27
- var gcpmmp * GCPManagedMachinePool
28
-
29
- var _ = Describe ("Test GCPManagedMachinePool Webhooks" , func () {
30
- BeforeEach (func () {
31
- machineType := "e2-medium"
32
- diskSizeGb := int32 (100 )
27
+ var (
28
+ minCount = int32 (1 )
29
+ maxCount = int32 (3 )
30
+ invalidMinCount = int32 (- 1 )
31
+ enableAutoscaling = false
32
+ diskSizeGb = int32 (200 )
33
+ maxPods = int64 (10 )
34
+ localSsds = int32 (0 )
35
+ invalidDiskSizeGb = int32 (- 200 )
36
+ invalidMaxPods = int64 (- 10 )
37
+ invalidLocalSsds = int32 (- 0 )
38
+ )
33
39
34
- gcpmmp = & GCPManagedMachinePool {
35
- Spec : GCPManagedMachinePoolSpec {
36
- NodePoolName : "test-gke-pool" ,
37
- MachineType : & machineType ,
38
- DiskSizeGb : & diskSizeGb ,
40
+ func TestGCPManagedMachinePoolValidatingWebhookCreate (t * testing.T ) {
41
+ tests := []struct {
42
+ name string
43
+ spec GCPManagedMachinePoolSpec
44
+ expectError bool
45
+ }{
46
+ {
47
+ name : "valid node pool name" ,
48
+ spec : GCPManagedMachinePoolSpec {
49
+ NodePoolName : "nodepool1" ,
39
50
},
40
- }
41
- })
42
-
43
- Context ("Test validateSpec" , func () {
44
- It ("should error when node pool name is too long" , func () {
45
- gcpmmp .Spec .NodePoolName = strings .Repeat ("A" , maxNodePoolNameLength + 1 )
46
- errs := gcpmmp .validateSpec ()
47
- Expect (errs ).ToNot (BeEmpty ())
48
- })
49
- It ("should pass when node pool name is within limit" , func () {
50
- gcpmmp .Spec .NodePoolName = strings .Repeat ("A" , maxNodePoolNameLength )
51
- errs := gcpmmp .validateSpec ()
52
- Expect (errs ).To (BeEmpty ())
53
- })
54
- })
51
+ expectError : false ,
52
+ },
53
+ {
54
+ name : "node pool name is too long" ,
55
+ spec : GCPManagedMachinePoolSpec {
56
+ NodePoolName : strings .Repeat ("A" , maxNodePoolNameLength + 1 ),
57
+ },
58
+ expectError : true ,
59
+ },
60
+ {
61
+ name : "scaling with valid min/max count" ,
62
+ spec : GCPManagedMachinePoolSpec {
63
+ NodePoolName : "nodepool1" ,
64
+ Scaling : & NodePoolAutoScaling {
65
+ MinCount : & minCount ,
66
+ MaxCount : & maxCount ,
67
+ },
68
+ },
69
+ expectError : false ,
70
+ },
71
+ {
72
+ name : "scaling with invalid min/max count" ,
73
+ spec : GCPManagedMachinePoolSpec {
74
+ NodePoolName : "nodepool1" ,
75
+ Scaling : & NodePoolAutoScaling {
76
+ MinCount : & invalidMinCount ,
77
+ MaxCount : & maxCount ,
78
+ },
79
+ },
80
+ expectError : true ,
81
+ },
82
+ {
83
+ name : "scaling with max < min count" ,
84
+ spec : GCPManagedMachinePoolSpec {
85
+ NodePoolName : "nodepool1" ,
86
+ Scaling : & NodePoolAutoScaling {
87
+ MinCount : & maxCount ,
88
+ MaxCount : & minCount ,
89
+ },
90
+ },
91
+ expectError : true ,
92
+ },
93
+ {
94
+ name : "autoscaling disabled and min/max provided" ,
95
+ spec : GCPManagedMachinePoolSpec {
96
+ NodePoolName : "nodepool1" ,
97
+ Scaling : & NodePoolAutoScaling {
98
+ EnableAutoscaling : & enableAutoscaling ,
99
+ MinCount : & minCount ,
100
+ MaxCount : & maxCount ,
101
+ },
102
+ },
103
+ expectError : true ,
104
+ },
105
+ {
106
+ name : "valid non-negative values" ,
107
+ spec : GCPManagedMachinePoolSpec {
108
+ NodePoolName : "nodepool1" ,
109
+ DiskSizeGb : & diskSizeGb ,
110
+ MaxPodsPerNode : & maxPods ,
111
+ LocalSsdCount : & localSsds ,
112
+ },
113
+ expectError : false ,
114
+ },
115
+ {
116
+ name : "invalid negative values" ,
117
+ spec : GCPManagedMachinePoolSpec {
118
+ NodePoolName : "nodepool1" ,
119
+ DiskSizeGb : & invalidDiskSizeGb ,
120
+ MaxPodsPerNode : & invalidMaxPods ,
121
+ LocalSsdCount : & invalidLocalSsds ,
122
+ },
123
+ expectError : true ,
124
+ },
125
+ }
55
126
56
- Context ("Test validateScaling" , func () {
57
- It ("should pass when scaling is not specified" , func () {
58
- errs := gcpmmp .validateScaling ()
59
- Expect (errs ).To (BeEmpty ())
60
- })
61
- It ("should pass when min/max count is valid" , func () {
62
- minCount := int32 (1 )
63
- maxCount := int32 (3 )
64
- gcpmmp .Spec .Scaling = & NodePoolAutoScaling {
65
- MinCount : & minCount ,
66
- MaxCount : & maxCount ,
67
- }
127
+ for _ , tc := range tests {
128
+ t .Run (tc .name , func (t * testing.T ) {
129
+ g := NewWithT (t )
68
130
69
- errs := gcpmmp .validateScaling ()
70
- Expect (errs ).To (BeEmpty ())
71
- })
72
- It ("should fail when min is negative" , func () {
73
- minCount := int32 (- 1 )
74
- gcpmmp .Spec .Scaling = & NodePoolAutoScaling {
75
- MinCount : & minCount ,
131
+ mmp := & GCPManagedMachinePool {
132
+ Spec : tc .spec ,
76
133
}
134
+ warn , err := mmp .ValidateCreate ()
77
135
78
- errs := gcpmmp .validateScaling ()
79
- Expect (errs ).ToNot (BeEmpty ())
80
- })
81
- It ("should fail when min > max" , func () {
82
- minCount := int32 (3 )
83
- maxCount := int32 (1 )
84
- gcpmmp .Spec .Scaling = & NodePoolAutoScaling {
85
- MinCount : & minCount ,
86
- MaxCount : & maxCount ,
136
+ if tc .expectError {
137
+ g .Expect (err ).To (HaveOccurred ())
138
+ } else {
139
+ g .Expect (err ).ToNot (HaveOccurred ())
87
140
}
88
-
89
- errs := gcpmmp .validateScaling ()
90
- Expect (errs ).ToNot (BeEmpty ())
141
+ // Nothing emits warnings yet
142
+ g .Expect (warn ).To (BeEmpty ())
91
143
})
92
- It ("should fail when autoscaling is disabled and min/max is specified" , func () {
93
- minCount := int32 (1 )
94
- maxCount := int32 (3 )
95
- enabled := false
96
- locationPolicy := ManagedNodePoolLocationPolicyAny
97
- gcpmmp .Spec .Scaling = & NodePoolAutoScaling {
98
- MinCount : & minCount ,
99
- MaxCount : & maxCount ,
100
- EnableAutoscaling : & enabled ,
101
- LocationPolicy : & locationPolicy ,
102
- }
144
+ }
145
+ }
146
+
147
+ func TestGCPManagedMachinePoolValidatingWebhookUpdate (t * testing.T ) {
148
+ tests := []struct {
149
+ name string
150
+ spec GCPManagedMachinePoolSpec
151
+ expectError bool
152
+ }{
153
+ {
154
+ name : "node pool is not mutated" ,
155
+ spec : GCPManagedMachinePoolSpec {
156
+ NodePoolName : "nodepool1" ,
157
+ },
158
+ expectError : false ,
159
+ },
160
+ {
161
+ name : "mutable fields are mutated" ,
162
+ spec : GCPManagedMachinePoolSpec {
163
+ NodePoolName : "nodepool1" ,
164
+ AdditionalLabels : infrav1.Labels {
165
+ "testKey" : "testVal" ,
166
+ },
167
+ },
168
+ expectError : false ,
169
+ },
170
+ {
171
+ name : "immutable field disk size is mutated" ,
172
+ spec : GCPManagedMachinePoolSpec {
173
+ NodePoolName : "nodepool1" ,
174
+ DiskSizeGb : & diskSizeGb ,
175
+ },
176
+ expectError : true ,
177
+ },
178
+ }
103
179
104
- errs := gcpmmp .validateScaling ()
105
- Expect (errs ).To (HaveLen (3 ))
106
- })
107
- })
108
- Context ("Test validateImmutable" , func () {
109
- It ("should pass when node pool is not mutated" , func () {
110
- old := gcpmmp .DeepCopy ()
111
- errs := gcpmmp .validateImmutable (old )
112
- Expect (errs ).To (BeEmpty ())
113
- })
114
- It ("should pass when mutable fields are mutated" , func () {
115
- old := gcpmmp .DeepCopy ()
116
- gcpmmp .Spec .AdditionalLabels = infrav1.Labels {
117
- "testKey" : "testVal" ,
118
- }
180
+ for _ , tc := range tests {
181
+ t .Run (tc .name , func (t * testing.T ) {
182
+ g := NewWithT (t )
119
183
120
- errs := gcpmmp .validateImmutable (old )
121
- Expect (errs ).To (BeEmpty ())
122
- })
123
- It ("should fail when immutable fields are mutated" , func () {
124
- old := gcpmmp .DeepCopy ()
125
- diskSizeGb := int32 (200 )
126
- gcpmmp .Spec .DiskSizeGb = & diskSizeGb
127
- gcpmmp .Spec .NodePoolName = "new-name"
128
- gcpmmp .Spec .Management = & NodePoolManagement {
129
- AutoUpgrade : false ,
130
- AutoRepair : false ,
184
+ newMMP := & GCPManagedMachinePool {
185
+ Spec : tc .spec ,
186
+ }
187
+ oldMMP := & GCPManagedMachinePool {
188
+ Spec : GCPManagedMachinePoolSpec {
189
+ NodePoolName : "nodepool1" ,
190
+ },
131
191
}
132
192
133
- errs := gcpmmp .validateImmutable (old )
134
- Expect (errs ).To (HaveLen (3 ))
135
- })
136
- })
193
+ warn , err := newMMP .ValidateUpdate (oldMMP )
137
194
138
- Context ("Test validateNonNegative" , func () {
139
- It ("should pass when number fields are not specified" , func () {
140
- errs := gcpmmp .validateNonNegative ()
141
- Expect (errs ).To (BeEmpty ())
142
- })
143
- It ("should pass when number fields are non-negative" , func () {
144
- maxPods := int64 (10 )
145
- localSsds := int32 (0 )
146
- diskSize := int32 (200 )
147
- gcpmmp .Spec .MaxPodsPerNode = & maxPods
148
- gcpmmp .Spec .LocalSsdCount = & localSsds
149
- gcpmmp .Spec .DiskSizeGb = & diskSize
150
-
151
- errs := gcpmmp .validateNonNegative ()
152
- Expect (errs ).To (BeEmpty ())
153
- })
154
- It ("should pass when some number fields are negative" , func () {
155
- maxPods := int64 (- 1 )
156
- localSsds := int32 (0 )
157
- diskSize := int32 (- 100 )
158
- gcpmmp .Spec .MaxPodsPerNode = & maxPods
159
- gcpmmp .Spec .LocalSsdCount = & localSsds
160
- gcpmmp .Spec .DiskSizeGb = & diskSize
161
-
162
- errs := gcpmmp .validateNonNegative ()
163
- Expect (errs ).To (HaveLen (2 ))
195
+ if tc .expectError {
196
+ g .Expect (err ).To (HaveOccurred ())
197
+ } else {
198
+ g .Expect (err ).ToNot (HaveOccurred ())
199
+ }
200
+ // Nothing emits warnings yet
201
+ g .Expect (warn ).To (BeEmpty ())
164
202
})
165
- })
166
- })
203
+ }
204
+ }
0 commit comments