|
4 | 4 | "context" |
5 | 5 | "testing" |
6 | 6 |
|
| 7 | +"github.com/stretchr/testify/assert" |
| 8 | + |
7 | 9 | "github.com/google/go-cmp/cmp" |
8 | 10 |
|
9 | 11 | "github.com/nginx/kubernetes-ingress/internal/telemetry" |
@@ -533,6 +535,210 @@ func TestGetServices(t *testing.T) { |
533 | 535 | } |
534 | 536 | } |
535 | 537 |
|
| 538 | +func TestConfigMapKeys(t *testing.T) { |
| 539 | +t.Parallel() |
| 540 | +testCases := []struct { |
| 541 | +name string |
| 542 | +config telemetry.CollectorConfig |
| 543 | +want []string |
| 544 | +}{ |
| 545 | +{ |
| 546 | +name: "ConfigMap With some keys", |
| 547 | +config: telemetry.CollectorConfig{ |
| 548 | +K8sClientReader: newTestClientset( |
| 549 | +&apiCoreV1.ConfigMap{ |
| 550 | +ObjectMeta: metaV1.ObjectMeta{ |
| 551 | +Name: "nginx-ingress", |
| 552 | +Namespace: "nginx-ingress", |
| 553 | +}, |
| 554 | +Data: map[string]string{ |
| 555 | +"proxy-buffering": "false", |
| 556 | +"zone-sync": "true", |
| 557 | +}, |
| 558 | +}, |
| 559 | +), |
| 560 | +MainConfigMapName: "nginx-ingress/nginx-ingress", |
| 561 | +}, |
| 562 | +want: []string{"proxy-buffering", "zone-sync"}, |
| 563 | +}, |
| 564 | +{ |
| 565 | +name: "ConfigMap With no keys", |
| 566 | +config: telemetry.CollectorConfig{ |
| 567 | +K8sClientReader: newTestClientset( |
| 568 | +&apiCoreV1.ConfigMap{ |
| 569 | +ObjectMeta: metaV1.ObjectMeta{ |
| 570 | +Name: "nginx-ingress", |
| 571 | +Namespace: "nginx-ingress", |
| 572 | +}, |
| 573 | +Data: map[string]string{}, |
| 574 | +}, |
| 575 | +), |
| 576 | +MainConfigMapName: "nginx-ingress/nginx-ingress", |
| 577 | +}, |
| 578 | +want: nil, |
| 579 | +}, |
| 580 | +{ |
| 581 | +name: "ConfigMap With ignored keys", |
| 582 | +config: telemetry.CollectorConfig{ |
| 583 | +K8sClientReader: newTestClientset( |
| 584 | +&apiCoreV1.ConfigMap{ |
| 585 | +ObjectMeta: metaV1.ObjectMeta{ |
| 586 | +Name: "nginx-ingress", |
| 587 | +Namespace: "nginx-ingress", |
| 588 | +}, |
| 589 | +Data: map[string]string{ |
| 590 | +"enforce-initial-report": "false", |
| 591 | +"sync": "hello", |
| 592 | +"hello": "world", |
| 593 | +}, |
| 594 | +}, |
| 595 | +), |
| 596 | +MainConfigMapName: "nginx-ingress/nginx-ingress", |
| 597 | +}, |
| 598 | +want: nil, |
| 599 | +}, |
| 600 | +{ |
| 601 | +name: "ConfigMap With ignored keys and valid keys", |
| 602 | +config: telemetry.CollectorConfig{ |
| 603 | +K8sClientReader: newTestClientset( |
| 604 | +&apiCoreV1.ConfigMap{ |
| 605 | +ObjectMeta: metaV1.ObjectMeta{ |
| 606 | +Name: "nginx-ingress", |
| 607 | +Namespace: "nginx-ingress", |
| 608 | +}, |
| 609 | +Data: map[string]string{ |
| 610 | +"proxy-buffering": "false", |
| 611 | +"enforce-initial-report": "false", |
| 612 | +"sync": "hello", |
| 613 | +"hello": "world", |
| 614 | +"zone-sync": "true", |
| 615 | +}, |
| 616 | +}, |
| 617 | +), |
| 618 | +MainConfigMapName: "nginx-ingress/nginx-ingress", |
| 619 | +}, |
| 620 | +want: []string{"proxy-buffering", "zone-sync"}, |
| 621 | +}, |
| 622 | +} |
| 623 | + |
| 624 | +for _, tc := range testCases { |
| 625 | +t.Run(tc.name, func(t *testing.T) { |
| 626 | +c, err := telemetry.NewCollector(tc.config) |
| 627 | +if err != nil { |
| 628 | +t.Fatal(err) |
| 629 | +} |
| 630 | +got, err := c.ConfigMapKeys(context.Background()) |
| 631 | +if err != nil { |
| 632 | +t.Fatal(err) |
| 633 | +} |
| 634 | +if !assert.ElementsMatch(t, tc.want, got, "MGMTConfigMap keys do not match") { |
| 635 | +t.Error(cmp.Diff(tc.want, got)) |
| 636 | +} |
| 637 | +}) |
| 638 | +} |
| 639 | +} |
| 640 | + |
| 641 | +func TestMGMTConfigMapKeys(t *testing.T) { |
| 642 | +t.Parallel() |
| 643 | +testCases := []struct { |
| 644 | +name string |
| 645 | +config telemetry.CollectorConfig |
| 646 | +want []string |
| 647 | +}{ |
| 648 | +{ |
| 649 | +name: "MGMTConfigMap With some keys", |
| 650 | +config: telemetry.CollectorConfig{ |
| 651 | +K8sClientReader: newTestClientset( |
| 652 | +&apiCoreV1.ConfigMap{ |
| 653 | +ObjectMeta: metaV1.ObjectMeta{ |
| 654 | +Name: "nginx-ingress-mgmt", |
| 655 | +Namespace: "nginx-ingress", |
| 656 | +}, |
| 657 | +Data: map[string]string{ |
| 658 | +"enforce-initial-report": "false", |
| 659 | +"license-token-secret-name": "license-token", |
| 660 | +}, |
| 661 | +}, |
| 662 | +), |
| 663 | +MGMTConfigMapName: "nginx-ingress/nginx-ingress-mgmt", |
| 664 | +}, |
| 665 | +want: []string{"enforce-initial-report", "license-token-secret-name"}, |
| 666 | +}, |
| 667 | +{ |
| 668 | +name: "MGMTConfigMap With no keys", |
| 669 | +config: telemetry.CollectorConfig{ |
| 670 | +K8sClientReader: newTestClientset( |
| 671 | +&apiCoreV1.ConfigMap{ |
| 672 | +ObjectMeta: metaV1.ObjectMeta{ |
| 673 | +Name: "nginx-ingress-mgmt", |
| 674 | +Namespace: "nginx-ingress", |
| 675 | +}, |
| 676 | +Data: map[string]string{}, |
| 677 | +}, |
| 678 | +), |
| 679 | +MGMTConfigMapName: "nginx-ingress/nginx-ingress-mgmt", |
| 680 | +}, |
| 681 | +want: nil, |
| 682 | +}, |
| 683 | +{ |
| 684 | +name: "MGMTConfigMap With ignored keys", |
| 685 | +config: telemetry.CollectorConfig{ |
| 686 | +K8sClientReader: newTestClientset( |
| 687 | +&apiCoreV1.ConfigMap{ |
| 688 | +ObjectMeta: metaV1.ObjectMeta{ |
| 689 | +Name: "nginx-ingress-mgmt", |
| 690 | +Namespace: "nginx-ingress", |
| 691 | +}, |
| 692 | +Data: map[string]string{ |
| 693 | +"zone-sync": "false", |
| 694 | +"license": "test", |
| 695 | +}, |
| 696 | +}, |
| 697 | +), |
| 698 | +MGMTConfigMapName: "nginx-ingress/nginx-ingress-mgmt", |
| 699 | +}, |
| 700 | +want: nil, |
| 701 | +}, |
| 702 | +{ |
| 703 | +name: "MGMTConfigMap With ignored keys and valid keys", |
| 704 | +config: telemetry.CollectorConfig{ |
| 705 | +K8sClientReader: newTestClientset( |
| 706 | +&apiCoreV1.ConfigMap{ |
| 707 | +ObjectMeta: metaV1.ObjectMeta{ |
| 708 | +Name: "nginx-ingress-mgmt", |
| 709 | +Namespace: "nginx-ingress", |
| 710 | +}, |
| 711 | +Data: map[string]string{ |
| 712 | +"zone-sync": "false", |
| 713 | +"license": "test", |
| 714 | +"enforce-initial-report": "false", |
| 715 | +"license-token-secret-name": "license-token", |
| 716 | +}, |
| 717 | +}, |
| 718 | +), |
| 719 | +MGMTConfigMapName: "nginx-ingress/nginx-ingress-mgmt", |
| 720 | +}, |
| 721 | +want: []string{"enforce-initial-report", "license-token-secret-name"}, |
| 722 | +}, |
| 723 | +} |
| 724 | + |
| 725 | +for _, tc := range testCases { |
| 726 | +t.Run(tc.name, func(t *testing.T) { |
| 727 | +c, err := telemetry.NewCollector(tc.config) |
| 728 | +if err != nil { |
| 729 | +t.Fatal(err) |
| 730 | +} |
| 731 | +got, err := c.MGMTConfigMapKeys(context.Background()) |
| 732 | +if err != nil { |
| 733 | +t.Fatal(err) |
| 734 | +} |
| 735 | +if !assert.ElementsMatch(t, tc.want, got, "MGMTConfigMap keys do not match") { |
| 736 | +t.Error(cmp.Diff(tc.want, got)) |
| 737 | +} |
| 738 | +}) |
| 739 | +} |
| 740 | +} |
| 741 | + |
536 | 742 | // newTestCollectorForClusterWithNodes returns a telemetry collector configured |
537 | 743 | // to simulate collecting data on a cluser with provided nodes. |
538 | 744 | func newTestCollectorForClusterWithNodes(t *testing.T, nodes ...runtime.Object) *telemetry.Collector { |
|
0 commit comments