aggregation_test.go 15 KB

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