path_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package pathing
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/opencost/opencost/core/pkg/opencost"
  6. )
  7. func TestBingenPathFormatter(t *testing.T) {
  8. type testCase struct {
  9. name string
  10. rootPath string
  11. clusterID string
  12. pipeline string
  13. resolution *time.Duration
  14. prefix string
  15. expected string
  16. }
  17. testCases := []testCase{
  18. {
  19. name: "no resolution",
  20. rootPath: "",
  21. clusterID: "cluster-a",
  22. pipeline: "allocation",
  23. resolution: nil,
  24. prefix: "",
  25. expected: "federated/cluster-a/etl/bingen/allocation/1704110400-1704114000",
  26. },
  27. {
  28. name: "with resolution",
  29. rootPath: "",
  30. clusterID: "cluster-a",
  31. pipeline: "allocation",
  32. resolution: &[]time.Duration{1 * time.Hour}[0],
  33. prefix: "",
  34. expected: "federated/cluster-a/etl/bingen/allocation/1h/1704110400-1704114000",
  35. },
  36. {
  37. name: "no resolution with prefix",
  38. rootPath: "",
  39. clusterID: "cluster-a",
  40. pipeline: "allocation",
  41. resolution: nil,
  42. prefix: "test",
  43. expected: "federated/cluster-a/etl/bingen/allocation/test.1704110400-1704114000",
  44. },
  45. {
  46. name: "with resolution with prefix",
  47. rootPath: "",
  48. clusterID: "cluster-a",
  49. pipeline: "allocation",
  50. resolution: &[]time.Duration{1 * time.Hour}[0],
  51. prefix: "test",
  52. expected: "federated/cluster-a/etl/bingen/allocation/1h/test.1704110400-1704114000",
  53. },
  54. {
  55. name: "daily resolution",
  56. rootPath: "",
  57. clusterID: "cluster-a",
  58. pipeline: "allocation",
  59. resolution: &[]time.Duration{24 * time.Hour}[0],
  60. prefix: "",
  61. expected: "federated/cluster-a/etl/bingen/allocation/1d/1704110400-1704196800",
  62. },
  63. {
  64. name: "weekly resolution",
  65. rootPath: "",
  66. clusterID: "cluster-a",
  67. pipeline: "allocation",
  68. resolution: &[]time.Duration{7 * 24 * time.Hour}[0],
  69. prefix: "",
  70. expected: "federated/cluster-a/etl/bingen/allocation/1w/1704110400-1704715200",
  71. },
  72. }
  73. for _, tc := range testCases {
  74. t.Run(tc.name, func(t *testing.T) {
  75. pathing, err := NewBingenStoragePathFormatter(tc.rootPath, tc.clusterID, tc.pipeline, tc.resolution)
  76. if err != nil {
  77. t.Fatalf("Unexpected error: %v", err)
  78. }
  79. start := time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC)
  80. end := time.Date(2024, 1, 1, 13, 0, 0, 0, time.UTC)
  81. if tc.resolution != nil {
  82. end = start.Add(*tc.resolution)
  83. }
  84. result := pathing.ToFullPath(tc.prefix, opencost.NewClosedWindow(start, end), "")
  85. if result != tc.expected {
  86. t.Errorf("Expected %s, got %s", tc.expected, result)
  87. }
  88. })
  89. }
  90. }
  91. func TestEventPathFormatter(t *testing.T) {
  92. type testCase struct {
  93. name string
  94. rootPath string
  95. clusterID string
  96. event string
  97. prefix string
  98. fileExt string
  99. expected string
  100. }
  101. testCases := []testCase{
  102. {
  103. name: "with root path with file extension",
  104. rootPath: "/tmp",
  105. clusterID: "cluster-a",
  106. event: "heartbeat",
  107. prefix: "",
  108. fileExt: "json",
  109. expected: "/tmp/federated/cluster-a/heartbeat/20240101124000.json",
  110. },
  111. {
  112. name: "with file extension",
  113. rootPath: "",
  114. clusterID: "cluster-a",
  115. event: "heartbeat",
  116. prefix: "",
  117. fileExt: "json",
  118. expected: "federated/cluster-a/heartbeat/20240101124000.json",
  119. },
  120. {
  121. name: "without file extension",
  122. rootPath: "",
  123. clusterID: "cluster-a",
  124. event: "heartbeat",
  125. prefix: "",
  126. fileExt: "",
  127. expected: "federated/cluster-a/heartbeat/20240101124000",
  128. },
  129. {
  130. name: "with prefix with file extension",
  131. rootPath: "",
  132. clusterID: "cluster-a",
  133. event: "heartbeat",
  134. prefix: "test",
  135. fileExt: "json",
  136. expected: "federated/cluster-a/heartbeat/test.20240101124000.json",
  137. },
  138. {
  139. name: "with prefix without file extension",
  140. rootPath: "",
  141. clusterID: "cluster-a",
  142. event: "heartbeat",
  143. prefix: "test",
  144. fileExt: "",
  145. expected: "federated/cluster-a/heartbeat/test.20240101124000",
  146. },
  147. }
  148. for _, tc := range testCases {
  149. t.Run(tc.name, func(t *testing.T) {
  150. pathing, err := NewEventStoragePathFormatter(tc.rootPath, tc.clusterID, tc.event)
  151. if err != nil {
  152. t.Fatalf("Unexpected error: %v", err)
  153. }
  154. timestamp := time.Date(2024, 1, 1, 12, 40, 0, 0, time.UTC)
  155. result := pathing.ToFullPath(tc.prefix, timestamp, tc.fileExt)
  156. if result != tc.expected {
  157. t.Errorf("Expected %s, got %s", tc.expected, result)
  158. }
  159. })
  160. }
  161. }