allocation_test.go 86 KB

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