path_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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: "federated",
  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: "federated",
  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: "federated",
  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: "federated",
  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: "federated",
  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: "federated",
  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. subPaths []string
  98. prefix string
  99. fileExt string
  100. expected string
  101. }
  102. testCases := []testCase{
  103. {
  104. name: "with root path with file extension",
  105. rootPath: "/tmp/federated",
  106. clusterID: "cluster-a",
  107. event: "heartbeat",
  108. subPaths: []string{},
  109. prefix: "",
  110. fileExt: "json",
  111. expected: "/tmp/federated/cluster-a/heartbeat/20240101124000.json",
  112. },
  113. {
  114. name: "with file extension",
  115. rootPath: "federated",
  116. clusterID: "cluster-a",
  117. event: "heartbeat",
  118. subPaths: []string{},
  119. prefix: "",
  120. fileExt: "json",
  121. expected: "federated/cluster-a/heartbeat/20240101124000.json",
  122. },
  123. {
  124. name: "with root path with file extension with sub-paths",
  125. rootPath: "/tmp/federated",
  126. clusterID: "cluster-a",
  127. event: "heartbeat",
  128. subPaths: []string{"foo", "bar"},
  129. prefix: "",
  130. fileExt: "json",
  131. expected: "/tmp/federated/cluster-a/heartbeat/foo/bar/20240101124000.json",
  132. },
  133. {
  134. name: "without file extension",
  135. rootPath: "federated",
  136. clusterID: "cluster-a",
  137. event: "heartbeat",
  138. subPaths: []string{},
  139. prefix: "",
  140. fileExt: "",
  141. expected: "federated/cluster-a/heartbeat/20240101124000",
  142. },
  143. {
  144. name: "with prefix with file extension",
  145. rootPath: "federated",
  146. clusterID: "cluster-a",
  147. event: "heartbeat",
  148. subPaths: []string{},
  149. prefix: "test",
  150. fileExt: "json",
  151. expected: "federated/cluster-a/heartbeat/test.20240101124000.json",
  152. },
  153. {
  154. name: "with prefix with file extension with sub-paths",
  155. rootPath: "federated",
  156. clusterID: "cluster-a",
  157. event: "heartbeat",
  158. subPaths: []string{"foo", "bar", "baz"},
  159. prefix: "test",
  160. fileExt: "json",
  161. expected: "federated/cluster-a/heartbeat/foo/bar/baz/test.20240101124000.json",
  162. },
  163. {
  164. name: "with prefix without file extension",
  165. rootPath: "federated",
  166. clusterID: "cluster-a",
  167. event: "heartbeat",
  168. subPaths: []string{},
  169. prefix: "test",
  170. fileExt: "",
  171. expected: "federated/cluster-a/heartbeat/test.20240101124000",
  172. },
  173. {
  174. name: "with prefix without file extension with sub-paths",
  175. rootPath: "federated",
  176. clusterID: "cluster-a",
  177. event: "heartbeat",
  178. subPaths: []string{"foo"},
  179. prefix: "test",
  180. fileExt: "",
  181. expected: "federated/cluster-a/heartbeat/foo/test.20240101124000",
  182. },
  183. }
  184. for _, tc := range testCases {
  185. t.Run(tc.name, func(t *testing.T) {
  186. pathing, err := NewEventStoragePathFormatter(tc.rootPath, tc.clusterID, tc.event, tc.subPaths...)
  187. if err != nil {
  188. t.Fatalf("Unexpected error: %v", err)
  189. }
  190. timestamp := time.Date(2024, 1, 1, 12, 40, 0, 0, time.UTC)
  191. result := pathing.ToFullPath(tc.prefix, timestamp, tc.fileExt)
  192. if result != tc.expected {
  193. t.Errorf("Expected %s, got %s", tc.expected, result)
  194. }
  195. })
  196. }
  197. }