allocation_test.go 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425
  1. package kubecost
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "math"
  6. "testing"
  7. "time"
  8. "github.com/kubecost/cost-model/pkg/util"
  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_MarshalJSON(t *testing.T) {
  331. start := time.Date(2021, time.January, 1, 0, 0, 0, 0, time.UTC)
  332. end := time.Date(2021, time.January, 2, 0, 0, 0, 0, time.UTC)
  333. hrs := 24.0
  334. gib := 1024.0 * 1024.0 * 1024.0
  335. cpuPrice := 0.02
  336. gpuPrice := 2.00
  337. ramPrice := 0.01
  338. pvPrice := 0.00005
  339. before := &Allocation{
  340. Name: "cluster1/namespace1/node1/pod1/container1",
  341. Properties: &AllocationProperties{
  342. Cluster: "cluster1",
  343. Node: "node1",
  344. Namespace: "namespace1",
  345. Pod: "pod1",
  346. Container: "container1",
  347. },
  348. Window: NewWindow(&start, &end),
  349. Start: start,
  350. End: end,
  351. CPUCoreHours: 2.0 * hrs,
  352. CPUCoreRequestAverage: 2.0,
  353. CPUCoreUsageAverage: 1.0,
  354. CPUCost: 2.0 * hrs * cpuPrice,
  355. CPUCostAdjustment: 3.0,
  356. GPUHours: 1.0 * hrs,
  357. GPUCost: 1.0 * hrs * gpuPrice,
  358. GPUCostAdjustment: 2.0,
  359. NetworkCost: 0.05,
  360. LoadBalancerCost: 0.02,
  361. PVs: PVAllocations{
  362. disk: {
  363. ByteHours: 100.0 * gib * hrs,
  364. Cost: 100.0 * hrs * pvPrice,
  365. },
  366. },
  367. PVCostAdjustment: 4.0,
  368. RAMByteHours: 8.0 * gib * hrs,
  369. RAMBytesRequestAverage: 8.0 * gib,
  370. RAMBytesUsageAverage: 4.0 * gib,
  371. RAMCost: 8.0 * hrs * ramPrice,
  372. RAMCostAdjustment: 1.0,
  373. SharedCost: 2.00,
  374. ExternalCost: 1.00,
  375. RawAllocationOnly: &RawAllocationOnlyData{},
  376. }
  377. data, err := json.Marshal(before)
  378. if err != nil {
  379. t.Fatalf("Allocation.MarshalJSON: unexpected error: %s", err)
  380. }
  381. after := &Allocation{}
  382. err = json.Unmarshal(data, after)
  383. if err != nil {
  384. t.Fatalf("Allocation.UnmarshalJSON: unexpected error: %s", err)
  385. }
  386. // TODO:CLEANUP fix json marshaling of Window so that all of this works.
  387. // In the meantime, just set the Window so that we can test the rest.
  388. after.Window = before.Window.Clone()
  389. // TODO Sean: fix JSON marshaling of PVs
  390. after.PVs = before.PVs
  391. if !after.Equal(before) {
  392. t.Fatalf("Allocation.MarshalJSON: before and after are not equal")
  393. }
  394. }
  395. func TestAllocationSet_generateKey(t *testing.T) {
  396. var alloc *Allocation
  397. var key string
  398. props := []string{
  399. AllocationClusterProp,
  400. }
  401. key = alloc.generateKey(props, nil)
  402. if key != "" {
  403. t.Fatalf("generateKey: expected \"\"; actual \"%s\"", key)
  404. }
  405. alloc = &Allocation{}
  406. alloc.Properties = &AllocationProperties{
  407. Cluster: "cluster1",
  408. Labels: map[string]string{
  409. "app": "app1",
  410. "env": "env1",
  411. },
  412. }
  413. key = alloc.generateKey(props, nil)
  414. if key != "cluster1" {
  415. t.Fatalf("generateKey: expected \"cluster1\"; actual \"%s\"", key)
  416. }
  417. props = []string{
  418. AllocationClusterProp,
  419. AllocationNamespaceProp,
  420. "label:app",
  421. }
  422. key = alloc.generateKey(props, nil)
  423. if key != "cluster1//app=app1" {
  424. t.Fatalf("generateKey: expected \"cluster1//app=app1\"; actual \"%s\"", key)
  425. }
  426. alloc.Properties = &AllocationProperties{
  427. Cluster: "cluster1",
  428. Namespace: "namespace1",
  429. Labels: map[string]string{
  430. "app": "app1",
  431. "env": "env1",
  432. },
  433. }
  434. key = alloc.generateKey(props, nil)
  435. if key != "cluster1/namespace1/app=app1" {
  436. t.Fatalf("generateKey: expected \"cluster1/namespace1/app=app1\"; actual \"%s\"", key)
  437. }
  438. props = []string{
  439. AllocationDepartmentProp,
  440. AllocationEnvironmentProp,
  441. AllocationOwnerProp,
  442. AllocationProductProp,
  443. AllocationTeamProp,
  444. }
  445. labelConfig := NewLabelConfig()
  446. alloc.Properties = &AllocationProperties{
  447. Cluster: "cluster1",
  448. Namespace: "namespace1",
  449. Labels: map[string]string{
  450. labelConfig.DepartmentLabel: "dept1",
  451. labelConfig.EnvironmentLabel: "envt1",
  452. labelConfig.OwnerLabel: "ownr1",
  453. labelConfig.ProductLabel: "prod1",
  454. labelConfig.TeamLabel: "team1",
  455. },
  456. }
  457. key = alloc.generateKey(props, nil)
  458. if key != "dept1/envt1/ownr1/prod1/team1" {
  459. t.Fatalf("generateKey: expected \"dept1/envt1/ownr1/prod1/team1\"; actual \"%s\"", key)
  460. }
  461. }
  462. func TestNewAllocationSet(t *testing.T) {
  463. // TODO niko/etl
  464. }
  465. func assertAllocationSetTotals(t *testing.T, as *AllocationSet, msg string, err error, length int, totalCost float64) {
  466. if err != nil {
  467. t.Fatalf("AllocationSet.AggregateBy[%s]: unexpected error: %s", msg, err)
  468. }
  469. if as.Length() != length {
  470. t.Fatalf("AllocationSet.AggregateBy[%s]: expected set of length %d, actual %d", msg, length, as.Length())
  471. }
  472. if math.Round(as.TotalCost()*100) != math.Round(totalCost*100) {
  473. t.Fatalf("AllocationSet.AggregateBy[%s]: expected total cost %.2f, actual %.2f", msg, totalCost, as.TotalCost())
  474. }
  475. }
  476. func assertAllocationTotals(t *testing.T, as *AllocationSet, msg string, exps map[string]float64) {
  477. as.Each(func(k string, a *Allocation) {
  478. if exp, ok := exps[a.Name]; ok {
  479. if math.Round(a.TotalCost()*100) != math.Round(exp*100) {
  480. t.Fatalf("AllocationSet.AggregateBy[%s]: expected total cost %f, actual %f", msg, exp, a.TotalCost())
  481. }
  482. } else {
  483. t.Fatalf("AllocationSet.AggregateBy[%s]: unexpected allocation: %s", msg, a.Name)
  484. }
  485. })
  486. }
  487. func assertAllocationWindow(t *testing.T, as *AllocationSet, msg string, expStart, expEnd time.Time, expMinutes float64) {
  488. as.Each(func(k string, a *Allocation) {
  489. if !a.Start.Equal(expStart) {
  490. t.Fatalf("AllocationSet.AggregateBy[%s]: expected start %s, actual %s", msg, expStart, a.Start)
  491. }
  492. if !a.End.Equal(expEnd) {
  493. t.Fatalf("AllocationSet.AggregateBy[%s]: expected end %s, actual %s", msg, expEnd, a.End)
  494. }
  495. if a.Minutes() != expMinutes {
  496. t.Fatalf("AllocationSet.AggregateBy[%s]: expected minutes %f, actual %f", msg, expMinutes, a.Minutes())
  497. }
  498. })
  499. }
  500. func printAllocationSet(msg string, as *AllocationSet) {
  501. fmt.Printf("--- %s ---\n", msg)
  502. as.Each(func(k string, a *Allocation) {
  503. fmt.Printf(" > %s\n", a)
  504. })
  505. }
  506. func TestAllocationSet_AggregateBy(t *testing.T) {
  507. // Test AggregateBy against the following workload topology, which is
  508. // generated by GenerateMockAllocationSet:
  509. // | Hierarchy | Cost | CPU | RAM | GPU | PV | Net | LB |
  510. // +----------------------------------------+------+------+------+------+------+------+------+
  511. // cluster1:
  512. // idle: 20.00 5.00 15.00 0.00 0.00 0.00 0.00
  513. // namespace1:
  514. // pod1:
  515. // container1: [app=app1, env=env1] 16.00 1.00 11.00 1.00 1.00 1.00 1.00
  516. // pod-abc: (deployment1)
  517. // container2: 6.00 1.00 1.00 1.00 1.00 1.00 1.00
  518. // pod-def: (deployment1)
  519. // container3: 6.00 1.00 1.00 1.00 1.00 1.00 1.00
  520. // namespace2:
  521. // pod-ghi: (deployment2)
  522. // container4: [app=app2, env=env2] 6.00 1.00 1.00 1.00 1.00 1.00 1.00
  523. // container5: [app=app2, env=env2] 6.00 1.00 1.00 1.00 1.00 1.00 1.00
  524. // pod-jkl: (daemonset1)
  525. // container6: {service1} 6.00 1.00 1.00 1.00 1.00 1.00 1.00
  526. // +-----------------------------------------+------+------+------+------+------+------+------+
  527. // cluster1 subtotal 66.00 11.00 31.00 6.00 6.00 6.00 6.00
  528. // +-----------------------------------------+------+------+------+------+------+------+------+
  529. // cluster2:
  530. // idle: 10.00 5.00 5.00 0.00 0.00 0.00 0.00
  531. // namespace2:
  532. // pod-mno: (deployment2)
  533. // container4: [app=app2] 6.00 1.00 1.00 1.00 1.00 1.00 1.00
  534. // container5: [app=app2] 6.00 1.00 1.00 1.00 1.00 1.00 1.00
  535. // pod-pqr: (daemonset1)
  536. // container6: {service1} 6.00 1.00 1.00 1.00 1.00 1.00 1.00
  537. // namespace3:
  538. // pod-stu: (deployment3)
  539. // container7: an[team=team1] 6.00 1.00 1.00 1.00 1.00 1.00 1.00
  540. // pod-vwx: (statefulset1)
  541. // container8: an[team=team2] 6.00 1.00 1.00 1.00 1.00 1.00 1.00
  542. // container9: an[team=team1] 6.00 1.00 1.00 1.00 1.00 1.00 1.00
  543. // +----------------------------------------+------+------+------+------+------+------+------+
  544. // cluster2 subtotal 46.00 11.00 11.00 6.00 6.00 6.00 6.00
  545. // +----------------------------------------+------+------+------+------+------+------+------+
  546. // total 112.00 22.00 42.00 12.00 12.00 12.00 12.00
  547. // +----------------------------------------+------+------+------+------+------+------+------+
  548. // Scenarios to test:
  549. // 1 Single-aggregation
  550. // 1a AggregationProperties=(Cluster)
  551. // 1b AggregationProperties=(Namespace)
  552. // 1c AggregationProperties=(Pod)
  553. // 1d AggregationProperties=(Container)
  554. // 1e AggregationProperties=(ControllerKind)
  555. // 1f AggregationProperties=(Controller)
  556. // 1g AggregationProperties=(Service)
  557. // 1h AggregationProperties=(Label:app)
  558. // 2 Multi-aggregation
  559. // 2a AggregationProperties=(Cluster, Namespace)
  560. // 2b AggregationProperties=(Namespace, Label:app)
  561. // 2c AggregationProperties=(Cluster, Namespace, Pod, Container)
  562. // 2d AggregationProperties=(Label:app, Label:environment)
  563. // 3 Share idle
  564. // 3a AggregationProperties=(Namespace) ShareIdle=ShareWeighted
  565. // 3b AggregationProperties=(Namespace) ShareIdle=ShareEven (TODO niko/etl)
  566. // 4 Share resources
  567. // 4a Share namespace ShareEven
  568. // 4b Share cluster ShareWeighted
  569. // 4c Share label ShareEven
  570. // 4d Share overhead ShareWeighted
  571. // 5 Filters
  572. // 5a Filter by cluster with separate idle
  573. // 5b Filter by cluster with shared idle
  574. // TODO niko/idle more filter tests
  575. // 6 Combinations and options
  576. // 6a SplitIdle
  577. // 6b Share idle with filters
  578. // 6c Share resources with filters
  579. // 6d Share idle and share resources
  580. // 6e IdleByNode
  581. // 7 Edge cases and errors
  582. // 7a Empty AggregationProperties
  583. // 7b Filter all
  584. // 7c Share all
  585. // 7d Share and filter the same allocations
  586. // Definitions and set-up:
  587. var as *AllocationSet
  588. var err error
  589. endYesterday := time.Now().UTC().Truncate(day)
  590. startYesterday := endYesterday.Add(-day)
  591. numClusters := 2
  592. numNamespaces := 3
  593. numPods := 9
  594. numContainers := 9
  595. numControllerKinds := 3
  596. numControllers := 5
  597. numServices := 1
  598. numLabelApps := 2
  599. // By default, idle is reported as a single, merged allocation
  600. numIdle := 1
  601. // There will only ever be one __unallocated__
  602. numUnallocated := 1
  603. // There are two clusters, so each gets an idle entry when they are split
  604. numSplitIdleCluster := 2
  605. // There are two clusters, so each gets an idle entry when they are split
  606. numSplitIdleNode := 4
  607. activeTotalCost := 82.0
  608. idleTotalCost := 30.0
  609. sharedOverheadHourlyCost := 7.0
  610. // Match Functions
  611. isNamespace3 := func(a *Allocation) bool {
  612. ns := a.Properties.Namespace
  613. return ns == "namespace3"
  614. }
  615. isApp1 := func(a *Allocation) bool {
  616. ls := a.Properties.Labels
  617. if app, ok := ls["app"]; ok && app == "app1" {
  618. return true
  619. }
  620. return false
  621. }
  622. // Filters
  623. isCluster := func(matchCluster string) func(*Allocation) bool {
  624. return func(a *Allocation) bool {
  625. cluster := a.Properties.Cluster
  626. return cluster == matchCluster
  627. }
  628. }
  629. isNamespace := func(matchNamespace string) func(*Allocation) bool {
  630. return func(a *Allocation) bool {
  631. namespace := a.Properties.Namespace
  632. return namespace == matchNamespace
  633. }
  634. }
  635. end := time.Now().UTC().Truncate(day)
  636. start := end.Add(-day)
  637. // Tests:
  638. cases := map[string]struct {
  639. start time.Time
  640. aggBy []string
  641. aggOpts *AllocationAggregationOptions
  642. numResults int
  643. totalCost float64
  644. results map[string]float64
  645. windowStart time.Time
  646. windowEnd time.Time
  647. expMinutes float64
  648. }{
  649. // 1 Single-aggregation
  650. // 1a AggregationProperties=(Cluster)
  651. "1a": {
  652. start: start,
  653. aggBy: []string{AllocationClusterProp},
  654. aggOpts: nil,
  655. numResults: numClusters + numIdle,
  656. totalCost: activeTotalCost + idleTotalCost,
  657. results: map[string]float64{
  658. "cluster1": 46.00,
  659. "cluster2": 36.00,
  660. IdleSuffix: 30.00,
  661. },
  662. windowStart: startYesterday,
  663. windowEnd: endYesterday,
  664. expMinutes: 1440.0,
  665. },
  666. // 1b AggregationProperties=(Namespace)
  667. "1b": {
  668. start: start,
  669. aggBy: []string{AllocationNamespaceProp},
  670. aggOpts: nil,
  671. numResults: numNamespaces + numIdle,
  672. totalCost: activeTotalCost + idleTotalCost,
  673. results: map[string]float64{
  674. "namespace1": 28.00,
  675. "namespace2": 36.00,
  676. "namespace3": 18.00,
  677. IdleSuffix: 30.00,
  678. },
  679. windowStart: startYesterday,
  680. windowEnd: endYesterday,
  681. expMinutes: 1440.0,
  682. },
  683. // 1c AggregationProperties=(Pod)
  684. "1c": {
  685. start: start,
  686. aggBy: []string{AllocationPodProp},
  687. aggOpts: nil,
  688. numResults: numPods + numIdle,
  689. totalCost: activeTotalCost + idleTotalCost,
  690. results: map[string]float64{
  691. "pod-jkl": 6.00,
  692. "pod-stu": 6.00,
  693. "pod-abc": 6.00,
  694. "pod-pqr": 6.00,
  695. "pod-def": 6.00,
  696. "pod-vwx": 12.00,
  697. "pod1": 16.00,
  698. "pod-mno": 12.00,
  699. "pod-ghi": 12.00,
  700. IdleSuffix: 30.00,
  701. },
  702. windowStart: startYesterday,
  703. windowEnd: endYesterday,
  704. expMinutes: 1440.0,
  705. },
  706. // 1d AggregationProperties=(Container)
  707. "1d": {
  708. start: start,
  709. aggBy: []string{AllocationContainerProp},
  710. aggOpts: nil,
  711. numResults: numContainers + numIdle,
  712. totalCost: activeTotalCost + idleTotalCost,
  713. results: map[string]float64{
  714. "container2": 6.00,
  715. "container9": 6.00,
  716. "container6": 12.00,
  717. "container3": 6.00,
  718. "container4": 12.00,
  719. "container7": 6.00,
  720. "container8": 6.00,
  721. "container5": 12.00,
  722. "container1": 16.00,
  723. IdleSuffix: 30.00,
  724. },
  725. windowStart: startYesterday,
  726. windowEnd: endYesterday,
  727. expMinutes: 1440.0,
  728. },
  729. // 1e AggregationProperties=(ControllerKind)
  730. "1e": {
  731. start: start,
  732. aggBy: []string{AllocationControllerKindProp},
  733. aggOpts: nil,
  734. numResults: numControllerKinds + numIdle + numUnallocated,
  735. totalCost: activeTotalCost + idleTotalCost,
  736. results: map[string]float64{
  737. "daemonset": 12.00,
  738. "deployment": 42.00,
  739. "statefulset": 12.00,
  740. IdleSuffix: 30.00,
  741. UnallocatedSuffix: 16.00,
  742. },
  743. windowStart: startYesterday,
  744. windowEnd: endYesterday,
  745. expMinutes: 1440.0,
  746. },
  747. // 1f AggregationProperties=(Controller)
  748. "1f": {
  749. start: start,
  750. aggBy: []string{AllocationControllerProp},
  751. aggOpts: nil,
  752. numResults: numControllers + numIdle + numUnallocated,
  753. totalCost: activeTotalCost + idleTotalCost,
  754. results: map[string]float64{
  755. "deployment:deployment2": 24.00,
  756. "daemonset:daemonset1": 12.00,
  757. "deployment:deployment3": 6.00,
  758. "statefulset:statefulset1": 12.00,
  759. "deployment:deployment1": 12.00,
  760. IdleSuffix: 30.00,
  761. UnallocatedSuffix: 16.00,
  762. },
  763. windowStart: startYesterday,
  764. windowEnd: endYesterday,
  765. expMinutes: 1440.0,
  766. },
  767. // 1g AggregationProperties=(Service)
  768. "1g": {
  769. start: start,
  770. aggBy: []string{AllocationServiceProp},
  771. aggOpts: nil,
  772. numResults: numServices + numIdle + numUnallocated,
  773. totalCost: activeTotalCost + idleTotalCost,
  774. results: map[string]float64{
  775. "service1": 12.00,
  776. IdleSuffix: 30.00,
  777. UnallocatedSuffix: 70.00,
  778. },
  779. windowStart: startYesterday,
  780. windowEnd: endYesterday,
  781. expMinutes: 1440.0,
  782. },
  783. // 1h AggregationProperties=(Label:app)
  784. "1h": {
  785. start: start,
  786. aggBy: []string{"label:app"},
  787. aggOpts: nil,
  788. numResults: numLabelApps + numIdle + numUnallocated,
  789. totalCost: activeTotalCost + idleTotalCost,
  790. results: map[string]float64{
  791. "app=app1": 16.00,
  792. "app=app2": 24.00,
  793. IdleSuffix: 30.00,
  794. UnallocatedSuffix: 42.00,
  795. },
  796. windowStart: startYesterday,
  797. windowEnd: endYesterday,
  798. expMinutes: 1440.0,
  799. },
  800. // 1i AggregationProperties=(deployment)
  801. "1i": {
  802. start: start,
  803. aggBy: []string{AllocationDeploymentProp},
  804. aggOpts: nil,
  805. numResults: 3 + numIdle + numUnallocated,
  806. totalCost: activeTotalCost + idleTotalCost,
  807. results: map[string]float64{
  808. "deployment1": 12.00,
  809. "deployment2": 24.00,
  810. "deployment3": 6.00,
  811. IdleSuffix: 30.00,
  812. UnallocatedSuffix: 40.00,
  813. },
  814. windowStart: startYesterday,
  815. windowEnd: endYesterday,
  816. expMinutes: 1440.0,
  817. },
  818. // 1j AggregationProperties=(Annotation:team)
  819. "1j": {
  820. start: start,
  821. aggBy: []string{"annotation:team"},
  822. aggOpts: nil,
  823. numResults: 2 + numIdle + numUnallocated,
  824. totalCost: activeTotalCost + idleTotalCost,
  825. results: map[string]float64{
  826. "team=team1": 12.00,
  827. "team=team2": 6.00,
  828. IdleSuffix: 30.00,
  829. UnallocatedSuffix: 64.00,
  830. },
  831. windowStart: startYesterday,
  832. windowEnd: endYesterday,
  833. expMinutes: 1440.0,
  834. },
  835. // 1k AggregationProperties=(daemonSet)
  836. "1k": {
  837. start: start,
  838. aggBy: []string{AllocationDaemonSetProp},
  839. aggOpts: nil,
  840. numResults: 1 + numIdle + numUnallocated,
  841. totalCost: activeTotalCost + idleTotalCost,
  842. results: map[string]float64{
  843. "daemonset1": 12.00,
  844. IdleSuffix: 30.00,
  845. UnallocatedSuffix: 70.00,
  846. },
  847. windowStart: startYesterday,
  848. windowEnd: endYesterday,
  849. expMinutes: 1440.0,
  850. },
  851. // 1l AggregationProperties=(statefulSet)
  852. "1l": {
  853. start: start,
  854. aggBy: []string{AllocationStatefulSetProp},
  855. aggOpts: nil,
  856. numResults: 1 + numIdle + numUnallocated,
  857. totalCost: activeTotalCost + idleTotalCost,
  858. results: map[string]float64{
  859. "statefulset1": 12.00,
  860. IdleSuffix: 30.00,
  861. UnallocatedSuffix: 70.00,
  862. },
  863. windowStart: startYesterday,
  864. windowEnd: endYesterday,
  865. expMinutes: 1440.0,
  866. },
  867. // 2 Multi-aggregation
  868. // 2a AggregationProperties=(Cluster, Namespace)
  869. // 2b AggregationProperties=(Namespace, Label:app)
  870. // 2c AggregationProperties=(Cluster, Namespace, Pod, Container)
  871. // 2d AggregationProperties=(Label:app, Label:environment)
  872. "2d": {
  873. start: start,
  874. aggBy: []string{"label:app", "label:env"},
  875. aggOpts: nil,
  876. numResults: 3 + numIdle + numUnallocated,
  877. totalCost: activeTotalCost + idleTotalCost,
  878. // sets should be {idle, unallocated, app1/env1, app2/env2, app2/unallocated}
  879. results: map[string]float64{
  880. "app=app1/env=env1": 16.00,
  881. "app=app2/env=env2": 12.00,
  882. "app=app2/" + UnallocatedSuffix: 12.00,
  883. IdleSuffix: 30.00,
  884. UnallocatedSuffix + "/" + UnallocatedSuffix: 42.00,
  885. },
  886. windowStart: startYesterday,
  887. windowEnd: endYesterday,
  888. expMinutes: 1440.0,
  889. },
  890. // 2e AggregationProperties=(Cluster, Label:app, Label:environment)
  891. "2e": {
  892. start: start,
  893. aggBy: []string{AllocationClusterProp, "label:app", "label:env"},
  894. aggOpts: nil,
  895. numResults: 6,
  896. totalCost: activeTotalCost + idleTotalCost,
  897. results: map[string]float64{
  898. "cluster1/app=app2/env=env2": 12.00,
  899. "__idle__": 30.00,
  900. "cluster1/app=app1/env=env1": 16.00,
  901. "cluster1/" + UnallocatedSuffix + "/" + UnallocatedSuffix: 18.00,
  902. "cluster2/app=app2/" + UnallocatedSuffix: 12.00,
  903. "cluster2/" + UnallocatedSuffix + "/" + UnallocatedSuffix: 24.00,
  904. },
  905. windowStart: startYesterday,
  906. windowEnd: endYesterday,
  907. expMinutes: 1440.0,
  908. },
  909. // 2f AggregationProperties=(annotation:team, pod)
  910. "2f": {
  911. start: start,
  912. aggBy: []string{AllocationPodProp, "annotation:team"},
  913. aggOpts: nil,
  914. numResults: 11,
  915. totalCost: activeTotalCost + idleTotalCost,
  916. results: map[string]float64{
  917. "pod-jkl/" + UnallocatedSuffix: 6.00,
  918. "pod-stu/team=team1": 6.00,
  919. "pod-abc/" + UnallocatedSuffix: 6.00,
  920. "pod-pqr/" + UnallocatedSuffix: 6.00,
  921. "pod-def/" + UnallocatedSuffix: 6.00,
  922. "pod-vwx/team=team1": 6.00,
  923. "pod-vwx/team=team2": 6.00,
  924. "pod1/" + UnallocatedSuffix: 16.00,
  925. "pod-mno/" + UnallocatedSuffix: 12.00,
  926. "pod-ghi/" + UnallocatedSuffix: 12.00,
  927. IdleSuffix: 30.00,
  928. },
  929. windowStart: startYesterday,
  930. windowEnd: endYesterday,
  931. expMinutes: 1440.0,
  932. },
  933. // 3 Share idle
  934. // 3a AggregationProperties=(Namespace) ShareIdle=ShareWeighted
  935. // namespace1: 42.6875 = 28.00 + 5.00*(3.00/6.00) + 15.0*(13.0/16.0)
  936. // 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)
  937. // namespace3: 23.0000 = 18.00 + 5.0*(3.0/6.0) + 5.0*(3.0/6.0)
  938. "3a": {
  939. start: start,
  940. aggBy: []string{AllocationNamespaceProp},
  941. aggOpts: &AllocationAggregationOptions{ShareIdle: ShareWeighted},
  942. numResults: numNamespaces,
  943. totalCost: activeTotalCost + idleTotalCost,
  944. results: map[string]float64{
  945. "namespace1": 42.69,
  946. "namespace2": 46.31,
  947. "namespace3": 23.00,
  948. },
  949. windowStart: startYesterday,
  950. windowEnd: endYesterday,
  951. expMinutes: 1440.0,
  952. },
  953. // 3b AggregationProperties=(Namespace) ShareIdle=ShareEven
  954. // namespace1: 38.0000 = 28.00 + 5.00*(1.0/2.0) + 15.0*(1.0/2.0)
  955. // 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)
  956. // namespace3: 23.0000 = 18.00 + 5.0*(1.0/2.0) + 5.0*(1.0/2.0)
  957. "3b": {
  958. start: start,
  959. aggBy: []string{AllocationNamespaceProp},
  960. aggOpts: &AllocationAggregationOptions{ShareIdle: ShareEven},
  961. numResults: numNamespaces,
  962. totalCost: activeTotalCost + idleTotalCost,
  963. results: map[string]float64{
  964. "namespace1": 38.00,
  965. "namespace2": 51.00,
  966. "namespace3": 23.00,
  967. },
  968. windowStart: startYesterday,
  969. windowEnd: endYesterday,
  970. expMinutes: 1440.0,
  971. },
  972. // 4 Share resources
  973. // 4a Share namespace ShareEven
  974. // namespace1: 37.5000 = 28.00 + 18.00*(1.0/2.0)
  975. // namespace2: 45.5000 = 36.00 + 18.00*(1.0/2.0)
  976. // idle: 30.0000
  977. "4a": {
  978. start: start,
  979. aggBy: []string{AllocationNamespaceProp},
  980. aggOpts: &AllocationAggregationOptions{
  981. ShareFuncs: []AllocationMatchFunc{isNamespace3},
  982. ShareSplit: ShareEven,
  983. },
  984. numResults: numNamespaces,
  985. totalCost: activeTotalCost + idleTotalCost,
  986. results: map[string]float64{
  987. "namespace1": 37.00,
  988. "namespace2": 45.00,
  989. IdleSuffix: 30.00,
  990. },
  991. windowStart: startYesterday,
  992. windowEnd: endYesterday,
  993. expMinutes: 1440.0,
  994. },
  995. // 4b Share namespace ShareWeighted
  996. // namespace1: 32.5000 =
  997. // namespace2: 37.5000 =
  998. // idle: 30.0000
  999. "4b": {
  1000. start: start,
  1001. aggBy: []string{AllocationNamespaceProp},
  1002. aggOpts: &AllocationAggregationOptions{
  1003. ShareFuncs: []AllocationMatchFunc{isNamespace3},
  1004. ShareSplit: ShareWeighted,
  1005. },
  1006. numResults: numNamespaces,
  1007. totalCost: activeTotalCost + idleTotalCost,
  1008. results: map[string]float64{
  1009. "namespace1": 35.88,
  1010. "namespace2": 46.125,
  1011. IdleSuffix: 30.00,
  1012. },
  1013. windowStart: startYesterday,
  1014. windowEnd: endYesterday,
  1015. expMinutes: 1440.0,
  1016. },
  1017. // 4c Share label ShareEven
  1018. // namespace1: 17.3333 = 28.00 - 16.00 + 16.00*(1.0/3.0)
  1019. // namespace2: 41.3333 = 36.00 + 16.00*(1.0/3.0)
  1020. // namespace3: 23.3333 = 18.00 + 16.00*(1.0/3.0)
  1021. // idle: 30.0000
  1022. "4c": {
  1023. start: start,
  1024. aggBy: []string{AllocationNamespaceProp},
  1025. aggOpts: &AllocationAggregationOptions{
  1026. ShareFuncs: []AllocationMatchFunc{isApp1},
  1027. ShareSplit: ShareEven,
  1028. },
  1029. numResults: numNamespaces + numIdle,
  1030. totalCost: activeTotalCost + idleTotalCost,
  1031. results: map[string]float64{
  1032. "namespace1": 17.33,
  1033. "namespace2": 41.33,
  1034. "namespace3": 23.33,
  1035. IdleSuffix: 30.00,
  1036. },
  1037. windowStart: startYesterday,
  1038. windowEnd: endYesterday,
  1039. expMinutes: 1440.0,
  1040. },
  1041. // 4d Share overhead ShareWeighted
  1042. // namespace1: 85.366 = 28.00 + (7.0*24.0)*(28.00/82.00)
  1043. // namespace2: 109.756 = 36.00 + (7.0*24.0)*(36.00/82.00)
  1044. // namespace3: 54.878 = 18.00 + (7.0*24.0)*(18.00/82.00)
  1045. // idle: 30.0000
  1046. "4d": {
  1047. start: start,
  1048. aggBy: []string{AllocationNamespaceProp},
  1049. aggOpts: &AllocationAggregationOptions{
  1050. SharedHourlyCosts: map[string]float64{"total": sharedOverheadHourlyCost},
  1051. ShareSplit: ShareWeighted,
  1052. },
  1053. numResults: numNamespaces + numIdle,
  1054. totalCost: activeTotalCost + idleTotalCost + (sharedOverheadHourlyCost * 24.0),
  1055. results: map[string]float64{
  1056. "namespace1": 85.366,
  1057. "namespace2": 109.756,
  1058. "namespace3": 54.878,
  1059. IdleSuffix: 30.00,
  1060. },
  1061. windowStart: startYesterday,
  1062. windowEnd: endYesterday,
  1063. expMinutes: 1440.0,
  1064. },
  1065. // 5 Filters
  1066. // 5a Filter by cluster with separate idle
  1067. "5a": {
  1068. start: start,
  1069. aggBy: []string{AllocationClusterProp},
  1070. aggOpts: &AllocationAggregationOptions{
  1071. FilterFuncs: []AllocationMatchFunc{isCluster("cluster1")},
  1072. ShareIdle: ShareNone,
  1073. },
  1074. numResults: 1 + numIdle,
  1075. totalCost: 66.0,
  1076. results: map[string]float64{
  1077. "cluster1": 46.00,
  1078. IdleSuffix: 20.00,
  1079. },
  1080. windowStart: startYesterday,
  1081. windowEnd: endYesterday,
  1082. expMinutes: 1440.0,
  1083. },
  1084. // 5b Filter by cluster with shared idle
  1085. "5b": {
  1086. start: start,
  1087. aggBy: []string{AllocationClusterProp},
  1088. aggOpts: &AllocationAggregationOptions{
  1089. FilterFuncs: []AllocationMatchFunc{isCluster("cluster1")},
  1090. ShareIdle: ShareWeighted,
  1091. },
  1092. numResults: 1,
  1093. totalCost: 66.0,
  1094. results: map[string]float64{
  1095. "cluster1": 66.00,
  1096. },
  1097. windowStart: startYesterday,
  1098. windowEnd: endYesterday,
  1099. expMinutes: 1440.0,
  1100. },
  1101. // 5c Filter by cluster, agg by namespace, with separate idle
  1102. "5c": {
  1103. start: start,
  1104. aggBy: []string{AllocationNamespaceProp},
  1105. aggOpts: &AllocationAggregationOptions{
  1106. FilterFuncs: []AllocationMatchFunc{isCluster("cluster1")},
  1107. ShareIdle: ShareNone,
  1108. },
  1109. numResults: 2 + numIdle,
  1110. totalCost: 66.0,
  1111. results: map[string]float64{
  1112. "namespace1": 28.00,
  1113. "namespace2": 18.00,
  1114. IdleSuffix: 20.00,
  1115. },
  1116. windowStart: startYesterday,
  1117. windowEnd: endYesterday,
  1118. expMinutes: 1440.0,
  1119. },
  1120. // 5d Filter by namespace, agg by cluster, with separate idle
  1121. "5d": {
  1122. start: start,
  1123. aggBy: []string{AllocationClusterProp},
  1124. aggOpts: &AllocationAggregationOptions{
  1125. FilterFuncs: []AllocationMatchFunc{isNamespace("namespace2")},
  1126. ShareIdle: ShareNone,
  1127. },
  1128. numResults: numClusters + numIdle,
  1129. totalCost: 46.31,
  1130. results: map[string]float64{
  1131. "cluster1": 18.00,
  1132. "cluster2": 18.00,
  1133. IdleSuffix: 10.31,
  1134. },
  1135. windowStart: startYesterday,
  1136. windowEnd: endYesterday,
  1137. expMinutes: 1440.0,
  1138. },
  1139. // 6 Combinations and options
  1140. // 6a SplitIdle
  1141. "6a": {
  1142. start: start,
  1143. aggBy: []string{AllocationNamespaceProp},
  1144. aggOpts: &AllocationAggregationOptions{
  1145. SplitIdle: true,
  1146. },
  1147. numResults: numNamespaces + numSplitIdleCluster,
  1148. totalCost: activeTotalCost + idleTotalCost,
  1149. results: map[string]float64{
  1150. "namespace1": 28.00,
  1151. "namespace2": 36.00,
  1152. "namespace3": 18.00,
  1153. fmt.Sprintf("cluster1/%s", IdleSuffix): 20.00,
  1154. fmt.Sprintf("cluster2/%s", IdleSuffix): 10.00,
  1155. },
  1156. windowStart: startYesterday,
  1157. windowEnd: endYesterday,
  1158. expMinutes: 1440.0,
  1159. },
  1160. // 6b Share idle weighted with filters
  1161. // Should match values from unfiltered aggregation (3a)
  1162. // 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)
  1163. "6b": {
  1164. start: start,
  1165. aggBy: []string{AllocationNamespaceProp},
  1166. aggOpts: &AllocationAggregationOptions{
  1167. FilterFuncs: []AllocationMatchFunc{isNamespace("namespace2")},
  1168. ShareIdle: ShareWeighted,
  1169. },
  1170. numResults: 1,
  1171. totalCost: 46.31,
  1172. results: map[string]float64{
  1173. "namespace2": 46.31,
  1174. },
  1175. windowStart: startYesterday,
  1176. windowEnd: endYesterday,
  1177. expMinutes: 1440.0,
  1178. },
  1179. // 6c Share idle even with filters
  1180. // Should match values from unfiltered aggregation (3b)
  1181. // 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)
  1182. "6c": {
  1183. start: start,
  1184. aggBy: []string{AllocationNamespaceProp},
  1185. aggOpts: &AllocationAggregationOptions{
  1186. FilterFuncs: []AllocationMatchFunc{isNamespace("namespace2")},
  1187. ShareIdle: ShareEven,
  1188. },
  1189. numResults: 1,
  1190. totalCost: 51.00,
  1191. results: map[string]float64{
  1192. "namespace2": 51.00,
  1193. },
  1194. windowStart: startYesterday,
  1195. windowEnd: endYesterday,
  1196. expMinutes: 1440.0,
  1197. },
  1198. // 6d Share overhead with filters
  1199. // namespace1: 85.366 = 28.00 + (7.0*24.0)*(28.00/82.00)
  1200. // namespace2: 109.756 = 36.00 + (7.0*24.0)*(36.00/82.00)
  1201. // namespace3: 54.878 = 18.00 + (7.0*24.0)*(18.00/82.00)
  1202. // idle: 30.0000
  1203. // Then namespace 2 is filtered.
  1204. "6d": {
  1205. start: start,
  1206. aggBy: []string{AllocationNamespaceProp},
  1207. aggOpts: &AllocationAggregationOptions{
  1208. FilterFuncs: []AllocationMatchFunc{isNamespace("namespace2")},
  1209. SharedHourlyCosts: map[string]float64{"total": sharedOverheadHourlyCost},
  1210. ShareSplit: ShareWeighted,
  1211. },
  1212. numResults: 1 + numIdle,
  1213. totalCost: 139.756,
  1214. results: map[string]float64{
  1215. "namespace2": 109.756,
  1216. IdleSuffix: 30.00,
  1217. },
  1218. windowStart: startYesterday,
  1219. windowEnd: endYesterday,
  1220. expMinutes: 1440.0,
  1221. },
  1222. // 6e Share resources with filters
  1223. // --- Shared ---
  1224. // namespace1: 28.00 (gets shared among namespace2 and namespace3)
  1225. // --- Filtered ---
  1226. // namespace3: 27.33 = 18.00 + (28.00)*(18.00/54.00) (filtered out)
  1227. // --- Results ---
  1228. // namespace2: 54.667 = 36.00 + (28.00)*(36.00/54.00)
  1229. // idle: 30.0000
  1230. "6e": {
  1231. start: start,
  1232. aggBy: []string{AllocationNamespaceProp},
  1233. aggOpts: &AllocationAggregationOptions{
  1234. FilterFuncs: []AllocationMatchFunc{isNamespace("namespace2")},
  1235. ShareFuncs: []AllocationMatchFunc{isNamespace("namespace1")},
  1236. ShareSplit: ShareWeighted,
  1237. },
  1238. numResults: 1 + numIdle,
  1239. totalCost: 84.667,
  1240. results: map[string]float64{
  1241. "namespace2": 54.667,
  1242. IdleSuffix: 30.00,
  1243. },
  1244. windowStart: startYesterday,
  1245. windowEnd: endYesterday,
  1246. expMinutes: 1440.0,
  1247. },
  1248. // 6f Share idle weighted and share resources weighted
  1249. //
  1250. // First, share idle weighted produces:
  1251. //
  1252. // namespace1: 42.6875
  1253. // initial cost 28.0000
  1254. // cluster1.cpu 2.5000 = 5.00*(3.00/6.00)
  1255. // cluster1.ram 12.1875 = 15.00*(13.0/16.0)
  1256. //
  1257. // namespace2: 46.3125
  1258. // initial cost 36.0000
  1259. // cluster1.cpu 2.5000 = 5.00*(3.0/6.0)
  1260. // cluster1.ram 2.8125 = 15.00*(3.0/16.0)
  1261. // cluster2.cpu 2.5000 = 5.00*(3.0/6.0)
  1262. // cluster2.ram 2.5000 = 5.00*(3.0/6.0)
  1263. //
  1264. // namespace3: 23.0000
  1265. // initial cost 18.0000
  1266. // cluster2.cpu 2.5000 = 5.00*(3.0/6.0)
  1267. // cluster2.ram 2.5000 = 5.00*(3.0/6.0)
  1268. //
  1269. // Then, sharing namespace1 means sharing 39.6875 according to coefficients
  1270. // computed before allocating idle (so that weighting idle differently
  1271. // doesn't adversely affect the sharing mechanism):
  1272. //
  1273. // namespace2: 74.7708
  1274. // initial cost 30.0000
  1275. // idle cost 10.3125
  1276. // shared cost 28.4583 = (42.6875)*(36.0/54.0)
  1277. //
  1278. // namespace3: 37.2292
  1279. // initial cost 18.0000
  1280. // idle cost 5.0000
  1281. // shared cost 14.2292 = (42.6875)*(18.0/54.0)
  1282. "6f": {
  1283. start: start,
  1284. aggBy: []string{AllocationNamespaceProp},
  1285. aggOpts: &AllocationAggregationOptions{
  1286. ShareFuncs: []AllocationMatchFunc{isNamespace("namespace1")},
  1287. ShareSplit: ShareWeighted,
  1288. ShareIdle: ShareWeighted,
  1289. },
  1290. numResults: 2,
  1291. totalCost: activeTotalCost + idleTotalCost,
  1292. results: map[string]float64{
  1293. "namespace2": 74.77,
  1294. "namespace3": 37.23,
  1295. },
  1296. windowStart: startYesterday,
  1297. windowEnd: endYesterday,
  1298. expMinutes: 1440.0,
  1299. },
  1300. // 6g Share idle, share resources, and filter
  1301. //
  1302. // First, share idle weighted produces:
  1303. //
  1304. // namespace1: 42.6875
  1305. // initial cost 28.0000
  1306. // cluster1.cpu 2.5000 = 5.00*(3.00/6.00)
  1307. // cluster1.ram 12.1875 = 15.00*(13.0/16.0)
  1308. //
  1309. // namespace2: 46.3125
  1310. // initial cost 36.0000
  1311. // cluster1.cpu 2.5000 = 5.00*(3.0/6.0)
  1312. // cluster1.ram 2.8125 = 15.00*(3.0/16.0)
  1313. // cluster2.cpu 2.5000 = 5.00*(3.0/6.0)
  1314. // cluster2.ram 2.5000 = 5.00*(3.0/6.0)
  1315. //
  1316. // namespace3: 23.0000
  1317. // initial cost 18.0000
  1318. // cluster2.cpu 2.5000 = 5.00*(3.0/6.0)
  1319. // cluster2.ram 2.5000 = 5.00*(3.0/6.0)
  1320. //
  1321. // Then, sharing namespace1 means sharing 39.6875 according to coefficients
  1322. // computed before allocating idle (so that weighting idle differently
  1323. // doesn't adversely affect the sharing mechanism):
  1324. //
  1325. // namespace2: 74.7708
  1326. // initial cost 36.0000
  1327. // idle cost 10.3125
  1328. // shared cost 28.4583 = (42.6875)*(36.0/54.0)
  1329. //
  1330. // namespace3: 37.2292
  1331. // initial cost 18.0000
  1332. // idle cost 5.0000
  1333. // shared cost 14.2292 = (42.6875)*(18.0/54.0)
  1334. //
  1335. // Then, filter for namespace2: 74.7708
  1336. "6g": {
  1337. start: start,
  1338. aggBy: []string{AllocationNamespaceProp},
  1339. aggOpts: &AllocationAggregationOptions{
  1340. FilterFuncs: []AllocationMatchFunc{isNamespace("namespace2")},
  1341. ShareFuncs: []AllocationMatchFunc{isNamespace("namespace1")},
  1342. ShareSplit: ShareWeighted,
  1343. ShareIdle: ShareWeighted,
  1344. },
  1345. numResults: 1,
  1346. totalCost: 74.77,
  1347. results: map[string]float64{
  1348. "namespace2": 74.77,
  1349. },
  1350. windowStart: startYesterday,
  1351. windowEnd: endYesterday,
  1352. expMinutes: 1440.0,
  1353. },
  1354. // 6h Share idle, share resources, share overhead
  1355. //
  1356. // Share idle weighted:
  1357. //
  1358. // namespace1: 42.6875
  1359. // initial cost 28.0000
  1360. // cluster1.cpu 2.5000 = 5.00*(3.00/6.00)
  1361. // cluster1.ram 12.1875 = 15.00*(13.0/16.0)
  1362. //
  1363. // namespace2: 46.3125
  1364. // initial cost 36.0000
  1365. // cluster1.cpu 2.5000 = 5.00*(3.0/6.0)
  1366. // cluster1.ram 2.8125 = 15.00*(3.0/16.0)
  1367. // cluster2.cpu 2.5000 = 5.00*(3.0/6.0)
  1368. // cluster2.ram 2.5000 = 5.00*(3.0/6.0)
  1369. //
  1370. // namespace3: 23.0000
  1371. // initial cost 18.0000
  1372. // cluster2.cpu 2.5000 = 5.00*(3.0/6.0)
  1373. // cluster2.ram 2.5000 = 5.00*(3.0/6.0)
  1374. //
  1375. // Then share overhead:
  1376. //
  1377. // namespace1: 100.0533 = 42.6875 + (7.0*24.0)*(28.00/82.00)
  1378. // namespace2: 120.0686 = 46.3125 + (7.0*24.0)*(36.00/82.00)
  1379. // namespace3: 59.8780 = 23.0000 + (7.0*24.0)*(18.00/82.00)
  1380. //
  1381. // Then namespace 2 is filtered.
  1382. "6h": {
  1383. start: start,
  1384. aggBy: []string{AllocationNamespaceProp},
  1385. aggOpts: &AllocationAggregationOptions{
  1386. FilterFuncs: []AllocationMatchFunc{isNamespace("namespace2")},
  1387. ShareSplit: ShareWeighted,
  1388. ShareIdle: ShareWeighted,
  1389. SharedHourlyCosts: map[string]float64{"total": sharedOverheadHourlyCost},
  1390. },
  1391. numResults: 1,
  1392. totalCost: 120.07,
  1393. results: map[string]float64{
  1394. "namespace2": 120.07,
  1395. },
  1396. windowStart: startYesterday,
  1397. windowEnd: endYesterday,
  1398. expMinutes: 1440.0,
  1399. },
  1400. // 6i Idle by Node
  1401. "6i": {
  1402. start: start,
  1403. aggBy: []string{AllocationNamespaceProp},
  1404. aggOpts: &AllocationAggregationOptions{
  1405. IdleByNode: true,
  1406. },
  1407. numResults: numNamespaces + numIdle,
  1408. totalCost: activeTotalCost + idleTotalCost,
  1409. results: map[string]float64{
  1410. "namespace1": 28.00,
  1411. "namespace2": 36.00,
  1412. "namespace3": 18.00,
  1413. IdleSuffix: 30.00,
  1414. },
  1415. windowStart: startYesterday,
  1416. windowEnd: endYesterday,
  1417. expMinutes: 1440.0,
  1418. },
  1419. // 6j Split Idle, Idle by Node
  1420. "6j": {
  1421. start: start,
  1422. aggBy: []string{AllocationNamespaceProp},
  1423. aggOpts: &AllocationAggregationOptions{
  1424. SplitIdle: true,
  1425. IdleByNode: true,
  1426. },
  1427. numResults: numNamespaces + numSplitIdleNode,
  1428. totalCost: activeTotalCost + idleTotalCost,
  1429. results: map[string]float64{
  1430. "namespace1": 28.00,
  1431. "namespace2": 36.00,
  1432. "namespace3": 18.00,
  1433. fmt.Sprintf("c1nodes/%s", IdleSuffix): 20.00,
  1434. fmt.Sprintf("node1/%s", IdleSuffix): 3.333333,
  1435. fmt.Sprintf("node2/%s", IdleSuffix): 3.333333,
  1436. fmt.Sprintf("node3/%s", IdleSuffix): 3.333333,
  1437. },
  1438. windowStart: startYesterday,
  1439. windowEnd: endYesterday,
  1440. expMinutes: 1440.0,
  1441. },
  1442. // 6k Share idle Even Idle by Node
  1443. // Should match values from unfiltered aggregation (3b)
  1444. "6k": {
  1445. start: start,
  1446. aggBy: []string{AllocationNamespaceProp},
  1447. aggOpts: &AllocationAggregationOptions{
  1448. ShareIdle: ShareEven,
  1449. IdleByNode: true,
  1450. },
  1451. numResults: 3,
  1452. totalCost: 112.00,
  1453. results: map[string]float64{
  1454. "namespace1": 38.00,
  1455. "namespace2": 51.00,
  1456. "namespace3": 23.00,
  1457. },
  1458. windowStart: startYesterday,
  1459. windowEnd: endYesterday,
  1460. expMinutes: 1440.0,
  1461. },
  1462. // 6l Share idle weighted with filters, Idle by Node
  1463. // Should match values from unfiltered aggregation (3a)
  1464. // 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)
  1465. "6l": {
  1466. start: start,
  1467. aggBy: []string{AllocationNamespaceProp},
  1468. aggOpts: &AllocationAggregationOptions{
  1469. FilterFuncs: []AllocationMatchFunc{isNamespace("namespace2")},
  1470. ShareIdle: ShareWeighted,
  1471. IdleByNode: true,
  1472. },
  1473. numResults: 1,
  1474. totalCost: 46.31,
  1475. results: map[string]float64{
  1476. "namespace2": 46.31,
  1477. },
  1478. windowStart: startYesterday,
  1479. windowEnd: endYesterday,
  1480. expMinutes: 1440.0,
  1481. },
  1482. // 7 Edge cases and errors
  1483. // 7a Empty AggregationProperties
  1484. // 7b Filter all
  1485. // 7c Share all
  1486. // 7d Share and filter the same allocations
  1487. }
  1488. for name, testcase := range cases {
  1489. t.Run(name, func(t *testing.T) {
  1490. if testcase.aggOpts != nil && testcase.aggOpts.IdleByNode {
  1491. as = GenerateMockAllocationSetNodeIdle(testcase.start)
  1492. } else {
  1493. as = GenerateMockAllocationSetClusterIdle(testcase.start)
  1494. }
  1495. err = as.AggregateBy(testcase.aggBy, testcase.aggOpts)
  1496. assertAllocationSetTotals(t, as, name, err, testcase.numResults, testcase.totalCost)
  1497. assertAllocationTotals(t, as, name, testcase.results)
  1498. assertAllocationWindow(t, as, name, testcase.windowStart, testcase.windowEnd, testcase.expMinutes)
  1499. })
  1500. }
  1501. }
  1502. // TODO niko/etl
  1503. //func TestAllocationSet_Clone(t *testing.T) {}
  1504. func TestAllocationSet_ComputeIdleAllocations(t *testing.T) {
  1505. var as *AllocationSet
  1506. var err error
  1507. var idles map[string]*Allocation
  1508. end := time.Now().UTC().Truncate(day)
  1509. start := end.Add(-day)
  1510. // Generate AllocationSet without idle allocations
  1511. as = GenerateMockAllocationSet(start)
  1512. assetSets := GenerateMockAssetSets(start, end)
  1513. cases := map[string]struct {
  1514. allocationSet *AllocationSet
  1515. assetSet *AssetSet
  1516. clusters map[string]Allocation
  1517. }{
  1518. "1a": {
  1519. allocationSet: as,
  1520. assetSet: assetSets[0],
  1521. clusters: map[string]Allocation{
  1522. "cluster1": {
  1523. CPUCost: 44.0,
  1524. RAMCost: 24.0,
  1525. GPUCost: 4.0,
  1526. },
  1527. "cluster2": {
  1528. CPUCost: 44.0,
  1529. RAMCost: 34.0,
  1530. GPUCost: 4.0,
  1531. },
  1532. },
  1533. },
  1534. "1b": {
  1535. allocationSet: as,
  1536. assetSet: assetSets[1],
  1537. clusters: map[string]Allocation{
  1538. "cluster1": {
  1539. CPUCost: 44.0,
  1540. RAMCost: 24.0,
  1541. GPUCost: 4.0,
  1542. },
  1543. "cluster2": {
  1544. CPUCost: 44.0,
  1545. RAMCost: 34.0,
  1546. GPUCost: 4.0,
  1547. },
  1548. },
  1549. },
  1550. }
  1551. for name, testcase := range cases {
  1552. t.Run(name, func(t *testing.T) {
  1553. idles, err = as.ComputeIdleAllocations(testcase.assetSet)
  1554. if err != nil {
  1555. t.Fatalf("unexpected error: %s", err)
  1556. }
  1557. if len(idles) != len(testcase.clusters) {
  1558. t.Fatalf("idles: expected length %d; got length %d", len(testcase.clusters), len(idles))
  1559. }
  1560. for clusterName, cluster := range testcase.clusters {
  1561. if idle, ok := idles[clusterName]; !ok {
  1562. t.Fatalf("expected idle cost for %s", clusterName)
  1563. } else {
  1564. if !util.IsApproximately(idle.TotalCost(), cluster.TotalCost()) {
  1565. t.Fatalf("%s idle: expected total cost %f; got total cost %f", clusterName, cluster.TotalCost(), idle.TotalCost())
  1566. }
  1567. }
  1568. if !util.IsApproximately(idles[clusterName].CPUCost, cluster.CPUCost) {
  1569. t.Fatalf("expected idle CPU cost for %s to be %.2f; got %.2f", clusterName, cluster.CPUCost, idles[clusterName].CPUCost)
  1570. }
  1571. if !util.IsApproximately(idles[clusterName].RAMCost, cluster.RAMCost) {
  1572. t.Fatalf("expected idle RAM cost for %s to be %.2f; got %.2f", clusterName, cluster.RAMCost, idles[clusterName].RAMCost)
  1573. }
  1574. if !util.IsApproximately(idles[clusterName].GPUCost, cluster.GPUCost) {
  1575. t.Fatalf("expected idle GPU cost for %s to be %.2f; got %.2f", clusterName, cluster.GPUCost, idles[clusterName].GPUCost)
  1576. }
  1577. }
  1578. })
  1579. }
  1580. }
  1581. func TestAllocationSet_ComputeIdleAllocationsPerNode(t *testing.T) {
  1582. var as *AllocationSet
  1583. var err error
  1584. var idles map[string]*Allocation
  1585. end := time.Now().UTC().Truncate(day)
  1586. start := end.Add(-day)
  1587. // Generate AllocationSet without idle allocations
  1588. as = GenerateMockAllocationSet(start)
  1589. assetSets := GenerateMockAssetSets(start, end)
  1590. cases := map[string]struct {
  1591. allocationSet *AllocationSet
  1592. assetSet *AssetSet
  1593. nodes map[string]Allocation
  1594. }{
  1595. "1a": {
  1596. allocationSet: as,
  1597. assetSet: assetSets[0],
  1598. nodes: map[string]Allocation{
  1599. "c1nodes": {
  1600. CPUCost: 44.0,
  1601. RAMCost: 24.0,
  1602. GPUCost: 4.0,
  1603. },
  1604. "node1": {
  1605. CPUCost: 18.0,
  1606. RAMCost: 13.0,
  1607. GPUCost: -2.0,
  1608. },
  1609. "node2": {
  1610. CPUCost: 18.0,
  1611. RAMCost: 13.0,
  1612. GPUCost: -2.0,
  1613. },
  1614. "node3": {
  1615. CPUCost: 8.0,
  1616. RAMCost: 8.0,
  1617. GPUCost: 8.0,
  1618. },
  1619. },
  1620. },
  1621. "1b": {
  1622. allocationSet: as,
  1623. assetSet: assetSets[1],
  1624. nodes: map[string]Allocation{
  1625. "c1nodes": {
  1626. CPUCost: 44.0,
  1627. RAMCost: 24.0,
  1628. GPUCost: 4.0,
  1629. },
  1630. "node1": {
  1631. CPUCost: 18.0,
  1632. RAMCost: 13.0,
  1633. GPUCost: -2.0,
  1634. },
  1635. "node2": {
  1636. CPUCost: 18.0,
  1637. RAMCost: 13.0,
  1638. GPUCost: -2.0,
  1639. },
  1640. "node3": {
  1641. CPUCost: 8.0,
  1642. RAMCost: 8.0,
  1643. GPUCost: 8.0,
  1644. },
  1645. },
  1646. },
  1647. }
  1648. for name, testcase := range cases {
  1649. t.Run(name, func(t *testing.T) {
  1650. idles, err = as.ComputeIdleAllocationsByNode(testcase.assetSet)
  1651. if err != nil {
  1652. t.Fatalf("unexpected error: %s", err)
  1653. }
  1654. if len(idles) != len(testcase.nodes) {
  1655. t.Fatalf("idles: expected length %d; got length %d", len(testcase.nodes), len(idles))
  1656. }
  1657. for nodeName, node := range testcase.nodes {
  1658. if idle, ok := idles[nodeName]; !ok {
  1659. t.Fatalf("expected idle cost for %s", nodeName)
  1660. } else {
  1661. if !util.IsApproximately(idle.TotalCost(), node.TotalCost()) {
  1662. t.Fatalf("%s idle: expected total cost %f; got total cost %f", nodeName, node.TotalCost(), idle.TotalCost())
  1663. }
  1664. }
  1665. if !util.IsApproximately(idles[nodeName].CPUCost, node.CPUCost) {
  1666. t.Fatalf("expected idle CPU cost for %s to be %.2f; got %.2f", nodeName, node.CPUCost, idles[nodeName].CPUCost)
  1667. }
  1668. if !util.IsApproximately(idles[nodeName].RAMCost, node.RAMCost) {
  1669. t.Fatalf("expected idle RAM cost for %s to be %.2f; got %.2f", nodeName, node.RAMCost, idles[nodeName].RAMCost)
  1670. }
  1671. if !util.IsApproximately(idles[nodeName].GPUCost, node.GPUCost) {
  1672. t.Fatalf("expected idle GPU cost for %s to be %.2f; got %.2f", nodeName, node.GPUCost, idles[nodeName].GPUCost)
  1673. }
  1674. }
  1675. })
  1676. }
  1677. }
  1678. // TODO niko/etl
  1679. //func TestAllocationSet_Delete(t *testing.T) {}
  1680. // TODO niko/etl
  1681. //func TestAllocationSet_End(t *testing.T) {}
  1682. // TODO niko/etl
  1683. //func TestAllocationSet_IdleAllocations(t *testing.T) {}
  1684. // TODO niko/etl
  1685. //func TestAllocationSet_Insert(t *testing.T) {}
  1686. // TODO niko/etl
  1687. //func TestAllocationSet_IsEmpty(t *testing.T) {}
  1688. // TODO niko/etl
  1689. //func TestAllocationSet_Length(t *testing.T) {}
  1690. // TODO niko/etl
  1691. //func TestAllocationSet_Map(t *testing.T) {}
  1692. // TODO niko/etl
  1693. //func TestAllocationSet_MarshalJSON(t *testing.T) {}
  1694. // TODO niko/etl
  1695. //func TestAllocationSet_Resolution(t *testing.T) {}
  1696. // TODO niko/etl
  1697. //func TestAllocationSet_Seconds(t *testing.T) {}
  1698. // TODO niko/etl
  1699. //func TestAllocationSet_Set(t *testing.T) {}
  1700. // TODO niko/etl
  1701. //func TestAllocationSet_Start(t *testing.T) {}
  1702. // TODO niko/etl
  1703. //func TestAllocationSet_TotalCost(t *testing.T) {}
  1704. // TODO niko/etl
  1705. //func TestNewAllocationSetRange(t *testing.T) {}
  1706. func TestAllocationSetRange_Accumulate(t *testing.T) {
  1707. ago2d := time.Now().UTC().Truncate(day).Add(-2 * day)
  1708. yesterday := time.Now().UTC().Truncate(day).Add(-day)
  1709. today := time.Now().UTC().Truncate(day)
  1710. tomorrow := time.Now().UTC().Truncate(day).Add(day)
  1711. // Accumulating any combination of nil and/or empty set should result in empty set
  1712. result, err := NewAllocationSetRange(nil).Accumulate()
  1713. if err != nil {
  1714. t.Fatalf("unexpected error accumulating nil AllocationSetRange: %s", err)
  1715. }
  1716. if !result.IsEmpty() {
  1717. t.Fatalf("accumulating nil AllocationSetRange: expected empty; actual %s", result)
  1718. }
  1719. result, err = NewAllocationSetRange(nil, nil).Accumulate()
  1720. if err != nil {
  1721. t.Fatalf("unexpected error accumulating nil AllocationSetRange: %s", err)
  1722. }
  1723. if !result.IsEmpty() {
  1724. t.Fatalf("accumulating nil AllocationSetRange: expected empty; actual %s", result)
  1725. }
  1726. result, err = NewAllocationSetRange(NewAllocationSet(yesterday, today)).Accumulate()
  1727. if err != nil {
  1728. t.Fatalf("unexpected error accumulating nil AllocationSetRange: %s", err)
  1729. }
  1730. if !result.IsEmpty() {
  1731. t.Fatalf("accumulating nil AllocationSetRange: expected empty; actual %s", result)
  1732. }
  1733. result, err = NewAllocationSetRange(nil, NewAllocationSet(ago2d, yesterday), nil, NewAllocationSet(today, tomorrow), nil).Accumulate()
  1734. if err != nil {
  1735. t.Fatalf("unexpected error accumulating nil AllocationSetRange: %s", err)
  1736. }
  1737. if !result.IsEmpty() {
  1738. t.Fatalf("accumulating nil AllocationSetRange: expected empty; actual %s", result)
  1739. }
  1740. todayAS := NewAllocationSet(today, tomorrow)
  1741. todayAS.Set(NewMockUnitAllocation("", today, day, nil))
  1742. yesterdayAS := NewAllocationSet(yesterday, today)
  1743. yesterdayAS.Set(NewMockUnitAllocation("", yesterday, day, nil))
  1744. // Accumulate non-nil with nil should result in copy of non-nil, regardless of order
  1745. result, err = NewAllocationSetRange(nil, todayAS).Accumulate()
  1746. if err != nil {
  1747. t.Fatalf("unexpected error accumulating AllocationSetRange of length 1: %s", err)
  1748. }
  1749. if result == nil {
  1750. t.Fatalf("accumulating AllocationSetRange: expected AllocationSet; actual %s", result)
  1751. }
  1752. if result.TotalCost() != 6.0 {
  1753. t.Fatalf("accumulating AllocationSetRange: expected total cost 6.0; actual %f", result.TotalCost())
  1754. }
  1755. result, err = NewAllocationSetRange(todayAS, nil).Accumulate()
  1756. if err != nil {
  1757. t.Fatalf("unexpected error accumulating AllocationSetRange of length 1: %s", err)
  1758. }
  1759. if result == nil {
  1760. t.Fatalf("accumulating AllocationSetRange: expected AllocationSet; actual %s", result)
  1761. }
  1762. if result.TotalCost() != 6.0 {
  1763. t.Fatalf("accumulating AllocationSetRange: expected total cost 6.0; actual %f", result.TotalCost())
  1764. }
  1765. result, err = NewAllocationSetRange(nil, todayAS, nil).Accumulate()
  1766. if err != nil {
  1767. t.Fatalf("unexpected error accumulating AllocationSetRange of length 1: %s", err)
  1768. }
  1769. if result == nil {
  1770. t.Fatalf("accumulating AllocationSetRange: expected AllocationSet; actual %s", result)
  1771. }
  1772. if result.TotalCost() != 6.0 {
  1773. t.Fatalf("accumulating AllocationSetRange: expected total cost 6.0; actual %f", result.TotalCost())
  1774. }
  1775. // Accumulate two non-nil should result in sum of both with appropriate start, end
  1776. result, err = NewAllocationSetRange(yesterdayAS, todayAS).Accumulate()
  1777. if err != nil {
  1778. t.Fatalf("unexpected error accumulating AllocationSetRange of length 1: %s", err)
  1779. }
  1780. if result == nil {
  1781. t.Fatalf("accumulating AllocationSetRange: expected AllocationSet; actual %s", result)
  1782. }
  1783. if result.TotalCost() != 12.0 {
  1784. t.Fatalf("accumulating AllocationSetRange: expected total cost 12.0; actual %f", result.TotalCost())
  1785. }
  1786. allocMap := result.Map()
  1787. if len(allocMap) != 1 {
  1788. t.Fatalf("accumulating AllocationSetRange: expected length 1; actual length %d", len(allocMap))
  1789. }
  1790. alloc := allocMap["cluster1/namespace1/pod1/container1"]
  1791. if alloc == nil {
  1792. t.Fatalf("accumulating AllocationSetRange: expected allocation 'cluster1/namespace1/pod1/container1'")
  1793. }
  1794. if alloc.CPUCoreHours != 2.0 {
  1795. t.Fatalf("accumulating AllocationSetRange: expected 2.0; actual %f", result.TotalCost())
  1796. }
  1797. if alloc.CPUCost != 2.0 {
  1798. t.Fatalf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.CPUCost)
  1799. }
  1800. if alloc.CPUEfficiency() != 1.0 {
  1801. t.Fatalf("accumulating AllocationSetRange: expected 1.0; actual %f", alloc.CPUEfficiency())
  1802. }
  1803. if alloc.GPUHours != 2.0 {
  1804. t.Fatalf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.GPUHours)
  1805. }
  1806. if alloc.GPUCost != 2.0 {
  1807. t.Fatalf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.GPUCost)
  1808. }
  1809. if alloc.NetworkCost != 2.0 {
  1810. t.Fatalf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.NetworkCost)
  1811. }
  1812. if alloc.LoadBalancerCost != 2.0 {
  1813. t.Fatalf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.LoadBalancerCost)
  1814. }
  1815. if alloc.PVByteHours() != 2.0 {
  1816. t.Fatalf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.PVByteHours())
  1817. }
  1818. if alloc.PVCost() != 2.0 {
  1819. t.Fatalf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.PVCost())
  1820. }
  1821. if alloc.RAMByteHours != 2.0 {
  1822. t.Fatalf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.RAMByteHours)
  1823. }
  1824. if alloc.RAMCost != 2.0 {
  1825. t.Fatalf("accumulating AllocationSetRange: expected 2.0; actual %f", alloc.RAMCost)
  1826. }
  1827. if alloc.RAMEfficiency() != 1.0 {
  1828. t.Fatalf("accumulating AllocationSetRange: expected 1.0; actual %f", alloc.RAMEfficiency())
  1829. }
  1830. if alloc.TotalCost() != 12.0 {
  1831. t.Fatalf("accumulating AllocationSetRange: expected 12.0; actual %f", alloc.TotalCost())
  1832. }
  1833. if alloc.TotalEfficiency() != 1.0 {
  1834. t.Fatalf("accumulating AllocationSetRange: expected 1.0; actual %f", alloc.TotalEfficiency())
  1835. }
  1836. if !alloc.Start.Equal(yesterday) {
  1837. t.Fatalf("accumulating AllocationSetRange: expected to start %s; actual %s", yesterday, alloc.Start)
  1838. }
  1839. if !alloc.End.Equal(tomorrow) {
  1840. t.Fatalf("accumulating AllocationSetRange: expected to end %s; actual %s", tomorrow, alloc.End)
  1841. }
  1842. if alloc.Minutes() != 2880.0 {
  1843. t.Fatalf("accumulating AllocationSetRange: expected %f minutes; actual %f", 2880.0, alloc.Minutes())
  1844. }
  1845. }
  1846. // TODO niko/etl
  1847. // func TestAllocationSetRange_AccumulateBy(t *testing.T) {}
  1848. // TODO niko/etl
  1849. // func TestAllocationSetRange_AggregateBy(t *testing.T) {}
  1850. // TODO niko/etl
  1851. // func TestAllocationSetRange_Append(t *testing.T) {}
  1852. // TODO niko/etl
  1853. // func TestAllocationSetRange_Each(t *testing.T) {}
  1854. // TODO niko/etl
  1855. // func TestAllocationSetRange_Get(t *testing.T) {}
  1856. func TestAllocationSetRange_InsertRange(t *testing.T) {
  1857. // Set up
  1858. ago2d := time.Now().UTC().Truncate(day).Add(-2 * day)
  1859. yesterday := time.Now().UTC().Truncate(day).Add(-day)
  1860. today := time.Now().UTC().Truncate(day)
  1861. tomorrow := time.Now().UTC().Truncate(day).Add(day)
  1862. unit := NewMockUnitAllocation("", today, day, nil)
  1863. ago2dAS := NewAllocationSet(ago2d, yesterday)
  1864. ago2dAS.Set(NewMockUnitAllocation("a", ago2d, day, nil))
  1865. ago2dAS.Set(NewMockUnitAllocation("b", ago2d, day, nil))
  1866. ago2dAS.Set(NewMockUnitAllocation("c", ago2d, day, nil))
  1867. yesterdayAS := NewAllocationSet(yesterday, today)
  1868. yesterdayAS.Set(NewMockUnitAllocation("a", yesterday, day, nil))
  1869. yesterdayAS.Set(NewMockUnitAllocation("b", yesterday, day, nil))
  1870. yesterdayAS.Set(NewMockUnitAllocation("c", yesterday, day, nil))
  1871. todayAS := NewAllocationSet(today, tomorrow)
  1872. todayAS.Set(NewMockUnitAllocation("a", today, day, nil))
  1873. todayAS.Set(NewMockUnitAllocation("b", today, day, nil))
  1874. todayAS.Set(NewMockUnitAllocation("c", today, day, nil))
  1875. var nilASR *AllocationSetRange
  1876. thisASR := NewAllocationSetRange(yesterdayAS.Clone(), todayAS.Clone())
  1877. thatASR := NewAllocationSetRange(yesterdayAS.Clone())
  1878. longASR := NewAllocationSetRange(ago2dAS.Clone(), yesterdayAS.Clone(), todayAS.Clone())
  1879. var err error
  1880. // Expect an error calling InsertRange on nil
  1881. err = nilASR.InsertRange(thatASR)
  1882. if err == nil {
  1883. t.Fatalf("expected error, got nil")
  1884. }
  1885. // Expect nothing to happen calling InsertRange(nil) on non-nil ASR
  1886. err = thisASR.InsertRange(nil)
  1887. if err != nil {
  1888. t.Fatalf("unexpected error: %s", err)
  1889. }
  1890. thisASR.Each(func(i int, as *AllocationSet) {
  1891. as.Each(func(k string, a *Allocation) {
  1892. if !util.IsApproximately(a.CPUCoreHours, unit.CPUCoreHours) {
  1893. t.Fatalf("allocation %s: expected %f; got %f", k, unit.CPUCoreHours, a.CPUCoreHours)
  1894. }
  1895. if !util.IsApproximately(a.CPUCost, unit.CPUCost) {
  1896. t.Fatalf("allocation %s: expected %f; got %f", k, unit.CPUCost, a.CPUCost)
  1897. }
  1898. if !util.IsApproximately(a.RAMByteHours, unit.RAMByteHours) {
  1899. t.Fatalf("allocation %s: expected %f; got %f", k, unit.RAMByteHours, a.RAMByteHours)
  1900. }
  1901. if !util.IsApproximately(a.RAMCost, unit.RAMCost) {
  1902. t.Fatalf("allocation %s: expected %f; got %f", k, unit.RAMCost, a.RAMCost)
  1903. }
  1904. if !util.IsApproximately(a.GPUHours, unit.GPUHours) {
  1905. t.Fatalf("allocation %s: expected %f; got %f", k, unit.GPUHours, a.GPUHours)
  1906. }
  1907. if !util.IsApproximately(a.GPUCost, unit.GPUCost) {
  1908. t.Fatalf("allocation %s: expected %f; got %f", k, unit.GPUCost, a.GPUCost)
  1909. }
  1910. if !util.IsApproximately(a.PVByteHours(), unit.PVByteHours()) {
  1911. t.Fatalf("allocation %s: expected %f; got %f", k, unit.PVByteHours(), a.PVByteHours())
  1912. }
  1913. if !util.IsApproximately(a.PVCost(), unit.PVCost()) {
  1914. t.Fatalf("allocation %s: expected %f; got %f", k, unit.PVCost(), a.PVCost())
  1915. }
  1916. if !util.IsApproximately(a.NetworkCost, unit.NetworkCost) {
  1917. t.Fatalf("allocation %s: expected %f; got %f", k, unit.NetworkCost, a.NetworkCost)
  1918. }
  1919. if !util.IsApproximately(a.LoadBalancerCost, unit.LoadBalancerCost) {
  1920. t.Fatalf("allocation %s: expected %f; got %f", k, unit.LoadBalancerCost, a.LoadBalancerCost)
  1921. }
  1922. if !util.IsApproximately(a.TotalCost(), unit.TotalCost()) {
  1923. t.Fatalf("allocation %s: expected %f; got %f", k, unit.TotalCost(), a.TotalCost())
  1924. }
  1925. })
  1926. })
  1927. // Expect an error calling InsertRange with a range exceeding the receiver
  1928. err = thisASR.InsertRange(longASR)
  1929. if err == nil {
  1930. t.Fatalf("expected error calling InsertRange with a range exceeding the receiver")
  1931. }
  1932. // Expect each Allocation in "today" to stay the same, but "yesterday" to
  1933. // precisely double when inserting a range that only has a duplicate of
  1934. // "yesterday", but no entry for "today"
  1935. err = thisASR.InsertRange(thatASR)
  1936. if err != nil {
  1937. t.Fatalf("unexpected error: %s", err)
  1938. }
  1939. yAS, err := thisASR.Get(0)
  1940. yAS.Each(func(k string, a *Allocation) {
  1941. if !util.IsApproximately(a.CPUCoreHours, 2*unit.CPUCoreHours) {
  1942. t.Fatalf("allocation %s: expected %f; got %f", k, unit.CPUCoreHours, a.CPUCoreHours)
  1943. }
  1944. if !util.IsApproximately(a.CPUCost, 2*unit.CPUCost) {
  1945. t.Fatalf("allocation %s: expected %f; got %f", k, unit.CPUCost, a.CPUCost)
  1946. }
  1947. if !util.IsApproximately(a.RAMByteHours, 2*unit.RAMByteHours) {
  1948. t.Fatalf("allocation %s: expected %f; got %f", k, unit.RAMByteHours, a.RAMByteHours)
  1949. }
  1950. if !util.IsApproximately(a.RAMCost, 2*unit.RAMCost) {
  1951. t.Fatalf("allocation %s: expected %f; got %f", k, unit.RAMCost, a.RAMCost)
  1952. }
  1953. if !util.IsApproximately(a.GPUHours, 2*unit.GPUHours) {
  1954. t.Fatalf("allocation %s: expected %f; got %f", k, unit.GPUHours, a.GPUHours)
  1955. }
  1956. if !util.IsApproximately(a.GPUCost, 2*unit.GPUCost) {
  1957. t.Fatalf("allocation %s: expected %f; got %f", k, unit.GPUCost, a.GPUCost)
  1958. }
  1959. if !util.IsApproximately(a.PVByteHours(), 2*unit.PVByteHours()) {
  1960. t.Fatalf("allocation %s: expected %f; got %f", k, unit.PVByteHours(), a.PVByteHours())
  1961. }
  1962. if !util.IsApproximately(a.PVCost(), 2*unit.PVCost()) {
  1963. t.Fatalf("allocation %s: expected %f; got %f", k, unit.PVCost(), a.PVCost())
  1964. }
  1965. if !util.IsApproximately(a.NetworkCost, 2*unit.NetworkCost) {
  1966. t.Fatalf("allocation %s: expected %f; got %f", k, unit.NetworkCost, a.NetworkCost)
  1967. }
  1968. if !util.IsApproximately(a.LoadBalancerCost, 2*unit.LoadBalancerCost) {
  1969. t.Fatalf("allocation %s: expected %f; got %f", k, unit.LoadBalancerCost, a.LoadBalancerCost)
  1970. }
  1971. if !util.IsApproximately(a.TotalCost(), 2*unit.TotalCost()) {
  1972. t.Fatalf("allocation %s: expected %f; got %f", k, unit.TotalCost(), a.TotalCost())
  1973. }
  1974. })
  1975. tAS, err := thisASR.Get(1)
  1976. tAS.Each(func(k string, a *Allocation) {
  1977. if !util.IsApproximately(a.CPUCoreHours, unit.CPUCoreHours) {
  1978. t.Fatalf("allocation %s: expected %f; got %f", k, unit.CPUCoreHours, a.CPUCoreHours)
  1979. }
  1980. if !util.IsApproximately(a.CPUCost, unit.CPUCost) {
  1981. t.Fatalf("allocation %s: expected %f; got %f", k, unit.CPUCost, a.CPUCost)
  1982. }
  1983. if !util.IsApproximately(a.RAMByteHours, unit.RAMByteHours) {
  1984. t.Fatalf("allocation %s: expected %f; got %f", k, unit.RAMByteHours, a.RAMByteHours)
  1985. }
  1986. if !util.IsApproximately(a.RAMCost, unit.RAMCost) {
  1987. t.Fatalf("allocation %s: expected %f; got %f", k, unit.RAMCost, a.RAMCost)
  1988. }
  1989. if !util.IsApproximately(a.GPUHours, unit.GPUHours) {
  1990. t.Fatalf("allocation %s: expected %f; got %f", k, unit.GPUHours, a.GPUHours)
  1991. }
  1992. if !util.IsApproximately(a.GPUCost, unit.GPUCost) {
  1993. t.Fatalf("allocation %s: expected %f; got %f", k, unit.GPUCost, a.GPUCost)
  1994. }
  1995. if !util.IsApproximately(a.PVByteHours(), unit.PVByteHours()) {
  1996. t.Fatalf("allocation %s: expected %f; got %f", k, unit.PVByteHours(), a.PVByteHours())
  1997. }
  1998. if !util.IsApproximately(a.PVCost(), unit.PVCost()) {
  1999. t.Fatalf("allocation %s: expected %f; got %f", k, unit.PVCost(), a.PVCost())
  2000. }
  2001. if !util.IsApproximately(a.NetworkCost, unit.NetworkCost) {
  2002. t.Fatalf("allocation %s: expected %f; got %f", k, unit.NetworkCost, a.NetworkCost)
  2003. }
  2004. if !util.IsApproximately(a.LoadBalancerCost, unit.LoadBalancerCost) {
  2005. t.Fatalf("allocation %s: expected %f; got %f", k, unit.LoadBalancerCost, a.LoadBalancerCost)
  2006. }
  2007. if !util.IsApproximately(a.TotalCost(), unit.TotalCost()) {
  2008. t.Fatalf("allocation %s: expected %f; got %f", k, unit.TotalCost(), a.TotalCost())
  2009. }
  2010. })
  2011. }
  2012. // TODO niko/etl
  2013. // func TestAllocationSetRange_Length(t *testing.T) {}
  2014. // TODO niko/etl
  2015. // func TestAllocationSetRange_MarshalJSON(t *testing.T) {}
  2016. // TODO niko/etl
  2017. // func TestAllocationSetRange_Slice(t *testing.T) {}
  2018. // TODO niko/etl
  2019. // func TestAllocationSetRange_Window(t *testing.T) {}
  2020. func TestAllocationSetRange_Start(t *testing.T) {
  2021. tests := []struct {
  2022. name string
  2023. arg *AllocationSetRange
  2024. expectError bool
  2025. expected time.Time
  2026. }{
  2027. {
  2028. name: "Empty ASR",
  2029. arg: nil,
  2030. expectError: true,
  2031. },
  2032. {
  2033. name: "Single allocation",
  2034. arg: &AllocationSetRange{
  2035. allocations: []*AllocationSet{
  2036. &AllocationSet{
  2037. allocations: map[string]*Allocation{
  2038. "a": &Allocation{
  2039. Start: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  2040. },
  2041. },
  2042. },
  2043. },
  2044. },
  2045. expected: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  2046. },
  2047. {
  2048. name: "Two allocations",
  2049. arg: &AllocationSetRange{
  2050. allocations: []*AllocationSet{
  2051. &AllocationSet{
  2052. allocations: map[string]*Allocation{
  2053. "a": &Allocation{
  2054. Start: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  2055. },
  2056. "b": &Allocation{
  2057. Start: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  2058. },
  2059. },
  2060. },
  2061. },
  2062. },
  2063. expected: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  2064. },
  2065. {
  2066. name: "Two AllocationSets",
  2067. arg: &AllocationSetRange{
  2068. allocations: []*AllocationSet{
  2069. &AllocationSet{
  2070. allocations: map[string]*Allocation{
  2071. "a": &Allocation{
  2072. Start: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  2073. },
  2074. },
  2075. },
  2076. &AllocationSet{
  2077. allocations: map[string]*Allocation{
  2078. "b": &Allocation{
  2079. Start: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  2080. },
  2081. },
  2082. },
  2083. },
  2084. },
  2085. expected: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  2086. },
  2087. }
  2088. for _, test := range tests {
  2089. result, err := test.arg.Start()
  2090. if test.expectError && err != nil {
  2091. continue
  2092. }
  2093. if test.expectError && err == nil {
  2094. t.Errorf("%s: expected error and got none", test.name)
  2095. } else if result != test.expected {
  2096. t.Errorf("%s: expected %s but got %s", test.name, test.expected, result)
  2097. }
  2098. }
  2099. }
  2100. func TestAllocationSetRange_End(t *testing.T) {
  2101. tests := []struct {
  2102. name string
  2103. arg *AllocationSetRange
  2104. expectError bool
  2105. expected time.Time
  2106. }{
  2107. {
  2108. name: "Empty ASR",
  2109. arg: nil,
  2110. expectError: true,
  2111. },
  2112. {
  2113. name: "Single allocation",
  2114. arg: &AllocationSetRange{
  2115. allocations: []*AllocationSet{
  2116. &AllocationSet{
  2117. allocations: map[string]*Allocation{
  2118. "a": &Allocation{
  2119. End: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  2120. },
  2121. },
  2122. },
  2123. },
  2124. },
  2125. expected: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  2126. },
  2127. {
  2128. name: "Two allocations",
  2129. arg: &AllocationSetRange{
  2130. allocations: []*AllocationSet{
  2131. &AllocationSet{
  2132. allocations: map[string]*Allocation{
  2133. "a": &Allocation{
  2134. End: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  2135. },
  2136. "b": &Allocation{
  2137. End: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  2138. },
  2139. },
  2140. },
  2141. },
  2142. },
  2143. expected: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  2144. },
  2145. {
  2146. name: "Two AllocationSets",
  2147. arg: &AllocationSetRange{
  2148. allocations: []*AllocationSet{
  2149. &AllocationSet{
  2150. allocations: map[string]*Allocation{
  2151. "a": &Allocation{
  2152. End: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  2153. },
  2154. },
  2155. },
  2156. &AllocationSet{
  2157. allocations: map[string]*Allocation{
  2158. "b": &Allocation{
  2159. End: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  2160. },
  2161. },
  2162. },
  2163. },
  2164. },
  2165. expected: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  2166. },
  2167. }
  2168. for _, test := range tests {
  2169. result, err := test.arg.End()
  2170. if test.expectError && err != nil {
  2171. continue
  2172. }
  2173. if test.expectError && err == nil {
  2174. t.Errorf("%s: expected error and got none", test.name)
  2175. } else if result != test.expected {
  2176. t.Errorf("%s: expected %s but got %s", test.name, test.expected, result)
  2177. }
  2178. }
  2179. }
  2180. func TestAllocationSetRange_Minutes(t *testing.T) {
  2181. tests := []struct {
  2182. name string
  2183. arg *AllocationSetRange
  2184. expected float64
  2185. }{
  2186. {
  2187. name: "Empty ASR",
  2188. arg: nil,
  2189. expected: 0,
  2190. },
  2191. {
  2192. name: "Single allocation",
  2193. arg: &AllocationSetRange{
  2194. allocations: []*AllocationSet{
  2195. &AllocationSet{
  2196. allocations: map[string]*Allocation{
  2197. "a": &Allocation{
  2198. Start: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  2199. End: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  2200. },
  2201. },
  2202. },
  2203. },
  2204. },
  2205. expected: 24 * 60,
  2206. },
  2207. {
  2208. name: "Two allocations",
  2209. arg: &AllocationSetRange{
  2210. allocations: []*AllocationSet{
  2211. &AllocationSet{
  2212. allocations: map[string]*Allocation{
  2213. "a": &Allocation{
  2214. Start: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  2215. End: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  2216. },
  2217. "b": &Allocation{
  2218. Start: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  2219. End: time.Date(1970, 1, 3, 0, 0, 0, 0, time.UTC),
  2220. },
  2221. },
  2222. },
  2223. },
  2224. },
  2225. expected: 2 * 24 * 60,
  2226. },
  2227. {
  2228. name: "Two AllocationSets",
  2229. arg: &AllocationSetRange{
  2230. allocations: []*AllocationSet{
  2231. &AllocationSet{
  2232. allocations: map[string]*Allocation{
  2233. "a": &Allocation{
  2234. Start: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  2235. End: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  2236. },
  2237. },
  2238. },
  2239. &AllocationSet{
  2240. allocations: map[string]*Allocation{
  2241. "b": &Allocation{
  2242. Start: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  2243. End: time.Date(1970, 1, 3, 0, 0, 0, 0, time.UTC),
  2244. },
  2245. },
  2246. },
  2247. },
  2248. },
  2249. expected: 2 * 24 * 60,
  2250. },
  2251. }
  2252. for _, test := range tests {
  2253. result := test.arg.Minutes()
  2254. if result != test.expected {
  2255. t.Errorf("%s: expected %f but got %f", test.name, test.expected, result)
  2256. }
  2257. }
  2258. }