allocation_test.go 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868
  1. package kubecost
  2. import (
  3. "fmt"
  4. "math"
  5. "testing"
  6. "time"
  7. "github.com/kubecost/cost-model/pkg/util"
  8. "github.com/kubecost/cost-model/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. isCluster := func(matchCluster string) func(*Allocation) bool {
  675. return func(a *Allocation) bool {
  676. cluster := a.Properties.Cluster
  677. return cluster == matchCluster
  678. }
  679. }
  680. isNamespace := func(matchNamespace string) func(*Allocation) bool {
  681. return func(a *Allocation) bool {
  682. namespace := a.Properties.Namespace
  683. return namespace == matchNamespace
  684. }
  685. }
  686. end := time.Now().UTC().Truncate(day)
  687. start := end.Add(-day)
  688. // Tests:
  689. cases := map[string]struct {
  690. start time.Time
  691. aggBy []string
  692. aggOpts *AllocationAggregationOptions
  693. numResults int
  694. totalCost float64
  695. results map[string]float64
  696. windowStart time.Time
  697. windowEnd time.Time
  698. expMinutes float64
  699. }{
  700. // 1 Single-aggregation
  701. // 1a AggregationProperties=(Cluster)
  702. "1a": {
  703. start: start,
  704. aggBy: []string{AllocationClusterProp},
  705. aggOpts: nil,
  706. numResults: numClusters + numIdle,
  707. totalCost: activeTotalCost + idleTotalCost,
  708. results: map[string]float64{
  709. "cluster1": 46.00,
  710. "cluster2": 36.00,
  711. IdleSuffix: 30.00,
  712. },
  713. windowStart: startYesterday,
  714. windowEnd: endYesterday,
  715. expMinutes: 1440.0,
  716. },
  717. // 1b AggregationProperties=(Namespace)
  718. "1b": {
  719. start: start,
  720. aggBy: []string{AllocationNamespaceProp},
  721. aggOpts: nil,
  722. numResults: numNamespaces + numIdle,
  723. totalCost: activeTotalCost + idleTotalCost,
  724. results: map[string]float64{
  725. "namespace1": 28.00,
  726. "namespace2": 36.00,
  727. "namespace3": 18.00,
  728. IdleSuffix: 30.00,
  729. },
  730. windowStart: startYesterday,
  731. windowEnd: endYesterday,
  732. expMinutes: 1440.0,
  733. },
  734. // 1c AggregationProperties=(Pod)
  735. "1c": {
  736. start: start,
  737. aggBy: []string{AllocationPodProp},
  738. aggOpts: nil,
  739. numResults: numPods + numIdle,
  740. totalCost: activeTotalCost + idleTotalCost,
  741. results: map[string]float64{
  742. "pod-jkl": 6.00,
  743. "pod-stu": 6.00,
  744. "pod-abc": 6.00,
  745. "pod-pqr": 6.00,
  746. "pod-def": 6.00,
  747. "pod-vwx": 12.00,
  748. "pod1": 16.00,
  749. "pod-mno": 12.00,
  750. "pod-ghi": 12.00,
  751. IdleSuffix: 30.00,
  752. },
  753. windowStart: startYesterday,
  754. windowEnd: endYesterday,
  755. expMinutes: 1440.0,
  756. },
  757. // 1d AggregationProperties=(Container)
  758. "1d": {
  759. start: start,
  760. aggBy: []string{AllocationContainerProp},
  761. aggOpts: nil,
  762. numResults: numContainers + numIdle,
  763. totalCost: activeTotalCost + idleTotalCost,
  764. results: map[string]float64{
  765. "container2": 6.00,
  766. "container9": 6.00,
  767. "container6": 12.00,
  768. "container3": 6.00,
  769. "container4": 12.00,
  770. "container7": 6.00,
  771. "container8": 6.00,
  772. "container5": 12.00,
  773. "container1": 16.00,
  774. IdleSuffix: 30.00,
  775. },
  776. windowStart: startYesterday,
  777. windowEnd: endYesterday,
  778. expMinutes: 1440.0,
  779. },
  780. // 1e AggregationProperties=(ControllerKind)
  781. "1e": {
  782. start: start,
  783. aggBy: []string{AllocationControllerKindProp},
  784. aggOpts: nil,
  785. numResults: numControllerKinds + numIdle + numUnallocated,
  786. totalCost: activeTotalCost + idleTotalCost,
  787. results: map[string]float64{
  788. "daemonset": 12.00,
  789. "deployment": 42.00,
  790. "statefulset": 12.00,
  791. IdleSuffix: 30.00,
  792. UnallocatedSuffix: 16.00,
  793. },
  794. windowStart: startYesterday,
  795. windowEnd: endYesterday,
  796. expMinutes: 1440.0,
  797. },
  798. // 1f AggregationProperties=(Controller)
  799. "1f": {
  800. start: start,
  801. aggBy: []string{AllocationControllerProp},
  802. aggOpts: nil,
  803. numResults: numControllers + numIdle + numUnallocated,
  804. totalCost: activeTotalCost + idleTotalCost,
  805. results: map[string]float64{
  806. "deployment:deployment2": 24.00,
  807. "daemonset:daemonset1": 12.00,
  808. "deployment:deployment3": 6.00,
  809. "statefulset:statefulset1": 12.00,
  810. "deployment:deployment1": 12.00,
  811. IdleSuffix: 30.00,
  812. UnallocatedSuffix: 16.00,
  813. },
  814. windowStart: startYesterday,
  815. windowEnd: endYesterday,
  816. expMinutes: 1440.0,
  817. },
  818. // 1g AggregationProperties=(Service)
  819. "1g": {
  820. start: start,
  821. aggBy: []string{AllocationServiceProp},
  822. aggOpts: nil,
  823. numResults: numServices + numIdle + numUnallocated,
  824. totalCost: activeTotalCost + idleTotalCost,
  825. results: map[string]float64{
  826. "service1": 12.00,
  827. IdleSuffix: 30.00,
  828. UnallocatedSuffix: 70.00,
  829. },
  830. windowStart: startYesterday,
  831. windowEnd: endYesterday,
  832. expMinutes: 1440.0,
  833. },
  834. // 1h AggregationProperties=(Label:app)
  835. "1h": {
  836. start: start,
  837. aggBy: []string{"label:app"},
  838. aggOpts: nil,
  839. numResults: numLabelApps + numIdle + numUnallocated,
  840. totalCost: activeTotalCost + idleTotalCost,
  841. results: map[string]float64{
  842. "app=app1": 16.00,
  843. "app=app2": 24.00,
  844. IdleSuffix: 30.00,
  845. UnallocatedSuffix: 42.00,
  846. },
  847. windowStart: startYesterday,
  848. windowEnd: endYesterday,
  849. expMinutes: 1440.0,
  850. },
  851. // 1i AggregationProperties=(deployment)
  852. "1i": {
  853. start: start,
  854. aggBy: []string{AllocationDeploymentProp},
  855. aggOpts: nil,
  856. numResults: 3 + numIdle + numUnallocated,
  857. totalCost: activeTotalCost + idleTotalCost,
  858. results: map[string]float64{
  859. "deployment1": 12.00,
  860. "deployment2": 24.00,
  861. "deployment3": 6.00,
  862. IdleSuffix: 30.00,
  863. UnallocatedSuffix: 40.00,
  864. },
  865. windowStart: startYesterday,
  866. windowEnd: endYesterday,
  867. expMinutes: 1440.0,
  868. },
  869. // 1j AggregationProperties=(Annotation:team)
  870. "1j": {
  871. start: start,
  872. aggBy: []string{"annotation:team"},
  873. aggOpts: nil,
  874. numResults: 2 + numIdle + numUnallocated,
  875. totalCost: activeTotalCost + idleTotalCost,
  876. results: map[string]float64{
  877. "team=team1": 12.00,
  878. "team=team2": 6.00,
  879. IdleSuffix: 30.00,
  880. UnallocatedSuffix: 64.00,
  881. },
  882. windowStart: startYesterday,
  883. windowEnd: endYesterday,
  884. expMinutes: 1440.0,
  885. },
  886. // 1k AggregationProperties=(daemonSet)
  887. "1k": {
  888. start: start,
  889. aggBy: []string{AllocationDaemonSetProp},
  890. aggOpts: nil,
  891. numResults: 1 + numIdle + numUnallocated,
  892. totalCost: activeTotalCost + idleTotalCost,
  893. results: map[string]float64{
  894. "daemonset1": 12.00,
  895. IdleSuffix: 30.00,
  896. UnallocatedSuffix: 70.00,
  897. },
  898. windowStart: startYesterday,
  899. windowEnd: endYesterday,
  900. expMinutes: 1440.0,
  901. },
  902. // 1l AggregationProperties=(statefulSet)
  903. "1l": {
  904. start: start,
  905. aggBy: []string{AllocationStatefulSetProp},
  906. aggOpts: nil,
  907. numResults: 1 + numIdle + numUnallocated,
  908. totalCost: activeTotalCost + idleTotalCost,
  909. results: map[string]float64{
  910. "statefulset1": 12.00,
  911. IdleSuffix: 30.00,
  912. UnallocatedSuffix: 70.00,
  913. },
  914. windowStart: startYesterday,
  915. windowEnd: endYesterday,
  916. expMinutes: 1440.0,
  917. },
  918. // 2 Multi-aggregation
  919. // 2a AggregationProperties=(Cluster, Namespace)
  920. // 2b AggregationProperties=(Namespace, Label:app)
  921. // 2c AggregationProperties=(Cluster, Namespace, Pod, Container)
  922. // 2d AggregationProperties=(Label:app, Label:environment)
  923. "2d": {
  924. start: start,
  925. aggBy: []string{"label:app", "label:env"},
  926. aggOpts: nil,
  927. numResults: 3 + numIdle + numUnallocated,
  928. totalCost: activeTotalCost + idleTotalCost,
  929. // sets should be {idle, unallocated, app1/env1, app2/env2, app2/unallocated}
  930. results: map[string]float64{
  931. "app=app1/env=env1": 16.00,
  932. "app=app2/env=env2": 12.00,
  933. "app=app2/" + UnallocatedSuffix: 12.00,
  934. IdleSuffix: 30.00,
  935. UnallocatedSuffix + "/" + UnallocatedSuffix: 42.00,
  936. },
  937. windowStart: startYesterday,
  938. windowEnd: endYesterday,
  939. expMinutes: 1440.0,
  940. },
  941. // 2e AggregationProperties=(Cluster, Label:app, Label:environment)
  942. "2e": {
  943. start: start,
  944. aggBy: []string{AllocationClusterProp, "label:app", "label:env"},
  945. aggOpts: nil,
  946. numResults: 6,
  947. totalCost: activeTotalCost + idleTotalCost,
  948. results: map[string]float64{
  949. "cluster1/app=app2/env=env2": 12.00,
  950. "__idle__": 30.00,
  951. "cluster1/app=app1/env=env1": 16.00,
  952. "cluster1/" + UnallocatedSuffix + "/" + UnallocatedSuffix: 18.00,
  953. "cluster2/app=app2/" + UnallocatedSuffix: 12.00,
  954. "cluster2/" + UnallocatedSuffix + "/" + UnallocatedSuffix: 24.00,
  955. },
  956. windowStart: startYesterday,
  957. windowEnd: endYesterday,
  958. expMinutes: 1440.0,
  959. },
  960. // 2f AggregationProperties=(annotation:team, pod)
  961. "2f": {
  962. start: start,
  963. aggBy: []string{AllocationPodProp, "annotation:team"},
  964. aggOpts: nil,
  965. numResults: 11,
  966. totalCost: activeTotalCost + idleTotalCost,
  967. results: map[string]float64{
  968. "pod-jkl/" + UnallocatedSuffix: 6.00,
  969. "pod-stu/team=team1": 6.00,
  970. "pod-abc/" + UnallocatedSuffix: 6.00,
  971. "pod-pqr/" + UnallocatedSuffix: 6.00,
  972. "pod-def/" + UnallocatedSuffix: 6.00,
  973. "pod-vwx/team=team1": 6.00,
  974. "pod-vwx/team=team2": 6.00,
  975. "pod1/" + UnallocatedSuffix: 16.00,
  976. "pod-mno/" + UnallocatedSuffix: 12.00,
  977. "pod-ghi/" + UnallocatedSuffix: 12.00,
  978. IdleSuffix: 30.00,
  979. },
  980. windowStart: startYesterday,
  981. windowEnd: endYesterday,
  982. expMinutes: 1440.0,
  983. },
  984. // 3 Share idle
  985. // 3a AggregationProperties=(Namespace) ShareIdle=ShareWeighted
  986. // namespace1: 42.6875 = 28.00 + 5.00*(3.00/6.00) + 15.0*(13.0/16.0)
  987. // 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)
  988. // namespace3: 23.0000 = 18.00 + 5.0*(3.0/6.0) + 5.0*(3.0/6.0)
  989. "3a": {
  990. start: start,
  991. aggBy: []string{AllocationNamespaceProp},
  992. aggOpts: &AllocationAggregationOptions{ShareIdle: ShareWeighted},
  993. numResults: numNamespaces,
  994. totalCost: activeTotalCost + idleTotalCost,
  995. results: map[string]float64{
  996. "namespace1": 42.69,
  997. "namespace2": 46.31,
  998. "namespace3": 23.00,
  999. },
  1000. windowStart: startYesterday,
  1001. windowEnd: endYesterday,
  1002. expMinutes: 1440.0,
  1003. },
  1004. // 3b AggregationProperties=(Namespace) ShareIdle=ShareEven
  1005. // namespace1: 38.0000 = 28.00 + 5.00*(1.0/2.0) + 15.0*(1.0/2.0)
  1006. // namespace2: 51.0000 = 36.00 + 5.0*(1.0/2.0) + 15.0*(1.0/2.0) + 5.0*(1.0/2.0) + 5.0*(1.0/2.0)
  1007. // namespace3: 23.0000 = 18.00 + 5.0*(1.0/2.0) + 5.0*(1.0/2.0)
  1008. "3b": {
  1009. start: start,
  1010. aggBy: []string{AllocationNamespaceProp},
  1011. aggOpts: &AllocationAggregationOptions{ShareIdle: ShareEven},
  1012. numResults: numNamespaces,
  1013. totalCost: activeTotalCost + idleTotalCost,
  1014. results: map[string]float64{
  1015. "namespace1": 38.00,
  1016. "namespace2": 51.00,
  1017. "namespace3": 23.00,
  1018. },
  1019. windowStart: startYesterday,
  1020. windowEnd: endYesterday,
  1021. expMinutes: 1440.0,
  1022. },
  1023. // 4 Share resources
  1024. // 4a Share namespace ShareEven
  1025. // namespace1: 37.5000 = 28.00 + 18.00*(1.0/2.0)
  1026. // namespace2: 45.5000 = 36.00 + 18.00*(1.0/2.0)
  1027. // idle: 30.0000
  1028. "4a": {
  1029. start: start,
  1030. aggBy: []string{AllocationNamespaceProp},
  1031. aggOpts: &AllocationAggregationOptions{
  1032. ShareFuncs: []AllocationMatchFunc{isNamespace3},
  1033. ShareSplit: ShareEven,
  1034. },
  1035. numResults: numNamespaces,
  1036. totalCost: activeTotalCost + idleTotalCost,
  1037. results: map[string]float64{
  1038. "namespace1": 37.00,
  1039. "namespace2": 45.00,
  1040. IdleSuffix: 30.00,
  1041. },
  1042. windowStart: startYesterday,
  1043. windowEnd: endYesterday,
  1044. expMinutes: 1440.0,
  1045. },
  1046. // 4b Share namespace ShareWeighted
  1047. // namespace1: 32.5000 =
  1048. // namespace2: 37.5000 =
  1049. // idle: 30.0000
  1050. "4b": {
  1051. start: start,
  1052. aggBy: []string{AllocationNamespaceProp},
  1053. aggOpts: &AllocationAggregationOptions{
  1054. ShareFuncs: []AllocationMatchFunc{isNamespace3},
  1055. ShareSplit: ShareWeighted,
  1056. },
  1057. numResults: numNamespaces,
  1058. totalCost: activeTotalCost + idleTotalCost,
  1059. results: map[string]float64{
  1060. "namespace1": 35.88,
  1061. "namespace2": 46.125,
  1062. IdleSuffix: 30.00,
  1063. },
  1064. windowStart: startYesterday,
  1065. windowEnd: endYesterday,
  1066. expMinutes: 1440.0,
  1067. },
  1068. // 4c Share label ShareEven
  1069. // namespace1: 17.3333 = 28.00 - 16.00 + 16.00*(1.0/3.0)
  1070. // namespace2: 41.3333 = 36.00 + 16.00*(1.0/3.0)
  1071. // namespace3: 23.3333 = 18.00 + 16.00*(1.0/3.0)
  1072. // idle: 30.0000
  1073. "4c": {
  1074. start: start,
  1075. aggBy: []string{AllocationNamespaceProp},
  1076. aggOpts: &AllocationAggregationOptions{
  1077. ShareFuncs: []AllocationMatchFunc{isApp1},
  1078. ShareSplit: ShareEven,
  1079. },
  1080. numResults: numNamespaces + numIdle,
  1081. totalCost: activeTotalCost + idleTotalCost,
  1082. results: map[string]float64{
  1083. "namespace1": 17.33,
  1084. "namespace2": 41.33,
  1085. "namespace3": 23.33,
  1086. IdleSuffix: 30.00,
  1087. },
  1088. windowStart: startYesterday,
  1089. windowEnd: endYesterday,
  1090. expMinutes: 1440.0,
  1091. },
  1092. // 4d Share overhead ShareWeighted
  1093. // namespace1: 85.366 = 28.00 + (7.0*24.0)*(28.00/82.00)
  1094. // namespace2: 109.756 = 36.00 + (7.0*24.0)*(36.00/82.00)
  1095. // namespace3: 54.878 = 18.00 + (7.0*24.0)*(18.00/82.00)
  1096. // idle: 30.0000
  1097. "4d": {
  1098. start: start,
  1099. aggBy: []string{AllocationNamespaceProp},
  1100. aggOpts: &AllocationAggregationOptions{
  1101. SharedHourlyCosts: map[string]float64{"total": sharedOverheadHourlyCost},
  1102. ShareSplit: ShareWeighted,
  1103. },
  1104. numResults: numNamespaces + numIdle,
  1105. totalCost: activeTotalCost + idleTotalCost + (sharedOverheadHourlyCost * 24.0),
  1106. results: map[string]float64{
  1107. "namespace1": 85.366,
  1108. "namespace2": 109.756,
  1109. "namespace3": 54.878,
  1110. IdleSuffix: 30.00,
  1111. },
  1112. windowStart: startYesterday,
  1113. windowEnd: endYesterday,
  1114. expMinutes: 1440.0,
  1115. },
  1116. // 5 Filters
  1117. // 5a Filter by cluster with separate idle
  1118. "5a": {
  1119. start: start,
  1120. aggBy: []string{AllocationClusterProp},
  1121. aggOpts: &AllocationAggregationOptions{
  1122. FilterFuncs: []AllocationMatchFunc{isCluster("cluster1")},
  1123. ShareIdle: ShareNone,
  1124. },
  1125. numResults: 1 + numIdle,
  1126. totalCost: 66.0,
  1127. results: map[string]float64{
  1128. "cluster1": 46.00,
  1129. IdleSuffix: 20.00,
  1130. },
  1131. windowStart: startYesterday,
  1132. windowEnd: endYesterday,
  1133. expMinutes: 1440.0,
  1134. },
  1135. // 5b Filter by cluster with shared idle
  1136. "5b": {
  1137. start: start,
  1138. aggBy: []string{AllocationClusterProp},
  1139. aggOpts: &AllocationAggregationOptions{
  1140. FilterFuncs: []AllocationMatchFunc{isCluster("cluster1")},
  1141. ShareIdle: ShareWeighted,
  1142. },
  1143. numResults: 1,
  1144. totalCost: 66.0,
  1145. results: map[string]float64{
  1146. "cluster1": 66.00,
  1147. },
  1148. windowStart: startYesterday,
  1149. windowEnd: endYesterday,
  1150. expMinutes: 1440.0,
  1151. },
  1152. // 5c Filter by cluster, agg by namespace, with separate idle
  1153. "5c": {
  1154. start: start,
  1155. aggBy: []string{AllocationNamespaceProp},
  1156. aggOpts: &AllocationAggregationOptions{
  1157. FilterFuncs: []AllocationMatchFunc{isCluster("cluster1")},
  1158. ShareIdle: ShareNone,
  1159. },
  1160. numResults: 2 + numIdle,
  1161. totalCost: 66.0,
  1162. results: map[string]float64{
  1163. "namespace1": 28.00,
  1164. "namespace2": 18.00,
  1165. IdleSuffix: 20.00,
  1166. },
  1167. windowStart: startYesterday,
  1168. windowEnd: endYesterday,
  1169. expMinutes: 1440.0,
  1170. },
  1171. // 5d Filter by namespace, agg by cluster, with separate idle
  1172. "5d": {
  1173. start: start,
  1174. aggBy: []string{AllocationClusterProp},
  1175. aggOpts: &AllocationAggregationOptions{
  1176. FilterFuncs: []AllocationMatchFunc{isNamespace("namespace2")},
  1177. ShareIdle: ShareNone,
  1178. },
  1179. numResults: numClusters + numIdle,
  1180. totalCost: 46.31,
  1181. results: map[string]float64{
  1182. "cluster1": 18.00,
  1183. "cluster2": 18.00,
  1184. IdleSuffix: 10.31,
  1185. },
  1186. windowStart: startYesterday,
  1187. windowEnd: endYesterday,
  1188. expMinutes: 1440.0,
  1189. },
  1190. // 6 Combinations and options
  1191. // 6a SplitIdle
  1192. "6a": {
  1193. start: start,
  1194. aggBy: []string{AllocationNamespaceProp},
  1195. aggOpts: &AllocationAggregationOptions{
  1196. SplitIdle: true,
  1197. },
  1198. numResults: numNamespaces + numSplitIdleCluster,
  1199. totalCost: activeTotalCost + idleTotalCost,
  1200. results: map[string]float64{
  1201. "namespace1": 28.00,
  1202. "namespace2": 36.00,
  1203. "namespace3": 18.00,
  1204. fmt.Sprintf("cluster1/%s", IdleSuffix): 20.00,
  1205. fmt.Sprintf("cluster2/%s", IdleSuffix): 10.00,
  1206. },
  1207. windowStart: startYesterday,
  1208. windowEnd: endYesterday,
  1209. expMinutes: 1440.0,
  1210. },
  1211. // 6b Share idle weighted with filters
  1212. // Should match values from unfiltered aggregation (3a)
  1213. // 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)
  1214. "6b": {
  1215. start: start,
  1216. aggBy: []string{AllocationNamespaceProp},
  1217. aggOpts: &AllocationAggregationOptions{
  1218. FilterFuncs: []AllocationMatchFunc{isNamespace("namespace2")},
  1219. ShareIdle: ShareWeighted,
  1220. },
  1221. numResults: 1,
  1222. totalCost: 46.31,
  1223. results: map[string]float64{
  1224. "namespace2": 46.31,
  1225. },
  1226. windowStart: startYesterday,
  1227. windowEnd: endYesterday,
  1228. expMinutes: 1440.0,
  1229. },
  1230. // 6c Share idle even with filters
  1231. // Should match values from unfiltered aggregation (3b)
  1232. // namespace2: 51.0000 = 36.00 + 5.0*(1.0/2.0) + 15.0*(1.0/2.0) + 5.0*(1.0/2.0) + 5.0*(1.0/2.0)
  1233. "6c": {
  1234. start: start,
  1235. aggBy: []string{AllocationNamespaceProp},
  1236. aggOpts: &AllocationAggregationOptions{
  1237. FilterFuncs: []AllocationMatchFunc{isNamespace("namespace2")},
  1238. ShareIdle: ShareEven,
  1239. },
  1240. numResults: 1,
  1241. totalCost: 51.00,
  1242. results: map[string]float64{
  1243. "namespace2": 51.00,
  1244. },
  1245. windowStart: startYesterday,
  1246. windowEnd: endYesterday,
  1247. expMinutes: 1440.0,
  1248. },
  1249. // 6d Share overhead with filters
  1250. // namespace1: 85.366 = 28.00 + (7.0*24.0)*(28.00/82.00)
  1251. // namespace2: 109.756 = 36.00 + (7.0*24.0)*(36.00/82.00)
  1252. // namespace3: 54.878 = 18.00 + (7.0*24.0)*(18.00/82.00)
  1253. // idle: 30.0000
  1254. // Then namespace 2 is filtered.
  1255. "6d": {
  1256. start: start,
  1257. aggBy: []string{AllocationNamespaceProp},
  1258. aggOpts: &AllocationAggregationOptions{
  1259. FilterFuncs: []AllocationMatchFunc{isNamespace("namespace2")},
  1260. SharedHourlyCosts: map[string]float64{"total": sharedOverheadHourlyCost},
  1261. ShareSplit: ShareWeighted,
  1262. },
  1263. numResults: 1 + numIdle,
  1264. totalCost: 139.756,
  1265. results: map[string]float64{
  1266. "namespace2": 109.756,
  1267. IdleSuffix: 30.00,
  1268. },
  1269. windowStart: startYesterday,
  1270. windowEnd: endYesterday,
  1271. expMinutes: 1440.0,
  1272. },
  1273. // 6e Share resources with filters
  1274. // --- Shared ---
  1275. // namespace1: 28.00 (gets shared among namespace2 and namespace3)
  1276. // --- Filtered ---
  1277. // namespace3: 27.33 = 18.00 + (28.00)*(18.00/54.00) (filtered out)
  1278. // --- Results ---
  1279. // namespace2: 54.667 = 36.00 + (28.00)*(36.00/54.00)
  1280. // idle: 30.0000
  1281. "6e": {
  1282. start: start,
  1283. aggBy: []string{AllocationNamespaceProp},
  1284. aggOpts: &AllocationAggregationOptions{
  1285. FilterFuncs: []AllocationMatchFunc{isNamespace("namespace2")},
  1286. ShareFuncs: []AllocationMatchFunc{isNamespace("namespace1")},
  1287. ShareSplit: ShareWeighted,
  1288. },
  1289. numResults: 1 + numIdle,
  1290. totalCost: 84.667,
  1291. results: map[string]float64{
  1292. "namespace2": 54.667,
  1293. IdleSuffix: 30.00,
  1294. },
  1295. windowStart: startYesterday,
  1296. windowEnd: endYesterday,
  1297. expMinutes: 1440.0,
  1298. },
  1299. // 6f Share idle weighted and share resources weighted
  1300. //
  1301. // First, share idle weighted produces:
  1302. //
  1303. // namespace1: 42.6875
  1304. // initial cost 28.0000
  1305. // cluster1.cpu 2.5000 = 5.00*(3.00/6.00)
  1306. // cluster1.ram 12.1875 = 15.00*(13.0/16.0)
  1307. //
  1308. // namespace2: 46.3125
  1309. // initial cost 36.0000
  1310. // cluster1.cpu 2.5000 = 5.00*(3.0/6.0)
  1311. // cluster1.ram 2.8125 = 15.00*(3.0/16.0)
  1312. // cluster2.cpu 2.5000 = 5.00*(3.0/6.0)
  1313. // cluster2.ram 2.5000 = 5.00*(3.0/6.0)
  1314. //
  1315. // namespace3: 23.0000
  1316. // initial cost 18.0000
  1317. // cluster2.cpu 2.5000 = 5.00*(3.0/6.0)
  1318. // cluster2.ram 2.5000 = 5.00*(3.0/6.0)
  1319. //
  1320. // Then, sharing namespace1 means sharing 39.6875 according to coefficients
  1321. // computed before allocating idle (so that weighting idle differently
  1322. // doesn't adversely affect the sharing mechanism):
  1323. //
  1324. // namespace2: 74.7708
  1325. // initial cost 30.0000
  1326. // idle cost 10.3125
  1327. // shared cost 28.4583 = (42.6875)*(36.0/54.0)
  1328. //
  1329. // namespace3: 37.2292
  1330. // initial cost 18.0000
  1331. // idle cost 5.0000
  1332. // shared cost 14.2292 = (42.6875)*(18.0/54.0)
  1333. "6f": {
  1334. start: start,
  1335. aggBy: []string{AllocationNamespaceProp},
  1336. aggOpts: &AllocationAggregationOptions{
  1337. ShareFuncs: []AllocationMatchFunc{isNamespace("namespace1")},
  1338. ShareSplit: ShareWeighted,
  1339. ShareIdle: ShareWeighted,
  1340. },
  1341. numResults: 2,
  1342. totalCost: activeTotalCost + idleTotalCost,
  1343. results: map[string]float64{
  1344. "namespace2": 74.77,
  1345. "namespace3": 37.23,
  1346. },
  1347. windowStart: startYesterday,
  1348. windowEnd: endYesterday,
  1349. expMinutes: 1440.0,
  1350. },
  1351. // 6g Share idle, share resources, and filter
  1352. //
  1353. // First, share idle weighted produces:
  1354. //
  1355. // namespace1: 42.6875
  1356. // initial cost 28.0000
  1357. // cluster1.cpu 2.5000 = 5.00*(3.00/6.00)
  1358. // cluster1.ram 12.1875 = 15.00*(13.0/16.0)
  1359. //
  1360. // namespace2: 46.3125
  1361. // initial cost 36.0000
  1362. // cluster1.cpu 2.5000 = 5.00*(3.0/6.0)
  1363. // cluster1.ram 2.8125 = 15.00*(3.0/16.0)
  1364. // cluster2.cpu 2.5000 = 5.00*(3.0/6.0)
  1365. // cluster2.ram 2.5000 = 5.00*(3.0/6.0)
  1366. //
  1367. // namespace3: 23.0000
  1368. // initial cost 18.0000
  1369. // cluster2.cpu 2.5000 = 5.00*(3.0/6.0)
  1370. // cluster2.ram 2.5000 = 5.00*(3.0/6.0)
  1371. //
  1372. // Then, sharing namespace1 means sharing 39.6875 according to coefficients
  1373. // computed before allocating idle (so that weighting idle differently
  1374. // doesn't adversely affect the sharing mechanism):
  1375. //
  1376. // namespace2: 74.7708
  1377. // initial cost 36.0000
  1378. // idle cost 10.3125
  1379. // shared cost 28.4583 = (42.6875)*(36.0/54.0)
  1380. //
  1381. // namespace3: 37.2292
  1382. // initial cost 18.0000
  1383. // idle cost 5.0000
  1384. // shared cost 14.2292 = (42.6875)*(18.0/54.0)
  1385. //
  1386. // Then, filter for namespace2: 74.7708
  1387. "6g": {
  1388. start: start,
  1389. aggBy: []string{AllocationNamespaceProp},
  1390. aggOpts: &AllocationAggregationOptions{
  1391. FilterFuncs: []AllocationMatchFunc{isNamespace("namespace2")},
  1392. ShareFuncs: []AllocationMatchFunc{isNamespace("namespace1")},
  1393. ShareSplit: ShareWeighted,
  1394. ShareIdle: ShareWeighted,
  1395. },
  1396. numResults: 1,
  1397. totalCost: 74.77,
  1398. results: map[string]float64{
  1399. "namespace2": 74.77,
  1400. },
  1401. windowStart: startYesterday,
  1402. windowEnd: endYesterday,
  1403. expMinutes: 1440.0,
  1404. },
  1405. // 6h Share idle, share resources, share overhead
  1406. //
  1407. // Share idle weighted:
  1408. //
  1409. // namespace1: 42.6875
  1410. // initial cost 28.0000
  1411. // cluster1.cpu 2.5000 = 5.00*(3.00/6.00)
  1412. // cluster1.ram 12.1875 = 15.00*(13.0/16.0)
  1413. //
  1414. // namespace2: 46.3125
  1415. // initial cost 36.0000
  1416. // cluster1.cpu 2.5000 = 5.00*(3.0/6.0)
  1417. // cluster1.ram 2.8125 = 15.00*(3.0/16.0)
  1418. // cluster2.cpu 2.5000 = 5.00*(3.0/6.0)
  1419. // cluster2.ram 2.5000 = 5.00*(3.0/6.0)
  1420. //
  1421. // namespace3: 23.0000
  1422. // initial cost 18.0000
  1423. // cluster2.cpu 2.5000 = 5.00*(3.0/6.0)
  1424. // cluster2.ram 2.5000 = 5.00*(3.0/6.0)
  1425. //
  1426. // Then share overhead:
  1427. //
  1428. // namespace1: 100.0533 = 42.6875 + (7.0*24.0)*(28.00/82.00)
  1429. // namespace2: 120.0686 = 46.3125 + (7.0*24.0)*(36.00/82.00)
  1430. // namespace3: 59.8780 = 23.0000 + (7.0*24.0)*(18.00/82.00)
  1431. //
  1432. // Then namespace 2 is filtered.
  1433. "6h": {
  1434. start: start,
  1435. aggBy: []string{AllocationNamespaceProp},
  1436. aggOpts: &AllocationAggregationOptions{
  1437. FilterFuncs: []AllocationMatchFunc{isNamespace("namespace2")},
  1438. ShareSplit: ShareWeighted,
  1439. ShareIdle: ShareWeighted,
  1440. SharedHourlyCosts: map[string]float64{"total": sharedOverheadHourlyCost},
  1441. },
  1442. numResults: 1,
  1443. totalCost: 120.07,
  1444. results: map[string]float64{
  1445. "namespace2": 120.07,
  1446. },
  1447. windowStart: startYesterday,
  1448. windowEnd: endYesterday,
  1449. expMinutes: 1440.0,
  1450. },
  1451. // 6i Idle by Node
  1452. "6i": {
  1453. start: start,
  1454. aggBy: []string{AllocationNamespaceProp},
  1455. aggOpts: &AllocationAggregationOptions{
  1456. IdleByNode: true,
  1457. },
  1458. numResults: numNamespaces + numIdle,
  1459. totalCost: activeTotalCost + idleTotalCost,
  1460. results: map[string]float64{
  1461. "namespace1": 28.00,
  1462. "namespace2": 36.00,
  1463. "namespace3": 18.00,
  1464. IdleSuffix: 30.00,
  1465. },
  1466. windowStart: startYesterday,
  1467. windowEnd: endYesterday,
  1468. expMinutes: 1440.0,
  1469. },
  1470. // 6j Split Idle, Idle by Node
  1471. "6j": {
  1472. start: start,
  1473. aggBy: []string{AllocationNamespaceProp},
  1474. aggOpts: &AllocationAggregationOptions{
  1475. SplitIdle: true,
  1476. IdleByNode: true,
  1477. },
  1478. numResults: numNamespaces + numSplitIdleNode,
  1479. totalCost: activeTotalCost + idleTotalCost,
  1480. results: map[string]float64{
  1481. "namespace1": 28.00,
  1482. "namespace2": 36.00,
  1483. "namespace3": 18.00,
  1484. fmt.Sprintf("c1nodes/%s", IdleSuffix): 20.00,
  1485. fmt.Sprintf("node1/%s", IdleSuffix): 3.333333,
  1486. fmt.Sprintf("node2/%s", IdleSuffix): 3.333333,
  1487. fmt.Sprintf("node3/%s", IdleSuffix): 3.333333,
  1488. },
  1489. windowStart: startYesterday,
  1490. windowEnd: endYesterday,
  1491. expMinutes: 1440.0,
  1492. },
  1493. // 6k Share idle Even Idle by Node
  1494. // Should match values from unfiltered aggregation (3b)
  1495. "6k": {
  1496. start: start,
  1497. aggBy: []string{AllocationNamespaceProp},
  1498. aggOpts: &AllocationAggregationOptions{
  1499. ShareIdle: ShareEven,
  1500. IdleByNode: true,
  1501. },
  1502. numResults: 3,
  1503. totalCost: 112.00,
  1504. results: map[string]float64{
  1505. "namespace1": 38.00,
  1506. "namespace2": 51.00,
  1507. "namespace3": 23.00,
  1508. },
  1509. windowStart: startYesterday,
  1510. windowEnd: endYesterday,
  1511. expMinutes: 1440.0,
  1512. },
  1513. // 6l Share idle weighted with filters, Idle by Node
  1514. // Should match values from unfiltered aggregation (3a)
  1515. // 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)
  1516. "6l": {
  1517. start: start,
  1518. aggBy: []string{AllocationNamespaceProp},
  1519. aggOpts: &AllocationAggregationOptions{
  1520. FilterFuncs: []AllocationMatchFunc{isNamespace("namespace2")},
  1521. ShareIdle: ShareWeighted,
  1522. IdleByNode: true,
  1523. },
  1524. numResults: 1,
  1525. totalCost: 46.31,
  1526. results: map[string]float64{
  1527. "namespace2": 46.31,
  1528. },
  1529. windowStart: startYesterday,
  1530. windowEnd: endYesterday,
  1531. expMinutes: 1440.0,
  1532. },
  1533. // 7 Edge cases and errors
  1534. // 7a Empty AggregationProperties
  1535. // 7b Filter all
  1536. // 7c Share all
  1537. // 7d Share and filter the same allocations
  1538. }
  1539. for name, testcase := range cases {
  1540. t.Run(name, func(t *testing.T) {
  1541. if testcase.aggOpts != nil && testcase.aggOpts.IdleByNode {
  1542. as = GenerateMockAllocationSetNodeIdle(testcase.start)
  1543. } else {
  1544. as = GenerateMockAllocationSetClusterIdle(testcase.start)
  1545. }
  1546. err = as.AggregateBy(testcase.aggBy, testcase.aggOpts)
  1547. assertAllocationSetTotals(t, as, name, err, testcase.numResults, testcase.totalCost)
  1548. assertAllocationTotals(t, as, name, testcase.results)
  1549. assertAllocationWindow(t, as, name, testcase.windowStart, testcase.windowEnd, testcase.expMinutes)
  1550. })
  1551. }
  1552. }
  1553. // TODO niko/etl
  1554. //func TestAllocationSet_Clone(t *testing.T) {}
  1555. func TestAllocationSet_ComputeIdleAllocations(t *testing.T) {
  1556. var as *AllocationSet
  1557. var err error
  1558. var idles map[string]*Allocation
  1559. end := time.Now().UTC().Truncate(day)
  1560. start := end.Add(-day)
  1561. // Generate AllocationSet without idle allocations
  1562. as = GenerateMockAllocationSet(start)
  1563. assetSets := GenerateMockAssetSets(start, end)
  1564. cases := map[string]struct {
  1565. allocationSet *AllocationSet
  1566. assetSet *AssetSet
  1567. clusters map[string]Allocation
  1568. }{
  1569. "1a": {
  1570. allocationSet: as,
  1571. assetSet: assetSets[0],
  1572. clusters: map[string]Allocation{
  1573. "cluster1": {
  1574. CPUCost: 44.0,
  1575. RAMCost: 24.0,
  1576. GPUCost: 4.0,
  1577. },
  1578. "cluster2": {
  1579. CPUCost: 44.0,
  1580. RAMCost: 34.0,
  1581. GPUCost: 4.0,
  1582. },
  1583. },
  1584. },
  1585. "1b": {
  1586. allocationSet: as,
  1587. assetSet: assetSets[1],
  1588. clusters: map[string]Allocation{
  1589. "cluster1": {
  1590. CPUCost: 44.0,
  1591. RAMCost: 24.0,
  1592. GPUCost: 4.0,
  1593. },
  1594. "cluster2": {
  1595. CPUCost: 44.0,
  1596. RAMCost: 34.0,
  1597. GPUCost: 4.0,
  1598. },
  1599. },
  1600. },
  1601. }
  1602. for name, testcase := range cases {
  1603. t.Run(name, func(t *testing.T) {
  1604. idles, err = as.ComputeIdleAllocations(testcase.assetSet)
  1605. if err != nil {
  1606. t.Fatalf("unexpected error: %s", err)
  1607. }
  1608. if len(idles) != len(testcase.clusters) {
  1609. t.Fatalf("idles: expected length %d; got length %d", len(testcase.clusters), len(idles))
  1610. }
  1611. for clusterName, cluster := range testcase.clusters {
  1612. if idle, ok := idles[clusterName]; !ok {
  1613. t.Fatalf("expected idle cost for %s", clusterName)
  1614. } else {
  1615. if !util.IsApproximately(idle.TotalCost(), cluster.TotalCost()) {
  1616. t.Fatalf("%s idle: expected total cost %f; got total cost %f", clusterName, cluster.TotalCost(), idle.TotalCost())
  1617. }
  1618. }
  1619. if !util.IsApproximately(idles[clusterName].CPUCost, cluster.CPUCost) {
  1620. t.Fatalf("expected idle CPU cost for %s to be %.2f; got %.2f", clusterName, cluster.CPUCost, idles[clusterName].CPUCost)
  1621. }
  1622. if !util.IsApproximately(idles[clusterName].RAMCost, cluster.RAMCost) {
  1623. t.Fatalf("expected idle RAM cost for %s to be %.2f; got %.2f", clusterName, cluster.RAMCost, idles[clusterName].RAMCost)
  1624. }
  1625. if !util.IsApproximately(idles[clusterName].GPUCost, cluster.GPUCost) {
  1626. t.Fatalf("expected idle GPU cost for %s to be %.2f; got %.2f", clusterName, cluster.GPUCost, idles[clusterName].GPUCost)
  1627. }
  1628. }
  1629. })
  1630. }
  1631. }
  1632. func TestAllocationSet_ComputeIdleAllocationsPerNode(t *testing.T) {
  1633. var as *AllocationSet
  1634. var err error
  1635. var idles map[string]*Allocation
  1636. end := time.Now().UTC().Truncate(day)
  1637. start := end.Add(-day)
  1638. // Generate AllocationSet without idle allocations
  1639. as = GenerateMockAllocationSet(start)
  1640. assetSets := GenerateMockAssetSets(start, end)
  1641. cases := map[string]struct {
  1642. allocationSet *AllocationSet
  1643. assetSet *AssetSet
  1644. nodes map[string]Allocation
  1645. }{
  1646. "1a": {
  1647. allocationSet: as,
  1648. assetSet: assetSets[0],
  1649. nodes: map[string]Allocation{
  1650. "c1nodes": {
  1651. CPUCost: 44.0,
  1652. RAMCost: 24.0,
  1653. GPUCost: 4.0,
  1654. },
  1655. "node1": {
  1656. CPUCost: 18.0,
  1657. RAMCost: 13.0,
  1658. GPUCost: -2.0,
  1659. },
  1660. "node2": {
  1661. CPUCost: 18.0,
  1662. RAMCost: 13.0,
  1663. GPUCost: -2.0,
  1664. },
  1665. "node3": {
  1666. CPUCost: 8.0,
  1667. RAMCost: 8.0,
  1668. GPUCost: 8.0,
  1669. },
  1670. },
  1671. },
  1672. "1b": {
  1673. allocationSet: as,
  1674. assetSet: assetSets[1],
  1675. nodes: map[string]Allocation{
  1676. "c1nodes": {
  1677. CPUCost: 44.0,
  1678. RAMCost: 24.0,
  1679. GPUCost: 4.0,
  1680. },
  1681. "node1": {
  1682. CPUCost: 18.0,
  1683. RAMCost: 13.0,
  1684. GPUCost: -2.0,
  1685. },
  1686. "node2": {
  1687. CPUCost: 18.0,
  1688. RAMCost: 13.0,
  1689. GPUCost: -2.0,
  1690. },
  1691. "node3": {
  1692. CPUCost: 8.0,
  1693. RAMCost: 8.0,
  1694. GPUCost: 8.0,
  1695. },
  1696. },
  1697. },
  1698. }
  1699. for name, testcase := range cases {
  1700. t.Run(name, func(t *testing.T) {
  1701. idles, err = as.ComputeIdleAllocationsByNode(testcase.assetSet)
  1702. if err != nil {
  1703. t.Fatalf("unexpected error: %s", err)
  1704. }
  1705. if len(idles) != len(testcase.nodes) {
  1706. t.Fatalf("idles: expected length %d; got length %d", len(testcase.nodes), len(idles))
  1707. }
  1708. for nodeName, node := range testcase.nodes {
  1709. if idle, ok := idles[nodeName]; !ok {
  1710. t.Fatalf("expected idle cost for %s", nodeName)
  1711. } else {
  1712. if !util.IsApproximately(idle.TotalCost(), node.TotalCost()) {
  1713. t.Fatalf("%s idle: expected total cost %f; got total cost %f", nodeName, node.TotalCost(), idle.TotalCost())
  1714. }
  1715. }
  1716. if !util.IsApproximately(idles[nodeName].CPUCost, node.CPUCost) {
  1717. t.Fatalf("expected idle CPU cost for %s to be %.2f; got %.2f", nodeName, node.CPUCost, idles[nodeName].CPUCost)
  1718. }
  1719. if !util.IsApproximately(idles[nodeName].RAMCost, node.RAMCost) {
  1720. t.Fatalf("expected idle RAM cost for %s to be %.2f; got %.2f", nodeName, node.RAMCost, idles[nodeName].RAMCost)
  1721. }
  1722. if !util.IsApproximately(idles[nodeName].GPUCost, node.GPUCost) {
  1723. t.Fatalf("expected idle GPU cost for %s to be %.2f; got %.2f", nodeName, node.GPUCost, idles[nodeName].GPUCost)
  1724. }
  1725. }
  1726. })
  1727. }
  1728. }
  1729. // TODO niko/etl
  1730. //func TestAllocationSet_Delete(t *testing.T) {}
  1731. // TODO niko/etl
  1732. //func TestAllocationSet_End(t *testing.T) {}
  1733. // TODO niko/etl
  1734. //func TestAllocationSet_IdleAllocations(t *testing.T) {}
  1735. // TODO niko/etl
  1736. //func TestAllocationSet_Insert(t *testing.T) {}
  1737. // Asserts that all Allocations within an AllocationSet have a Window that
  1738. // matches that of the AllocationSet.
  1739. func TestAllocationSet_insertMatchingWindow(t *testing.T) {
  1740. setStart := time.Now().Round(time.Hour)
  1741. setEnd := setStart.Add(1 * time.Hour)
  1742. a1WindowStart := setStart.Add(5 * time.Minute)
  1743. a1WindowEnd := setStart.Add(50 * time.Minute)
  1744. a2WindowStart := setStart.Add(17 * time.Minute)
  1745. a2WindowEnd := setStart.Add(34 * time.Minute)
  1746. a1 := &Allocation{
  1747. Name: "allocation-1",
  1748. Window: Window(NewClosedWindow(a1WindowStart, a1WindowEnd)),
  1749. }
  1750. a2 := &Allocation{
  1751. Name: "allocation-2",
  1752. Window: Window(NewClosedWindow(a2WindowStart, a2WindowEnd)),
  1753. }
  1754. as := NewAllocationSet(setStart, setEnd)
  1755. as.insert(a1)
  1756. as.insert(a2)
  1757. if as.Length() != 2 {
  1758. t.Errorf("AS length got %d, expected %d", as.Length(), 2)
  1759. }
  1760. as.Each(func(k string, a *Allocation) {
  1761. if !(*a.Window.Start()).Equal(setStart) {
  1762. t.Errorf("Allocation %s window start is %s, expected %s", a.Name, *a.Window.Start(), setStart)
  1763. }
  1764. if !(*a.Window.End()).Equal(setEnd) {
  1765. t.Errorf("Allocation %s window end is %s, expected %s", a.Name, *a.Window.End(), setEnd)
  1766. }
  1767. })
  1768. }
  1769. // TODO niko/etl
  1770. //func TestAllocationSet_IsEmpty(t *testing.T) {}
  1771. // TODO niko/etl
  1772. //func TestAllocationSet_Length(t *testing.T) {}
  1773. // TODO niko/etl
  1774. //func TestAllocationSet_Map(t *testing.T) {}
  1775. // TODO niko/etl
  1776. //func TestAllocationSet_MarshalJSON(t *testing.T) {}
  1777. // TODO niko/etl
  1778. //func TestAllocationSet_Resolution(t *testing.T) {}
  1779. // TODO niko/etl
  1780. //func TestAllocationSet_Seconds(t *testing.T) {}
  1781. // TODO niko/etl
  1782. //func TestAllocationSet_Set(t *testing.T) {}
  1783. // TODO niko/etl
  1784. //func TestAllocationSet_Start(t *testing.T) {}
  1785. // TODO niko/etl
  1786. //func TestAllocationSet_TotalCost(t *testing.T) {}
  1787. // TODO niko/etl
  1788. //func TestNewAllocationSetRange(t *testing.T) {}
  1789. func TestAllocationSetRange_Accumulate(t *testing.T) {
  1790. ago2d := time.Now().UTC().Truncate(day).Add(-2 * day)
  1791. yesterday := time.Now().UTC().Truncate(day).Add(-day)
  1792. today := time.Now().UTC().Truncate(day)
  1793. tomorrow := time.Now().UTC().Truncate(day).Add(day)
  1794. // Accumulating any combination of nil and/or empty set should result in empty set
  1795. result, err := NewAllocationSetRange(nil).Accumulate()
  1796. if err != nil {
  1797. t.Fatalf("unexpected error accumulating nil AllocationSetRange: %s", err)
  1798. }
  1799. if !result.IsEmpty() {
  1800. t.Fatalf("accumulating nil AllocationSetRange: expected empty; actual %s", result)
  1801. }
  1802. result, err = NewAllocationSetRange(nil, nil).Accumulate()
  1803. if err != nil {
  1804. t.Fatalf("unexpected error accumulating nil AllocationSetRange: %s", err)
  1805. }
  1806. if !result.IsEmpty() {
  1807. t.Fatalf("accumulating nil AllocationSetRange: expected empty; actual %s", result)
  1808. }
  1809. result, err = NewAllocationSetRange(NewAllocationSet(yesterday, today)).Accumulate()
  1810. if err != nil {
  1811. t.Fatalf("unexpected error accumulating nil AllocationSetRange: %s", err)
  1812. }
  1813. if !result.IsEmpty() {
  1814. t.Fatalf("accumulating nil AllocationSetRange: expected empty; actual %s", result)
  1815. }
  1816. result, err = NewAllocationSetRange(nil, NewAllocationSet(ago2d, yesterday), nil, NewAllocationSet(today, tomorrow), nil).Accumulate()
  1817. if err != nil {
  1818. t.Fatalf("unexpected error accumulating nil AllocationSetRange: %s", err)
  1819. }
  1820. if !result.IsEmpty() {
  1821. t.Fatalf("accumulating nil AllocationSetRange: expected empty; actual %s", result)
  1822. }
  1823. todayAS := NewAllocationSet(today, tomorrow)
  1824. todayAS.Set(NewMockUnitAllocation("", today, day, nil))
  1825. yesterdayAS := NewAllocationSet(yesterday, today)
  1826. yesterdayAS.Set(NewMockUnitAllocation("", yesterday, day, nil))
  1827. // Accumulate non-nil with nil should result in copy of non-nil, regardless of order
  1828. result, err = NewAllocationSetRange(nil, todayAS).Accumulate()
  1829. if err != nil {
  1830. t.Fatalf("unexpected error accumulating AllocationSetRange of length 1: %s", err)
  1831. }
  1832. if result == nil {
  1833. t.Fatalf("accumulating AllocationSetRange: expected AllocationSet; actual %s", result)
  1834. }
  1835. if result.TotalCost() != 6.0 {
  1836. t.Fatalf("accumulating AllocationSetRange: expected total cost 6.0; actual %f", result.TotalCost())
  1837. }
  1838. result, err = NewAllocationSetRange(todayAS, nil).Accumulate()
  1839. if err != nil {
  1840. t.Fatalf("unexpected error accumulating AllocationSetRange of length 1: %s", err)
  1841. }
  1842. if result == nil {
  1843. t.Fatalf("accumulating AllocationSetRange: expected AllocationSet; actual %s", result)
  1844. }
  1845. if result.TotalCost() != 6.0 {
  1846. t.Fatalf("accumulating AllocationSetRange: expected total cost 6.0; actual %f", result.TotalCost())
  1847. }
  1848. result, err = NewAllocationSetRange(nil, todayAS, nil).Accumulate()
  1849. if err != nil {
  1850. t.Fatalf("unexpected error accumulating AllocationSetRange of length 1: %s", err)
  1851. }
  1852. if result == nil {
  1853. t.Fatalf("accumulating AllocationSetRange: expected AllocationSet; actual %s", result)
  1854. }
  1855. if result.TotalCost() != 6.0 {
  1856. t.Fatalf("accumulating AllocationSetRange: expected total cost 6.0; actual %f", result.TotalCost())
  1857. }
  1858. // Accumulate two non-nil should result in sum of both with appropriate start, end
  1859. result, err = NewAllocationSetRange(yesterdayAS, todayAS).Accumulate()
  1860. if err != nil {
  1861. t.Fatalf("unexpected error accumulating AllocationSetRange of length 1: %s", err)
  1862. }
  1863. if result == nil {
  1864. t.Fatalf("accumulating AllocationSetRange: expected AllocationSet; actual %s", result)
  1865. }
  1866. if result.TotalCost() != 12.0 {
  1867. t.Fatalf("accumulating AllocationSetRange: expected total cost 12.0; actual %f", result.TotalCost())
  1868. }
  1869. allocMap := result.Map()
  1870. if len(allocMap) != 1 {
  1871. t.Fatalf("accumulating AllocationSetRange: expected length 1; actual length %d", len(allocMap))
  1872. }
  1873. alloc := allocMap["cluster1/namespace1/pod1/container1"]
  1874. if alloc == nil {
  1875. t.Fatalf("accumulating AllocationSetRange: expected allocation 'cluster1/namespace1/pod1/container1'")
  1876. }
  1877. if alloc.CPUCoreHours != 2.0 {
  1878. t.Fatalf("accumulating AllocationSetRange: expected 2.0; actual %f", result.TotalCost())
  1879. }
  1880. if alloc.CPUCost != 2.0 {
  1881. t.Fatalf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.CPUCost)
  1882. }
  1883. if alloc.CPUEfficiency() != 1.0 {
  1884. t.Fatalf("accumulating AllocationSetRange: expected 1.0; actual %f", alloc.CPUEfficiency())
  1885. }
  1886. if alloc.GPUHours != 2.0 {
  1887. t.Fatalf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.GPUHours)
  1888. }
  1889. if alloc.GPUCost != 2.0 {
  1890. t.Fatalf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.GPUCost)
  1891. }
  1892. if alloc.NetworkCost != 2.0 {
  1893. t.Fatalf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.NetworkCost)
  1894. }
  1895. if alloc.LoadBalancerCost != 2.0 {
  1896. t.Fatalf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.LoadBalancerCost)
  1897. }
  1898. if alloc.PVByteHours() != 2.0 {
  1899. t.Fatalf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.PVByteHours())
  1900. }
  1901. if alloc.PVCost() != 2.0 {
  1902. t.Fatalf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.PVCost())
  1903. }
  1904. if alloc.RAMByteHours != 2.0 {
  1905. t.Fatalf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.RAMByteHours)
  1906. }
  1907. if alloc.RAMCost != 2.0 {
  1908. t.Fatalf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.RAMCost)
  1909. }
  1910. if alloc.RAMEfficiency() != 1.0 {
  1911. t.Fatalf("accumulating AllocationSetRange: expected 1.0; actual %f", alloc.RAMEfficiency())
  1912. }
  1913. if alloc.TotalCost() != 12.0 {
  1914. t.Fatalf("accumulating AllocationSetRange: expected 12.0; actual %f", alloc.TotalCost())
  1915. }
  1916. if alloc.TotalEfficiency() != 1.0 {
  1917. t.Fatalf("accumulating AllocationSetRange: expected 1.0; actual %f", alloc.TotalEfficiency())
  1918. }
  1919. if !alloc.Start.Equal(yesterday) {
  1920. t.Fatalf("accumulating AllocationSetRange: expected to start %s; actual %s", yesterday, alloc.Start)
  1921. }
  1922. if !alloc.End.Equal(tomorrow) {
  1923. t.Fatalf("accumulating AllocationSetRange: expected to end %s; actual %s", tomorrow, alloc.End)
  1924. }
  1925. if alloc.Minutes() != 2880.0 {
  1926. t.Fatalf("accumulating AllocationSetRange: expected %f minutes; actual %f", 2880.0, alloc.Minutes())
  1927. }
  1928. }
  1929. func TestAllocationSetRange_AccumulateBy_Nils(t *testing.T) {
  1930. var err error
  1931. var result *AllocationSetRange
  1932. ago2d := time.Now().UTC().Truncate(day).Add(-2 * day)
  1933. yesterday := time.Now().UTC().Truncate(day).Add(-day)
  1934. today := time.Now().UTC().Truncate(day)
  1935. tomorrow := time.Now().UTC().Truncate(day).Add(day)
  1936. // Test nil & empty sets
  1937. nilEmptycases := []struct {
  1938. asr *AllocationSetRange
  1939. resolution time.Duration
  1940. testId string
  1941. }{
  1942. {
  1943. asr: NewAllocationSetRange(nil),
  1944. resolution: time.Hour * 24 * 2,
  1945. testId: "AccumulateBy_Nils Empty Test 1",
  1946. },
  1947. {
  1948. asr: NewAllocationSetRange(nil, nil),
  1949. resolution: time.Hour * 1,
  1950. testId: "AccumulateBy_Nils Empty Test 2",
  1951. },
  1952. {
  1953. asr: NewAllocationSetRange(nil, NewAllocationSet(ago2d, yesterday), nil, NewAllocationSet(today, tomorrow)),
  1954. resolution: time.Hour * 24 * 7,
  1955. testId: "AccumulateBy_Nils Empty Test 3",
  1956. },
  1957. }
  1958. for _, c := range nilEmptycases {
  1959. result, err = c.asr.AccumulateBy(c.resolution)
  1960. for _, as := range result.allocations {
  1961. if !as.IsEmpty() {
  1962. t.Errorf("accumulating nil AllocationSetRange: expected empty; actual %s; TestId: %s", result, c.testId)
  1963. }
  1964. }
  1965. }
  1966. if err != nil {
  1967. t.Errorf("unexpected error accumulating nil AllocationSetRange: %s", err)
  1968. }
  1969. yesterdayAS := NewAllocationSet(yesterday, today)
  1970. yesterdayAS.Set(NewMockUnitAllocation("a", yesterday, day, nil))
  1971. todayAS := NewAllocationSet(today, tomorrow)
  1972. todayAS.Set(NewMockUnitAllocation("b", today, day, nil))
  1973. nilAndNonEmptyCases := []struct {
  1974. asr *AllocationSetRange
  1975. resolution time.Duration
  1976. expected float64
  1977. testId string
  1978. }{
  1979. {
  1980. asr: NewAllocationSetRange(nil, todayAS),
  1981. resolution: time.Hour * 2,
  1982. expected: 6.0,
  1983. testId: "AccumulateBy_Nils NonEmpty Test 1",
  1984. },
  1985. {
  1986. asr: NewAllocationSetRange(todayAS, nil),
  1987. resolution: time.Hour * 24,
  1988. expected: 6.0,
  1989. testId: "AccumulateBy_Nils NonEmpty Test 2",
  1990. },
  1991. {
  1992. asr: NewAllocationSetRange(yesterdayAS, nil, todayAS, nil),
  1993. resolution: time.Hour * 24 * 2,
  1994. expected: 12.0,
  1995. testId: "AccumulateBy_Nils NonEmpty Test 3",
  1996. },
  1997. }
  1998. for _, c := range nilAndNonEmptyCases {
  1999. result, err = c.asr.AccumulateBy(c.resolution)
  2000. sumCost := 0.0
  2001. if result == nil {
  2002. t.Errorf("accumulating AllocationSetRange: expected AllocationSet; actual %s; TestId: %s", result, c.testId)
  2003. }
  2004. for _, as := range result.allocations {
  2005. sumCost += as.TotalCost()
  2006. }
  2007. if sumCost != c.expected {
  2008. t.Errorf("accumulating AllocationSetRange: expected total cost %f; actual %f; TestId: %s", c.expected, sumCost, c.testId)
  2009. }
  2010. }
  2011. if err != nil {
  2012. t.Errorf("unexpected error accumulating nil AllocationSetRange: %s", err)
  2013. }
  2014. }
  2015. func TestAllocationSetRange_AccumulateBy(t *testing.T) {
  2016. var err error
  2017. var result *AllocationSetRange
  2018. ago4d := time.Now().UTC().Truncate(day).Add(-4 * day)
  2019. ago3d := time.Now().UTC().Truncate(day).Add(-3 * day)
  2020. ago2d := time.Now().UTC().Truncate(day).Add(-2 * day)
  2021. yesterday := time.Now().UTC().Truncate(day).Add(-day)
  2022. today := time.Now().UTC().Truncate(day)
  2023. tomorrow := time.Now().UTC().Truncate(day).Add(day)
  2024. ago4dAS := NewAllocationSet(ago4d, ago3d)
  2025. ago4dAS.Set(NewMockUnitAllocation("4", ago4d, day, nil))
  2026. ago3dAS := NewAllocationSet(ago3d, ago2d)
  2027. ago3dAS.Set(NewMockUnitAllocation("a", ago3d, day, nil))
  2028. ago2dAS := NewAllocationSet(ago2d, yesterday)
  2029. ago2dAS.Set(NewMockUnitAllocation("", ago2d, day, nil))
  2030. yesterdayAS := NewAllocationSet(yesterday, today)
  2031. yesterdayAS.Set(NewMockUnitAllocation("", yesterday, day, nil))
  2032. todayAS := NewAllocationSet(today, tomorrow)
  2033. todayAS.Set(NewMockUnitAllocation("", today, day, nil))
  2034. yesterHour := time.Now().UTC().Truncate(time.Hour).Add(-1 * time.Hour)
  2035. currentHour := time.Now().UTC().Truncate(time.Hour)
  2036. nextHour := time.Now().UTC().Truncate(time.Hour).Add(time.Hour)
  2037. yesterHourAS := NewAllocationSet(yesterHour, currentHour)
  2038. yesterHourAS.Set(NewMockUnitAllocation("123", yesterHour, time.Hour, nil))
  2039. currentHourAS := NewAllocationSet(currentHour, nextHour)
  2040. currentHourAS.Set(NewMockUnitAllocation("456", currentHour, time.Hour, nil))
  2041. sumCost := 0.0
  2042. // Test nil & empty sets
  2043. cases := []struct {
  2044. asr *AllocationSetRange
  2045. resolution time.Duration
  2046. expectedCost float64
  2047. expectedSets int
  2048. testId string
  2049. }{
  2050. {
  2051. asr: NewAllocationSetRange(yesterdayAS, todayAS),
  2052. resolution: time.Hour * 24 * 2,
  2053. expectedCost: 12.0,
  2054. expectedSets: 1,
  2055. testId: "AccumulateBy Test 1",
  2056. },
  2057. {
  2058. asr: NewAllocationSetRange(ago3dAS, ago2dAS),
  2059. resolution: time.Hour * 24,
  2060. expectedCost: 12.0,
  2061. expectedSets: 2,
  2062. testId: "AccumulateBy Test 2",
  2063. },
  2064. {
  2065. asr: NewAllocationSetRange(ago2dAS, yesterdayAS, todayAS),
  2066. resolution: time.Hour * 13,
  2067. expectedCost: 18.0,
  2068. expectedSets: 3,
  2069. testId: "AccumulateBy Test 3",
  2070. },
  2071. {
  2072. asr: NewAllocationSetRange(ago2dAS, yesterdayAS, todayAS),
  2073. resolution: time.Hour * 24 * 7,
  2074. expectedCost: 18.0,
  2075. expectedSets: 1,
  2076. testId: "AccumulateBy Test 4",
  2077. },
  2078. {
  2079. asr: NewAllocationSetRange(yesterHourAS, currentHourAS),
  2080. resolution: time.Hour * 2,
  2081. //Due to how mock Allocation Sets are generated, hourly sets are still 6.0 cost per set
  2082. expectedCost: 12.0,
  2083. expectedSets: 1,
  2084. testId: "AccumulateBy Test 5",
  2085. },
  2086. {
  2087. asr: NewAllocationSetRange(yesterHourAS, currentHourAS),
  2088. resolution: time.Hour,
  2089. expectedCost: 12.0,
  2090. expectedSets: 2,
  2091. testId: "AccumulateBy Test 6",
  2092. },
  2093. {
  2094. asr: NewAllocationSetRange(yesterHourAS, currentHourAS),
  2095. resolution: time.Minute * 11,
  2096. expectedCost: 12.0,
  2097. expectedSets: 2,
  2098. testId: "AccumulateBy Test 7",
  2099. },
  2100. {
  2101. asr: NewAllocationSetRange(yesterHourAS, currentHourAS),
  2102. resolution: time.Hour * 3,
  2103. expectedCost: 12.0,
  2104. expectedSets: 1,
  2105. testId: "AccumulateBy Test 8",
  2106. },
  2107. {
  2108. asr: NewAllocationSetRange(ago2dAS, yesterdayAS, todayAS),
  2109. resolution: time.Hour * 24 * 2,
  2110. expectedCost: 18.0,
  2111. expectedSets: 2,
  2112. testId: "AccumulateBy Test 9",
  2113. },
  2114. {
  2115. asr: NewAllocationSetRange(ago3dAS, ago2dAS, yesterdayAS, todayAS),
  2116. resolution: time.Hour * 25,
  2117. expectedCost: 24.0,
  2118. expectedSets: 2,
  2119. testId: "AccumulateBy Test 10",
  2120. },
  2121. {
  2122. asr: NewAllocationSetRange(ago4dAS, ago3dAS, ago2dAS, yesterdayAS, todayAS),
  2123. resolution: time.Hour * 72,
  2124. expectedCost: 30.0,
  2125. expectedSets: 2,
  2126. testId: "AccumulateBy Test 11",
  2127. },
  2128. }
  2129. for _, c := range cases {
  2130. result, err = c.asr.AccumulateBy(c.resolution)
  2131. sumCost := 0.0
  2132. if result == nil {
  2133. t.Errorf("accumulating AllocationSetRange: expected AllocationSet; actual %s; TestId: %s", result, c.testId)
  2134. }
  2135. if result.Length() != c.expectedSets {
  2136. t.Errorf("accumulating AllocationSetRange: expected %v number of allocation sets; actual %v; TestId: %s", c.expectedSets, result.Length(), c.testId)
  2137. }
  2138. for _, as := range result.allocations {
  2139. sumCost += as.TotalCost()
  2140. }
  2141. if sumCost != c.expectedCost {
  2142. t.Errorf("accumulating AllocationSetRange: expected total cost %f; actual %f; TestId: %s", c.expectedCost, sumCost, c.testId)
  2143. }
  2144. }
  2145. if err != nil {
  2146. t.Errorf("unexpected error accumulating nil AllocationSetRange: %s", err)
  2147. }
  2148. // // Accumulate three non-nil should result in sum of both with appropriate start, end
  2149. result, err = NewAllocationSetRange(ago2dAS, yesterdayAS, todayAS).AccumulateBy(time.Hour * 24 * 2)
  2150. if err != nil {
  2151. t.Errorf("unexpected error accumulating AllocationSetRange of length 1: %s", err)
  2152. }
  2153. if result == nil {
  2154. t.Errorf("accumulating AllocationSetRange: expected AllocationSet; actual %s", result)
  2155. }
  2156. sumCost = 0.0
  2157. for _, as := range result.allocations {
  2158. sumCost += as.TotalCost()
  2159. }
  2160. allocMap := result.allocations[0].Map()
  2161. if len(allocMap) != 1 {
  2162. t.Errorf("accumulating AllocationSetRange: expected length 1; actual length %d", len(allocMap))
  2163. }
  2164. alloc := allocMap["cluster1/namespace1/pod1/container1"]
  2165. if alloc == nil {
  2166. t.Fatalf("accumulating AllocationSetRange: expected allocation 'cluster1/namespace1/pod1/container1'")
  2167. }
  2168. if alloc.CPUCoreHours != 2.0 {
  2169. t.Errorf("accumulating AllocationSetRange: expected 2.0; actual %f", sumCost)
  2170. }
  2171. if alloc.CPUCost != 2.0 {
  2172. t.Errorf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.CPUCost)
  2173. }
  2174. if alloc.CPUEfficiency() != 1.0 {
  2175. t.Errorf("accumulating AllocationSetRange: expected 1.0; actual %f", alloc.CPUEfficiency())
  2176. }
  2177. if alloc.GPUHours != 2.0 {
  2178. t.Errorf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.GPUHours)
  2179. }
  2180. if alloc.GPUCost != 2.0 {
  2181. t.Errorf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.GPUCost)
  2182. }
  2183. if alloc.NetworkCost != 2.0 {
  2184. t.Errorf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.NetworkCost)
  2185. }
  2186. if alloc.LoadBalancerCost != 2.0 {
  2187. t.Errorf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.LoadBalancerCost)
  2188. }
  2189. if alloc.PVByteHours() != 2.0 {
  2190. t.Errorf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.PVByteHours())
  2191. }
  2192. if alloc.PVCost() != 2.0 {
  2193. t.Errorf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.PVCost())
  2194. }
  2195. if alloc.RAMByteHours != 2.0 {
  2196. t.Errorf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.RAMByteHours)
  2197. }
  2198. if alloc.RAMCost != 2.0 {
  2199. t.Errorf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.RAMCost)
  2200. }
  2201. if alloc.RAMEfficiency() != 1.0 {
  2202. t.Errorf("accumulating AllocationSetRange: expected 1.0; actual %f", alloc.RAMEfficiency())
  2203. }
  2204. if alloc.TotalCost() != 12.0 {
  2205. t.Errorf("accumulating AllocationSetRange: expected 12.0; actual %f", alloc.TotalCost())
  2206. }
  2207. if alloc.TotalEfficiency() != 1.0 {
  2208. t.Errorf("accumulating AllocationSetRange: expected 1.0; actual %f", alloc.TotalEfficiency())
  2209. }
  2210. if !alloc.Start.Equal(ago2d) {
  2211. t.Errorf("accumulating AllocationSetRange: expected to start %s; actual %s", ago2d, alloc.Start)
  2212. }
  2213. if !alloc.End.Equal(today) {
  2214. t.Errorf("accumulating AllocationSetRange: expected to end %s; actual %s", today, alloc.End)
  2215. }
  2216. if alloc.Minutes() != 2880.0 {
  2217. t.Errorf("accumulating AllocationSetRange: expected %f minutes; actual %f", 2880.0, alloc.Minutes())
  2218. }
  2219. }
  2220. // TODO niko/etl
  2221. // func TestAllocationSetRange_AggregateBy(t *testing.T) {}
  2222. // TODO niko/etl
  2223. // func TestAllocationSetRange_Append(t *testing.T) {}
  2224. // TODO niko/etl
  2225. // func TestAllocationSetRange_Each(t *testing.T) {}
  2226. // TODO niko/etl
  2227. // func TestAllocationSetRange_Get(t *testing.T) {}
  2228. func TestAllocationSetRange_InsertRange(t *testing.T) {
  2229. // Set up
  2230. ago2d := time.Now().UTC().Truncate(day).Add(-2 * day)
  2231. yesterday := time.Now().UTC().Truncate(day).Add(-day)
  2232. today := time.Now().UTC().Truncate(day)
  2233. tomorrow := time.Now().UTC().Truncate(day).Add(day)
  2234. unit := NewMockUnitAllocation("", today, day, nil)
  2235. ago2dAS := NewAllocationSet(ago2d, yesterday)
  2236. ago2dAS.Set(NewMockUnitAllocation("a", ago2d, day, nil))
  2237. ago2dAS.Set(NewMockUnitAllocation("b", ago2d, day, nil))
  2238. ago2dAS.Set(NewMockUnitAllocation("c", ago2d, day, nil))
  2239. yesterdayAS := NewAllocationSet(yesterday, today)
  2240. yesterdayAS.Set(NewMockUnitAllocation("a", yesterday, day, nil))
  2241. yesterdayAS.Set(NewMockUnitAllocation("b", yesterday, day, nil))
  2242. yesterdayAS.Set(NewMockUnitAllocation("c", yesterday, day, nil))
  2243. todayAS := NewAllocationSet(today, tomorrow)
  2244. todayAS.Set(NewMockUnitAllocation("a", today, day, nil))
  2245. todayAS.Set(NewMockUnitAllocation("b", today, day, nil))
  2246. todayAS.Set(NewMockUnitAllocation("c", today, day, nil))
  2247. var nilASR *AllocationSetRange
  2248. thisASR := NewAllocationSetRange(yesterdayAS.Clone(), todayAS.Clone())
  2249. thatASR := NewAllocationSetRange(yesterdayAS.Clone())
  2250. longASR := NewAllocationSetRange(ago2dAS.Clone(), yesterdayAS.Clone(), todayAS.Clone())
  2251. var err error
  2252. // Expect an error calling InsertRange on nil
  2253. err = nilASR.InsertRange(thatASR)
  2254. if err == nil {
  2255. t.Fatalf("expected error, got nil")
  2256. }
  2257. // Expect nothing to happen calling InsertRange(nil) on non-nil ASR
  2258. err = thisASR.InsertRange(nil)
  2259. if err != nil {
  2260. t.Fatalf("unexpected error: %s", err)
  2261. }
  2262. thisASR.Each(func(i int, as *AllocationSet) {
  2263. as.Each(func(k string, a *Allocation) {
  2264. if !util.IsApproximately(a.CPUCoreHours, unit.CPUCoreHours) {
  2265. t.Fatalf("allocation %s: expected %f; got %f", k, unit.CPUCoreHours, a.CPUCoreHours)
  2266. }
  2267. if !util.IsApproximately(a.CPUCost, unit.CPUCost) {
  2268. t.Fatalf("allocation %s: expected %f; got %f", k, unit.CPUCost, a.CPUCost)
  2269. }
  2270. if !util.IsApproximately(a.RAMByteHours, unit.RAMByteHours) {
  2271. t.Fatalf("allocation %s: expected %f; got %f", k, unit.RAMByteHours, a.RAMByteHours)
  2272. }
  2273. if !util.IsApproximately(a.RAMCost, unit.RAMCost) {
  2274. t.Fatalf("allocation %s: expected %f; got %f", k, unit.RAMCost, a.RAMCost)
  2275. }
  2276. if !util.IsApproximately(a.GPUHours, unit.GPUHours) {
  2277. t.Fatalf("allocation %s: expected %f; got %f", k, unit.GPUHours, a.GPUHours)
  2278. }
  2279. if !util.IsApproximately(a.GPUCost, unit.GPUCost) {
  2280. t.Fatalf("allocation %s: expected %f; got %f", k, unit.GPUCost, a.GPUCost)
  2281. }
  2282. if !util.IsApproximately(a.PVByteHours(), unit.PVByteHours()) {
  2283. t.Fatalf("allocation %s: expected %f; got %f", k, unit.PVByteHours(), a.PVByteHours())
  2284. }
  2285. if !util.IsApproximately(a.PVCost(), unit.PVCost()) {
  2286. t.Fatalf("allocation %s: expected %f; got %f", k, unit.PVCost(), a.PVCost())
  2287. }
  2288. if !util.IsApproximately(a.NetworkCost, unit.NetworkCost) {
  2289. t.Fatalf("allocation %s: expected %f; got %f", k, unit.NetworkCost, a.NetworkCost)
  2290. }
  2291. if !util.IsApproximately(a.LoadBalancerCost, unit.LoadBalancerCost) {
  2292. t.Fatalf("allocation %s: expected %f; got %f", k, unit.LoadBalancerCost, a.LoadBalancerCost)
  2293. }
  2294. if !util.IsApproximately(a.TotalCost(), unit.TotalCost()) {
  2295. t.Fatalf("allocation %s: expected %f; got %f", k, unit.TotalCost(), a.TotalCost())
  2296. }
  2297. })
  2298. })
  2299. // Expect an error calling InsertRange with a range exceeding the receiver
  2300. err = thisASR.InsertRange(longASR)
  2301. if err == nil {
  2302. t.Fatalf("expected error calling InsertRange with a range exceeding the receiver")
  2303. }
  2304. // Expect each Allocation in "today" to stay the same, but "yesterday" to
  2305. // precisely double when inserting a range that only has a duplicate of
  2306. // "yesterday", but no entry for "today"
  2307. err = thisASR.InsertRange(thatASR)
  2308. if err != nil {
  2309. t.Fatalf("unexpected error: %s", err)
  2310. }
  2311. yAS, err := thisASR.Get(0)
  2312. yAS.Each(func(k string, a *Allocation) {
  2313. if !util.IsApproximately(a.CPUCoreHours, 2*unit.CPUCoreHours) {
  2314. t.Fatalf("allocation %s: expected %f; got %f", k, unit.CPUCoreHours, a.CPUCoreHours)
  2315. }
  2316. if !util.IsApproximately(a.CPUCost, 2*unit.CPUCost) {
  2317. t.Fatalf("allocation %s: expected %f; got %f", k, unit.CPUCost, a.CPUCost)
  2318. }
  2319. if !util.IsApproximately(a.RAMByteHours, 2*unit.RAMByteHours) {
  2320. t.Fatalf("allocation %s: expected %f; got %f", k, unit.RAMByteHours, a.RAMByteHours)
  2321. }
  2322. if !util.IsApproximately(a.RAMCost, 2*unit.RAMCost) {
  2323. t.Fatalf("allocation %s: expected %f; got %f", k, unit.RAMCost, a.RAMCost)
  2324. }
  2325. if !util.IsApproximately(a.GPUHours, 2*unit.GPUHours) {
  2326. t.Fatalf("allocation %s: expected %f; got %f", k, unit.GPUHours, a.GPUHours)
  2327. }
  2328. if !util.IsApproximately(a.GPUCost, 2*unit.GPUCost) {
  2329. t.Fatalf("allocation %s: expected %f; got %f", k, unit.GPUCost, a.GPUCost)
  2330. }
  2331. if !util.IsApproximately(a.PVByteHours(), 2*unit.PVByteHours()) {
  2332. t.Fatalf("allocation %s: expected %f; got %f", k, unit.PVByteHours(), a.PVByteHours())
  2333. }
  2334. if !util.IsApproximately(a.PVCost(), 2*unit.PVCost()) {
  2335. t.Fatalf("allocation %s: expected %f; got %f", k, unit.PVCost(), a.PVCost())
  2336. }
  2337. if !util.IsApproximately(a.NetworkCost, 2*unit.NetworkCost) {
  2338. t.Fatalf("allocation %s: expected %f; got %f", k, unit.NetworkCost, a.NetworkCost)
  2339. }
  2340. if !util.IsApproximately(a.LoadBalancerCost, 2*unit.LoadBalancerCost) {
  2341. t.Fatalf("allocation %s: expected %f; got %f", k, unit.LoadBalancerCost, a.LoadBalancerCost)
  2342. }
  2343. if !util.IsApproximately(a.TotalCost(), 2*unit.TotalCost()) {
  2344. t.Fatalf("allocation %s: expected %f; got %f", k, unit.TotalCost(), a.TotalCost())
  2345. }
  2346. })
  2347. tAS, err := thisASR.Get(1)
  2348. tAS.Each(func(k string, a *Allocation) {
  2349. if !util.IsApproximately(a.CPUCoreHours, unit.CPUCoreHours) {
  2350. t.Fatalf("allocation %s: expected %f; got %f", k, unit.CPUCoreHours, a.CPUCoreHours)
  2351. }
  2352. if !util.IsApproximately(a.CPUCost, unit.CPUCost) {
  2353. t.Fatalf("allocation %s: expected %f; got %f", k, unit.CPUCost, a.CPUCost)
  2354. }
  2355. if !util.IsApproximately(a.RAMByteHours, unit.RAMByteHours) {
  2356. t.Fatalf("allocation %s: expected %f; got %f", k, unit.RAMByteHours, a.RAMByteHours)
  2357. }
  2358. if !util.IsApproximately(a.RAMCost, unit.RAMCost) {
  2359. t.Fatalf("allocation %s: expected %f; got %f", k, unit.RAMCost, a.RAMCost)
  2360. }
  2361. if !util.IsApproximately(a.GPUHours, unit.GPUHours) {
  2362. t.Fatalf("allocation %s: expected %f; got %f", k, unit.GPUHours, a.GPUHours)
  2363. }
  2364. if !util.IsApproximately(a.GPUCost, unit.GPUCost) {
  2365. t.Fatalf("allocation %s: expected %f; got %f", k, unit.GPUCost, a.GPUCost)
  2366. }
  2367. if !util.IsApproximately(a.PVByteHours(), unit.PVByteHours()) {
  2368. t.Fatalf("allocation %s: expected %f; got %f", k, unit.PVByteHours(), a.PVByteHours())
  2369. }
  2370. if !util.IsApproximately(a.PVCost(), unit.PVCost()) {
  2371. t.Fatalf("allocation %s: expected %f; got %f", k, unit.PVCost(), a.PVCost())
  2372. }
  2373. if !util.IsApproximately(a.NetworkCost, unit.NetworkCost) {
  2374. t.Fatalf("allocation %s: expected %f; got %f", k, unit.NetworkCost, a.NetworkCost)
  2375. }
  2376. if !util.IsApproximately(a.LoadBalancerCost, unit.LoadBalancerCost) {
  2377. t.Fatalf("allocation %s: expected %f; got %f", k, unit.LoadBalancerCost, a.LoadBalancerCost)
  2378. }
  2379. if !util.IsApproximately(a.TotalCost(), unit.TotalCost()) {
  2380. t.Fatalf("allocation %s: expected %f; got %f", k, unit.TotalCost(), a.TotalCost())
  2381. }
  2382. })
  2383. }
  2384. // TODO niko/etl
  2385. // func TestAllocationSetRange_Length(t *testing.T) {}
  2386. // TODO niko/etl
  2387. // func TestAllocationSetRange_MarshalJSON(t *testing.T) {}
  2388. // TODO niko/etl
  2389. // func TestAllocationSetRange_Slice(t *testing.T) {}
  2390. // TODO niko/etl
  2391. // func TestAllocationSetRange_Window(t *testing.T) {}
  2392. func TestAllocationSetRange_Start(t *testing.T) {
  2393. tests := []struct {
  2394. name string
  2395. arg *AllocationSetRange
  2396. expectError bool
  2397. expected time.Time
  2398. }{
  2399. {
  2400. name: "Empty ASR",
  2401. arg: nil,
  2402. expectError: true,
  2403. },
  2404. {
  2405. name: "Single allocation",
  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. },
  2413. },
  2414. },
  2415. },
  2416. },
  2417. expected: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  2418. },
  2419. {
  2420. name: "Two allocations",
  2421. arg: &AllocationSetRange{
  2422. allocations: []*AllocationSet{
  2423. {
  2424. allocations: map[string]*Allocation{
  2425. "a": {
  2426. Start: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  2427. },
  2428. "b": {
  2429. Start: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  2430. },
  2431. },
  2432. },
  2433. },
  2434. },
  2435. expected: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  2436. },
  2437. {
  2438. name: "Two AllocationSets",
  2439. arg: &AllocationSetRange{
  2440. allocations: []*AllocationSet{
  2441. {
  2442. allocations: map[string]*Allocation{
  2443. "a": {
  2444. Start: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  2445. },
  2446. },
  2447. },
  2448. {
  2449. allocations: map[string]*Allocation{
  2450. "b": {
  2451. Start: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  2452. },
  2453. },
  2454. },
  2455. },
  2456. },
  2457. expected: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  2458. },
  2459. }
  2460. for _, test := range tests {
  2461. result, err := test.arg.Start()
  2462. if test.expectError && err != nil {
  2463. continue
  2464. }
  2465. if test.expectError && err == nil {
  2466. t.Errorf("%s: expected error and got none", test.name)
  2467. } else if result != test.expected {
  2468. t.Errorf("%s: expected %s but got %s", test.name, test.expected, result)
  2469. }
  2470. }
  2471. }
  2472. func TestAllocationSetRange_End(t *testing.T) {
  2473. tests := []struct {
  2474. name string
  2475. arg *AllocationSetRange
  2476. expectError bool
  2477. expected time.Time
  2478. }{
  2479. {
  2480. name: "Empty ASR",
  2481. arg: nil,
  2482. expectError: true,
  2483. },
  2484. {
  2485. name: "Single allocation",
  2486. arg: &AllocationSetRange{
  2487. allocations: []*AllocationSet{
  2488. {
  2489. allocations: map[string]*Allocation{
  2490. "a": {
  2491. End: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  2492. },
  2493. },
  2494. },
  2495. },
  2496. },
  2497. expected: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  2498. },
  2499. {
  2500. name: "Two allocations",
  2501. arg: &AllocationSetRange{
  2502. allocations: []*AllocationSet{
  2503. {
  2504. allocations: map[string]*Allocation{
  2505. "a": {
  2506. End: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  2507. },
  2508. "b": {
  2509. End: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  2510. },
  2511. },
  2512. },
  2513. },
  2514. },
  2515. expected: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  2516. },
  2517. {
  2518. name: "Two AllocationSets",
  2519. arg: &AllocationSetRange{
  2520. allocations: []*AllocationSet{
  2521. {
  2522. allocations: map[string]*Allocation{
  2523. "a": {
  2524. End: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  2525. },
  2526. },
  2527. },
  2528. {
  2529. allocations: map[string]*Allocation{
  2530. "b": {
  2531. End: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  2532. },
  2533. },
  2534. },
  2535. },
  2536. },
  2537. expected: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  2538. },
  2539. }
  2540. for _, test := range tests {
  2541. result, err := test.arg.End()
  2542. if test.expectError && err != nil {
  2543. continue
  2544. }
  2545. if test.expectError && err == nil {
  2546. t.Errorf("%s: expected error and got none", test.name)
  2547. } else if result != test.expected {
  2548. t.Errorf("%s: expected %s but got %s", test.name, test.expected, result)
  2549. }
  2550. }
  2551. }
  2552. func TestAllocationSetRange_Minutes(t *testing.T) {
  2553. tests := []struct {
  2554. name string
  2555. arg *AllocationSetRange
  2556. expected float64
  2557. }{
  2558. {
  2559. name: "Empty ASR",
  2560. arg: nil,
  2561. expected: 0,
  2562. },
  2563. {
  2564. name: "Single allocation",
  2565. arg: &AllocationSetRange{
  2566. allocations: []*AllocationSet{
  2567. {
  2568. allocations: map[string]*Allocation{
  2569. "a": {
  2570. Start: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  2571. End: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  2572. },
  2573. },
  2574. },
  2575. },
  2576. },
  2577. expected: 24 * 60,
  2578. },
  2579. {
  2580. name: "Two allocations",
  2581. arg: &AllocationSetRange{
  2582. allocations: []*AllocationSet{
  2583. {
  2584. allocations: map[string]*Allocation{
  2585. "a": {
  2586. Start: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  2587. End: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  2588. },
  2589. "b": {
  2590. Start: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  2591. End: time.Date(1970, 1, 3, 0, 0, 0, 0, time.UTC),
  2592. },
  2593. },
  2594. },
  2595. },
  2596. },
  2597. expected: 2 * 24 * 60,
  2598. },
  2599. {
  2600. name: "Two AllocationSets",
  2601. arg: &AllocationSetRange{
  2602. allocations: []*AllocationSet{
  2603. {
  2604. allocations: map[string]*Allocation{
  2605. "a": {
  2606. Start: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  2607. End: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  2608. },
  2609. },
  2610. },
  2611. {
  2612. allocations: map[string]*Allocation{
  2613. "b": {
  2614. Start: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  2615. End: time.Date(1970, 1, 3, 0, 0, 0, 0, time.UTC),
  2616. },
  2617. },
  2618. },
  2619. },
  2620. },
  2621. expected: 2 * 24 * 60,
  2622. },
  2623. }
  2624. for _, test := range tests {
  2625. result := test.arg.Minutes()
  2626. if result != test.expected {
  2627. t.Errorf("%s: expected %f but got %f", test.name, test.expected, result)
  2628. }
  2629. }
  2630. }