|
1 | 1 | package targetconfigcontroller |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "strings" |
5 | 6 | "testing" |
6 | 7 |
|
@@ -110,7 +111,7 @@ func TestIsRequiredConfigPresent(t *testing.T) { |
110 | 111 |
|
111 | 112 | for _, test := range tests { |
112 | 113 | t.Run(test.name, func(t *testing.T) { |
113 | | - actual := isRequiredConfigPresent([]byte(test.config)) |
| 114 | + actual := isRequiredConfigPresent([]byte(test.config), false) |
114 | 115 | switch { |
115 | 116 | case actual == nil && len(test.expectedError) == 0: |
116 | 117 | case actual == nil && len(test.expectedError) != 0: |
@@ -237,3 +238,101 @@ func TestManageTemplate(t *testing.T) { |
237 | 238 | }) |
238 | 239 | } |
239 | 240 | } |
| 241 | + |
| 242 | +func TestIsRequiredConfigPresentEtcdEndpoints(t *testing.T) { |
| 243 | + configTemplate := `{ |
| 244 | + "servingInfo": { |
| 245 | + "namedCertificates": [ |
| 246 | + { |
| 247 | + "certFile": "/etc/kubernetes/static-pod-certs/secrets/localhost-serving-cert-certkey/tls.crt", |
| 248 | + "keyFile": "/etc/kubernetes/static-pod-certs/secrets/localhost-serving-cert-certkey/tls.key" |
| 249 | + } |
| 250 | + ] |
| 251 | + }, |
| 252 | + "admission": {"pluginConfig": { "network.openshift.io/RestrictedEndpointsAdmission": {}}}, |
| 253 | + "apiServerArguments": { |
| 254 | + "etcd-servers": %s |
| 255 | + } |
| 256 | + } |
| 257 | + ` |
| 258 | + tests := []struct { |
| 259 | + name string |
| 260 | + etcdServers string |
| 261 | + expectedError string |
| 262 | + isNotSingleNode bool |
| 263 | + }{ |
| 264 | + { |
| 265 | + name: "nil-storage-urls", |
| 266 | + etcdServers: "null", |
| 267 | + expectedError: "apiServerArguments.etcd-servers null in config", |
| 268 | + }, |
| 269 | + { |
| 270 | + name: "missing-storage-urls", |
| 271 | + etcdServers: "[]", |
| 272 | + expectedError: "apiServerArguments.etcd-servers empty in config", |
| 273 | + }, |
| 274 | + { |
| 275 | + name: "empty-string-storage-urls", |
| 276 | + etcdServers: `""`, |
| 277 | + expectedError: "apiServerArguments.etcd-servers empty in config", |
| 278 | + }, |
| 279 | + { |
| 280 | + name: "one-etcd-server", |
| 281 | + etcdServers: `[ "val" ]`, |
| 282 | + isNotSingleNode: true, |
| 283 | + expectedError: "apiServerArguments.etcd-servers has less than three endpoints", |
| 284 | + }, |
| 285 | + { |
| 286 | + name: "one-etcd-server-sno", |
| 287 | + etcdServers: `[ "val" ]`, |
| 288 | + isNotSingleNode: false, |
| 289 | + }, |
| 290 | + { |
| 291 | + name: "two-etcd-servers", |
| 292 | + etcdServers: `[ "val1", "val2" ]`, |
| 293 | + isNotSingleNode: true, |
| 294 | + expectedError: "apiServerArguments.etcd-servers has less than three endpoints", |
| 295 | + }, |
| 296 | + { |
| 297 | + name: "two-etcd-servers-sno", |
| 298 | + etcdServers: `[ "val1", "val2" ]`, |
| 299 | + isNotSingleNode: false, |
| 300 | + }, |
| 301 | + { |
| 302 | + name: "three-etcd-servers", |
| 303 | + etcdServers: `[ "val1", "val2", "val3" ]`, |
| 304 | + isNotSingleNode: true, |
| 305 | + }, |
| 306 | + { |
| 307 | + name: "three-etcd-servers-sno", |
| 308 | + etcdServers: `[ "val1", "val2", "val3" ]`, |
| 309 | + isNotSingleNode: false, |
| 310 | + }, |
| 311 | + { |
| 312 | + name: "four-etcd-servers", |
| 313 | + etcdServers: `[ "val1", "val2", "val3", "val4" ]`, |
| 314 | + isNotSingleNode: true, |
| 315 | + }, |
| 316 | + { |
| 317 | + name: "four-etcd-servers-sno", |
| 318 | + etcdServers: `[ "val1", "val2", "val3", "val4" ]`, |
| 319 | + isNotSingleNode: false, |
| 320 | + }, |
| 321 | + } |
| 322 | + |
| 323 | + for _, test := range tests { |
| 324 | + t.Run(test.name, func(t *testing.T) { |
| 325 | + config := fmt.Sprintf(configTemplate, test.etcdServers) |
| 326 | + actual := isRequiredConfigPresent([]byte(config), test.isNotSingleNode) |
| 327 | + switch { |
| 328 | + case actual == nil && len(test.expectedError) == 0: |
| 329 | + case actual == nil && len(test.expectedError) != 0: |
| 330 | + t.Fatal(actual) |
| 331 | + case actual != nil && len(test.expectedError) == 0: |
| 332 | + t.Fatal(actual) |
| 333 | + case actual != nil && len(test.expectedError) != 0 && !strings.Contains(actual.Error(), test.expectedError): |
| 334 | + t.Fatal(actual) |
| 335 | + } |
| 336 | + }) |
| 337 | + } |
| 338 | +} |
0 commit comments