aggregation_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. package costmodel
  2. import (
  3. "net/url"
  4. "testing"
  5. "time"
  6. "github.com/opencost/opencost/core/pkg/opencost"
  7. "github.com/opencost/opencost/core/pkg/util/httputil"
  8. )
  9. func TestParseAggregationProperties_Default(t *testing.T) {
  10. got, err := ParseAggregationProperties([]string{})
  11. expected := []string{
  12. opencost.AllocationClusterProp,
  13. opencost.AllocationNodeProp,
  14. opencost.AllocationNamespaceProp,
  15. opencost.AllocationPodProp,
  16. opencost.AllocationContainerProp,
  17. }
  18. if err != nil {
  19. t.Fatalf("TestParseAggregationPropertiesDefault: unexpected error: %s", err)
  20. }
  21. if len(expected) != len(got) {
  22. t.Fatalf("TestParseAggregationPropertiesDefault: expected length of %d, got: %d", len(expected), len(got))
  23. }
  24. for i := range got {
  25. if got[i] != expected[i] {
  26. t.Fatalf("TestParseAggregationPropertiesDefault: expected[i] should be %s, got[i]:%s", expected[i], got[i])
  27. }
  28. }
  29. }
  30. func TestParseAggregationProperties_All(t *testing.T) {
  31. got, err := ParseAggregationProperties([]string{"all"})
  32. if err != nil {
  33. t.Fatalf("TestParseAggregationPropertiesDefault: unexpected error: %s", err)
  34. }
  35. if len(got) != 0 {
  36. t.Fatalf("TestParseAggregationPropertiesDefault: expected length of 0, got: %d", len(got))
  37. }
  38. }
  39. func TestResolveAccumulateOption(t *testing.T) {
  40. tests := []struct {
  41. name string
  42. accumulate opencost.AccumulateOption
  43. input string
  44. expected opencost.AccumulateOption
  45. expectErr bool
  46. }{
  47. {
  48. name: "accumulate false without accumulateBy",
  49. accumulate: opencost.AccumulateOptionNone,
  50. input: "",
  51. expected: opencost.AccumulateOptionNone,
  52. },
  53. {
  54. name: "accumulate true without accumulateBy defaults to all",
  55. accumulate: opencost.AccumulateOptionAll,
  56. input: "",
  57. expected: opencost.AccumulateOptionAll,
  58. },
  59. {
  60. name: "accumulate day is preserved",
  61. accumulate: opencost.AccumulateOptionDay,
  62. input: "",
  63. expected: opencost.AccumulateOptionDay,
  64. },
  65. {
  66. name: "accumulate week is preserved",
  67. accumulate: opencost.AccumulateOptionWeek,
  68. input: "",
  69. expected: opencost.AccumulateOptionWeek,
  70. },
  71. {
  72. name: "accumulateBy overrides accumulate",
  73. accumulate: opencost.AccumulateOptionDay,
  74. input: string(opencost.AccumulateOptionWeek),
  75. expected: opencost.AccumulateOptionWeek,
  76. },
  77. {
  78. name: "accumulate none with explicit accumulateBy",
  79. accumulate: opencost.AccumulateOptionNone,
  80. input: string(opencost.AccumulateOptionHour),
  81. expected: opencost.AccumulateOptionHour,
  82. },
  83. {
  84. name: "accumulateBy none is valid",
  85. accumulate: opencost.AccumulateOptionWeek,
  86. input: "none",
  87. expected: opencost.AccumulateOptionNone,
  88. },
  89. {
  90. name: "accumulateBy all is valid",
  91. accumulate: opencost.AccumulateOptionNone,
  92. input: "all",
  93. expected: opencost.AccumulateOptionAll,
  94. },
  95. {
  96. name: "accumulateBy normalizes case",
  97. accumulate: opencost.AccumulateOptionNone,
  98. input: "Week",
  99. expected: opencost.AccumulateOptionWeek,
  100. },
  101. {
  102. name: "accumulateBy quarter is valid",
  103. accumulate: opencost.AccumulateOptionNone,
  104. input: string(opencost.AccumulateOptionQuarter),
  105. expected: opencost.AccumulateOptionQuarter,
  106. },
  107. {
  108. name: "accumulate quarter is preserved",
  109. accumulate: opencost.AccumulateOptionQuarter,
  110. input: "",
  111. expected: opencost.AccumulateOptionQuarter,
  112. },
  113. {
  114. name: "invalid accumulateBy is flagged",
  115. accumulate: opencost.AccumulateOptionNone,
  116. input: "nonsense",
  117. expected: opencost.AccumulateOptionNone,
  118. expectErr: true,
  119. },
  120. }
  121. for _, tc := range tests {
  122. t.Run(tc.name, func(t *testing.T) {
  123. got, err := resolveAccumulateOption(tc.accumulate, tc.input)
  124. if tc.expectErr && err == nil {
  125. t.Fatalf("expected error but got nil")
  126. }
  127. if !tc.expectErr && err != nil {
  128. t.Fatalf("unexpected error: %s", err)
  129. }
  130. if got != tc.expected {
  131. t.Fatalf("expected %q, got %q", tc.expected, got)
  132. }
  133. })
  134. }
  135. }
  136. func TestResolveAccumulateFromQuery_BackwardCompatibleTruthyValues(t *testing.T) {
  137. tests := []struct {
  138. name string
  139. input string
  140. }{
  141. {name: "true supported", input: "true"},
  142. {name: "all supported", input: "all"},
  143. {name: "1 supported", input: "1"},
  144. {name: "t supported", input: "t"},
  145. {name: "TRUE supported", input: "TRUE"},
  146. }
  147. for _, tc := range tests {
  148. t.Run(tc.name, func(t *testing.T) {
  149. values := url.Values{}
  150. values.Set("accumulate", tc.input)
  151. qp := httputil.NewQueryParams(values)
  152. got := resolveAccumulateFromQuery(qp)
  153. if got != opencost.AccumulateOptionAll {
  154. t.Fatalf("expected %q for %q, got %q", opencost.AccumulateOptionAll, tc.input, got)
  155. }
  156. })
  157. }
  158. }
  159. func TestResolveStepForAccumulate(t *testing.T) {
  160. tests := []struct {
  161. name string
  162. step time.Duration
  163. accumulateBy opencost.AccumulateOption
  164. expected time.Duration
  165. }{
  166. {
  167. name: "none keeps requested step",
  168. step: 14 * 24 * time.Hour,
  169. accumulateBy: opencost.AccumulateOptionNone,
  170. expected: 14 * 24 * time.Hour,
  171. },
  172. {
  173. name: "day uses hourly step",
  174. step: 14 * 24 * time.Hour,
  175. accumulateBy: opencost.AccumulateOptionDay,
  176. expected: time.Hour,
  177. },
  178. {
  179. name: "day keeps daily step",
  180. step: 24 * time.Hour,
  181. accumulateBy: opencost.AccumulateOptionDay,
  182. expected: 24 * time.Hour,
  183. },
  184. {
  185. name: "week uses daily step",
  186. step: 14 * 24 * time.Hour,
  187. accumulateBy: opencost.AccumulateOptionWeek,
  188. expected: 24 * time.Hour,
  189. },
  190. {
  191. name: "week keeps weekly step",
  192. step: 7 * 24 * time.Hour,
  193. accumulateBy: opencost.AccumulateOptionWeek,
  194. expected: 7 * 24 * time.Hour,
  195. },
  196. {
  197. name: "quarter uses daily step",
  198. step: 7 * 24 * time.Hour,
  199. accumulateBy: opencost.AccumulateOptionQuarter,
  200. expected: 24 * time.Hour,
  201. },
  202. }
  203. for _, tc := range tests {
  204. t.Run(tc.name, func(t *testing.T) {
  205. got := resolveStepForAccumulate(tc.step, tc.accumulateBy)
  206. if got != tc.expected {
  207. t.Fatalf("expected %v, got %v", tc.expected, got)
  208. }
  209. })
  210. }
  211. }
  212. func TestResolveDefaultStepFromAccumulate(t *testing.T) {
  213. window := opencost.NewClosedWindow(
  214. time.Date(2026, 4, 1, 0, 0, 0, 0, time.UTC),
  215. time.Date(2026, 4, 15, 0, 0, 0, 0, time.UTC),
  216. )
  217. tests := []struct {
  218. name string
  219. accumulateBy opencost.AccumulateOption
  220. expected time.Duration
  221. }{
  222. {
  223. name: "none defaults to window duration",
  224. accumulateBy: opencost.AccumulateOptionNone,
  225. expected: window.Duration(),
  226. },
  227. {
  228. name: "day defaults to daily",
  229. accumulateBy: opencost.AccumulateOptionDay,
  230. expected: 24 * time.Hour,
  231. },
  232. {
  233. name: "week defaults to weekly",
  234. accumulateBy: opencost.AccumulateOptionWeek,
  235. expected: 7 * 24 * time.Hour,
  236. },
  237. {
  238. name: "month defaults to daily",
  239. accumulateBy: opencost.AccumulateOptionMonth,
  240. expected: 24 * time.Hour,
  241. },
  242. {
  243. name: "all defaults to window duration",
  244. accumulateBy: opencost.AccumulateOptionAll,
  245. expected: window.Duration(),
  246. },
  247. }
  248. for _, tc := range tests {
  249. t.Run(tc.name, func(t *testing.T) {
  250. got := resolveDefaultStepFromAccumulate(window, tc.accumulateBy)
  251. if got != tc.expected {
  252. t.Fatalf("expected %v, got %v", tc.expected, got)
  253. }
  254. })
  255. }
  256. }
  257. func TestResolveStepFromQuery(t *testing.T) {
  258. window := opencost.NewClosedWindow(
  259. time.Date(2026, 4, 1, 0, 0, 0, 0, time.UTC),
  260. time.Date(2026, 4, 15, 0, 0, 0, 0, time.UTC),
  261. )
  262. tests := []struct {
  263. name string
  264. stepRaw string
  265. accumulateBy opencost.AccumulateOption
  266. expected time.Duration
  267. expectErr bool
  268. }{
  269. {
  270. name: "unset step defaults from weekly accumulate",
  271. stepRaw: "",
  272. accumulateBy: opencost.AccumulateOptionWeek,
  273. expected: 7 * 24 * time.Hour,
  274. },
  275. {
  276. name: "monthly step keyword supported",
  277. stepRaw: "month",
  278. accumulateBy: opencost.AccumulateOptionNone,
  279. expected: 24 * time.Hour,
  280. },
  281. {
  282. name: "weekly step keyword supported",
  283. stepRaw: "week",
  284. accumulateBy: opencost.AccumulateOptionWeek,
  285. expected: 7 * 24 * time.Hour,
  286. },
  287. {
  288. name: "day shorthand duration supported",
  289. stepRaw: "1d",
  290. accumulateBy: opencost.AccumulateOptionNone,
  291. expected: 24 * time.Hour,
  292. },
  293. {
  294. name: "week shorthand duration supported",
  295. stepRaw: "1w",
  296. accumulateBy: opencost.AccumulateOptionNone,
  297. expected: 7 * 24 * time.Hour,
  298. },
  299. {
  300. name: "invalid duration errors",
  301. stepRaw: "not-a-duration",
  302. accumulateBy: opencost.AccumulateOptionNone,
  303. expectErr: true,
  304. },
  305. }
  306. for _, tc := range tests {
  307. t.Run(tc.name, func(t *testing.T) {
  308. values := url.Values{}
  309. if tc.stepRaw != "" {
  310. values.Set("step", tc.stepRaw)
  311. }
  312. qp := httputil.NewQueryParams(values)
  313. got, err := resolveStepFromQuery(qp, window, tc.accumulateBy)
  314. if tc.expectErr && err == nil {
  315. t.Fatalf("expected error but got nil")
  316. }
  317. if tc.expectErr {
  318. return
  319. }
  320. if err != nil {
  321. t.Fatalf("unexpected error: %s", err)
  322. }
  323. if got != tc.expected {
  324. t.Fatalf("expected %v, got %v", tc.expected, got)
  325. }
  326. })
  327. }
  328. }
  329. func TestWeeklyAccumulateTwoWeeksProducesTwoSets(t *testing.T) {
  330. start := time.Date(2026, 4, 5, 0, 0, 0, 0, time.UTC) // Sunday
  331. end := start.Add(14 * 24 * time.Hour)
  332. requestedStep := end.Sub(start)
  333. accumulateBy, err := resolveAccumulateOption(opencost.AccumulateOptionNone, string(opencost.AccumulateOptionWeek))
  334. if err != nil {
  335. t.Fatalf("unexpected error resolving accumulate option: %s", err)
  336. }
  337. step := resolveStepForAccumulate(requestedStep, accumulateBy)
  338. if step != 24*time.Hour {
  339. t.Fatalf("expected daily step for weekly accumulation, got %v", step)
  340. }
  341. asr := opencost.NewAllocationSetRange()
  342. for ts := start; ts.Before(end); ts = ts.Add(step) {
  343. next := ts.Add(step)
  344. as := opencost.NewAllocationSet(ts, next)
  345. as.Set(opencost.NewMockUnitAllocation("workload", ts, step, nil))
  346. asr.Append(as)
  347. }
  348. weekly, err := asr.Accumulate(opencost.AccumulateOptionWeek)
  349. if err != nil {
  350. t.Fatalf("unexpected weekly accumulate error: %s", err)
  351. }
  352. if len(weekly.Allocations) != 2 {
  353. t.Fatalf("expected 2 weekly sets from 2 weeks of data, got %d", len(weekly.Allocations))
  354. }
  355. for i, as := range weekly.Allocations {
  356. if got := as.Window.Duration(); got != 7*24*time.Hour {
  357. t.Fatalf("set %d expected 7d window, got %s", i, got)
  358. }
  359. }
  360. }
  361. func TestResolveQueryWindowForAccumulate_WeekRoundsToCalendarWeeks(t *testing.T) {
  362. start := time.Date(2026, 4, 6, 0, 0, 0, 0, time.UTC) // Monday
  363. end := start.Add(14 * 24 * time.Hour)
  364. window := opencost.NewClosedWindow(start, end)
  365. got, err := resolveQueryWindowForAccumulate(window, opencost.AccumulateOptionWeek)
  366. if err != nil {
  367. t.Fatalf("unexpected error: %s", err)
  368. }
  369. expectedStart := time.Date(2026, 4, 5, 0, 0, 0, 0, time.UTC) // Sunday
  370. expectedEnd := time.Date(2026, 4, 26, 0, 0, 0, 0, time.UTC) // Sunday after 3 calendar weeks
  371. if !got.Start().Equal(expectedStart) {
  372. t.Fatalf("expected rounded start %s, got %s", expectedStart, got.Start())
  373. }
  374. if !got.End().Equal(expectedEnd) {
  375. t.Fatalf("expected rounded end %s, got %s", expectedEnd, got.End())
  376. }
  377. }
  378. func TestTrimAllocationSetRangeToRequestWindow(t *testing.T) {
  379. requestStart := time.Date(2026, 4, 13, 0, 0, 0, 0, time.UTC)
  380. requestEnd := time.Date(2026, 4, 26, 0, 0, 0, 0, time.UTC)
  381. requestWindow := opencost.NewClosedWindow(requestStart, requestEnd)
  382. before := opencost.NewAllocationSet(
  383. time.Date(2026, 4, 5, 0, 0, 0, 0, time.UTC),
  384. time.Date(2026, 4, 12, 0, 0, 0, 0, time.UTC),
  385. )
  386. overlap := opencost.NewAllocationSet(
  387. time.Date(2026, 4, 12, 0, 0, 0, 0, time.UTC),
  388. time.Date(2026, 4, 19, 0, 0, 0, 0, time.UTC),
  389. )
  390. inside := opencost.NewAllocationSet(
  391. time.Date(2026, 4, 19, 0, 0, 0, 0, time.UTC),
  392. time.Date(2026, 4, 26, 0, 0, 0, 0, time.UTC),
  393. )
  394. after := opencost.NewAllocationSet(
  395. time.Date(2026, 4, 26, 0, 0, 0, 0, time.UTC),
  396. time.Date(2026, 5, 3, 0, 0, 0, 0, time.UTC),
  397. )
  398. asr := opencost.NewAllocationSetRange(before, overlap, inside, after)
  399. asr.FromStore = "test-store"
  400. trimmed := trimAllocationSetRangeToRequestWindow(asr, requestWindow)
  401. if len(trimmed.Allocations) != 2 {
  402. t.Fatalf("expected 2 overlapping sets, got %d", len(trimmed.Allocations))
  403. }
  404. if !trimmed.Allocations[0].Start().Equal(overlap.Start()) {
  405. t.Fatalf("expected first set to start at %s, got %s", overlap.Start(), trimmed.Allocations[0].Start())
  406. }
  407. if !trimmed.Allocations[1].Start().Equal(inside.Start()) {
  408. t.Fatalf("expected second set to start at %s, got %s", inside.Start(), trimmed.Allocations[1].Start())
  409. }
  410. if trimmed.FromStore != asr.FromStore {
  411. t.Fatalf("expected FromStore to be preserved")
  412. }
  413. }