allocation_test.go 76 KB

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