cpsmetrics_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cpsmetrics
  14. import (
  15. "testing"
  16. "github.com/prometheus/client_golang/prometheus"
  17. "github.com/prometheus/client_golang/prometheus/testutil"
  18. v1 "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  21. "github.com/external-secrets/external-secrets/pkg/controllers/metrics"
  22. )
  23. func TestUpdateClusterPushSecretCondition(t *testing.T) {
  24. // Evacuate the original condition metric labels
  25. tmpConditionMetricLabels := metrics.ConditionMetricLabels
  26. defer func() {
  27. metrics.ConditionMetricLabels = tmpConditionMetricLabels
  28. }()
  29. metrics.ConditionMetricLabels = map[string]string{"name": "", "namespace": "", "condition": "", "status": ""}
  30. name := "test"
  31. tests := []struct {
  32. desc string
  33. condition *v1alpha1.PushSecretStatusCondition
  34. expectedCount int
  35. expectedValues []struct {
  36. labels prometheus.Labels
  37. expectedValue float64
  38. }
  39. }{
  40. {
  41. desc: "ConditionTrue",
  42. condition: &v1alpha1.PushSecretStatusCondition{
  43. Type: v1alpha1.PushSecretReady,
  44. Status: v1.ConditionTrue,
  45. },
  46. expectedValues: []struct {
  47. labels prometheus.Labels
  48. expectedValue float64
  49. }{
  50. {
  51. labels: prometheus.Labels{
  52. "namespace": "",
  53. "name": name,
  54. "condition": "Ready",
  55. "status": "True",
  56. },
  57. expectedValue: 1.0,
  58. },
  59. {
  60. labels: prometheus.Labels{
  61. "namespace": "",
  62. "name": name,
  63. "condition": "Ready",
  64. "status": "False",
  65. },
  66. expectedValue: 0.0,
  67. },
  68. },
  69. },
  70. {
  71. desc: "ConditionFalse",
  72. condition: &v1alpha1.PushSecretStatusCondition{
  73. Type: v1alpha1.PushSecretReady,
  74. Status: v1.ConditionFalse,
  75. },
  76. },
  77. }
  78. for _, test := range tests {
  79. t.Run(test.desc, func(t *testing.T) {
  80. ces := &v1alpha1.ClusterPushSecret{
  81. ObjectMeta: metav1.ObjectMeta{
  82. Name: name,
  83. },
  84. }
  85. // Evacuate the original gauge vec
  86. tmpGaugeVec := GetGaugeVec(ClusterPushSecretStatusConditionKey)
  87. defer func() {
  88. gaugeVecMetrics[ClusterPushSecretStatusConditionKey] = tmpGaugeVec
  89. }()
  90. gaugeVec := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  91. Subsystem: "psmetrics",
  92. Name: "TestUpdateClusterPushSecretCondition",
  93. }, []string{"name", "namespace", "condition", "status"})
  94. gaugeVecMetrics[ClusterPushSecretStatusConditionKey] = gaugeVec
  95. UpdateClusterPushSecretCondition(ces, test.condition)
  96. if got := testutil.CollectAndCount(gaugeVec); got != len(test.expectedValues) {
  97. t.Fatalf("unexpected number of calls: got: %d, expected: %d", got, len(test.expectedValues))
  98. }
  99. for i, expected := range test.expectedValues {
  100. if got := testutil.ToFloat64(gaugeVec.With(expected.labels)); got != expected.expectedValue {
  101. t.Fatalf("#%d received unexpected gauge value: got: %v, expected: %v", i, got, expected.expectedValue)
  102. }
  103. }
  104. })
  105. }
  106. }