|
1 | 1 | package algorithm |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "testing" |
| 5 | + |
4 | 6 | "github.com/stretchr/testify/assert" |
5 | 7 | "k8s.io/apimachinery/pkg/util/sets" |
6 | | - "testing" |
7 | 8 | ) |
8 | 9 |
|
9 | 10 | func TestMapFindFirst(t *testing.T) { |
@@ -358,3 +359,114 @@ func TestStringSetToCSV(t *testing.T) { |
358 | 359 | }) |
359 | 360 | } |
360 | 361 | } |
| 362 | + |
| 363 | +func TestRemoveKeysByPrefix(t *testing.T) { |
| 364 | + tests := []struct { |
| 365 | + name string |
| 366 | + input map[string]string |
| 367 | + prefix string |
| 368 | + want map[string]string |
| 369 | + }{ |
| 370 | + { |
| 371 | + name: "removes aws: prefixed keys", |
| 372 | + input: map[string]string{"aws:cloudformation:stack-name": "my-stack", "elbv2.k8s.aws/cluster": "my-cluster", "aws:cloudformation:logical-id": "NLB"}, |
| 373 | + prefix: "aws:", |
| 374 | + want: map[string]string{"elbv2.k8s.aws/cluster": "my-cluster"}, |
| 375 | + }, |
| 376 | + { |
| 377 | + name: "no matching prefix", |
| 378 | + input: map[string]string{"elbv2.k8s.aws/cluster": "my-cluster", "service.k8s.aws/stack": "default/svc"}, |
| 379 | + prefix: "aws:", |
| 380 | + want: map[string]string{"elbv2.k8s.aws/cluster": "my-cluster", "service.k8s.aws/stack": "default/svc"}, |
| 381 | + }, |
| 382 | + { |
| 383 | + name: "all keys match prefix", |
| 384 | + input: map[string]string{"aws:cloudformation:stack-name": "s", "aws:cloudformation:stack-id": "id"}, |
| 385 | + prefix: "aws:", |
| 386 | + want: map[string]string{}, |
| 387 | + }, |
| 388 | + { |
| 389 | + name: "empty map", |
| 390 | + input: map[string]string{}, |
| 391 | + prefix: "aws:", |
| 392 | + want: map[string]string{}, |
| 393 | + }, |
| 394 | + } |
| 395 | + for _, tt := range tests { |
| 396 | + t.Run(tt.name, func(t *testing.T) { |
| 397 | + RemoveKeysByPrefix(tt.input, tt.prefix) |
| 398 | + assert.Equal(t, tt.want, tt.input) |
| 399 | + }) |
| 400 | + } |
| 401 | +} |
| 402 | + |
| 403 | +func TestDiffStringMapIgnoreAWSTags(t *testing.T) { |
| 404 | + tests := []struct { |
| 405 | + name string |
| 406 | + desired map[string]string |
| 407 | + current map[string]string |
| 408 | + wantUpdate map[string]string |
| 409 | + wantRemove map[string]string |
| 410 | + }{ |
| 411 | + { |
| 412 | + name: "aws: tags in current are not removed", |
| 413 | + desired: map[string]string{ |
| 414 | + "elbv2.k8s.aws/cluster": "my-cluster", |
| 415 | + "service.k8s.aws/stack": "default/svc", |
| 416 | + }, |
| 417 | + current: map[string]string{ |
| 418 | + "elbv2.k8s.aws/cluster": "my-cluster", |
| 419 | + "aws:cloudformation:stack-name": "my-stack", |
| 420 | + "aws:cloudformation:stack-id": "arn:aws:cloudformation:us-east-1:123:stack/my-stack/abc", |
| 421 | + "aws:cloudformation:logical-id": "NLB", |
| 422 | + }, |
| 423 | + wantUpdate: map[string]string{ |
| 424 | + "service.k8s.aws/stack": "default/svc", |
| 425 | + }, |
| 426 | + wantRemove: map[string]string{}, |
| 427 | + }, |
| 428 | + { |
| 429 | + name: "aws: tags in desired are not added", |
| 430 | + desired: map[string]string{ |
| 431 | + "elbv2.k8s.aws/cluster": "my-cluster", |
| 432 | + "aws:createdBy": "should-be-ignored", |
| 433 | + }, |
| 434 | + current: map[string]string{ |
| 435 | + "elbv2.k8s.aws/cluster": "my-cluster", |
| 436 | + }, |
| 437 | + wantUpdate: map[string]string{}, |
| 438 | + wantRemove: map[string]string{}, |
| 439 | + }, |
| 440 | + { |
| 441 | + name: "no aws: tags behaves like DiffStringMap", |
| 442 | + desired: map[string]string{ |
| 443 | + "a": "1", |
| 444 | + "b": "2", |
| 445 | + }, |
| 446 | + current: map[string]string{ |
| 447 | + "a": "1", |
| 448 | + "c": "3", |
| 449 | + }, |
| 450 | + wantUpdate: map[string]string{ |
| 451 | + "b": "2", |
| 452 | + }, |
| 453 | + wantRemove: map[string]string{ |
| 454 | + "c": "3", |
| 455 | + }, |
| 456 | + }, |
| 457 | + { |
| 458 | + name: "both empty", |
| 459 | + desired: map[string]string{}, |
| 460 | + current: map[string]string{}, |
| 461 | + wantUpdate: map[string]string{}, |
| 462 | + wantRemove: map[string]string{}, |
| 463 | + }, |
| 464 | + } |
| 465 | + for _, tt := range tests { |
| 466 | + t.Run(tt.name, func(t *testing.T) { |
| 467 | + gotUpdate, gotRemove := DiffStringMapIgnoreAWSTags(tt.desired, tt.current) |
| 468 | + assert.Equal(t, tt.wantUpdate, gotUpdate) |
| 469 | + assert.Equal(t, tt.wantRemove, gotRemove) |
| 470 | + }) |
| 471 | + } |
| 472 | +} |
0 commit comments