cesmetrics_test.go 3.4 KB

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