path_test.go 5.4 KB

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