metrics_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. Copyright © The ESO Authors
  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 metrics
  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. esapi "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  21. "github.com/external-secrets/external-secrets/pkg/controllers/metrics"
  22. )
  23. func TestUpdateStatusCondition(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. namespace := "test-namespace"
  32. tests := []struct {
  33. desc string
  34. condition esapi.SecretStoreStatusCondition
  35. expectedCount int
  36. expectedValues []struct {
  37. labels prometheus.Labels
  38. expectedValue float64
  39. }
  40. }{
  41. {
  42. desc: "ConditionTrue",
  43. condition: esapi.SecretStoreStatusCondition{
  44. Type: esapi.SecretStoreReady,
  45. Status: v1.ConditionTrue,
  46. },
  47. expectedValues: []struct {
  48. labels prometheus.Labels
  49. expectedValue float64
  50. }{
  51. {
  52. labels: prometheus.Labels{
  53. "namespace": namespace,
  54. "name": name,
  55. "condition": "Ready",
  56. "status": "True",
  57. },
  58. expectedValue: 1,
  59. },
  60. {
  61. labels: prometheus.Labels{
  62. "namespace": namespace,
  63. "name": name,
  64. "condition": "Ready",
  65. "status": "False",
  66. },
  67. expectedValue: 0,
  68. },
  69. },
  70. },
  71. {
  72. desc: "ConditionFalse",
  73. condition: esapi.SecretStoreStatusCondition{
  74. Type: esapi.SecretStoreReady,
  75. Status: v1.ConditionFalse,
  76. },
  77. expectedValues: []struct {
  78. labels prometheus.Labels
  79. expectedValue float64
  80. }{
  81. {
  82. labels: prometheus.Labels{
  83. "namespace": namespace,
  84. "name": name,
  85. "condition": "Ready",
  86. "status": "True",
  87. },
  88. expectedValue: 0,
  89. },
  90. {
  91. labels: prometheus.Labels{
  92. "namespace": namespace,
  93. "name": name,
  94. "condition": "Ready",
  95. "status": "False",
  96. },
  97. expectedValue: 1,
  98. },
  99. },
  100. },
  101. }
  102. for _, test := range tests {
  103. t.Run(test.desc, func(t *testing.T) {
  104. ss := &esapi.SecretStore{
  105. ObjectMeta: metav1.ObjectMeta{
  106. Name: name,
  107. Namespace: namespace,
  108. },
  109. }
  110. gaugeVec := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  111. Subsystem: "metrics",
  112. Name: "TestUpdateStatusCondition",
  113. }, []string{"name", "namespace", "condition", "status"})
  114. getter := func(key string) *prometheus.GaugeVec {
  115. if key == StatusConditionKey {
  116. return gaugeVec
  117. }
  118. return nil
  119. }
  120. UpdateStatusCondition(ss, test.condition, getter)
  121. if got := testutil.CollectAndCount(gaugeVec); got != len(test.expectedValues) {
  122. t.Fatalf("unexpected number of calls: got: %d, expected: %d", got, len(test.expectedValues))
  123. }
  124. for i, expected := range test.expectedValues {
  125. if got := testutil.ToFloat64(gaugeVec.With(expected.labels)); got != expected.expectedValue {
  126. t.Fatalf("#%d received unexpected gauge value: got: %v, expected: %v", i, got, expected.expectedValue)
  127. }
  128. }
  129. })
  130. }
  131. }