allocation_test.go 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689
  1. package kubecost
  2. import (
  3. "fmt"
  4. "math"
  5. "testing"
  6. "time"
  7. "github.com/opencost/opencost/pkg/util"
  8. "github.com/opencost/opencost/pkg/util/json"
  9. )
  10. func TestAllocation_Add(t *testing.T) {
  11. var nilAlloc *Allocation
  12. zeroAlloc := &Allocation{}
  13. // nil + nil == nil
  14. nilNilSum, err := nilAlloc.Add(nilAlloc)
  15. if err != nil {
  16. t.Fatalf("Allocation.Add unexpected error: %s", err)
  17. }
  18. if nilNilSum != nil {
  19. t.Fatalf("Allocation.Add failed; exp: nil; act: %s", nilNilSum)
  20. }
  21. // nil + zero == zero
  22. nilZeroSum, err := nilAlloc.Add(zeroAlloc)
  23. if err != nil {
  24. t.Fatalf("Allocation.Add unexpected error: %s", err)
  25. }
  26. if nilZeroSum == nil || nilZeroSum.TotalCost() != 0.0 {
  27. t.Fatalf("Allocation.Add failed; exp: 0.0; act: %s", nilZeroSum)
  28. }
  29. cpuPrice := 0.02
  30. gpuPrice := 2.00
  31. ramPrice := 0.01
  32. pvPrice := 0.00005
  33. gib := 1024.0 * 1024.0 * 1024.0
  34. s1 := time.Date(2021, time.January, 1, 0, 0, 0, 0, time.UTC)
  35. e1 := time.Date(2021, time.January, 1, 12, 0, 0, 0, time.UTC)
  36. hrs1 := e1.Sub(s1).Hours()
  37. a1 := &Allocation{
  38. Start: s1,
  39. End: e1,
  40. Properties: &AllocationProperties{},
  41. CPUCoreHours: 2.0 * hrs1,
  42. CPUCoreRequestAverage: 2.0,
  43. CPUCoreUsageAverage: 1.0,
  44. CPUCost: 2.0 * hrs1 * cpuPrice,
  45. CPUCostAdjustment: 3.0,
  46. GPUHours: 1.0 * hrs1,
  47. GPUCost: 1.0 * hrs1 * gpuPrice,
  48. GPUCostAdjustment: 2.0,
  49. PVs: PVAllocations{
  50. disk: {
  51. ByteHours: 100.0 * gib * hrs1,
  52. Cost: 100.0 * hrs1 * pvPrice,
  53. },
  54. },
  55. PVCostAdjustment: 4.0,
  56. RAMByteHours: 8.0 * gib * hrs1,
  57. RAMBytesRequestAverage: 8.0 * gib,
  58. RAMBytesUsageAverage: 4.0 * gib,
  59. RAMCost: 8.0 * hrs1 * ramPrice,
  60. RAMCostAdjustment: 1.0,
  61. SharedCost: 2.00,
  62. ExternalCost: 1.00,
  63. RawAllocationOnly: &RawAllocationOnlyData{},
  64. }
  65. a1b := a1.Clone()
  66. s2 := time.Date(2021, time.January, 1, 6, 0, 0, 0, time.UTC)
  67. e2 := time.Date(2021, time.January, 1, 24, 0, 0, 0, time.UTC)
  68. hrs2 := e1.Sub(s1).Hours()
  69. a2 := &Allocation{
  70. Start: s2,
  71. End: e2,
  72. Properties: &AllocationProperties{},
  73. CPUCoreHours: 1.0 * hrs2,
  74. CPUCoreRequestAverage: 1.0,
  75. CPUCoreUsageAverage: 1.0,
  76. CPUCost: 1.0 * hrs2 * cpuPrice,
  77. GPUHours: 0.0,
  78. GPUCost: 0.0,
  79. RAMByteHours: 8.0 * gib * hrs2,
  80. RAMBytesRequestAverage: 0.0,
  81. RAMBytesUsageAverage: 8.0 * gib,
  82. RAMCost: 8.0 * hrs2 * ramPrice,
  83. NetworkCost: 0.01,
  84. LoadBalancerCost: 0.05,
  85. SharedCost: 0.00,
  86. ExternalCost: 1.00,
  87. RawAllocationOnly: &RawAllocationOnlyData{},
  88. }
  89. a2b := a2.Clone()
  90. act, err := a1.Add(a2)
  91. if err != nil {
  92. t.Fatalf("Allocation.Add: unexpected error: %s", err)
  93. }
  94. // Neither Allocation should be mutated
  95. if !a1.Equal(a1b) {
  96. t.Fatalf("Allocation.Add: a1 illegally mutated")
  97. }
  98. if !a2.Equal(a2b) {
  99. t.Fatalf("Allocation.Add: a1 illegally mutated")
  100. }
  101. // Costs should be cumulative
  102. if !util.IsApproximately(a1.TotalCost()+a2.TotalCost(), act.TotalCost()) {
  103. t.Fatalf("Allocation.Add: expected %f; actual %f", a1.TotalCost()+a2.TotalCost(), act.TotalCost())
  104. }
  105. if !util.IsApproximately(a1.CPUCost+a2.CPUCost, act.CPUCost) {
  106. t.Fatalf("Allocation.Add: expected %f; actual %f", a1.CPUCost+a2.CPUCost, act.CPUCost)
  107. }
  108. if !util.IsApproximately(a1.CPUCostAdjustment+a2.CPUCostAdjustment, act.CPUCostAdjustment) {
  109. t.Fatalf("Allocation.Add: expected %f; actual %f", a1.CPUCostAdjustment+a2.CPUCostAdjustment, act.CPUCostAdjustment)
  110. }
  111. if !util.IsApproximately(a1.GPUCost+a2.GPUCost, act.GPUCost) {
  112. t.Fatalf("Allocation.Add: expected %f; actual %f", a1.GPUCost+a2.GPUCost, act.GPUCost)
  113. }
  114. if !util.IsApproximately(a1.GPUCostAdjustment+a2.GPUCostAdjustment, act.GPUCostAdjustment) {
  115. t.Fatalf("Allocation.Add: expected %f; actual %f", a1.GPUCostAdjustment+a2.GPUCostAdjustment, act.GPUCostAdjustment)
  116. }
  117. if !util.IsApproximately(a1.RAMCost+a2.RAMCost, act.RAMCost) {
  118. t.Fatalf("Allocation.Add: expected %f; actual %f", a1.RAMCost+a2.RAMCost, act.RAMCost)
  119. }
  120. if !util.IsApproximately(a1.RAMCostAdjustment+a2.RAMCostAdjustment, act.RAMCostAdjustment) {
  121. t.Fatalf("Allocation.Add: expected %f; actual %f", a1.RAMCostAdjustment+a2.RAMCostAdjustment, act.RAMCostAdjustment)
  122. }
  123. if !util.IsApproximately(a1.PVCost()+a2.PVCost(), act.PVCost()) {
  124. t.Fatalf("Allocation.Add: expected %f; actual %f", a1.PVCost()+a2.PVCost(), act.PVCost())
  125. }
  126. if !util.IsApproximately(a1.NetworkCost+a2.NetworkCost, act.NetworkCost) {
  127. t.Fatalf("Allocation.Add: expected %f; actual %f", a1.NetworkCost+a2.NetworkCost, act.NetworkCost)
  128. }
  129. if !util.IsApproximately(a1.LoadBalancerCost+a2.LoadBalancerCost, act.LoadBalancerCost) {
  130. t.Fatalf("Allocation.Add: expected %f; actual %f", a1.LoadBalancerCost+a2.LoadBalancerCost, act.LoadBalancerCost)
  131. }
  132. if !util.IsApproximately(a1.SharedCost+a2.SharedCost, act.SharedCost) {
  133. t.Fatalf("Allocation.Add: expected %f; actual %f", a1.SharedCost+a2.SharedCost, act.SharedCost)
  134. }
  135. if !util.IsApproximately(a1.ExternalCost+a2.ExternalCost, act.ExternalCost) {
  136. t.Fatalf("Allocation.Add: expected %f; actual %f", a1.ExternalCost+a2.ExternalCost, act.ExternalCost)
  137. }
  138. // ResourceHours should be cumulative
  139. if !util.IsApproximately(a1.CPUCoreHours+a2.CPUCoreHours, act.CPUCoreHours) {
  140. t.Fatalf("Allocation.Add: expected %f; actual %f", a1.CPUCoreHours+a2.CPUCoreHours, act.CPUCoreHours)
  141. }
  142. if !util.IsApproximately(a1.RAMByteHours+a2.RAMByteHours, act.RAMByteHours) {
  143. t.Fatalf("Allocation.Add: expected %f; actual %f", a1.RAMByteHours+a2.RAMByteHours, act.RAMByteHours)
  144. }
  145. if !util.IsApproximately(a1.PVByteHours()+a2.PVByteHours(), act.PVByteHours()) {
  146. t.Fatalf("Allocation.Add: expected %f; actual %f", a1.PVByteHours()+a2.PVByteHours(), act.PVByteHours())
  147. }
  148. // Minutes should be the duration between min(starts) and max(ends)
  149. if !act.Start.Equal(a1.Start) || !act.End.Equal(a2.End) {
  150. t.Fatalf("Allocation.Add: expected %s; actual %s", NewWindow(&a1.Start, &a2.End), NewWindow(&act.Start, &act.End))
  151. }
  152. if act.Minutes() != 1440.0 {
  153. t.Fatalf("Allocation.Add: expected %f; actual %f", 1440.0, act.Minutes())
  154. }
  155. // Requests and Usage should be averaged correctly
  156. // CPU requests = (2.0*12.0 + 1.0*18.0)/(24.0) = 1.75
  157. // CPU usage = (1.0*12.0 + 1.0*18.0)/(24.0) = 1.25
  158. // RAM requests = (8.0*12.0 + 0.0*18.0)/(24.0) = 4.00
  159. // RAM usage = (4.0*12.0 + 8.0*18.0)/(24.0) = 8.00
  160. if !util.IsApproximately(1.75, act.CPUCoreRequestAverage) {
  161. t.Fatalf("Allocation.Add: expected %f; actual %f", 1.75, act.CPUCoreRequestAverage)
  162. }
  163. if !util.IsApproximately(1.25, act.CPUCoreUsageAverage) {
  164. t.Fatalf("Allocation.Add: expected %f; actual %f", 1.25, act.CPUCoreUsageAverage)
  165. }
  166. if !util.IsApproximately(4.00*gib, act.RAMBytesRequestAverage) {
  167. t.Fatalf("Allocation.Add: expected %f; actual %f", 4.00*gib, act.RAMBytesRequestAverage)
  168. }
  169. if !util.IsApproximately(8.00*gib, act.RAMBytesUsageAverage) {
  170. t.Fatalf("Allocation.Add: expected %f; actual %f", 8.00*gib, act.RAMBytesUsageAverage)
  171. }
  172. // Efficiency should be computed accurately from new request/usage
  173. // CPU efficiency = 1.25/1.75 = 0.7142857
  174. // RAM efficiency = 8.00/4.00 = 2.0000000
  175. // Total efficiency = (0.7142857*0.72 + 2.0*1.92)/(2.64) = 1.6493506
  176. if !util.IsApproximately(0.7142857, act.CPUEfficiency()) {
  177. t.Fatalf("Allocation.Add: expected %f; actual %f", 0.7142857, act.CPUEfficiency())
  178. }
  179. if !util.IsApproximately(2.0000000, act.RAMEfficiency()) {
  180. t.Fatalf("Allocation.Add: expected %f; actual %f", 2.0000000, act.RAMEfficiency())
  181. }
  182. if !util.IsApproximately(1.279690, act.TotalEfficiency()) {
  183. t.Fatalf("Allocation.Add: expected %f; actual %f", 1.279690, act.TotalEfficiency())
  184. }
  185. if act.RawAllocationOnly != nil {
  186. t.Errorf("Allocation.Add: Raw only data must be nil after an add")
  187. }
  188. }
  189. func TestAllocation_Share(t *testing.T) {
  190. cpuPrice := 0.02
  191. gpuPrice := 2.00
  192. ramPrice := 0.01
  193. pvPrice := 0.00005
  194. gib := 1024.0 * 1024.0 * 1024.0
  195. s1 := time.Date(2021, time.January, 1, 0, 0, 0, 0, time.UTC)
  196. e1 := time.Date(2021, time.January, 1, 12, 0, 0, 0, time.UTC)
  197. hrs1 := e1.Sub(s1).Hours()
  198. a1 := &Allocation{
  199. Start: s1,
  200. End: e1,
  201. Properties: &AllocationProperties{},
  202. CPUCoreHours: 2.0 * hrs1,
  203. CPUCoreRequestAverage: 2.0,
  204. CPUCoreUsageAverage: 1.0,
  205. CPUCost: 2.0 * hrs1 * cpuPrice,
  206. CPUCostAdjustment: 3.0,
  207. GPUHours: 1.0 * hrs1,
  208. GPUCost: 1.0 * hrs1 * gpuPrice,
  209. GPUCostAdjustment: 2.0,
  210. PVs: PVAllocations{
  211. disk: {
  212. ByteHours: 100.0 * gib * hrs1,
  213. Cost: 100.0 * hrs1 * pvPrice,
  214. },
  215. },
  216. PVCostAdjustment: 4.0,
  217. RAMByteHours: 8.0 * gib * hrs1,
  218. RAMBytesRequestAverage: 8.0 * gib,
  219. RAMBytesUsageAverage: 4.0 * gib,
  220. RAMCost: 8.0 * hrs1 * ramPrice,
  221. RAMCostAdjustment: 1.0,
  222. SharedCost: 2.00,
  223. ExternalCost: 1.00,
  224. }
  225. a1b := a1.Clone()
  226. s2 := time.Date(2021, time.January, 1, 6, 0, 0, 0, time.UTC)
  227. e2 := time.Date(2021, time.January, 1, 24, 0, 0, 0, time.UTC)
  228. hrs2 := e1.Sub(s1).Hours()
  229. a2 := &Allocation{
  230. Start: s2,
  231. End: e2,
  232. Properties: &AllocationProperties{},
  233. CPUCoreHours: 1.0 * hrs2,
  234. CPUCoreRequestAverage: 1.0,
  235. CPUCoreUsageAverage: 1.0,
  236. CPUCost: 1.0 * hrs2 * cpuPrice,
  237. GPUHours: 0.0,
  238. GPUCost: 0.0,
  239. RAMByteHours: 8.0 * gib * hrs2,
  240. RAMBytesRequestAverage: 0.0,
  241. RAMBytesUsageAverage: 8.0 * gib,
  242. RAMCost: 8.0 * hrs2 * ramPrice,
  243. NetworkCost: 0.01,
  244. LoadBalancerCost: 0.05,
  245. SharedCost: 0.00,
  246. ExternalCost: 1.00,
  247. }
  248. a2b := a2.Clone()
  249. act, err := a1.Share(a2)
  250. if err != nil {
  251. t.Fatalf("Allocation.Share: unexpected error: %s", err)
  252. }
  253. // Neither Allocation should be mutated
  254. if !a1.Equal(a1b) {
  255. t.Fatalf("Allocation.Share: a1 illegally mutated")
  256. }
  257. if !a2.Equal(a2b) {
  258. t.Fatalf("Allocation.Share: a1 illegally mutated")
  259. }
  260. // SharedCost and TotalCost should reflect increase by a2.TotalCost
  261. if !util.IsApproximately(a1.TotalCost()+a2.TotalCost(), act.TotalCost()) {
  262. t.Fatalf("Allocation.Share: expected %f; actual %f", a1.TotalCost()+a2.TotalCost(), act.TotalCost())
  263. }
  264. if !util.IsApproximately(a1.SharedCost+a2.TotalCost(), act.SharedCost) {
  265. t.Fatalf("Allocation.Share: expected %f; actual %f", a1.SharedCost+a2.TotalCost(), act.SharedCost)
  266. }
  267. // Costs should match before (expect TotalCost and SharedCost)
  268. if !util.IsApproximately(a1.CPUTotalCost(), act.CPUTotalCost()) {
  269. t.Fatalf("Allocation.Share: expected %f; actual %f", a1.CPUTotalCost(), act.CPUTotalCost())
  270. }
  271. if !util.IsApproximately(a1.GPUTotalCost(), act.GPUTotalCost()) {
  272. t.Fatalf("Allocation.Share: expected %f; actual %f", a1.GPUTotalCost(), act.GPUTotalCost())
  273. }
  274. if !util.IsApproximately(a1.RAMTotalCost(), act.RAMTotalCost()) {
  275. t.Fatalf("Allocation.Share: expected %f; actual %f", a1.RAMTotalCost(), act.RAMTotalCost())
  276. }
  277. if !util.IsApproximately(a1.PVTotalCost(), act.PVTotalCost()) {
  278. t.Fatalf("Allocation.Share: expected %f; actual %f", a1.PVTotalCost(), act.PVTotalCost())
  279. }
  280. if !util.IsApproximately(a1.NetworkCost, act.NetworkCost) {
  281. t.Fatalf("Allocation.Share: expected %f; actual %f", a1.NetworkCost, act.NetworkCost)
  282. }
  283. if !util.IsApproximately(a1.LoadBalancerCost, act.LoadBalancerCost) {
  284. t.Fatalf("Allocation.Share: expected %f; actual %f", a1.LoadBalancerCost, act.LoadBalancerCost)
  285. }
  286. if !util.IsApproximately(a1.ExternalCost, act.ExternalCost) {
  287. t.Fatalf("Allocation.Share: expected %f; actual %f", a1.ExternalCost, act.ExternalCost)
  288. }
  289. // ResourceHours should match before
  290. if !util.IsApproximately(a1.CPUCoreHours, act.CPUCoreHours) {
  291. t.Fatalf("Allocation.Share: expected %f; actual %f", a1.CPUCoreHours, act.CPUCoreHours)
  292. }
  293. if !util.IsApproximately(a1.RAMByteHours, act.RAMByteHours) {
  294. t.Fatalf("Allocation.Share: expected %f; actual %f", a1.RAMByteHours, act.RAMByteHours)
  295. }
  296. if !util.IsApproximately(a1.PVByteHours(), act.PVByteHours()) {
  297. t.Fatalf("Allocation.Share: expected %f; actual %f", a1.PVByteHours(), act.PVByteHours())
  298. }
  299. // Minutes should match before
  300. if !act.Start.Equal(a1.Start) || !act.End.Equal(a1.End) {
  301. t.Fatalf("Allocation.Share: expected %s; actual %s", NewWindow(&a1.Start, &a1.End), NewWindow(&act.Start, &act.End))
  302. }
  303. if act.Minutes() != a1.Minutes() {
  304. t.Fatalf("Allocation.Share: expected %f; actual %f", a1.Minutes(), act.Minutes())
  305. }
  306. // Requests and Usage should match before
  307. if !util.IsApproximately(a1.CPUCoreRequestAverage, act.CPUCoreRequestAverage) {
  308. t.Fatalf("Allocation.Share: expected %f; actual %f", a1.CPUCoreRequestAverage, act.CPUCoreRequestAverage)
  309. }
  310. if !util.IsApproximately(a1.CPUCoreUsageAverage, act.CPUCoreUsageAverage) {
  311. t.Fatalf("Allocation.Share: expected %f; actual %f", a1.CPUCoreUsageAverage, act.CPUCoreUsageAverage)
  312. }
  313. if !util.IsApproximately(a1.RAMBytesRequestAverage, act.RAMBytesRequestAverage) {
  314. t.Fatalf("Allocation.Share: expected %f; actual %f", a1.RAMBytesRequestAverage, act.RAMBytesRequestAverage)
  315. }
  316. if !util.IsApproximately(a1.RAMBytesUsageAverage, act.RAMBytesUsageAverage) {
  317. t.Fatalf("Allocation.Share: expected %f; actual %f", a1.RAMBytesUsageAverage, act.RAMBytesUsageAverage)
  318. }
  319. // Efficiency should match before
  320. if !util.IsApproximately(a1.CPUEfficiency(), act.CPUEfficiency()) {
  321. t.Fatalf("Allocation.Share: expected %f; actual %f", a1.CPUEfficiency(), act.CPUEfficiency())
  322. }
  323. if !util.IsApproximately(a1.RAMEfficiency(), act.RAMEfficiency()) {
  324. t.Fatalf("Allocation.Share: expected %f; actual %f", a1.RAMEfficiency(), act.RAMEfficiency())
  325. }
  326. if !util.IsApproximately(a1.TotalEfficiency(), act.TotalEfficiency()) {
  327. t.Fatalf("Allocation.Share: expected %f; actual %f", a1.TotalEfficiency(), act.TotalEfficiency())
  328. }
  329. }
  330. func TestAllocation_AddDifferentController(t *testing.T) {
  331. a1 := &Allocation{
  332. Properties: &AllocationProperties{
  333. Container: "container",
  334. Pod: "pod",
  335. Namespace: "ns",
  336. Cluster: "cluster",
  337. Controller: "controller 1",
  338. },
  339. }
  340. a2 := a1.Clone()
  341. a2.Properties.Controller = "controller 2"
  342. result, err := a1.Add(a2)
  343. if err != nil {
  344. t.Fatalf("Allocation.Add: unexpected error: %s", err)
  345. }
  346. if result.Properties.Controller == "" {
  347. t.Errorf("Adding allocations whose properties only differ in controller name should not result in an empty string controller name.")
  348. }
  349. }
  350. func TestAllocation_MarshalJSON(t *testing.T) {
  351. start := time.Date(2021, time.January, 1, 0, 0, 0, 0, time.UTC)
  352. end := time.Date(2021, time.January, 2, 0, 0, 0, 0, time.UTC)
  353. hrs := 24.0
  354. gib := 1024.0 * 1024.0 * 1024.0
  355. cpuPrice := 0.02
  356. gpuPrice := 2.00
  357. ramPrice := 0.01
  358. pvPrice := 0.00005
  359. before := &Allocation{
  360. Name: "cluster1/namespace1/node1/pod1/container1",
  361. Properties: &AllocationProperties{
  362. Cluster: "cluster1",
  363. Node: "node1",
  364. Namespace: "namespace1",
  365. Pod: "pod1",
  366. Container: "container1",
  367. },
  368. Window: NewWindow(&start, &end),
  369. Start: start,
  370. End: end,
  371. CPUCoreHours: 2.0 * hrs,
  372. CPUCoreRequestAverage: 2.0,
  373. CPUCoreUsageAverage: 1.0,
  374. CPUCost: 2.0 * hrs * cpuPrice,
  375. CPUCostAdjustment: 3.0,
  376. GPUHours: 1.0 * hrs,
  377. GPUCost: 1.0 * hrs * gpuPrice,
  378. GPUCostAdjustment: 2.0,
  379. NetworkCost: 0.05,
  380. LoadBalancerCost: 0.02,
  381. PVs: PVAllocations{
  382. disk: {
  383. ByteHours: 100.0 * gib * hrs,
  384. Cost: 100.0 * hrs * pvPrice,
  385. },
  386. },
  387. PVCostAdjustment: 4.0,
  388. RAMByteHours: 8.0 * gib * hrs,
  389. RAMBytesRequestAverage: 8.0 * gib,
  390. RAMBytesUsageAverage: 4.0 * gib,
  391. RAMCost: 8.0 * hrs * ramPrice,
  392. RAMCostAdjustment: 1.0,
  393. SharedCost: 2.00,
  394. ExternalCost: 1.00,
  395. RawAllocationOnly: &RawAllocationOnlyData{},
  396. }
  397. data, err := json.Marshal(before)
  398. if err != nil {
  399. t.Fatalf("Allocation.MarshalJSON: unexpected error: %s", err)
  400. }
  401. after := &Allocation{}
  402. err = json.Unmarshal(data, after)
  403. if err != nil {
  404. t.Fatalf("Allocation.UnmarshalJSON: unexpected error: %s", err)
  405. }
  406. // TODO:CLEANUP fix json marshaling of Window so that all of this works.
  407. // In the meantime, just set the Window so that we can test the rest.
  408. after.Window = before.Window.Clone()
  409. // TODO Sean: fix JSON marshaling of PVs
  410. after.PVs = before.PVs
  411. if !after.Equal(before) {
  412. t.Fatalf("Allocation.MarshalJSON: before and after are not equal")
  413. }
  414. }
  415. func TestAllocationSet_generateKey(t *testing.T) {
  416. var alloc *Allocation
  417. var key string
  418. props := []string{
  419. AllocationClusterProp,
  420. }
  421. key = alloc.generateKey(props, nil)
  422. if key != "" {
  423. t.Fatalf("generateKey: expected \"\"; actual \"%s\"", key)
  424. }
  425. alloc = &Allocation{}
  426. alloc.Properties = &AllocationProperties{
  427. Cluster: "cluster1",
  428. Labels: map[string]string{
  429. "app": "app1",
  430. "env": "env1",
  431. },
  432. }
  433. key = alloc.generateKey(props, nil)
  434. if key != "cluster1" {
  435. t.Fatalf("generateKey: expected \"cluster1\"; actual \"%s\"", key)
  436. }
  437. props = []string{
  438. AllocationClusterProp,
  439. AllocationNamespaceProp,
  440. "label:app",
  441. }
  442. key = alloc.generateKey(props, nil)
  443. if key != "cluster1//app=app1" {
  444. t.Fatalf("generateKey: expected \"cluster1//app=app1\"; actual \"%s\"", key)
  445. }
  446. alloc.Properties = &AllocationProperties{
  447. Cluster: "cluster1",
  448. Namespace: "namespace1",
  449. Labels: map[string]string{
  450. "app": "app1",
  451. "env": "env1",
  452. },
  453. }
  454. key = alloc.generateKey(props, nil)
  455. if key != "cluster1/namespace1/app=app1" {
  456. t.Fatalf("generateKey: expected \"cluster1/namespace1/app=app1\"; actual \"%s\"", key)
  457. }
  458. props = []string{
  459. AllocationDepartmentProp,
  460. AllocationEnvironmentProp,
  461. AllocationOwnerProp,
  462. AllocationProductProp,
  463. AllocationTeamProp,
  464. }
  465. labelConfig := NewLabelConfig()
  466. alloc.Properties = &AllocationProperties{
  467. Cluster: "cluster1",
  468. Namespace: "namespace1",
  469. Labels: map[string]string{
  470. labelConfig.DepartmentLabel: "dept1",
  471. labelConfig.EnvironmentLabel: "envt1",
  472. labelConfig.OwnerLabel: "ownr1",
  473. labelConfig.ProductLabel: "prod1",
  474. labelConfig.TeamLabel: "team1",
  475. },
  476. }
  477. key = alloc.generateKey(props, nil)
  478. if key != "dept1/envt1/ownr1/prod1/team1" {
  479. t.Fatalf("generateKey: expected \"dept1/envt1/ownr1/prod1/team1\"; actual \"%s\"", key)
  480. }
  481. // Ensure that labels with illegal Prometheus characters in LabelConfig
  482. // still match their sanitized values. Ensure also that multiple comma-
  483. // separated values work.
  484. labelConfig.DepartmentLabel = "prom/illegal-department"
  485. labelConfig.EnvironmentLabel = " env "
  486. labelConfig.OwnerLabel = "$owner%"
  487. labelConfig.ProductLabel = "app.kubernetes.io/app"
  488. labelConfig.TeamLabel = "team,app.kubernetes.io/team,k8s-team"
  489. alloc.Properties = &AllocationProperties{
  490. Cluster: "cluster1",
  491. Namespace: "namespace1",
  492. Labels: map[string]string{
  493. "prom_illegal_department": "dept1",
  494. "env": "envt1",
  495. "_owner_": "ownr1",
  496. "team": "team1",
  497. "app_kubernetes_io_app": "prod1",
  498. "app_kubernetes_io_team": "team2",
  499. },
  500. }
  501. props = []string{
  502. AllocationDepartmentProp,
  503. AllocationEnvironmentProp,
  504. AllocationOwnerProp,
  505. AllocationProductProp,
  506. AllocationTeamProp,
  507. }
  508. key = alloc.generateKey(props, labelConfig)
  509. if key != "dept1/envt1/ownr1/prod1/team1/team2/__unallocated__" {
  510. t.Fatalf("generateKey: expected \"dept1/envt1/ownr1/prod1/team1/team2/__unallocated__\"; actual \"%s\"", key)
  511. }
  512. }
  513. func TestNewAllocationSet(t *testing.T) {
  514. // TODO niko/etl
  515. }
  516. func assertAllocationSetTotals(t *testing.T, as *AllocationSet, msg string, err error, length int, totalCost float64) {
  517. if err != nil {
  518. t.Fatalf("AllocationSet.AggregateBy[%s]: unexpected error: %s", msg, err)
  519. }
  520. if as.Length() != length {
  521. t.Fatalf("AllocationSet.AggregateBy[%s]: expected set of length %d, actual %d", msg, length, as.Length())
  522. }
  523. if math.Round(as.TotalCost()*100) != math.Round(totalCost*100) {
  524. t.Fatalf("AllocationSet.AggregateBy[%s]: expected total cost %.2f, actual %.2f", msg, totalCost, as.TotalCost())
  525. }
  526. }
  527. func assertAllocationTotals(t *testing.T, as *AllocationSet, msg string, exps map[string]float64) {
  528. as.Each(func(k string, a *Allocation) {
  529. if exp, ok := exps[a.Name]; ok {
  530. if math.Round(a.TotalCost()*100) != math.Round(exp*100) {
  531. t.Fatalf("AllocationSet.AggregateBy[%s]: expected total cost %f, actual %f", msg, exp, a.TotalCost())
  532. }
  533. } else {
  534. t.Fatalf("AllocationSet.AggregateBy[%s]: unexpected allocation: %s", msg, a.Name)
  535. }
  536. })
  537. }
  538. func assertAllocationWindow(t *testing.T, as *AllocationSet, msg string, expStart, expEnd time.Time, expMinutes float64) {
  539. as.Each(func(k string, a *Allocation) {
  540. if !a.Start.Equal(expStart) {
  541. t.Fatalf("AllocationSet.AggregateBy[%s]: expected start %s, actual %s", msg, expStart, a.Start)
  542. }
  543. if !a.End.Equal(expEnd) {
  544. t.Fatalf("AllocationSet.AggregateBy[%s]: expected end %s, actual %s", msg, expEnd, a.End)
  545. }
  546. if a.Minutes() != expMinutes {
  547. t.Fatalf("AllocationSet.AggregateBy[%s]: expected minutes %f, actual %f", msg, expMinutes, a.Minutes())
  548. }
  549. })
  550. }
  551. func printAllocationSet(msg string, as *AllocationSet) {
  552. fmt.Printf("--- %s ---\n", msg)
  553. as.Each(func(k string, a *Allocation) {
  554. fmt.Printf(" > %s\n", a)
  555. })
  556. }
  557. func TestAllocationSet_AggregateBy(t *testing.T) {
  558. // Test AggregateBy against the following workload topology, which is
  559. // generated by GenerateMockAllocationSet:
  560. // | Hierarchy | Cost | CPU | RAM | GPU | PV | Net | LB |
  561. // +----------------------------------------+------+------+------+------+------+------+------+
  562. // cluster1:
  563. // idle: 20.00 5.00 15.00 0.00 0.00 0.00 0.00
  564. // namespace1:
  565. // pod1:
  566. // container1: [app=app1, env=env1] 16.00 1.00 11.00 1.00 1.00 1.00 1.00
  567. // pod-abc: (deployment1)
  568. // container2: 6.00 1.00 1.00 1.00 1.00 1.00 1.00
  569. // pod-def: (deployment1)
  570. // container3: 6.00 1.00 1.00 1.00 1.00 1.00 1.00
  571. // namespace2:
  572. // pod-ghi: (deployment2)
  573. // container4: [app=app2, env=env2] 6.00 1.00 1.00 1.00 1.00 1.00 1.00
  574. // container5: [app=app2, env=env2] 6.00 1.00 1.00 1.00 1.00 1.00 1.00
  575. // pod-jkl: (daemonset1)
  576. // container6: {service1} 6.00 1.00 1.00 1.00 1.00 1.00 1.00
  577. // +-----------------------------------------+------+------+------+------+------+------+------+
  578. // cluster1 subtotal 66.00 11.00 31.00 6.00 6.00 6.00 6.00
  579. // +-----------------------------------------+------+------+------+------+------+------+------+
  580. // cluster2:
  581. // idle: 10.00 5.00 5.00 0.00 0.00 0.00 0.00
  582. // namespace2:
  583. // pod-mno: (deployment2)
  584. // container4: [app=app2] 6.00 1.00 1.00 1.00 1.00 1.00 1.00
  585. // container5: [app=app2] 6.00 1.00 1.00 1.00 1.00 1.00 1.00
  586. // pod-pqr: (daemonset1)
  587. // container6: {service1} 6.00 1.00 1.00 1.00 1.00 1.00 1.00
  588. // namespace3:
  589. // pod-stu: (deployment3)
  590. // container7: an[team=team1] 6.00 1.00 1.00 1.00 1.00 1.00 1.00
  591. // pod-vwx: (statefulset1)
  592. // container8: an[team=team2] 6.00 1.00 1.00 1.00 1.00 1.00 1.00
  593. // container9: an[team=team1] 6.00 1.00 1.00 1.00 1.00 1.00 1.00
  594. // +----------------------------------------+------+------+------+------+------+------+------+
  595. // cluster2 subtotal 46.00 11.00 11.00 6.00 6.00 6.00 6.00
  596. // +----------------------------------------+------+------+------+------+------+------+------+
  597. // total 112.00 22.00 42.00 12.00 12.00 12.00 12.00
  598. // +----------------------------------------+------+------+------+------+------+------+------+
  599. // Scenarios to test:
  600. // 1 Single-aggregation
  601. // 1a AggregationProperties=(Cluster)
  602. // 1b AggregationProperties=(Namespace)
  603. // 1c AggregationProperties=(Pod)
  604. // 1d AggregationProperties=(Container)
  605. // 1e AggregationProperties=(ControllerKind)
  606. // 1f AggregationProperties=(Controller)
  607. // 1g AggregationProperties=(Service)
  608. // 1h AggregationProperties=(Label:app)
  609. // 2 Multi-aggregation
  610. // 2a AggregationProperties=(Cluster, Namespace)
  611. // 2b AggregationProperties=(Namespace, Label:app)
  612. // 2c AggregationProperties=(Cluster, Namespace, Pod, Container)
  613. // 2d AggregationProperties=(Label:app, Label:environment)
  614. // 3 Share idle
  615. // 3a AggregationProperties=(Namespace) ShareIdle=ShareWeighted
  616. // 3b AggregationProperties=(Namespace) ShareIdle=ShareEven (TODO niko/etl)
  617. // 4 Share resources
  618. // 4a Share namespace ShareEven
  619. // 4b Share cluster ShareWeighted
  620. // 4c Share label ShareEven
  621. // 4d Share overhead ShareWeighted
  622. // 5 Filters
  623. // 5a Filter by cluster with separate idle
  624. // 5b Filter by cluster with shared idle
  625. // TODO niko/idle more filter tests
  626. // 6 Combinations and options
  627. // 6a SplitIdle
  628. // 6b Share idle with filters
  629. // 6c Share resources with filters
  630. // 6d Share idle and share resources
  631. // 6e IdleByNode
  632. // 7 Edge cases and errors
  633. // 7a Empty AggregationProperties
  634. // 7b Filter all
  635. // 7c Share all
  636. // 7d Share and filter the same allocations
  637. // Definitions and set-up:
  638. var as *AllocationSet
  639. var err error
  640. endYesterday := time.Now().UTC().Truncate(day)
  641. startYesterday := endYesterday.Add(-day)
  642. numClusters := 2
  643. numNamespaces := 3
  644. numPods := 9
  645. numContainers := 9
  646. numControllerKinds := 3
  647. numControllers := 5
  648. numServices := 1
  649. numLabelApps := 2
  650. // By default, idle is reported as a single, merged allocation
  651. numIdle := 1
  652. // There will only ever be one __unallocated__
  653. numUnallocated := 1
  654. // There are two clusters, so each gets an idle entry when they are split
  655. numSplitIdleCluster := 2
  656. // There are two clusters, so each gets an idle entry when they are split
  657. numSplitIdleNode := 4
  658. activeTotalCost := 82.0
  659. idleTotalCost := 30.0
  660. sharedOverheadHourlyCost := 7.0
  661. // Match Functions
  662. isNamespace3 := func(a *Allocation) bool {
  663. ns := a.Properties.Namespace
  664. return ns == "namespace3"
  665. }
  666. isApp1 := func(a *Allocation) bool {
  667. ls := a.Properties.Labels
  668. if app, ok := ls["app"]; ok && app == "app1" {
  669. return true
  670. }
  671. return false
  672. }
  673. // Filters
  674. isNamespace := func(matchNamespace string) func(*Allocation) bool {
  675. return func(a *Allocation) bool {
  676. namespace := a.Properties.Namespace
  677. return namespace == matchNamespace
  678. }
  679. }
  680. end := time.Now().UTC().Truncate(day)
  681. start := end.Add(-day)
  682. // Tests:
  683. cases := map[string]struct {
  684. start time.Time
  685. aggBy []string
  686. aggOpts *AllocationAggregationOptions
  687. numResults int
  688. totalCost float64
  689. results map[string]float64
  690. windowStart time.Time
  691. windowEnd time.Time
  692. expMinutes float64
  693. }{
  694. // 1 Single-aggregation
  695. // 1a AggregationProperties=(Cluster)
  696. "1a": {
  697. start: start,
  698. aggBy: []string{AllocationClusterProp},
  699. aggOpts: nil,
  700. numResults: numClusters + numIdle,
  701. totalCost: activeTotalCost + idleTotalCost,
  702. results: map[string]float64{
  703. "cluster1": 46.00,
  704. "cluster2": 36.00,
  705. IdleSuffix: 30.00,
  706. },
  707. windowStart: startYesterday,
  708. windowEnd: endYesterday,
  709. expMinutes: 1440.0,
  710. },
  711. // 1b AggregationProperties=(Namespace)
  712. "1b": {
  713. start: start,
  714. aggBy: []string{AllocationNamespaceProp},
  715. aggOpts: nil,
  716. numResults: numNamespaces + numIdle,
  717. totalCost: activeTotalCost + idleTotalCost,
  718. results: map[string]float64{
  719. "namespace1": 28.00,
  720. "namespace2": 36.00,
  721. "namespace3": 18.00,
  722. IdleSuffix: 30.00,
  723. },
  724. windowStart: startYesterday,
  725. windowEnd: endYesterday,
  726. expMinutes: 1440.0,
  727. },
  728. // 1c AggregationProperties=(Pod)
  729. "1c": {
  730. start: start,
  731. aggBy: []string{AllocationPodProp},
  732. aggOpts: nil,
  733. numResults: numPods + numIdle,
  734. totalCost: activeTotalCost + idleTotalCost,
  735. results: map[string]float64{
  736. "pod-jkl": 6.00,
  737. "pod-stu": 6.00,
  738. "pod-abc": 6.00,
  739. "pod-pqr": 6.00,
  740. "pod-def": 6.00,
  741. "pod-vwx": 12.00,
  742. "pod1": 16.00,
  743. "pod-mno": 12.00,
  744. "pod-ghi": 12.00,
  745. IdleSuffix: 30.00,
  746. },
  747. windowStart: startYesterday,
  748. windowEnd: endYesterday,
  749. expMinutes: 1440.0,
  750. },
  751. // 1d AggregationProperties=(Container)
  752. "1d": {
  753. start: start,
  754. aggBy: []string{AllocationContainerProp},
  755. aggOpts: nil,
  756. numResults: numContainers + numIdle,
  757. totalCost: activeTotalCost + idleTotalCost,
  758. results: map[string]float64{
  759. "container2": 6.00,
  760. "container9": 6.00,
  761. "container6": 12.00,
  762. "container3": 6.00,
  763. "container4": 12.00,
  764. "container7": 6.00,
  765. "container8": 6.00,
  766. "container5": 12.00,
  767. "container1": 16.00,
  768. IdleSuffix: 30.00,
  769. },
  770. windowStart: startYesterday,
  771. windowEnd: endYesterday,
  772. expMinutes: 1440.0,
  773. },
  774. // 1e AggregationProperties=(ControllerKind)
  775. "1e": {
  776. start: start,
  777. aggBy: []string{AllocationControllerKindProp},
  778. aggOpts: nil,
  779. numResults: numControllerKinds + numIdle + numUnallocated,
  780. totalCost: activeTotalCost + idleTotalCost,
  781. results: map[string]float64{
  782. "daemonset": 12.00,
  783. "deployment": 42.00,
  784. "statefulset": 12.00,
  785. IdleSuffix: 30.00,
  786. UnallocatedSuffix: 16.00,
  787. },
  788. windowStart: startYesterday,
  789. windowEnd: endYesterday,
  790. expMinutes: 1440.0,
  791. },
  792. // 1f AggregationProperties=(Controller)
  793. "1f": {
  794. start: start,
  795. aggBy: []string{AllocationControllerProp},
  796. aggOpts: nil,
  797. numResults: numControllers + numIdle + numUnallocated,
  798. totalCost: activeTotalCost + idleTotalCost,
  799. results: map[string]float64{
  800. "deployment:deployment2": 24.00,
  801. "daemonset:daemonset1": 12.00,
  802. "deployment:deployment3": 6.00,
  803. "statefulset:statefulset1": 12.00,
  804. "deployment:deployment1": 12.00,
  805. IdleSuffix: 30.00,
  806. UnallocatedSuffix: 16.00,
  807. },
  808. windowStart: startYesterday,
  809. windowEnd: endYesterday,
  810. expMinutes: 1440.0,
  811. },
  812. // 1g AggregationProperties=(Service)
  813. "1g": {
  814. start: start,
  815. aggBy: []string{AllocationServiceProp},
  816. aggOpts: nil,
  817. numResults: numServices + numIdle + numUnallocated,
  818. totalCost: activeTotalCost + idleTotalCost,
  819. results: map[string]float64{
  820. "service1": 12.00,
  821. IdleSuffix: 30.00,
  822. UnallocatedSuffix: 70.00,
  823. },
  824. windowStart: startYesterday,
  825. windowEnd: endYesterday,
  826. expMinutes: 1440.0,
  827. },
  828. // 1h AggregationProperties=(Label:app)
  829. "1h": {
  830. start: start,
  831. aggBy: []string{"label:app"},
  832. aggOpts: nil,
  833. numResults: numLabelApps + numIdle + numUnallocated,
  834. totalCost: activeTotalCost + idleTotalCost,
  835. results: map[string]float64{
  836. "app=app1": 16.00,
  837. "app=app2": 24.00,
  838. IdleSuffix: 30.00,
  839. UnallocatedSuffix: 42.00,
  840. },
  841. windowStart: startYesterday,
  842. windowEnd: endYesterday,
  843. expMinutes: 1440.0,
  844. },
  845. // 1i AggregationProperties=(deployment)
  846. "1i": {
  847. start: start,
  848. aggBy: []string{AllocationDeploymentProp},
  849. aggOpts: nil,
  850. numResults: 3 + numIdle + numUnallocated,
  851. totalCost: activeTotalCost + idleTotalCost,
  852. results: map[string]float64{
  853. "deployment1": 12.00,
  854. "deployment2": 24.00,
  855. "deployment3": 6.00,
  856. IdleSuffix: 30.00,
  857. UnallocatedSuffix: 40.00,
  858. },
  859. windowStart: startYesterday,
  860. windowEnd: endYesterday,
  861. expMinutes: 1440.0,
  862. },
  863. // 1j AggregationProperties=(Annotation:team)
  864. "1j": {
  865. start: start,
  866. aggBy: []string{"annotation:team"},
  867. aggOpts: nil,
  868. numResults: 2 + numIdle + numUnallocated,
  869. totalCost: activeTotalCost + idleTotalCost,
  870. results: map[string]float64{
  871. "team=team1": 12.00,
  872. "team=team2": 6.00,
  873. IdleSuffix: 30.00,
  874. UnallocatedSuffix: 64.00,
  875. },
  876. windowStart: startYesterday,
  877. windowEnd: endYesterday,
  878. expMinutes: 1440.0,
  879. },
  880. // 1k AggregationProperties=(daemonSet)
  881. "1k": {
  882. start: start,
  883. aggBy: []string{AllocationDaemonSetProp},
  884. aggOpts: nil,
  885. numResults: 1 + numIdle + numUnallocated,
  886. totalCost: activeTotalCost + idleTotalCost,
  887. results: map[string]float64{
  888. "daemonset1": 12.00,
  889. IdleSuffix: 30.00,
  890. UnallocatedSuffix: 70.00,
  891. },
  892. windowStart: startYesterday,
  893. windowEnd: endYesterday,
  894. expMinutes: 1440.0,
  895. },
  896. // 1l AggregationProperties=(statefulSet)
  897. "1l": {
  898. start: start,
  899. aggBy: []string{AllocationStatefulSetProp},
  900. aggOpts: nil,
  901. numResults: 1 + numIdle + numUnallocated,
  902. totalCost: activeTotalCost + idleTotalCost,
  903. results: map[string]float64{
  904. "statefulset1": 12.00,
  905. IdleSuffix: 30.00,
  906. UnallocatedSuffix: 70.00,
  907. },
  908. windowStart: startYesterday,
  909. windowEnd: endYesterday,
  910. expMinutes: 1440.0,
  911. },
  912. // 2 Multi-aggregation
  913. // 2a AggregationProperties=(Cluster, Namespace)
  914. // 2b AggregationProperties=(Namespace, Label:app)
  915. // 2c AggregationProperties=(Cluster, Namespace, Pod, Container)
  916. // 2d AggregationProperties=(Label:app, Label:environment)
  917. "2d": {
  918. start: start,
  919. aggBy: []string{"label:app", "label:env"},
  920. aggOpts: nil,
  921. numResults: 3 + numIdle + numUnallocated,
  922. totalCost: activeTotalCost + idleTotalCost,
  923. // sets should be {idle, unallocated, app1/env1, app2/env2, app2/unallocated}
  924. results: map[string]float64{
  925. "app=app1/env=env1": 16.00,
  926. "app=app2/env=env2": 12.00,
  927. "app=app2/" + UnallocatedSuffix: 12.00,
  928. IdleSuffix: 30.00,
  929. UnallocatedSuffix + "/" + UnallocatedSuffix: 42.00,
  930. },
  931. windowStart: startYesterday,
  932. windowEnd: endYesterday,
  933. expMinutes: 1440.0,
  934. },
  935. // 2e AggregationProperties=(Cluster, Label:app, Label:environment)
  936. "2e": {
  937. start: start,
  938. aggBy: []string{AllocationClusterProp, "label:app", "label:env"},
  939. aggOpts: nil,
  940. numResults: 6,
  941. totalCost: activeTotalCost + idleTotalCost,
  942. results: map[string]float64{
  943. "cluster1/app=app2/env=env2": 12.00,
  944. "__idle__": 30.00,
  945. "cluster1/app=app1/env=env1": 16.00,
  946. "cluster1/" + UnallocatedSuffix + "/" + UnallocatedSuffix: 18.00,
  947. "cluster2/app=app2/" + UnallocatedSuffix: 12.00,
  948. "cluster2/" + UnallocatedSuffix + "/" + UnallocatedSuffix: 24.00,
  949. },
  950. windowStart: startYesterday,
  951. windowEnd: endYesterday,
  952. expMinutes: 1440.0,
  953. },
  954. // 2f AggregationProperties=(annotation:team, pod)
  955. "2f": {
  956. start: start,
  957. aggBy: []string{AllocationPodProp, "annotation:team"},
  958. aggOpts: nil,
  959. numResults: 11,
  960. totalCost: activeTotalCost + idleTotalCost,
  961. results: map[string]float64{
  962. "pod-jkl/" + UnallocatedSuffix: 6.00,
  963. "pod-stu/team=team1": 6.00,
  964. "pod-abc/" + UnallocatedSuffix: 6.00,
  965. "pod-pqr/" + UnallocatedSuffix: 6.00,
  966. "pod-def/" + UnallocatedSuffix: 6.00,
  967. "pod-vwx/team=team1": 6.00,
  968. "pod-vwx/team=team2": 6.00,
  969. "pod1/" + UnallocatedSuffix: 16.00,
  970. "pod-mno/" + UnallocatedSuffix: 12.00,
  971. "pod-ghi/" + UnallocatedSuffix: 12.00,
  972. IdleSuffix: 30.00,
  973. },
  974. windowStart: startYesterday,
  975. windowEnd: endYesterday,
  976. expMinutes: 1440.0,
  977. },
  978. // 3 Share idle
  979. // 3a AggregationProperties=(Namespace) ShareIdle=ShareWeighted
  980. // namespace1: 42.6875 = 28.00 + 5.00*(3.00/6.00) + 15.0*(13.0/16.0)
  981. // namespace2: 46.3125 = 36.00 + 5.0*(3.0/6.0) + 15.0*(3.0/16.0) + 5.0*(3.0/6.0) + 5.0*(3.0/6.0)
  982. // namespace3: 23.0000 = 18.00 + 5.0*(3.0/6.0) + 5.0*(3.0/6.0)
  983. "3a": {
  984. start: start,
  985. aggBy: []string{AllocationNamespaceProp},
  986. aggOpts: &AllocationAggregationOptions{ShareIdle: ShareWeighted},
  987. numResults: numNamespaces,
  988. totalCost: activeTotalCost + idleTotalCost,
  989. results: map[string]float64{
  990. "namespace1": 42.69,
  991. "namespace2": 46.31,
  992. "namespace3": 23.00,
  993. },
  994. windowStart: startYesterday,
  995. windowEnd: endYesterday,
  996. expMinutes: 1440.0,
  997. },
  998. // 3b: sharing idle evenly is deprecated
  999. // 4 Share resources
  1000. // 4a Share namespace ShareEven
  1001. // namespace1: 37.5000 = 28.00 + 18.00*(1.0/2.0)
  1002. // namespace2: 45.5000 = 36.00 + 18.00*(1.0/2.0)
  1003. // idle: 30.0000
  1004. "4a": {
  1005. start: start,
  1006. aggBy: []string{AllocationNamespaceProp},
  1007. aggOpts: &AllocationAggregationOptions{
  1008. ShareFuncs: []AllocationMatchFunc{isNamespace3},
  1009. ShareSplit: ShareEven,
  1010. },
  1011. numResults: numNamespaces,
  1012. totalCost: activeTotalCost + idleTotalCost,
  1013. results: map[string]float64{
  1014. "namespace1": 37.00,
  1015. "namespace2": 45.00,
  1016. IdleSuffix: 30.00,
  1017. },
  1018. windowStart: startYesterday,
  1019. windowEnd: endYesterday,
  1020. expMinutes: 1440.0,
  1021. },
  1022. // 4b Share namespace ShareWeighted
  1023. // namespace1: 32.5000 =
  1024. // namespace2: 37.5000 =
  1025. // idle: 30.0000
  1026. "4b": {
  1027. start: start,
  1028. aggBy: []string{AllocationNamespaceProp},
  1029. aggOpts: &AllocationAggregationOptions{
  1030. ShareFuncs: []AllocationMatchFunc{isNamespace3},
  1031. ShareSplit: ShareWeighted,
  1032. },
  1033. numResults: numNamespaces,
  1034. totalCost: activeTotalCost + idleTotalCost,
  1035. results: map[string]float64{
  1036. "namespace1": 35.88,
  1037. "namespace2": 46.125,
  1038. IdleSuffix: 30.00,
  1039. },
  1040. windowStart: startYesterday,
  1041. windowEnd: endYesterday,
  1042. expMinutes: 1440.0,
  1043. },
  1044. // 4c Share label ShareEven
  1045. // namespace1: 17.3333 = 28.00 - 16.00 + 16.00*(1.0/3.0)
  1046. // namespace2: 41.3333 = 36.00 + 16.00*(1.0/3.0)
  1047. // namespace3: 23.3333 = 18.00 + 16.00*(1.0/3.0)
  1048. // idle: 30.0000
  1049. "4c": {
  1050. start: start,
  1051. aggBy: []string{AllocationNamespaceProp},
  1052. aggOpts: &AllocationAggregationOptions{
  1053. ShareFuncs: []AllocationMatchFunc{isApp1},
  1054. ShareSplit: ShareEven,
  1055. },
  1056. numResults: numNamespaces + numIdle,
  1057. totalCost: activeTotalCost + idleTotalCost,
  1058. results: map[string]float64{
  1059. "namespace1": 17.33,
  1060. "namespace2": 41.33,
  1061. "namespace3": 23.33,
  1062. IdleSuffix: 30.00,
  1063. },
  1064. windowStart: startYesterday,
  1065. windowEnd: endYesterday,
  1066. expMinutes: 1440.0,
  1067. },
  1068. // 4d Share overhead ShareWeighted
  1069. // namespace1: 85.366 = 28.00 + (7.0*24.0)*(28.00/82.00)
  1070. // namespace2: 109.756 = 36.00 + (7.0*24.0)*(36.00/82.00)
  1071. // namespace3: 54.878 = 18.00 + (7.0*24.0)*(18.00/82.00)
  1072. // idle: 30.0000
  1073. "4d": {
  1074. start: start,
  1075. aggBy: []string{AllocationNamespaceProp},
  1076. aggOpts: &AllocationAggregationOptions{
  1077. SharedHourlyCosts: map[string]float64{"total": sharedOverheadHourlyCost},
  1078. ShareSplit: ShareWeighted,
  1079. },
  1080. numResults: numNamespaces + numIdle,
  1081. totalCost: activeTotalCost + idleTotalCost + (sharedOverheadHourlyCost * 24.0),
  1082. results: map[string]float64{
  1083. "namespace1": 85.366,
  1084. "namespace2": 109.756,
  1085. "namespace3": 54.878,
  1086. IdleSuffix: 30.00,
  1087. },
  1088. windowStart: startYesterday,
  1089. windowEnd: endYesterday,
  1090. expMinutes: 1440.0,
  1091. },
  1092. // 5 Filters
  1093. // 5a Filter by cluster with separate idle
  1094. "5a": {
  1095. start: start,
  1096. aggBy: []string{AllocationClusterProp},
  1097. aggOpts: &AllocationAggregationOptions{
  1098. Filter: AllocationFilterCondition{
  1099. Field: FilterClusterID,
  1100. Op: FilterEquals,
  1101. Value: "cluster1",
  1102. },
  1103. ShareIdle: ShareNone,
  1104. },
  1105. numResults: 1 + numIdle,
  1106. totalCost: 66.0,
  1107. results: map[string]float64{
  1108. "cluster1": 46.00,
  1109. IdleSuffix: 20.00,
  1110. },
  1111. windowStart: startYesterday,
  1112. windowEnd: endYesterday,
  1113. expMinutes: 1440.0,
  1114. },
  1115. // 5b Filter by cluster with shared idle
  1116. "5b": {
  1117. start: start,
  1118. aggBy: []string{AllocationClusterProp},
  1119. aggOpts: &AllocationAggregationOptions{
  1120. Filter: AllocationFilterCondition{Field: FilterClusterID, Op: FilterEquals, Value: "cluster1"},
  1121. ShareIdle: ShareWeighted,
  1122. },
  1123. numResults: 1,
  1124. totalCost: 66.0,
  1125. results: map[string]float64{
  1126. "cluster1": 66.00,
  1127. },
  1128. windowStart: startYesterday,
  1129. windowEnd: endYesterday,
  1130. expMinutes: 1440.0,
  1131. },
  1132. // 5c Filter by cluster, agg by namespace, with separate idle
  1133. "5c": {
  1134. start: start,
  1135. aggBy: []string{AllocationNamespaceProp},
  1136. aggOpts: &AllocationAggregationOptions{
  1137. Filter: AllocationFilterCondition{Field: FilterClusterID, Op: FilterEquals, Value: "cluster1"},
  1138. ShareIdle: ShareNone,
  1139. },
  1140. numResults: 2 + numIdle,
  1141. totalCost: 66.0,
  1142. results: map[string]float64{
  1143. "namespace1": 28.00,
  1144. "namespace2": 18.00,
  1145. IdleSuffix: 20.00,
  1146. },
  1147. windowStart: startYesterday,
  1148. windowEnd: endYesterday,
  1149. expMinutes: 1440.0,
  1150. },
  1151. // 5d Filter by namespace, agg by cluster, with separate idle
  1152. "5d": {
  1153. start: start,
  1154. aggBy: []string{AllocationClusterProp},
  1155. aggOpts: &AllocationAggregationOptions{
  1156. Filter: AllocationFilterCondition{Field: FilterNamespace, Op: FilterEquals, Value: "namespace2"},
  1157. ShareIdle: ShareNone,
  1158. },
  1159. numResults: numClusters + numIdle,
  1160. totalCost: 46.31,
  1161. results: map[string]float64{
  1162. "cluster1": 18.00,
  1163. "cluster2": 18.00,
  1164. IdleSuffix: 10.31,
  1165. },
  1166. windowStart: startYesterday,
  1167. windowEnd: endYesterday,
  1168. expMinutes: 1440.0,
  1169. },
  1170. // 6 Combinations and options
  1171. // 6a SplitIdle
  1172. "6a": {
  1173. start: start,
  1174. aggBy: []string{AllocationNamespaceProp},
  1175. aggOpts: &AllocationAggregationOptions{
  1176. SplitIdle: true,
  1177. },
  1178. numResults: numNamespaces + numSplitIdleCluster,
  1179. totalCost: activeTotalCost + idleTotalCost,
  1180. results: map[string]float64{
  1181. "namespace1": 28.00,
  1182. "namespace2": 36.00,
  1183. "namespace3": 18.00,
  1184. fmt.Sprintf("cluster1/%s", IdleSuffix): 20.00,
  1185. fmt.Sprintf("cluster2/%s", IdleSuffix): 10.00,
  1186. },
  1187. windowStart: startYesterday,
  1188. windowEnd: endYesterday,
  1189. expMinutes: 1440.0,
  1190. },
  1191. // 6b Share idle weighted with filters
  1192. // Should match values from unfiltered aggregation (3a)
  1193. // namespace2: 46.3125 = 36.00 + 5.0*(3.0/6.0) + 15.0*(3.0/16.0) + 5.0*(3.0/6.0) + 5.0*(3.0/6.0)
  1194. "6b": {
  1195. start: start,
  1196. aggBy: []string{AllocationNamespaceProp},
  1197. aggOpts: &AllocationAggregationOptions{
  1198. Filter: AllocationFilterCondition{Field: FilterNamespace, Op: FilterEquals, Value: "namespace2"},
  1199. ShareIdle: ShareWeighted,
  1200. },
  1201. numResults: 1,
  1202. totalCost: 46.31,
  1203. results: map[string]float64{
  1204. "namespace2": 46.31,
  1205. },
  1206. windowStart: startYesterday,
  1207. windowEnd: endYesterday,
  1208. expMinutes: 1440.0,
  1209. },
  1210. // 6c Share idle even with filters (share idle even is deprecated)
  1211. // 6d Share overhead with filters
  1212. // namespace1: 85.366 = 28.00 + (7.0*24.0)*(28.00/82.00)
  1213. // namespace2: 109.756 = 36.00 + (7.0*24.0)*(36.00/82.00)
  1214. // namespace3: 54.878 = 18.00 + (7.0*24.0)*(18.00/82.00)
  1215. // idle: 10.3125 = % of idle paired with namespace2
  1216. // Then namespace 2 is filtered.
  1217. "6d": {
  1218. start: start,
  1219. aggBy: []string{AllocationNamespaceProp},
  1220. aggOpts: &AllocationAggregationOptions{
  1221. Filter: AllocationFilterCondition{Field: FilterNamespace, Op: FilterEquals, Value: "namespace2"},
  1222. SharedHourlyCosts: map[string]float64{"total": sharedOverheadHourlyCost},
  1223. ShareSplit: ShareWeighted,
  1224. },
  1225. numResults: 1 + numIdle,
  1226. totalCost: 120.0686,
  1227. results: map[string]float64{
  1228. "namespace2": 109.7561,
  1229. IdleSuffix: 10.3125,
  1230. },
  1231. windowStart: startYesterday,
  1232. windowEnd: endYesterday,
  1233. expMinutes: 1440.0,
  1234. },
  1235. // 6e Share resources with filters
  1236. "6e": {
  1237. start: start,
  1238. aggBy: []string{AllocationNamespaceProp},
  1239. aggOpts: &AllocationAggregationOptions{
  1240. Filter: AllocationFilterCondition{Field: FilterNamespace, Op: FilterEquals, Value: "namespace2"},
  1241. ShareFuncs: []AllocationMatchFunc{isNamespace("namespace1")},
  1242. ShareSplit: ShareWeighted,
  1243. },
  1244. numResults: 1 + numIdle,
  1245. totalCost: 79.6667, // should be 74.7708, but I'm punting -- too difficult (NK)
  1246. results: map[string]float64{
  1247. "namespace2": 54.6667,
  1248. IdleSuffix: 25.000, // should be 20.1042, but I'm punting -- too difficult (NK)
  1249. },
  1250. windowStart: startYesterday,
  1251. windowEnd: endYesterday,
  1252. expMinutes: 1440.0,
  1253. },
  1254. // 6f Share resources with filters and share idle
  1255. "6f": {
  1256. start: start,
  1257. aggBy: []string{AllocationNamespaceProp},
  1258. aggOpts: &AllocationAggregationOptions{
  1259. Filter: AllocationFilterCondition{Field: FilterNamespace, Op: FilterEquals, Value: "namespace2"},
  1260. ShareFuncs: []AllocationMatchFunc{isNamespace("namespace1")},
  1261. ShareSplit: ShareWeighted,
  1262. ShareIdle: ShareWeighted,
  1263. },
  1264. numResults: 1,
  1265. totalCost: 74.77083,
  1266. results: map[string]float64{
  1267. "namespace2": 74.77083,
  1268. },
  1269. windowStart: startYesterday,
  1270. windowEnd: endYesterday,
  1271. expMinutes: 1440.0,
  1272. },
  1273. // 6g Share idle weighted and share resources weighted
  1274. //
  1275. // First, share idle weighted produces:
  1276. //
  1277. // namespace1: 42.6875
  1278. // initial cost 28.0000
  1279. // cluster1.cpu 2.5000 = 5.00*(3.00/6.00)
  1280. // cluster1.ram 12.1875 = 15.00*(13.0/16.0)
  1281. //
  1282. // namespace2: 46.3125
  1283. // initial cost 36.0000
  1284. // cluster1.cpu 2.5000 = 5.00*(3.0/6.0)
  1285. // cluster1.ram 2.8125 = 15.00*(3.0/16.0)
  1286. // cluster2.cpu 2.5000 = 5.00*(3.0/6.0)
  1287. // cluster2.ram 2.5000 = 5.00*(3.0/6.0)
  1288. //
  1289. // namespace3: 23.0000
  1290. // initial cost 18.0000
  1291. // cluster2.cpu 2.5000 = 5.00*(3.0/6.0)
  1292. // cluster2.ram 2.5000 = 5.00*(3.0/6.0)
  1293. //
  1294. // Then, sharing namespace1 means sharing 39.6875 according to coefficients
  1295. // computed before allocating idle (so that weighting idle differently
  1296. // doesn't adversely affect the sharing mechanism):
  1297. //
  1298. // namespace2: 74.7708
  1299. // initial cost 30.0000
  1300. // idle cost 10.3125
  1301. // shared cost 28.4583 = (42.6875)*(36.0/54.0)
  1302. //
  1303. // namespace3: 37.2292
  1304. // initial cost 18.0000
  1305. // idle cost 5.0000
  1306. // shared cost 14.2292 = (42.6875)*(18.0/54.0)
  1307. "6g": {
  1308. start: start,
  1309. aggBy: []string{AllocationNamespaceProp},
  1310. aggOpts: &AllocationAggregationOptions{
  1311. ShareFuncs: []AllocationMatchFunc{isNamespace("namespace1")},
  1312. ShareSplit: ShareWeighted,
  1313. ShareIdle: ShareWeighted,
  1314. },
  1315. numResults: 2,
  1316. totalCost: activeTotalCost + idleTotalCost,
  1317. results: map[string]float64{
  1318. "namespace2": 74.77,
  1319. "namespace3": 37.23,
  1320. },
  1321. windowStart: startYesterday,
  1322. windowEnd: endYesterday,
  1323. expMinutes: 1440.0,
  1324. },
  1325. // 6h Share idle, share resources, and filter
  1326. //
  1327. // First, share idle weighted produces:
  1328. //
  1329. // namespace1: 42.6875
  1330. // initial cost 28.0000
  1331. // cluster1.cpu 2.5000 = 5.00*(3.00/6.00)
  1332. // cluster1.ram 12.1875 = 15.00*(13.0/16.0)
  1333. //
  1334. // namespace2: 46.3125
  1335. // initial cost 36.0000
  1336. // cluster1.cpu 2.5000 = 5.00*(3.0/6.0)
  1337. // cluster1.ram 2.8125 = 15.00*(3.0/16.0)
  1338. // cluster2.cpu 2.5000 = 5.00*(3.0/6.0)
  1339. // cluster2.ram 2.5000 = 5.00*(3.0/6.0)
  1340. //
  1341. // namespace3: 23.0000
  1342. // initial cost 18.0000
  1343. // cluster2.cpu 2.5000 = 5.00*(3.0/6.0)
  1344. // cluster2.ram 2.5000 = 5.00*(3.0/6.0)
  1345. //
  1346. // Then, sharing namespace1 means sharing 39.6875 according to coefficients
  1347. // computed before allocating idle (so that weighting idle differently
  1348. // doesn't adversely affect the sharing mechanism):
  1349. //
  1350. // namespace2: 74.7708
  1351. // initial cost 36.0000
  1352. // idle cost 10.3125
  1353. // shared cost 28.4583 = (42.6875)*(36.0/54.0)
  1354. //
  1355. // namespace3: 37.2292
  1356. // initial cost 18.0000
  1357. // idle cost 5.0000
  1358. // shared cost 14.2292 = (42.6875)*(18.0/54.0)
  1359. //
  1360. // Then, filter for namespace2: 74.7708
  1361. "6h": {
  1362. start: start,
  1363. aggBy: []string{AllocationNamespaceProp},
  1364. aggOpts: &AllocationAggregationOptions{
  1365. Filter: AllocationFilterCondition{Field: FilterNamespace, Op: FilterEquals, Value: "namespace2"},
  1366. ShareFuncs: []AllocationMatchFunc{isNamespace("namespace1")},
  1367. ShareSplit: ShareWeighted,
  1368. ShareIdle: ShareWeighted,
  1369. },
  1370. numResults: 1,
  1371. totalCost: 74.77,
  1372. results: map[string]float64{
  1373. "namespace2": 74.77,
  1374. },
  1375. windowStart: startYesterday,
  1376. windowEnd: endYesterday,
  1377. expMinutes: 1440.0,
  1378. },
  1379. // 6i Share idle, share resources, share overhead
  1380. //
  1381. // Share idle weighted:
  1382. //
  1383. // namespace1: 42.6875
  1384. // initial cost 28.0000
  1385. // cluster1.cpu 2.5000 = 5.00*(3.00/6.00)
  1386. // cluster1.ram 12.1875 = 15.00*(13.0/16.0)
  1387. //
  1388. // namespace2: 46.3125
  1389. // initial cost 36.0000
  1390. // cluster1.cpu 2.5000 = 5.00*(3.0/6.0)
  1391. // cluster1.ram 2.8125 = 15.00*(3.0/16.0)
  1392. // cluster2.cpu 2.5000 = 5.00*(3.0/6.0)
  1393. // cluster2.ram 2.5000 = 5.00*(3.0/6.0)
  1394. //
  1395. // namespace3: 23.0000
  1396. // initial cost 18.0000
  1397. // cluster2.cpu 2.5000 = 5.00*(3.0/6.0)
  1398. // cluster2.ram 2.5000 = 5.00*(3.0/6.0)
  1399. //
  1400. // Then share overhead:
  1401. //
  1402. // namespace1: 100.0533 = 42.6875 + (7.0*24.0)*(28.00/82.00)
  1403. // namespace2: 120.0686 = 46.3125 + (7.0*24.0)*(36.00/82.00)
  1404. // namespace3: 59.8780 = 23.0000 + (7.0*24.0)*(18.00/82.00)
  1405. //
  1406. // Then namespace 2 is filtered.
  1407. "6i": {
  1408. start: start,
  1409. aggBy: []string{AllocationNamespaceProp},
  1410. aggOpts: &AllocationAggregationOptions{
  1411. Filter: AllocationFilterCondition{Field: FilterNamespace, Op: FilterEquals, Value: "namespace2"},
  1412. ShareSplit: ShareWeighted,
  1413. ShareIdle: ShareWeighted,
  1414. SharedHourlyCosts: map[string]float64{"total": sharedOverheadHourlyCost},
  1415. },
  1416. numResults: 1,
  1417. totalCost: 120.07,
  1418. results: map[string]float64{
  1419. "namespace2": 120.07,
  1420. },
  1421. windowStart: startYesterday,
  1422. windowEnd: endYesterday,
  1423. expMinutes: 1440.0,
  1424. },
  1425. // 6j Idle by Node
  1426. "6j": {
  1427. start: start,
  1428. aggBy: []string{AllocationNamespaceProp},
  1429. aggOpts: &AllocationAggregationOptions{
  1430. IdleByNode: true,
  1431. },
  1432. numResults: numNamespaces + numIdle,
  1433. totalCost: activeTotalCost + idleTotalCost,
  1434. results: map[string]float64{
  1435. "namespace1": 28.00,
  1436. "namespace2": 36.00,
  1437. "namespace3": 18.00,
  1438. IdleSuffix: 30.00,
  1439. },
  1440. windowStart: startYesterday,
  1441. windowEnd: endYesterday,
  1442. expMinutes: 1440.0,
  1443. },
  1444. // 6k Split Idle, Idle by Node
  1445. "6k": {
  1446. start: start,
  1447. aggBy: []string{AllocationNamespaceProp},
  1448. aggOpts: &AllocationAggregationOptions{
  1449. SplitIdle: true,
  1450. IdleByNode: true,
  1451. },
  1452. numResults: numNamespaces + numSplitIdleNode,
  1453. totalCost: activeTotalCost + idleTotalCost,
  1454. results: map[string]float64{
  1455. "namespace1": 28.00,
  1456. "namespace2": 36.00,
  1457. "namespace3": 18.00,
  1458. fmt.Sprintf("c1nodes/%s", IdleSuffix): 20.00,
  1459. fmt.Sprintf("node1/%s", IdleSuffix): 3.333333,
  1460. fmt.Sprintf("node2/%s", IdleSuffix): 3.333333,
  1461. fmt.Sprintf("node3/%s", IdleSuffix): 3.333333,
  1462. },
  1463. windowStart: startYesterday,
  1464. windowEnd: endYesterday,
  1465. expMinutes: 1440.0,
  1466. },
  1467. // Old 6k Share idle Even Idle by Node (share idle even deprecated)
  1468. // 6l Share idle weighted with filters, Idle by Node
  1469. // Should match values from unfiltered aggregation (3a)
  1470. // namespace2: 46.3125 = 36.00 + 5.0*(3.0/6.0) + 15.0*(3.0/16.0) + 5.0*(3.0/6.0) + 5.0*(3.0/6.0)
  1471. "6l": {
  1472. start: start,
  1473. aggBy: []string{AllocationNamespaceProp},
  1474. aggOpts: &AllocationAggregationOptions{
  1475. Filter: AllocationFilterCondition{Field: FilterNamespace, Op: FilterEquals, Value: "namespace2"},
  1476. ShareIdle: ShareWeighted,
  1477. IdleByNode: true,
  1478. },
  1479. numResults: 1,
  1480. totalCost: 46.31,
  1481. results: map[string]float64{
  1482. "namespace2": 46.31,
  1483. },
  1484. windowStart: startYesterday,
  1485. windowEnd: endYesterday,
  1486. expMinutes: 1440.0,
  1487. },
  1488. // 7 Edge cases and errors
  1489. // 7a Empty AggregationProperties
  1490. // 7b Filter all
  1491. // 7c Share all
  1492. // 7d Share and filter the same allocations
  1493. }
  1494. for name, testcase := range cases {
  1495. t.Run(name, func(t *testing.T) {
  1496. if testcase.aggOpts != nil && testcase.aggOpts.IdleByNode {
  1497. as = GenerateMockAllocationSetNodeIdle(testcase.start)
  1498. } else {
  1499. as = GenerateMockAllocationSetClusterIdle(testcase.start)
  1500. }
  1501. err = as.AggregateBy(testcase.aggBy, testcase.aggOpts)
  1502. assertAllocationSetTotals(t, as, name, err, testcase.numResults, testcase.totalCost)
  1503. assertAllocationTotals(t, as, name, testcase.results)
  1504. assertAllocationWindow(t, as, name, testcase.windowStart, testcase.windowEnd, testcase.expMinutes)
  1505. })
  1506. }
  1507. }
  1508. // TODO niko/etl
  1509. //func TestAllocationSet_Clone(t *testing.T) {}
  1510. // TODO niko/etl
  1511. //func TestAllocationSet_Delete(t *testing.T) {}
  1512. // TODO niko/etl
  1513. //func TestAllocationSet_End(t *testing.T) {}
  1514. // TODO niko/etl
  1515. //func TestAllocationSet_IdleAllocations(t *testing.T) {}
  1516. // TODO niko/etl
  1517. //func TestAllocationSet_Insert(t *testing.T) {}
  1518. // Asserts that all Allocations within an AllocationSet have a Window that
  1519. // matches that of the AllocationSet.
  1520. func TestAllocationSet_insertMatchingWindow(t *testing.T) {
  1521. setStart := time.Now().Round(time.Hour)
  1522. setEnd := setStart.Add(1 * time.Hour)
  1523. a1WindowStart := setStart.Add(5 * time.Minute)
  1524. a1WindowEnd := setStart.Add(50 * time.Minute)
  1525. a2WindowStart := setStart.Add(17 * time.Minute)
  1526. a2WindowEnd := setStart.Add(34 * time.Minute)
  1527. a1 := &Allocation{
  1528. Name: "allocation-1",
  1529. Window: Window(NewClosedWindow(a1WindowStart, a1WindowEnd)),
  1530. }
  1531. a2 := &Allocation{
  1532. Name: "allocation-2",
  1533. Window: Window(NewClosedWindow(a2WindowStart, a2WindowEnd)),
  1534. }
  1535. as := NewAllocationSet(setStart, setEnd)
  1536. as.insert(a1)
  1537. as.insert(a2)
  1538. if as.Length() != 2 {
  1539. t.Errorf("AS length got %d, expected %d", as.Length(), 2)
  1540. }
  1541. as.Each(func(k string, a *Allocation) {
  1542. if !(*a.Window.Start()).Equal(setStart) {
  1543. t.Errorf("Allocation %s window start is %s, expected %s", a.Name, *a.Window.Start(), setStart)
  1544. }
  1545. if !(*a.Window.End()).Equal(setEnd) {
  1546. t.Errorf("Allocation %s window end is %s, expected %s", a.Name, *a.Window.End(), setEnd)
  1547. }
  1548. })
  1549. }
  1550. // TODO niko/etl
  1551. //func TestAllocationSet_IsEmpty(t *testing.T) {}
  1552. // TODO niko/etl
  1553. //func TestAllocationSet_Length(t *testing.T) {}
  1554. // TODO niko/etl
  1555. //func TestAllocationSet_Map(t *testing.T) {}
  1556. // TODO niko/etl
  1557. //func TestAllocationSet_MarshalJSON(t *testing.T) {}
  1558. // TODO niko/etl
  1559. //func TestAllocationSet_Resolution(t *testing.T) {}
  1560. // TODO niko/etl
  1561. //func TestAllocationSet_Seconds(t *testing.T) {}
  1562. // TODO niko/etl
  1563. //func TestAllocationSet_Set(t *testing.T) {}
  1564. // TODO niko/etl
  1565. //func TestAllocationSet_Start(t *testing.T) {}
  1566. // TODO niko/etl
  1567. //func TestAllocationSet_TotalCost(t *testing.T) {}
  1568. // TODO niko/etl
  1569. //func TestNewAllocationSetRange(t *testing.T) {}
  1570. func TestAllocationSetRange_Accumulate(t *testing.T) {
  1571. ago2d := time.Now().UTC().Truncate(day).Add(-2 * day)
  1572. yesterday := time.Now().UTC().Truncate(day).Add(-day)
  1573. today := time.Now().UTC().Truncate(day)
  1574. tomorrow := time.Now().UTC().Truncate(day).Add(day)
  1575. // Accumulating any combination of nil and/or empty set should result in empty set
  1576. result, err := NewAllocationSetRange(nil).Accumulate()
  1577. if err != nil {
  1578. t.Fatalf("unexpected error accumulating nil AllocationSetRange: %s", err)
  1579. }
  1580. if !result.IsEmpty() {
  1581. t.Fatalf("accumulating nil AllocationSetRange: expected empty; actual %s", result)
  1582. }
  1583. result, err = NewAllocationSetRange(nil, nil).Accumulate()
  1584. if err != nil {
  1585. t.Fatalf("unexpected error accumulating nil AllocationSetRange: %s", err)
  1586. }
  1587. if !result.IsEmpty() {
  1588. t.Fatalf("accumulating nil AllocationSetRange: expected empty; actual %s", result)
  1589. }
  1590. result, err = NewAllocationSetRange(NewAllocationSet(yesterday, today)).Accumulate()
  1591. if err != nil {
  1592. t.Fatalf("unexpected error accumulating nil AllocationSetRange: %s", err)
  1593. }
  1594. if !result.IsEmpty() {
  1595. t.Fatalf("accumulating nil AllocationSetRange: expected empty; actual %s", result)
  1596. }
  1597. result, err = NewAllocationSetRange(nil, NewAllocationSet(ago2d, yesterday), nil, NewAllocationSet(today, tomorrow), nil).Accumulate()
  1598. if err != nil {
  1599. t.Fatalf("unexpected error accumulating nil AllocationSetRange: %s", err)
  1600. }
  1601. if !result.IsEmpty() {
  1602. t.Fatalf("accumulating nil AllocationSetRange: expected empty; actual %s", result)
  1603. }
  1604. todayAS := NewAllocationSet(today, tomorrow)
  1605. todayAS.Set(NewMockUnitAllocation("", today, day, nil))
  1606. yesterdayAS := NewAllocationSet(yesterday, today)
  1607. yesterdayAS.Set(NewMockUnitAllocation("", yesterday, day, nil))
  1608. // Accumulate non-nil with nil should result in copy of non-nil, regardless of order
  1609. result, err = NewAllocationSetRange(nil, todayAS).Accumulate()
  1610. if err != nil {
  1611. t.Fatalf("unexpected error accumulating AllocationSetRange of length 1: %s", err)
  1612. }
  1613. if result == nil {
  1614. t.Fatalf("accumulating AllocationSetRange: expected AllocationSet; actual %s", result)
  1615. }
  1616. if result.TotalCost() != 6.0 {
  1617. t.Fatalf("accumulating AllocationSetRange: expected total cost 6.0; actual %f", result.TotalCost())
  1618. }
  1619. result, err = NewAllocationSetRange(todayAS, nil).Accumulate()
  1620. if err != nil {
  1621. t.Fatalf("unexpected error accumulating AllocationSetRange of length 1: %s", err)
  1622. }
  1623. if result == nil {
  1624. t.Fatalf("accumulating AllocationSetRange: expected AllocationSet; actual %s", result)
  1625. }
  1626. if result.TotalCost() != 6.0 {
  1627. t.Fatalf("accumulating AllocationSetRange: expected total cost 6.0; actual %f", result.TotalCost())
  1628. }
  1629. result, err = NewAllocationSetRange(nil, todayAS, nil).Accumulate()
  1630. if err != nil {
  1631. t.Fatalf("unexpected error accumulating AllocationSetRange of length 1: %s", err)
  1632. }
  1633. if result == nil {
  1634. t.Fatalf("accumulating AllocationSetRange: expected AllocationSet; actual %s", result)
  1635. }
  1636. if result.TotalCost() != 6.0 {
  1637. t.Fatalf("accumulating AllocationSetRange: expected total cost 6.0; actual %f", result.TotalCost())
  1638. }
  1639. // Accumulate two non-nil should result in sum of both with appropriate start, end
  1640. result, err = NewAllocationSetRange(yesterdayAS, todayAS).Accumulate()
  1641. if err != nil {
  1642. t.Fatalf("unexpected error accumulating AllocationSetRange of length 1: %s", err)
  1643. }
  1644. if result == nil {
  1645. t.Fatalf("accumulating AllocationSetRange: expected AllocationSet; actual %s", result)
  1646. }
  1647. if result.TotalCost() != 12.0 {
  1648. t.Fatalf("accumulating AllocationSetRange: expected total cost 12.0; actual %f", result.TotalCost())
  1649. }
  1650. allocMap := result.Map()
  1651. if len(allocMap) != 1 {
  1652. t.Fatalf("accumulating AllocationSetRange: expected length 1; actual length %d", len(allocMap))
  1653. }
  1654. alloc := allocMap["cluster1/namespace1/pod1/container1"]
  1655. if alloc == nil {
  1656. t.Fatalf("accumulating AllocationSetRange: expected allocation 'cluster1/namespace1/pod1/container1'")
  1657. }
  1658. if alloc.CPUCoreHours != 2.0 {
  1659. t.Fatalf("accumulating AllocationSetRange: expected 2.0; actual %f", result.TotalCost())
  1660. }
  1661. if alloc.CPUCost != 2.0 {
  1662. t.Fatalf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.CPUCost)
  1663. }
  1664. if alloc.CPUEfficiency() != 1.0 {
  1665. t.Fatalf("accumulating AllocationSetRange: expected 1.0; actual %f", alloc.CPUEfficiency())
  1666. }
  1667. if alloc.GPUHours != 2.0 {
  1668. t.Fatalf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.GPUHours)
  1669. }
  1670. if alloc.GPUCost != 2.0 {
  1671. t.Fatalf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.GPUCost)
  1672. }
  1673. if alloc.NetworkCost != 2.0 {
  1674. t.Fatalf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.NetworkCost)
  1675. }
  1676. if alloc.LoadBalancerCost != 2.0 {
  1677. t.Fatalf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.LoadBalancerCost)
  1678. }
  1679. if alloc.PVByteHours() != 2.0 {
  1680. t.Fatalf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.PVByteHours())
  1681. }
  1682. if alloc.PVCost() != 2.0 {
  1683. t.Fatalf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.PVCost())
  1684. }
  1685. if alloc.RAMByteHours != 2.0 {
  1686. t.Fatalf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.RAMByteHours)
  1687. }
  1688. if alloc.RAMCost != 2.0 {
  1689. t.Fatalf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.RAMCost)
  1690. }
  1691. if alloc.RAMEfficiency() != 1.0 {
  1692. t.Fatalf("accumulating AllocationSetRange: expected 1.0; actual %f", alloc.RAMEfficiency())
  1693. }
  1694. if alloc.TotalCost() != 12.0 {
  1695. t.Fatalf("accumulating AllocationSetRange: expected 12.0; actual %f", alloc.TotalCost())
  1696. }
  1697. if alloc.TotalEfficiency() != 1.0 {
  1698. t.Fatalf("accumulating AllocationSetRange: expected 1.0; actual %f", alloc.TotalEfficiency())
  1699. }
  1700. if !alloc.Start.Equal(yesterday) {
  1701. t.Fatalf("accumulating AllocationSetRange: expected to start %s; actual %s", yesterday, alloc.Start)
  1702. }
  1703. if !alloc.End.Equal(tomorrow) {
  1704. t.Fatalf("accumulating AllocationSetRange: expected to end %s; actual %s", tomorrow, alloc.End)
  1705. }
  1706. if alloc.Minutes() != 2880.0 {
  1707. t.Fatalf("accumulating AllocationSetRange: expected %f minutes; actual %f", 2880.0, alloc.Minutes())
  1708. }
  1709. }
  1710. func TestAllocationSetRange_AccumulateBy_Nils(t *testing.T) {
  1711. var err error
  1712. var result *AllocationSetRange
  1713. ago2d := time.Now().UTC().Truncate(day).Add(-2 * day)
  1714. yesterday := time.Now().UTC().Truncate(day).Add(-day)
  1715. today := time.Now().UTC().Truncate(day)
  1716. tomorrow := time.Now().UTC().Truncate(day).Add(day)
  1717. // Test nil & empty sets
  1718. nilEmptycases := []struct {
  1719. asr *AllocationSetRange
  1720. resolution time.Duration
  1721. testId string
  1722. }{
  1723. {
  1724. asr: NewAllocationSetRange(nil),
  1725. resolution: time.Hour * 24 * 2,
  1726. testId: "AccumulateBy_Nils Empty Test 1",
  1727. },
  1728. {
  1729. asr: NewAllocationSetRange(nil, nil),
  1730. resolution: time.Hour * 1,
  1731. testId: "AccumulateBy_Nils Empty Test 2",
  1732. },
  1733. {
  1734. asr: NewAllocationSetRange(nil, NewAllocationSet(ago2d, yesterday), nil, NewAllocationSet(today, tomorrow)),
  1735. resolution: time.Hour * 24 * 7,
  1736. testId: "AccumulateBy_Nils Empty Test 3",
  1737. },
  1738. }
  1739. for _, c := range nilEmptycases {
  1740. result, err = c.asr.AccumulateBy(c.resolution)
  1741. for _, as := range result.allocations {
  1742. if !as.IsEmpty() {
  1743. t.Errorf("accumulating nil AllocationSetRange: expected empty; actual %s; TestId: %s", result, c.testId)
  1744. }
  1745. }
  1746. }
  1747. if err != nil {
  1748. t.Errorf("unexpected error accumulating nil AllocationSetRange: %s", err)
  1749. }
  1750. yesterdayAS := NewAllocationSet(yesterday, today)
  1751. yesterdayAS.Set(NewMockUnitAllocation("a", yesterday, day, nil))
  1752. todayAS := NewAllocationSet(today, tomorrow)
  1753. todayAS.Set(NewMockUnitAllocation("b", today, day, nil))
  1754. nilAndNonEmptyCases := []struct {
  1755. asr *AllocationSetRange
  1756. resolution time.Duration
  1757. expected float64
  1758. testId string
  1759. }{
  1760. {
  1761. asr: NewAllocationSetRange(nil, todayAS),
  1762. resolution: time.Hour * 2,
  1763. expected: 6.0,
  1764. testId: "AccumulateBy_Nils NonEmpty Test 1",
  1765. },
  1766. {
  1767. asr: NewAllocationSetRange(todayAS, nil),
  1768. resolution: time.Hour * 24,
  1769. expected: 6.0,
  1770. testId: "AccumulateBy_Nils NonEmpty Test 2",
  1771. },
  1772. {
  1773. asr: NewAllocationSetRange(yesterdayAS, nil, todayAS, nil),
  1774. resolution: time.Hour * 24 * 2,
  1775. expected: 12.0,
  1776. testId: "AccumulateBy_Nils NonEmpty Test 3",
  1777. },
  1778. }
  1779. for _, c := range nilAndNonEmptyCases {
  1780. result, err = c.asr.AccumulateBy(c.resolution)
  1781. sumCost := 0.0
  1782. if result == nil {
  1783. t.Errorf("accumulating AllocationSetRange: expected AllocationSet; actual %s; TestId: %s", result, c.testId)
  1784. }
  1785. for _, as := range result.allocations {
  1786. sumCost += as.TotalCost()
  1787. }
  1788. if sumCost != c.expected {
  1789. t.Errorf("accumulating AllocationSetRange: expected total cost %f; actual %f; TestId: %s", c.expected, sumCost, c.testId)
  1790. }
  1791. }
  1792. if err != nil {
  1793. t.Errorf("unexpected error accumulating nil AllocationSetRange: %s", err)
  1794. }
  1795. }
  1796. func TestAllocationSetRange_AccumulateBy(t *testing.T) {
  1797. var err error
  1798. var result *AllocationSetRange
  1799. ago4d := time.Now().UTC().Truncate(day).Add(-4 * day)
  1800. ago3d := time.Now().UTC().Truncate(day).Add(-3 * day)
  1801. ago2d := time.Now().UTC().Truncate(day).Add(-2 * day)
  1802. yesterday := time.Now().UTC().Truncate(day).Add(-day)
  1803. today := time.Now().UTC().Truncate(day)
  1804. tomorrow := time.Now().UTC().Truncate(day).Add(day)
  1805. ago4dAS := NewAllocationSet(ago4d, ago3d)
  1806. ago4dAS.Set(NewMockUnitAllocation("4", ago4d, day, nil))
  1807. ago3dAS := NewAllocationSet(ago3d, ago2d)
  1808. ago3dAS.Set(NewMockUnitAllocation("a", ago3d, day, nil))
  1809. ago2dAS := NewAllocationSet(ago2d, yesterday)
  1810. ago2dAS.Set(NewMockUnitAllocation("", ago2d, day, nil))
  1811. yesterdayAS := NewAllocationSet(yesterday, today)
  1812. yesterdayAS.Set(NewMockUnitAllocation("", yesterday, day, nil))
  1813. todayAS := NewAllocationSet(today, tomorrow)
  1814. todayAS.Set(NewMockUnitAllocation("", today, day, nil))
  1815. yesterHour := time.Now().UTC().Truncate(time.Hour).Add(-1 * time.Hour)
  1816. currentHour := time.Now().UTC().Truncate(time.Hour)
  1817. nextHour := time.Now().UTC().Truncate(time.Hour).Add(time.Hour)
  1818. yesterHourAS := NewAllocationSet(yesterHour, currentHour)
  1819. yesterHourAS.Set(NewMockUnitAllocation("123", yesterHour, time.Hour, nil))
  1820. currentHourAS := NewAllocationSet(currentHour, nextHour)
  1821. currentHourAS.Set(NewMockUnitAllocation("456", currentHour, time.Hour, nil))
  1822. sumCost := 0.0
  1823. // Test nil & empty sets
  1824. cases := []struct {
  1825. asr *AllocationSetRange
  1826. resolution time.Duration
  1827. expectedCost float64
  1828. expectedSets int
  1829. testId string
  1830. }{
  1831. {
  1832. asr: NewAllocationSetRange(yesterdayAS, todayAS),
  1833. resolution: time.Hour * 24 * 2,
  1834. expectedCost: 12.0,
  1835. expectedSets: 1,
  1836. testId: "AccumulateBy Test 1",
  1837. },
  1838. {
  1839. asr: NewAllocationSetRange(ago3dAS, ago2dAS),
  1840. resolution: time.Hour * 24,
  1841. expectedCost: 12.0,
  1842. expectedSets: 2,
  1843. testId: "AccumulateBy Test 2",
  1844. },
  1845. {
  1846. asr: NewAllocationSetRange(ago2dAS, yesterdayAS, todayAS),
  1847. resolution: time.Hour * 13,
  1848. expectedCost: 18.0,
  1849. expectedSets: 3,
  1850. testId: "AccumulateBy Test 3",
  1851. },
  1852. {
  1853. asr: NewAllocationSetRange(ago2dAS, yesterdayAS, todayAS),
  1854. resolution: time.Hour * 24 * 7,
  1855. expectedCost: 18.0,
  1856. expectedSets: 1,
  1857. testId: "AccumulateBy Test 4",
  1858. },
  1859. {
  1860. asr: NewAllocationSetRange(yesterHourAS, currentHourAS),
  1861. resolution: time.Hour * 2,
  1862. //Due to how mock Allocation Sets are generated, hourly sets are still 6.0 cost per set
  1863. expectedCost: 12.0,
  1864. expectedSets: 1,
  1865. testId: "AccumulateBy Test 5",
  1866. },
  1867. {
  1868. asr: NewAllocationSetRange(yesterHourAS, currentHourAS),
  1869. resolution: time.Hour,
  1870. expectedCost: 12.0,
  1871. expectedSets: 2,
  1872. testId: "AccumulateBy Test 6",
  1873. },
  1874. {
  1875. asr: NewAllocationSetRange(yesterHourAS, currentHourAS),
  1876. resolution: time.Minute * 11,
  1877. expectedCost: 12.0,
  1878. expectedSets: 2,
  1879. testId: "AccumulateBy Test 7",
  1880. },
  1881. {
  1882. asr: NewAllocationSetRange(yesterHourAS, currentHourAS),
  1883. resolution: time.Hour * 3,
  1884. expectedCost: 12.0,
  1885. expectedSets: 1,
  1886. testId: "AccumulateBy Test 8",
  1887. },
  1888. {
  1889. asr: NewAllocationSetRange(ago2dAS, yesterdayAS, todayAS),
  1890. resolution: time.Hour * 24 * 2,
  1891. expectedCost: 18.0,
  1892. expectedSets: 2,
  1893. testId: "AccumulateBy Test 9",
  1894. },
  1895. {
  1896. asr: NewAllocationSetRange(ago3dAS, ago2dAS, yesterdayAS, todayAS),
  1897. resolution: time.Hour * 25,
  1898. expectedCost: 24.0,
  1899. expectedSets: 2,
  1900. testId: "AccumulateBy Test 10",
  1901. },
  1902. {
  1903. asr: NewAllocationSetRange(ago4dAS, ago3dAS, ago2dAS, yesterdayAS, todayAS),
  1904. resolution: time.Hour * 72,
  1905. expectedCost: 30.0,
  1906. expectedSets: 2,
  1907. testId: "AccumulateBy Test 11",
  1908. },
  1909. }
  1910. for _, c := range cases {
  1911. result, err = c.asr.AccumulateBy(c.resolution)
  1912. sumCost := 0.0
  1913. if result == nil {
  1914. t.Errorf("accumulating AllocationSetRange: expected AllocationSet; actual %s; TestId: %s", result, c.testId)
  1915. }
  1916. if result.Length() != c.expectedSets {
  1917. t.Errorf("accumulating AllocationSetRange: expected %v number of allocation sets; actual %v; TestId: %s", c.expectedSets, result.Length(), c.testId)
  1918. }
  1919. for _, as := range result.allocations {
  1920. sumCost += as.TotalCost()
  1921. }
  1922. if sumCost != c.expectedCost {
  1923. t.Errorf("accumulating AllocationSetRange: expected total cost %f; actual %f; TestId: %s", c.expectedCost, sumCost, c.testId)
  1924. }
  1925. }
  1926. if err != nil {
  1927. t.Errorf("unexpected error accumulating nil AllocationSetRange: %s", err)
  1928. }
  1929. // // Accumulate three non-nil should result in sum of both with appropriate start, end
  1930. result, err = NewAllocationSetRange(ago2dAS, yesterdayAS, todayAS).AccumulateBy(time.Hour * 24 * 2)
  1931. if err != nil {
  1932. t.Errorf("unexpected error accumulating AllocationSetRange of length 1: %s", err)
  1933. }
  1934. if result == nil {
  1935. t.Errorf("accumulating AllocationSetRange: expected AllocationSet; actual %s", result)
  1936. }
  1937. sumCost = 0.0
  1938. for _, as := range result.allocations {
  1939. sumCost += as.TotalCost()
  1940. }
  1941. allocMap := result.allocations[0].Map()
  1942. if len(allocMap) != 1 {
  1943. t.Errorf("accumulating AllocationSetRange: expected length 1; actual length %d", len(allocMap))
  1944. }
  1945. alloc := allocMap["cluster1/namespace1/pod1/container1"]
  1946. if alloc == nil {
  1947. t.Fatalf("accumulating AllocationSetRange: expected allocation 'cluster1/namespace1/pod1/container1'")
  1948. }
  1949. if alloc.CPUCoreHours != 2.0 {
  1950. t.Errorf("accumulating AllocationSetRange: expected 2.0; actual %f", sumCost)
  1951. }
  1952. if alloc.CPUCost != 2.0 {
  1953. t.Errorf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.CPUCost)
  1954. }
  1955. if alloc.CPUEfficiency() != 1.0 {
  1956. t.Errorf("accumulating AllocationSetRange: expected 1.0; actual %f", alloc.CPUEfficiency())
  1957. }
  1958. if alloc.GPUHours != 2.0 {
  1959. t.Errorf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.GPUHours)
  1960. }
  1961. if alloc.GPUCost != 2.0 {
  1962. t.Errorf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.GPUCost)
  1963. }
  1964. if alloc.NetworkCost != 2.0 {
  1965. t.Errorf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.NetworkCost)
  1966. }
  1967. if alloc.LoadBalancerCost != 2.0 {
  1968. t.Errorf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.LoadBalancerCost)
  1969. }
  1970. if alloc.PVByteHours() != 2.0 {
  1971. t.Errorf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.PVByteHours())
  1972. }
  1973. if alloc.PVCost() != 2.0 {
  1974. t.Errorf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.PVCost())
  1975. }
  1976. if alloc.RAMByteHours != 2.0 {
  1977. t.Errorf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.RAMByteHours)
  1978. }
  1979. if alloc.RAMCost != 2.0 {
  1980. t.Errorf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.RAMCost)
  1981. }
  1982. if alloc.RAMEfficiency() != 1.0 {
  1983. t.Errorf("accumulating AllocationSetRange: expected 1.0; actual %f", alloc.RAMEfficiency())
  1984. }
  1985. if alloc.TotalCost() != 12.0 {
  1986. t.Errorf("accumulating AllocationSetRange: expected 12.0; actual %f", alloc.TotalCost())
  1987. }
  1988. if alloc.TotalEfficiency() != 1.0 {
  1989. t.Errorf("accumulating AllocationSetRange: expected 1.0; actual %f", alloc.TotalEfficiency())
  1990. }
  1991. if !alloc.Start.Equal(ago2d) {
  1992. t.Errorf("accumulating AllocationSetRange: expected to start %s; actual %s", ago2d, alloc.Start)
  1993. }
  1994. if !alloc.End.Equal(today) {
  1995. t.Errorf("accumulating AllocationSetRange: expected to end %s; actual %s", today, alloc.End)
  1996. }
  1997. if alloc.Minutes() != 2880.0 {
  1998. t.Errorf("accumulating AllocationSetRange: expected %f minutes; actual %f", 2880.0, alloc.Minutes())
  1999. }
  2000. }
  2001. // TODO niko/etl
  2002. // func TestAllocationSetRange_AggregateBy(t *testing.T) {}
  2003. // TODO niko/etl
  2004. // func TestAllocationSetRange_Append(t *testing.T) {}
  2005. // TODO niko/etl
  2006. // func TestAllocationSetRange_Each(t *testing.T) {}
  2007. // TODO niko/etl
  2008. // func TestAllocationSetRange_Get(t *testing.T) {}
  2009. func TestAllocationSetRange_InsertRange(t *testing.T) {
  2010. // Set up
  2011. ago2d := time.Now().UTC().Truncate(day).Add(-2 * day)
  2012. yesterday := time.Now().UTC().Truncate(day).Add(-day)
  2013. today := time.Now().UTC().Truncate(day)
  2014. tomorrow := time.Now().UTC().Truncate(day).Add(day)
  2015. unit := NewMockUnitAllocation("", today, day, nil)
  2016. ago2dAS := NewAllocationSet(ago2d, yesterday)
  2017. ago2dAS.Set(NewMockUnitAllocation("a", ago2d, day, nil))
  2018. ago2dAS.Set(NewMockUnitAllocation("b", ago2d, day, nil))
  2019. ago2dAS.Set(NewMockUnitAllocation("c", ago2d, day, nil))
  2020. yesterdayAS := NewAllocationSet(yesterday, today)
  2021. yesterdayAS.Set(NewMockUnitAllocation("a", yesterday, day, nil))
  2022. yesterdayAS.Set(NewMockUnitAllocation("b", yesterday, day, nil))
  2023. yesterdayAS.Set(NewMockUnitAllocation("c", yesterday, day, nil))
  2024. todayAS := NewAllocationSet(today, tomorrow)
  2025. todayAS.Set(NewMockUnitAllocation("a", today, day, nil))
  2026. todayAS.Set(NewMockUnitAllocation("b", today, day, nil))
  2027. todayAS.Set(NewMockUnitAllocation("c", today, day, nil))
  2028. var nilASR *AllocationSetRange
  2029. thisASR := NewAllocationSetRange(yesterdayAS.Clone(), todayAS.Clone())
  2030. thatASR := NewAllocationSetRange(yesterdayAS.Clone())
  2031. longASR := NewAllocationSetRange(ago2dAS.Clone(), yesterdayAS.Clone(), todayAS.Clone())
  2032. var err error
  2033. // Expect an error calling InsertRange on nil
  2034. err = nilASR.InsertRange(thatASR)
  2035. if err == nil {
  2036. t.Fatalf("expected error, got nil")
  2037. }
  2038. // Expect nothing to happen calling InsertRange(nil) on non-nil ASR
  2039. err = thisASR.InsertRange(nil)
  2040. if err != nil {
  2041. t.Fatalf("unexpected error: %s", err)
  2042. }
  2043. thisASR.Each(func(i int, as *AllocationSet) {
  2044. as.Each(func(k string, a *Allocation) {
  2045. if !util.IsApproximately(a.CPUCoreHours, unit.CPUCoreHours) {
  2046. t.Fatalf("allocation %s: expected %f; got %f", k, unit.CPUCoreHours, a.CPUCoreHours)
  2047. }
  2048. if !util.IsApproximately(a.CPUCost, unit.CPUCost) {
  2049. t.Fatalf("allocation %s: expected %f; got %f", k, unit.CPUCost, a.CPUCost)
  2050. }
  2051. if !util.IsApproximately(a.RAMByteHours, unit.RAMByteHours) {
  2052. t.Fatalf("allocation %s: expected %f; got %f", k, unit.RAMByteHours, a.RAMByteHours)
  2053. }
  2054. if !util.IsApproximately(a.RAMCost, unit.RAMCost) {
  2055. t.Fatalf("allocation %s: expected %f; got %f", k, unit.RAMCost, a.RAMCost)
  2056. }
  2057. if !util.IsApproximately(a.GPUHours, unit.GPUHours) {
  2058. t.Fatalf("allocation %s: expected %f; got %f", k, unit.GPUHours, a.GPUHours)
  2059. }
  2060. if !util.IsApproximately(a.GPUCost, unit.GPUCost) {
  2061. t.Fatalf("allocation %s: expected %f; got %f", k, unit.GPUCost, a.GPUCost)
  2062. }
  2063. if !util.IsApproximately(a.PVByteHours(), unit.PVByteHours()) {
  2064. t.Fatalf("allocation %s: expected %f; got %f", k, unit.PVByteHours(), a.PVByteHours())
  2065. }
  2066. if !util.IsApproximately(a.PVCost(), unit.PVCost()) {
  2067. t.Fatalf("allocation %s: expected %f; got %f", k, unit.PVCost(), a.PVCost())
  2068. }
  2069. if !util.IsApproximately(a.NetworkCost, unit.NetworkCost) {
  2070. t.Fatalf("allocation %s: expected %f; got %f", k, unit.NetworkCost, a.NetworkCost)
  2071. }
  2072. if !util.IsApproximately(a.LoadBalancerCost, unit.LoadBalancerCost) {
  2073. t.Fatalf("allocation %s: expected %f; got %f", k, unit.LoadBalancerCost, a.LoadBalancerCost)
  2074. }
  2075. if !util.IsApproximately(a.TotalCost(), unit.TotalCost()) {
  2076. t.Fatalf("allocation %s: expected %f; got %f", k, unit.TotalCost(), a.TotalCost())
  2077. }
  2078. })
  2079. })
  2080. // Expect an error calling InsertRange with a range exceeding the receiver
  2081. err = thisASR.InsertRange(longASR)
  2082. if err == nil {
  2083. t.Fatalf("expected error calling InsertRange with a range exceeding the receiver")
  2084. }
  2085. // Expect each Allocation in "today" to stay the same, but "yesterday" to
  2086. // precisely double when inserting a range that only has a duplicate of
  2087. // "yesterday", but no entry for "today"
  2088. err = thisASR.InsertRange(thatASR)
  2089. if err != nil {
  2090. t.Fatalf("unexpected error: %s", err)
  2091. }
  2092. yAS, err := thisASR.Get(0)
  2093. yAS.Each(func(k string, a *Allocation) {
  2094. if !util.IsApproximately(a.CPUCoreHours, 2*unit.CPUCoreHours) {
  2095. t.Fatalf("allocation %s: expected %f; got %f", k, unit.CPUCoreHours, a.CPUCoreHours)
  2096. }
  2097. if !util.IsApproximately(a.CPUCost, 2*unit.CPUCost) {
  2098. t.Fatalf("allocation %s: expected %f; got %f", k, unit.CPUCost, a.CPUCost)
  2099. }
  2100. if !util.IsApproximately(a.RAMByteHours, 2*unit.RAMByteHours) {
  2101. t.Fatalf("allocation %s: expected %f; got %f", k, unit.RAMByteHours, a.RAMByteHours)
  2102. }
  2103. if !util.IsApproximately(a.RAMCost, 2*unit.RAMCost) {
  2104. t.Fatalf("allocation %s: expected %f; got %f", k, unit.RAMCost, a.RAMCost)
  2105. }
  2106. if !util.IsApproximately(a.GPUHours, 2*unit.GPUHours) {
  2107. t.Fatalf("allocation %s: expected %f; got %f", k, unit.GPUHours, a.GPUHours)
  2108. }
  2109. if !util.IsApproximately(a.GPUCost, 2*unit.GPUCost) {
  2110. t.Fatalf("allocation %s: expected %f; got %f", k, unit.GPUCost, a.GPUCost)
  2111. }
  2112. if !util.IsApproximately(a.PVByteHours(), 2*unit.PVByteHours()) {
  2113. t.Fatalf("allocation %s: expected %f; got %f", k, unit.PVByteHours(), a.PVByteHours())
  2114. }
  2115. if !util.IsApproximately(a.PVCost(), 2*unit.PVCost()) {
  2116. t.Fatalf("allocation %s: expected %f; got %f", k, unit.PVCost(), a.PVCost())
  2117. }
  2118. if !util.IsApproximately(a.NetworkCost, 2*unit.NetworkCost) {
  2119. t.Fatalf("allocation %s: expected %f; got %f", k, unit.NetworkCost, a.NetworkCost)
  2120. }
  2121. if !util.IsApproximately(a.LoadBalancerCost, 2*unit.LoadBalancerCost) {
  2122. t.Fatalf("allocation %s: expected %f; got %f", k, unit.LoadBalancerCost, a.LoadBalancerCost)
  2123. }
  2124. if !util.IsApproximately(a.TotalCost(), 2*unit.TotalCost()) {
  2125. t.Fatalf("allocation %s: expected %f; got %f", k, unit.TotalCost(), a.TotalCost())
  2126. }
  2127. })
  2128. tAS, err := thisASR.Get(1)
  2129. tAS.Each(func(k string, a *Allocation) {
  2130. if !util.IsApproximately(a.CPUCoreHours, unit.CPUCoreHours) {
  2131. t.Fatalf("allocation %s: expected %f; got %f", k, unit.CPUCoreHours, a.CPUCoreHours)
  2132. }
  2133. if !util.IsApproximately(a.CPUCost, unit.CPUCost) {
  2134. t.Fatalf("allocation %s: expected %f; got %f", k, unit.CPUCost, a.CPUCost)
  2135. }
  2136. if !util.IsApproximately(a.RAMByteHours, unit.RAMByteHours) {
  2137. t.Fatalf("allocation %s: expected %f; got %f", k, unit.RAMByteHours, a.RAMByteHours)
  2138. }
  2139. if !util.IsApproximately(a.RAMCost, unit.RAMCost) {
  2140. t.Fatalf("allocation %s: expected %f; got %f", k, unit.RAMCost, a.RAMCost)
  2141. }
  2142. if !util.IsApproximately(a.GPUHours, unit.GPUHours) {
  2143. t.Fatalf("allocation %s: expected %f; got %f", k, unit.GPUHours, a.GPUHours)
  2144. }
  2145. if !util.IsApproximately(a.GPUCost, unit.GPUCost) {
  2146. t.Fatalf("allocation %s: expected %f; got %f", k, unit.GPUCost, a.GPUCost)
  2147. }
  2148. if !util.IsApproximately(a.PVByteHours(), unit.PVByteHours()) {
  2149. t.Fatalf("allocation %s: expected %f; got %f", k, unit.PVByteHours(), a.PVByteHours())
  2150. }
  2151. if !util.IsApproximately(a.PVCost(), unit.PVCost()) {
  2152. t.Fatalf("allocation %s: expected %f; got %f", k, unit.PVCost(), a.PVCost())
  2153. }
  2154. if !util.IsApproximately(a.NetworkCost, unit.NetworkCost) {
  2155. t.Fatalf("allocation %s: expected %f; got %f", k, unit.NetworkCost, a.NetworkCost)
  2156. }
  2157. if !util.IsApproximately(a.LoadBalancerCost, unit.LoadBalancerCost) {
  2158. t.Fatalf("allocation %s: expected %f; got %f", k, unit.LoadBalancerCost, a.LoadBalancerCost)
  2159. }
  2160. if !util.IsApproximately(a.TotalCost(), unit.TotalCost()) {
  2161. t.Fatalf("allocation %s: expected %f; got %f", k, unit.TotalCost(), a.TotalCost())
  2162. }
  2163. })
  2164. }
  2165. // TODO niko/etl
  2166. // func TestAllocationSetRange_Length(t *testing.T) {}
  2167. func TestAllocationSetRange_MarshalJSON(t *testing.T) {
  2168. tests := []struct {
  2169. name string
  2170. arg *AllocationSetRange
  2171. expected *AllocationSetRange
  2172. }{
  2173. {
  2174. name: "Nil ASR",
  2175. arg: nil,
  2176. },
  2177. {
  2178. name: "Nil AS in ASR",
  2179. arg: NewAllocationSetRange(nil),
  2180. },
  2181. {
  2182. name: "Normal ASR",
  2183. arg: &AllocationSetRange{
  2184. allocations: []*AllocationSet{
  2185. {
  2186. allocations: map[string]*Allocation{
  2187. "a": {
  2188. Start: time.Now().UTC().Truncate(day),
  2189. },
  2190. },
  2191. },
  2192. },
  2193. },
  2194. },
  2195. }
  2196. for _, test := range tests {
  2197. bytes, err := json.Marshal(test.arg)
  2198. if err != nil {
  2199. t.Fatalf("ASR Marshal: test %s, unexpected error: %s", test.name, err)
  2200. }
  2201. var testASR []*AllocationSet
  2202. marshaled := &testASR
  2203. err = json.Unmarshal(bytes, marshaled)
  2204. if err != nil {
  2205. t.Fatalf("ASR Unmarshal: test %s: unexpected error: %s", test.name, err)
  2206. }
  2207. if test.arg.Length() != len(testASR) {
  2208. t.Fatalf("ASR Unmarshal: test %s: length mutated in encoding: expected %d but got %d", test.name, test.arg.Length(), len(testASR))
  2209. }
  2210. // Allocations don't unmarshal back from json
  2211. }
  2212. }
  2213. // TODO niko/etl
  2214. // func TestAllocationSetRange_Slice(t *testing.T) {}
  2215. // TODO niko/etl
  2216. // func TestAllocationSetRange_Window(t *testing.T) {}
  2217. func TestAllocationSetRange_Start(t *testing.T) {
  2218. tests := []struct {
  2219. name string
  2220. arg *AllocationSetRange
  2221. expectError bool
  2222. expected time.Time
  2223. }{
  2224. {
  2225. name: "Empty ASR",
  2226. arg: nil,
  2227. expectError: true,
  2228. },
  2229. {
  2230. name: "Single allocation",
  2231. arg: &AllocationSetRange{
  2232. allocations: []*AllocationSet{
  2233. {
  2234. allocations: map[string]*Allocation{
  2235. "a": {
  2236. Start: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  2237. },
  2238. },
  2239. },
  2240. },
  2241. },
  2242. expected: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  2243. },
  2244. {
  2245. name: "Two allocations",
  2246. arg: &AllocationSetRange{
  2247. allocations: []*AllocationSet{
  2248. {
  2249. allocations: map[string]*Allocation{
  2250. "a": {
  2251. Start: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  2252. },
  2253. "b": {
  2254. Start: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  2255. },
  2256. },
  2257. },
  2258. },
  2259. },
  2260. expected: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  2261. },
  2262. {
  2263. name: "Two AllocationSets",
  2264. arg: &AllocationSetRange{
  2265. allocations: []*AllocationSet{
  2266. {
  2267. allocations: map[string]*Allocation{
  2268. "a": {
  2269. Start: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  2270. },
  2271. },
  2272. },
  2273. {
  2274. allocations: map[string]*Allocation{
  2275. "b": {
  2276. Start: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  2277. },
  2278. },
  2279. },
  2280. },
  2281. },
  2282. expected: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  2283. },
  2284. }
  2285. for _, test := range tests {
  2286. result, err := test.arg.Start()
  2287. if test.expectError && err != nil {
  2288. continue
  2289. }
  2290. if test.expectError && err == nil {
  2291. t.Errorf("%s: expected error and got none", test.name)
  2292. } else if result != test.expected {
  2293. t.Errorf("%s: expected %s but got %s", test.name, test.expected, result)
  2294. }
  2295. }
  2296. }
  2297. func TestAllocationSetRange_End(t *testing.T) {
  2298. tests := []struct {
  2299. name string
  2300. arg *AllocationSetRange
  2301. expectError bool
  2302. expected time.Time
  2303. }{
  2304. {
  2305. name: "Empty ASR",
  2306. arg: nil,
  2307. expectError: true,
  2308. },
  2309. {
  2310. name: "Single allocation",
  2311. arg: &AllocationSetRange{
  2312. allocations: []*AllocationSet{
  2313. {
  2314. allocations: map[string]*Allocation{
  2315. "a": {
  2316. End: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  2317. },
  2318. },
  2319. },
  2320. },
  2321. },
  2322. expected: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  2323. },
  2324. {
  2325. name: "Two allocations",
  2326. arg: &AllocationSetRange{
  2327. allocations: []*AllocationSet{
  2328. {
  2329. allocations: map[string]*Allocation{
  2330. "a": {
  2331. End: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  2332. },
  2333. "b": {
  2334. End: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  2335. },
  2336. },
  2337. },
  2338. },
  2339. },
  2340. expected: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  2341. },
  2342. {
  2343. name: "Two AllocationSets",
  2344. arg: &AllocationSetRange{
  2345. allocations: []*AllocationSet{
  2346. {
  2347. allocations: map[string]*Allocation{
  2348. "a": {
  2349. End: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  2350. },
  2351. },
  2352. },
  2353. {
  2354. allocations: map[string]*Allocation{
  2355. "b": {
  2356. End: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  2357. },
  2358. },
  2359. },
  2360. },
  2361. },
  2362. expected: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  2363. },
  2364. }
  2365. for _, test := range tests {
  2366. result, err := test.arg.End()
  2367. if test.expectError && err != nil {
  2368. continue
  2369. }
  2370. if test.expectError && err == nil {
  2371. t.Errorf("%s: expected error and got none", test.name)
  2372. } else if result != test.expected {
  2373. t.Errorf("%s: expected %s but got %s", test.name, test.expected, result)
  2374. }
  2375. }
  2376. }
  2377. func TestAllocationSetRange_Minutes(t *testing.T) {
  2378. tests := []struct {
  2379. name string
  2380. arg *AllocationSetRange
  2381. expected float64
  2382. }{
  2383. {
  2384. name: "Empty ASR",
  2385. arg: nil,
  2386. expected: 0,
  2387. },
  2388. {
  2389. name: "Single allocation",
  2390. arg: &AllocationSetRange{
  2391. allocations: []*AllocationSet{
  2392. {
  2393. allocations: map[string]*Allocation{
  2394. "a": {
  2395. Start: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  2396. End: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  2397. },
  2398. },
  2399. },
  2400. },
  2401. },
  2402. expected: 24 * 60,
  2403. },
  2404. {
  2405. name: "Two allocations",
  2406. arg: &AllocationSetRange{
  2407. allocations: []*AllocationSet{
  2408. {
  2409. allocations: map[string]*Allocation{
  2410. "a": {
  2411. Start: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  2412. End: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  2413. },
  2414. "b": {
  2415. Start: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  2416. End: time.Date(1970, 1, 3, 0, 0, 0, 0, time.UTC),
  2417. },
  2418. },
  2419. },
  2420. },
  2421. },
  2422. expected: 2 * 24 * 60,
  2423. },
  2424. {
  2425. name: "Two AllocationSets",
  2426. arg: &AllocationSetRange{
  2427. allocations: []*AllocationSet{
  2428. {
  2429. allocations: map[string]*Allocation{
  2430. "a": {
  2431. Start: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  2432. End: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  2433. },
  2434. },
  2435. },
  2436. {
  2437. allocations: map[string]*Allocation{
  2438. "b": {
  2439. Start: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  2440. End: time.Date(1970, 1, 3, 0, 0, 0, 0, time.UTC),
  2441. },
  2442. },
  2443. },
  2444. },
  2445. },
  2446. expected: 2 * 24 * 60,
  2447. },
  2448. }
  2449. for _, test := range tests {
  2450. result := test.arg.Minutes()
  2451. if result != test.expected {
  2452. t.Errorf("%s: expected %f but got %f", test.name, test.expected, result)
  2453. }
  2454. }
  2455. }