allocation_test.go 71 KB

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