metrics_test.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 clusterprovider
  14. import (
  15. "testing"
  16. "github.com/prometheus/client_golang/prometheus"
  17. "github.com/prometheus/client_golang/prometheus/testutil"
  18. )
  19. func TestUpdateStatusCondition(t *testing.T) {
  20. tmpGaugeVecMetrics := gaugeVecMetrics
  21. defer func() {
  22. gaugeVecMetrics = tmpGaugeVecMetrics
  23. }()
  24. conditionGauge := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  25. Subsystem: "clusterprovider",
  26. Name: "status_condition_test",
  27. }, []string{"name", "condition", "status"})
  28. gaugeVecMetrics = map[string]*prometheus.GaugeVec{
  29. StatusConditionKey: conditionGauge,
  30. }
  31. UpdateStatusCondition("aws-shared", "Ready", "False")
  32. if got := testutil.CollectAndCount(conditionGauge); got != 2 {
  33. t.Fatalf("unexpected number of condition samples: got %d, want 2", got)
  34. }
  35. if got := testutil.ToFloat64(conditionGauge.With(prometheus.Labels{
  36. "name": "aws-shared",
  37. "condition": "Ready",
  38. "status": "True",
  39. })); got != 0 {
  40. t.Fatalf("unexpected Ready=True value: got %v, want 0", got)
  41. }
  42. if got := testutil.ToFloat64(conditionGauge.With(prometheus.Labels{
  43. "name": "aws-shared",
  44. "condition": "Ready",
  45. "status": "False",
  46. })); got != 1 {
  47. t.Fatalf("unexpected Ready=False value: got %v, want 1", got)
  48. }
  49. }
  50. func TestRecordReconcileDuration(t *testing.T) {
  51. tmpGaugeVecMetrics := gaugeVecMetrics
  52. defer func() {
  53. gaugeVecMetrics = tmpGaugeVecMetrics
  54. }()
  55. durationGauge := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  56. Subsystem: "clusterprovider",
  57. Name: "reconcile_duration_test",
  58. }, []string{"name"})
  59. gaugeVecMetrics = map[string]*prometheus.GaugeVec{
  60. ClusterProviderReconcileDurationKey: durationGauge,
  61. }
  62. RecordReconcileDuration("aws-shared", 1.75)
  63. if got := testutil.ToFloat64(durationGauge.With(prometheus.Labels{
  64. "name": "aws-shared",
  65. })); got != 1.75 {
  66. t.Fatalf("unexpected reconcile duration: got %v, want 1.75", got)
  67. }
  68. }