metrics_test.go 3.7 KB

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