path_test.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. package pathing
  2. import (
  3. "fmt"
  4. "testing"
  5. "time"
  6. "github.com/opencost/opencost/core/pkg/opencost"
  7. "github.com/stretchr/testify/require"
  8. )
  9. func TestBingenPathFormatter(t *testing.T) {
  10. type testCase struct {
  11. name string
  12. clusterID string
  13. pipeline string
  14. resolution *time.Duration
  15. prefix string
  16. expected string
  17. }
  18. testCases := []testCase{
  19. {
  20. name: "no resolution",
  21. clusterID: "cluster-a",
  22. pipeline: "allocation",
  23. resolution: nil,
  24. prefix: "",
  25. expected: fmt.Sprintf("%s/cluster-a/%s/allocation/1704110400-1704114000", DefaultRootDir, BaseStorageDir),
  26. },
  27. {
  28. name: "with resolution",
  29. clusterID: "cluster-a",
  30. pipeline: "allocation",
  31. resolution: &[]time.Duration{1 * time.Hour}[0],
  32. prefix: "",
  33. expected: fmt.Sprintf("%s/cluster-a/%s/allocation/1h/1704110400-1704114000", DefaultRootDir, BaseStorageDir),
  34. },
  35. {
  36. name: "no resolution with prefix",
  37. clusterID: "cluster-a",
  38. pipeline: "allocation",
  39. resolution: nil,
  40. prefix: "test",
  41. expected: fmt.Sprintf("%s/cluster-a/%s/allocation/test.1704110400-1704114000", DefaultRootDir, BaseStorageDir),
  42. },
  43. {
  44. name: "with resolution with prefix",
  45. clusterID: "cluster-a",
  46. pipeline: "allocation",
  47. resolution: &[]time.Duration{1 * time.Hour}[0],
  48. prefix: "test",
  49. expected: fmt.Sprintf("%s/cluster-a/%s/allocation/1h/test.1704110400-1704114000", DefaultRootDir, BaseStorageDir),
  50. },
  51. {
  52. name: "daily resolution",
  53. clusterID: "cluster-a",
  54. pipeline: "allocation",
  55. resolution: &[]time.Duration{24 * time.Hour}[0],
  56. prefix: "",
  57. expected: fmt.Sprintf("%s/cluster-a/%s/allocation/1d/1704110400-1704196800", DefaultRootDir, BaseStorageDir),
  58. },
  59. {
  60. name: "weekly resolution",
  61. clusterID: "cluster-a",
  62. pipeline: "allocation",
  63. resolution: &[]time.Duration{7 * 24 * time.Hour}[0],
  64. prefix: "",
  65. expected: fmt.Sprintf("%s/cluster-a/%s/allocation/1w/1704110400-1704715200", DefaultRootDir, BaseStorageDir),
  66. },
  67. }
  68. for _, tc := range testCases {
  69. t.Run(tc.name, func(t *testing.T) {
  70. pathing, err := NewDefaultStoragePathFormatter(tc.clusterID, tc.pipeline, tc.resolution)
  71. if err != nil {
  72. t.Fatalf("Unexpected error: %v", err)
  73. }
  74. start := time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC)
  75. end := time.Date(2024, 1, 1, 13, 0, 0, 0, time.UTC)
  76. if tc.resolution != nil {
  77. end = start.Add(*tc.resolution)
  78. }
  79. result := pathing.ToFullPath(tc.prefix, opencost.NewClosedWindow(start, end), "")
  80. if result != tc.expected {
  81. t.Errorf("Expected %s, got %s", tc.expected, result)
  82. }
  83. })
  84. }
  85. }
  86. func TestEventPathFormatter(t *testing.T) {
  87. type testCase struct {
  88. name string
  89. rootPath string
  90. clusterID string
  91. event string
  92. subPaths []string
  93. prefix string
  94. fileExt string
  95. expected string
  96. }
  97. testCases := []testCase{
  98. {
  99. name: "with root path with file extension",
  100. rootPath: "/tmp/root",
  101. clusterID: "cluster-a",
  102. event: "heartbeat",
  103. subPaths: []string{},
  104. prefix: "",
  105. fileExt: "json",
  106. expected: "/tmp/root/cluster-a/heartbeat/20240101124000.json",
  107. },
  108. {
  109. name: "with file extension",
  110. rootPath: "root",
  111. clusterID: "cluster-a",
  112. event: "heartbeat",
  113. subPaths: []string{},
  114. prefix: "",
  115. fileExt: "json",
  116. expected: "root/cluster-a/heartbeat/20240101124000.json",
  117. },
  118. {
  119. name: "with root path with file extension with sub-paths",
  120. rootPath: "/tmp/root",
  121. clusterID: "cluster-a",
  122. event: "heartbeat",
  123. subPaths: []string{"foo", "bar"},
  124. prefix: "",
  125. fileExt: "json",
  126. expected: "/tmp/root/cluster-a/heartbeat/foo/bar/20240101124000.json",
  127. },
  128. {
  129. name: "without file extension",
  130. rootPath: "root",
  131. clusterID: "cluster-a",
  132. event: "heartbeat",
  133. subPaths: []string{},
  134. prefix: "",
  135. fileExt: "",
  136. expected: "root/cluster-a/heartbeat/20240101124000",
  137. },
  138. {
  139. name: "with prefix with file extension",
  140. rootPath: "root",
  141. clusterID: "cluster-a",
  142. event: "heartbeat",
  143. subPaths: []string{},
  144. prefix: "test",
  145. fileExt: "json",
  146. expected: "root/cluster-a/heartbeat/test.20240101124000.json",
  147. },
  148. {
  149. name: "with prefix with file extension with sub-paths",
  150. rootPath: "root",
  151. clusterID: "cluster-a",
  152. event: "heartbeat",
  153. subPaths: []string{"foo", "bar", "baz"},
  154. prefix: "test",
  155. fileExt: "json",
  156. expected: "root/cluster-a/heartbeat/foo/bar/baz/test.20240101124000.json",
  157. },
  158. {
  159. name: "with prefix without file extension",
  160. rootPath: "root",
  161. clusterID: "cluster-a",
  162. event: "heartbeat",
  163. subPaths: []string{},
  164. prefix: "test",
  165. fileExt: "",
  166. expected: "root/cluster-a/heartbeat/test.20240101124000",
  167. },
  168. {
  169. name: "with prefix without file extension with sub-paths",
  170. rootPath: "root",
  171. clusterID: "cluster-a",
  172. event: "heartbeat",
  173. subPaths: []string{"foo"},
  174. prefix: "test",
  175. fileExt: "",
  176. expected: "root/cluster-a/heartbeat/foo/test.20240101124000",
  177. },
  178. }
  179. for _, tc := range testCases {
  180. t.Run(tc.name, func(t *testing.T) {
  181. pathing, err := NewEventStoragePathFormatter(tc.rootPath, tc.clusterID, tc.event, tc.subPaths...)
  182. if err != nil {
  183. t.Fatalf("Unexpected error: %v", err)
  184. }
  185. timestamp := time.Date(2024, 1, 1, 12, 40, 0, 0, time.UTC)
  186. result := pathing.ToFullPath(tc.prefix, timestamp, tc.fileExt)
  187. if result != tc.expected {
  188. t.Errorf("Expected %s, got %s", tc.expected, result)
  189. }
  190. })
  191. }
  192. }
  193. func TestKubeModelPathFormatter(t *testing.T) {
  194. type testCase struct {
  195. name string
  196. start time.Time
  197. rootDir string
  198. clusterID string
  199. resolution string
  200. prefix string
  201. exp string
  202. }
  203. rootDir := "/path/to/root"
  204. testCases := []testCase{
  205. {
  206. name: "10m no prefix",
  207. start: time.Date(2025, time.December, 15, 12, 0, 0, 0, time.UTC),
  208. rootDir: rootDir,
  209. clusterID: "96d1c1d0-2183-416c-b8f7-754f42fd461a",
  210. resolution: "10m",
  211. prefix: "",
  212. exp: fmt.Sprintf("%s/96d1c1d0-2183-416c-b8f7-754f42fd461a/kubemodel/%s/%s/%s", rootDir, "10m", "2025/12/15", "20251215120000"),
  213. },
  214. {
  215. name: "1h no prefix",
  216. start: time.Date(2025, time.December, 15, 12, 0, 0, 0, time.UTC),
  217. rootDir: rootDir,
  218. clusterID: "96d1c1d0-2183-416c-b8f7-754f42fd461a",
  219. resolution: "1h",
  220. prefix: "",
  221. exp: fmt.Sprintf("%s/96d1c1d0-2183-416c-b8f7-754f42fd461a/kubemodel/%s/%s/%s", rootDir, "1h", "2025/12/15", "20251215120000"),
  222. },
  223. {
  224. name: "1d no prefix",
  225. start: time.Date(2025, time.December, 15, 12, 0, 0, 0, time.UTC),
  226. rootDir: rootDir,
  227. clusterID: "96d1c1d0-2183-416c-b8f7-754f42fd461a",
  228. resolution: "1d",
  229. prefix: "",
  230. exp: fmt.Sprintf("%s/96d1c1d0-2183-416c-b8f7-754f42fd461a/kubemodel/%s/%s/%s", rootDir, "1d", "2025/12/15", "20251215120000"),
  231. },
  232. {
  233. name: "1d prefix",
  234. start: time.Date(2025, time.December, 15, 12, 0, 0, 0, time.UTC),
  235. rootDir: rootDir,
  236. clusterID: "96d1c1d0-2183-416c-b8f7-754f42fd461a",
  237. resolution: "1d",
  238. prefix: "pre",
  239. exp: fmt.Sprintf("%s/96d1c1d0-2183-416c-b8f7-754f42fd461a/kubemodel/%s/%s/%s", rootDir, "1d", "2025/12/15", "pre.20251215120000"),
  240. },
  241. }
  242. for _, tc := range testCases {
  243. t.Run(tc.name, func(t *testing.T) {
  244. pathing, err := NewKubeModelStoragePathFormatter(tc.rootDir, tc.clusterID, tc.resolution)
  245. if err != nil {
  246. t.Fatalf("Unexpected error: %v", err)
  247. }
  248. var dur time.Duration
  249. switch tc.resolution {
  250. case "10m":
  251. dur = 10 * time.Minute
  252. case "1h":
  253. dur = time.Hour
  254. case "1d":
  255. dur = 24 * time.Hour
  256. default:
  257. t.Errorf("unexpected resolution: %s", tc.resolution)
  258. }
  259. end := tc.start.Add(dur)
  260. // dir := pathing.Dir()
  261. act := pathing.ToFullPath(tc.prefix, opencost.NewClosedWindow(tc.start, end), "")
  262. require.Equal(t, tc.exp, act)
  263. })
  264. }
  265. }