@@ -95,6 +95,18 @@ func TestClusterSerivceIPRange(t *testing.T) {
95
95
options : makeOptionsWithCIDRs ("10.0.0.0/16" , "3000::/108" ),
96
96
enableDualStack : false ,
97
97
},
98
+ {
99
+ name : "service cidr to big" ,
100
+ expectErrors : true ,
101
+ options : makeOptionsWithCIDRs ("10.0.0.0/8" , "" ),
102
+ enableDualStack : true ,
103
+ },
104
+ {
105
+ name : "dual-stack secondary cidr to big" ,
106
+ expectErrors : true ,
107
+ options : makeOptionsWithCIDRs ("10.0.0.0/16" , "3000::/64" ),
108
+ enableDualStack : true ,
109
+ },
98
110
/* success cases */
99
111
{
100
112
name : "valid primary" ,
@@ -130,3 +142,70 @@ func TestClusterSerivceIPRange(t *testing.T) {
130
142
})
131
143
}
132
144
}
145
+
146
+ func getIPnetFromCIDR (cidr string ) * net.IPNet {
147
+ _ , ipnet , _ := net .ParseCIDR (cidr )
148
+ return ipnet
149
+ }
150
+
151
+ func TestValidateMaxCIDRRange (t * testing.T ) {
152
+ testCases := []struct {
153
+ // tc.cidr, tc.maxCIDRBits, tc.cidrFlag) tc.expectedErrorMessage
154
+ name string
155
+ cidr net.IPNet
156
+ maxCIDRBits int
157
+ cidrFlag string
158
+ expectedErrorMessage string
159
+ expectErrors bool
160
+ }{
161
+ {
162
+ name : "valid ipv4 cidr" ,
163
+ cidr : * getIPnetFromCIDR ("10.92.0.0/12" ),
164
+ maxCIDRBits : 20 ,
165
+ cidrFlag : "--service-cluster-ip-range" ,
166
+ expectedErrorMessage : "" ,
167
+ expectErrors : false ,
168
+ },
169
+ {
170
+ name : "valid ipv6 cidr" ,
171
+ cidr : * getIPnetFromCIDR ("3000::/108" ),
172
+ maxCIDRBits : 20 ,
173
+ cidrFlag : "--service-cluster-ip-range" ,
174
+ expectedErrorMessage : "" ,
175
+ expectErrors : false ,
176
+ },
177
+ {
178
+ name : "ipv4 cidr to big" ,
179
+ cidr : * getIPnetFromCIDR ("10.92.0.0/8" ),
180
+ maxCIDRBits : 20 ,
181
+ cidrFlag : "--service-cluster-ip-range" ,
182
+ expectedErrorMessage : "specified --service-cluster-ip-range is too large; for 32-bit addresses, the mask must be >= 12" ,
183
+ expectErrors : true ,
184
+ },
185
+ {
186
+ name : "ipv6 cidr to big" ,
187
+ cidr : * getIPnetFromCIDR ("3000::/64" ),
188
+ maxCIDRBits : 20 ,
189
+ cidrFlag : "--service-cluster-ip-range" ,
190
+ expectedErrorMessage : "specified --service-cluster-ip-range is too large; for 128-bit addresses, the mask must be >= 108" ,
191
+ expectErrors : true ,
192
+ },
193
+ }
194
+
195
+ for _ , tc := range testCases {
196
+ t .Run (tc .name , func (t * testing.T ) {
197
+ err := validateMaxCIDRRange (tc .cidr , tc .maxCIDRBits , tc .cidrFlag )
198
+ if err != nil && ! tc .expectErrors {
199
+ t .Errorf ("expected no errors, error found %+v" , err )
200
+ }
201
+
202
+ if err == nil && tc .expectErrors {
203
+ t .Errorf ("expected errors, no errors found" )
204
+ }
205
+
206
+ if err != nil && tc .expectErrors && err .Error () != tc .expectedErrorMessage {
207
+ t .Errorf ("Expected error message: \" %s\" \n Got: \" %s\" " , tc .expectedErrorMessage , err .Error ())
208
+ }
209
+ })
210
+ }
211
+ }
0 commit comments