allocation_test.go 75 KB

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