metrics_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 provider
  14. import (
  15. "testing"
  16. "github.com/prometheus/client_golang/prometheus"
  17. "github.com/prometheus/client_golang/prometheus/testutil"
  18. ctrlmetrics "github.com/external-secrets/external-secrets/pkg/controllers/metrics"
  19. )
  20. func TestUpdateStatusCondition(t *testing.T) {
  21. tmpConditionMetricLabels := ctrlmetrics.ConditionMetricLabels
  22. defer func() {
  23. ctrlmetrics.ConditionMetricLabels = tmpConditionMetricLabels
  24. }()
  25. ctrlmetrics.ConditionMetricLabels = map[string]string{"name": "", "namespace": "", "condition": "", "status": ""}
  26. tmpGaugeVecMetrics := gaugeVecMetrics
  27. defer func() {
  28. gaugeVecMetrics = tmpGaugeVecMetrics
  29. }()
  30. conditionGauge := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  31. Subsystem: "provider",
  32. Name: "status_condition_test",
  33. }, []string{"name", "namespace", "condition", "status"})
  34. gaugeVecMetrics = map[string]*prometheus.GaugeVec{
  35. StatusConditionKey: conditionGauge,
  36. }
  37. UpdateStatusCondition("aws-prod", "tenant-a", map[string]string{"team": "platform"}, "Ready", "True")
  38. if got := testutil.CollectAndCount(conditionGauge); got != 2 {
  39. t.Fatalf("unexpected number of condition samples: got %d, want 2", got)
  40. }
  41. if got := testutil.ToFloat64(conditionGauge.With(prometheus.Labels{
  42. "name": "aws-prod",
  43. "namespace": "tenant-a",
  44. "condition": "Ready",
  45. "status": "True",
  46. })); got != 1 {
  47. t.Fatalf("unexpected Ready=True value: got %v, want 1", got)
  48. }
  49. if got := testutil.ToFloat64(conditionGauge.With(prometheus.Labels{
  50. "name": "aws-prod",
  51. "namespace": "tenant-a",
  52. "condition": "Ready",
  53. "status": "False",
  54. })); got != 0 {
  55. t.Fatalf("unexpected Ready=False value: got %v, want 0", got)
  56. }
  57. }
  58. func TestRecordReconcileDuration(t *testing.T) {
  59. tmpNonConditionMetricLabels := ctrlmetrics.NonConditionMetricLabels
  60. defer func() {
  61. ctrlmetrics.NonConditionMetricLabels = tmpNonConditionMetricLabels
  62. }()
  63. ctrlmetrics.NonConditionMetricLabels = map[string]string{"name": "", "namespace": ""}
  64. tmpGaugeVecMetrics := gaugeVecMetrics
  65. defer func() {
  66. gaugeVecMetrics = tmpGaugeVecMetrics
  67. }()
  68. durationGauge := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  69. Subsystem: "provider",
  70. Name: "reconcile_duration_test",
  71. }, []string{"name", "namespace"})
  72. gaugeVecMetrics = map[string]*prometheus.GaugeVec{
  73. ProviderReconcileDurationKey: durationGauge,
  74. }
  75. RecordReconcileDuration("aws-prod", "tenant-a", map[string]string{"team": "platform"}, 2.5)
  76. if got := testutil.ToFloat64(durationGauge.With(prometheus.Labels{
  77. "name": "aws-prod",
  78. "namespace": "tenant-a",
  79. })); got != 2.5 {
  80. t.Fatalf("unexpected reconcile duration: got %v, want 2.5", got)
  81. }
  82. }