allocation_test.go 87 KB

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