1
0
Fork 0
mirror of https://github.com/external-secrets/external-secrets.git synced 2024-12-14 11:57:59 +00:00
external-secrets/pkg/controllers/metrics/labels_test.go
Shuhei Kitagawa d879f37d9e
Add reconcile duration metric for ClusterExternalSecret controller (#2334)
* Add reconcile duration metric for ClusterExternalSecret controller

Signed-off-by: shuheiktgw <s-kitagawa@mercari.com>

* chore: fmt imports

Signed-off-by: Moritz Johner <beller.moritz@googlemail.com>

---------

Signed-off-by: shuheiktgw <s-kitagawa@mercari.com>
Signed-off-by: Moritz Johner <beller.moritz@googlemail.com>
Co-authored-by: Moritz Johner <beller.moritz@googlemail.com>
2023-05-22 22:43:23 +02:00

199 lines
5.7 KiB
Go

/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package metrics
import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/prometheus/client_golang/prometheus"
)
func TestSetUpLabelNames(t *testing.T) {
testCases := []struct {
description string
addKubeStandardLabels bool
expectedNonConditionMetricLabelNames []string
expectedConditionMetricLabelNames []string
expectedNonConditionMetricLabels map[string]string
expectedConditionMetricLabels map[string]string
}{
{
description: "Add standard labels disabled",
addKubeStandardLabels: false,
expectedNonConditionMetricLabelNames: []string{
"name",
"namespace",
},
expectedConditionMetricLabelNames: []string{
"name",
"namespace",
"condition",
"status",
},
expectedNonConditionMetricLabels: map[string]string{
"name": "",
"namespace": "",
},
expectedConditionMetricLabels: map[string]string{
"name": "",
"namespace": "",
"condition": "",
"status": "",
},
},
{
description: "Add standard labels enabled",
addKubeStandardLabels: true,
expectedNonConditionMetricLabelNames: []string{
"name",
"namespace",
"app_kubernetes_io_name",
"app_kubernetes_io_instance",
"app_kubernetes_io_version",
"app_kubernetes_io_component",
"app_kubernetes_io_part_of",
"app_kubernetes_io_managed_by",
},
expectedConditionMetricLabelNames: []string{
"name",
"namespace",
"condition",
"status",
"app_kubernetes_io_name",
"app_kubernetes_io_instance",
"app_kubernetes_io_version",
"app_kubernetes_io_component",
"app_kubernetes_io_part_of",
"app_kubernetes_io_managed_by",
},
expectedNonConditionMetricLabels: map[string]string{
"name": "",
"namespace": "",
"app_kubernetes_io_name": "",
"app_kubernetes_io_instance": "",
"app_kubernetes_io_version": "",
"app_kubernetes_io_component": "",
"app_kubernetes_io_part_of": "",
"app_kubernetes_io_managed_by": "",
},
expectedConditionMetricLabels: map[string]string{
"name": "",
"namespace": "",
"condition": "",
"status": "",
"app_kubernetes_io_name": "",
"app_kubernetes_io_instance": "",
"app_kubernetes_io_version": "",
"app_kubernetes_io_component": "",
"app_kubernetes_io_part_of": "",
"app_kubernetes_io_managed_by": "",
},
},
}
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
SetUpLabelNames(tc.addKubeStandardLabels)
if diff := cmp.Diff(NonConditionMetricLabelNames, tc.expectedNonConditionMetricLabelNames); diff != "" {
t.Errorf("NonConditionMetricLabelNames does not match the expected value. (-got +want)\n%s", diff)
}
if diff := cmp.Diff(ConditionMetricLabelNames, tc.expectedConditionMetricLabelNames); diff != "" {
t.Errorf("ConditionMetricLabelNames does not match the expected value. (-got +want)\n%s", diff)
}
if diff := cmp.Diff(NonConditionMetricLabels, tc.expectedNonConditionMetricLabels); diff != "" {
t.Errorf("NonConditionMetricLabels are not initialized with empty strings. (-got +want)\n%s", diff)
}
if diff := cmp.Diff(ConditionMetricLabels, tc.expectedConditionMetricLabels); diff != "" {
t.Errorf("ConditionMetricLabels are not initialized with empty strings. (-got +want)\n%s", diff)
}
})
}
}
func TestRefineLabels(t *testing.T) {
testCases := []struct {
description string
promLabels prometheus.Labels
newLabels map[string]string
expectedRefinement prometheus.Labels
}{
{
description: "No new labels",
promLabels: prometheus.Labels{
"label1": "value1",
"label2": "value2",
},
newLabels: map[string]string{},
expectedRefinement: prometheus.Labels{"label1": "value1", "label2": "value2"},
},
{
description: "Add unregistered labels",
promLabels: prometheus.Labels{
"label1": "value1",
"label2": "value2",
},
newLabels: map[string]string{
"new_label1": "new_value1",
"new_label2": "new_value2",
},
expectedRefinement: prometheus.Labels{
"label1": "value1",
"label2": "value2",
},
},
{
description: "Overwrite existing labels",
promLabels: prometheus.Labels{
"label1": "value1",
"label2": "value2",
},
newLabels: map[string]string{
"label1": "new_value1",
"label2": "new_value2",
},
expectedRefinement: prometheus.Labels{
"label1": "new_value1",
"label2": "new_value2",
},
},
{
description: "Clean non-alphanumeric characters in new labels",
promLabels: prometheus.Labels{
"label_1": "value1",
},
newLabels: map[string]string{
"label@1": "new_value",
},
expectedRefinement: prometheus.Labels{
"label_1": "new_value",
},
},
}
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
refinement := RefineLabels(tc.promLabels, tc.newLabels)
if diff := cmp.Diff(refinement, tc.expectedRefinement); diff != "" {
t.Errorf("Refinement does not match the expected value. (-got +want)\n%s", diff)
}
})
}
}