repository_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package metric
  2. import (
  3. "reflect"
  4. "testing"
  5. "time"
  6. "github.com/opencost/opencost/core/pkg/storage"
  7. "github.com/opencost/opencost/core/pkg/util/timeutil"
  8. "github.com/opencost/opencost/modules/collector-source/pkg/metric/aggregator"
  9. "github.com/opencost/opencost/modules/collector-source/pkg/util"
  10. )
  11. const TestActiveMinutesID = "TestActiveMinutes"
  12. const TestAverageID = "TestAverage"
  13. const TestMetric = "test_metric"
  14. func testMetricCollector() MetricStore {
  15. memStore := NewInMemoryMetricStore()
  16. memStore.Register(NewMetricCollector(
  17. TestActiveMinutesID,
  18. TestMetric,
  19. []string{
  20. "test",
  21. },
  22. aggregator.ActiveMinutes,
  23. nil,
  24. ))
  25. memStore.Register(NewMetricCollector(
  26. TestAverageID,
  27. TestMetric,
  28. []string{
  29. "test",
  30. },
  31. aggregator.AverageOverTime,
  32. nil,
  33. ))
  34. return memStore
  35. }
  36. func TestNewMetricRepository_DisasterRecovery(t *testing.T) {
  37. time3 := time.Now().UTC().Truncate(timeutil.Day)
  38. time2 := time3.Add(-12 * time.Hour)
  39. time1 := time3.Add(-timeutil.Day)
  40. store := storage.NewMemoryStorage()
  41. repo := NewMetricRepository(
  42. "test",
  43. []util.ResolutionConfiguration{
  44. {
  45. Interval: "1d",
  46. Retention: 3,
  47. },
  48. },
  49. store,
  50. testMetricCollector,
  51. )
  52. inputUpdateSet1 := UpdateSet{
  53. Updates: []Update{
  54. {
  55. Name: TestMetric,
  56. Labels: map[string]string{
  57. "test": "test",
  58. },
  59. Value: 1,
  60. AdditionalInfo: nil,
  61. },
  62. },
  63. }
  64. inputUpdateSet2 := UpdateSet{
  65. Updates: []Update{
  66. {
  67. Name: TestMetric,
  68. Labels: map[string]string{
  69. "test": "test",
  70. },
  71. Value: 2,
  72. AdditionalInfo: nil,
  73. },
  74. },
  75. }
  76. inputUpdateSet3 := UpdateSet{
  77. Updates: []Update{
  78. {
  79. Name: TestMetric,
  80. Labels: map[string]string{
  81. "test": "test",
  82. },
  83. Value: 3,
  84. AdditionalInfo: nil,
  85. },
  86. },
  87. }
  88. repo.Update(inputUpdateSet1.Updates, time1)
  89. repo.Update(inputUpdateSet2.Updates, time2)
  90. repo.Update(inputUpdateSet3.Updates, time3)
  91. repo2 := NewMetricRepository(
  92. "test",
  93. []util.ResolutionConfiguration{
  94. {
  95. Interval: "1d",
  96. Retention: 3,
  97. },
  98. },
  99. store,
  100. testMetricCollector,
  101. )
  102. collector1, err := repo.GetCollector("1d", time3)
  103. if err != nil {
  104. t.Fatalf("failed to get collector from repo1: %s", err.Error())
  105. }
  106. activeMinutesRes1, err := collector1.Query(TestActiveMinutesID)
  107. if err != nil {
  108. t.Fatalf("failed to query %s from repo1: %s", TestActiveMinutesID, err.Error())
  109. }
  110. averageRes1, err := collector1.Query(TestAverageID)
  111. if err != nil {
  112. t.Fatalf("failed to query %s from repo1: %s", TestAverageID, err.Error())
  113. }
  114. collector2, err := repo2.GetCollector("1d", time3)
  115. if err != nil {
  116. t.Fatalf("failed to get collector from repo2: %s", err.Error())
  117. }
  118. activeMinutesRes2, err := collector2.Query(TestActiveMinutesID)
  119. if err != nil {
  120. t.Fatalf("failed to query %s from repo2: %s", TestActiveMinutesID, err.Error())
  121. }
  122. averageRes2, err := collector2.Query(TestAverageID)
  123. if err != nil {
  124. t.Fatalf("failed to query %s from repo2: %s", TestAverageID, err.Error())
  125. }
  126. if !reflect.DeepEqual(activeMinutesRes1, activeMinutesRes2) {
  127. t.Errorf("active minute query results did not match 1: %v, 2: %v", activeMinutesRes1, activeMinutesRes2)
  128. }
  129. if !reflect.DeepEqual(averageRes1, averageRes2) {
  130. t.Errorf("average query results did not match 1: %v, 2: %v", averageRes1, averageRes2)
  131. }
  132. }