collectorprovider_test.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package collector
  2. import (
  3. "reflect"
  4. "testing"
  5. "time"
  6. "github.com/opencost/opencost/modules/collector-source/pkg/util"
  7. )
  8. func Test_repoStoreProvider_getStoreKeys(t *testing.T) {
  9. defaultResConfigs := []util.ResolutionConfiguration{
  10. {
  11. Interval: "10m",
  12. },
  13. {
  14. Interval: "1h",
  15. },
  16. {
  17. Interval: "1d",
  18. },
  19. }
  20. tests := map[string]struct {
  21. configs []util.ResolutionConfiguration
  22. start time.Time
  23. end time.Time
  24. intevalKey string
  25. startKey time.Time
  26. }{
  27. "10m": {
  28. configs: defaultResConfigs,
  29. start: time.Date(2025, time.May, 3, 0, 0, 0, 0, time.UTC),
  30. end: time.Date(2025, time.May, 3, 0, 10, 0, 0, time.UTC),
  31. intevalKey: "10m",
  32. startKey: time.Date(2025, time.May, 3, 0, 0, 0, 0, time.UTC),
  33. },
  34. "1h": {
  35. configs: defaultResConfigs,
  36. start: time.Date(2025, time.May, 3, 0, 0, 0, 0, time.UTC),
  37. end: time.Date(2025, time.May, 3, 1, 0, 0, 0, time.UTC),
  38. intevalKey: "1h",
  39. startKey: time.Date(2025, time.May, 3, 0, 0, 0, 0, time.UTC),
  40. },
  41. "1d": {
  42. configs: defaultResConfigs,
  43. start: time.Date(2025, time.May, 3, 0, 0, 0, 0, time.UTC),
  44. end: time.Date(2025, time.May, 4, 0, 10, 0, 0, time.UTC),
  45. intevalKey: "1d",
  46. startKey: time.Date(2025, time.May, 3, 0, 0, 0, 0, time.UTC),
  47. },
  48. "2m": {
  49. configs: defaultResConfigs,
  50. start: time.Date(2025, time.May, 3, 0, 0, 0, 0, time.UTC),
  51. end: time.Date(2025, time.May, 3, 0, 2, 0, 0, time.UTC),
  52. intevalKey: "10m",
  53. startKey: time.Date(2025, time.May, 3, 0, 0, 0, 0, time.UTC),
  54. },
  55. "2m offset": {
  56. configs: defaultResConfigs,
  57. start: time.Date(2025, time.May, 3, 0, 9, 0, 0, time.UTC),
  58. end: time.Date(2025, time.May, 3, 0, 11, 0, 0, time.UTC),
  59. intevalKey: "10m",
  60. startKey: time.Date(2025, time.May, 3, 0, 0, 0, 0, time.UTC),
  61. },
  62. }
  63. for name, tt := range tests {
  64. t.Run(name, func(t *testing.T) {
  65. r := newRepoStoreProvider(nil, tt.configs)
  66. intevalKey, startKey := r.getStoreKeys(tt.start, tt.end)
  67. if intevalKey != tt.intevalKey {
  68. t.Errorf("getStoreKeys() got = %v, want %v", intevalKey, tt.intevalKey)
  69. }
  70. if !reflect.DeepEqual(startKey, tt.startKey) {
  71. t.Errorf("getStoreKeys() got1 = %v, want %v", startKey, tt.startKey)
  72. }
  73. })
  74. }
  75. }