allocation_test.go 70 KB

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