asset_test.go 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481
  1. package kubecost
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "math"
  6. "testing"
  7. "time"
  8. "github.com/opencost/opencost/pkg/util"
  9. )
  10. var start1 = time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)
  11. var start2 = start1.Add(day)
  12. var start3 = start2.Add(day)
  13. var start4 = start2.Add(day)
  14. var windows = []Window{
  15. NewWindow(&start1, &start2),
  16. NewWindow(&start2, &start3),
  17. NewWindow(&start3, &start4),
  18. }
  19. func assertAssetSet(t *testing.T, as *AssetSet, msg string, window Window, exps map[string]float64, err error) {
  20. if err != nil {
  21. t.Fatalf("AssetSet.AggregateBy[%s]: unexpected error: %s", msg, err)
  22. }
  23. if as.Length() != len(exps) {
  24. t.Fatalf("AssetSet.AggregateBy[%s]: expected set of length %d, actual %d", msg, len(exps), as.Length())
  25. }
  26. if !as.Window.Equal(window) {
  27. t.Fatalf("AssetSet.AggregateBy[%s]: expected window %s, actual %s", msg, window, as.Window)
  28. }
  29. for key, a := range as.Assets {
  30. if exp, ok := exps[key]; ok {
  31. if math.Round(a.TotalCost()*100) != math.Round(exp*100) {
  32. t.Fatalf("AssetSet.AggregateBy[%s]: key %s expected total cost %.2f, actual %.2f", msg, key, exp, a.TotalCost())
  33. }
  34. if !a.GetWindow().Equal(window) {
  35. t.Fatalf("AssetSet.AggregateBy[%s]: key %s expected window %s, actual %s", msg, key, window, a.GetWindow())
  36. }
  37. } else {
  38. t.Fatalf("AssetSet.AggregateBy[%s]: unexpected asset: %s", msg, key)
  39. }
  40. }
  41. }
  42. func printAssetSet(msg string, as *AssetSet) {
  43. fmt.Printf("--- %s ---\n", msg)
  44. for key, a := range as.Assets {
  45. fmt.Printf(" > %s: %s\n", key, a)
  46. }
  47. }
  48. func TestAny_Add(t *testing.T) {
  49. any1 := NewAsset(*windows[0].start, *windows[0].end, windows[0])
  50. any1.SetProperties(&AssetProperties{
  51. Name: "any1",
  52. Cluster: "cluster1",
  53. ProviderID: "any1",
  54. })
  55. any1.Cost = 9.0
  56. any1.SetAdjustment(1.0)
  57. any2 := NewAsset(*windows[0].start, *windows[0].end, windows[0])
  58. any2.SetProperties(&AssetProperties{
  59. Name: "any2",
  60. Cluster: "cluster1",
  61. ProviderID: "any2",
  62. })
  63. any2.Cost = 4.0
  64. any2.SetAdjustment(1.0)
  65. any3 := any1.Add(any2)
  66. // Check that the sums and properties are correct
  67. if any3.TotalCost() != 15.0 {
  68. t.Fatalf("Any.Add: expected %f; got %f", 15.0, any3.TotalCost())
  69. }
  70. if any3.GetAdjustment() != 2.0 {
  71. t.Fatalf("Any.Add: expected %f; got %f", 2.0, any3.GetAdjustment())
  72. }
  73. if any3.GetProperties().Cluster != "cluster1" {
  74. t.Fatalf("Any.Add: expected %s; got %s", "cluster1", any3.GetProperties().Cluster)
  75. }
  76. if any3.Type() != AnyAssetType {
  77. t.Fatalf("Any.Add: expected %s; got %s", AnyAssetType, any3.Type())
  78. }
  79. if any3.GetProperties().ProviderID != "" {
  80. t.Fatalf("Any.Add: expected %s; got %s", "", any3.GetProperties().ProviderID)
  81. }
  82. if any3.GetProperties().Name != "" {
  83. t.Fatalf("Any.Add: expected %s; got %s", "", any3.GetProperties().Name)
  84. }
  85. // Check that the original assets are unchanged
  86. if any1.TotalCost() != 10.0 {
  87. t.Fatalf("Any.Add: expected %f; got %f", 10.0, any1.TotalCost())
  88. }
  89. if any1.Adjustment != 1.0 {
  90. t.Fatalf("Any.Add: expected %f; got %f", 1.0, any1.Adjustment)
  91. }
  92. if any2.TotalCost() != 5.0 {
  93. t.Fatalf("Any.Add: expected %f; got %f", 5.0, any2.TotalCost())
  94. }
  95. if any2.Adjustment != 1.0 {
  96. t.Fatalf("Any.Add: expected %f; got %f", 1.0, any2.Adjustment)
  97. }
  98. }
  99. func TestAny_Clone(t *testing.T) {
  100. any1 := NewAsset(*windows[0].start, *windows[0].end, windows[0])
  101. any1.SetProperties(&AssetProperties{
  102. Name: "any1",
  103. Cluster: "cluster1",
  104. ProviderID: "any1",
  105. })
  106. any1.Cost = 9.0
  107. any1.SetAdjustment(1.0)
  108. any2 := any1.Clone()
  109. any1.Cost = 18.0
  110. any1.SetAdjustment(2.0)
  111. // any2 should match any1, even after mutating any1
  112. if any2.TotalCost() != 10.0 {
  113. t.Fatalf("Any.Clone: expected %f; got %f", 10.0, any2.TotalCost())
  114. }
  115. if any2.GetAdjustment() != 1.0 {
  116. t.Fatalf("Any.Clone: expected %f; got %f", 1.0, any2.GetAdjustment())
  117. }
  118. }
  119. func TestAny_MarshalJSON(t *testing.T) {
  120. any1 := NewAsset(*windows[0].start, *windows[0].end, windows[0])
  121. any1.SetProperties(&AssetProperties{
  122. Name: "any1",
  123. Cluster: "cluster1",
  124. ProviderID: "any1",
  125. })
  126. any1.Cost = 9.0
  127. any1.SetAdjustment(1.0)
  128. _, err := json.Marshal(any1)
  129. if err != nil {
  130. t.Fatalf("Any.MarshalJSON: unexpected error: %s", err)
  131. }
  132. any2 := NewAsset(*windows[0].start, *windows[0].end, windows[0])
  133. any2.SetProperties(&AssetProperties{
  134. Name: "any2",
  135. Cluster: "cluster1",
  136. ProviderID: "any2",
  137. })
  138. any2.Cost = math.NaN()
  139. any2.SetAdjustment(1.0)
  140. _, err = json.Marshal(any2)
  141. if err != nil {
  142. t.Fatalf("Any.MarshalJSON: unexpected error: %s", err)
  143. }
  144. }
  145. func TestDisk_Add(t *testing.T) {
  146. // 1. aggregate: add size, local
  147. // 2. accumulate: don't add size, local
  148. hours := windows[0].Duration().Hours()
  149. // Aggregate: two disks, one window
  150. disk1 := NewDisk("disk1", "cluster1", "disk1", *windows[0].start, *windows[0].end, windows[0])
  151. disk1.ByteHours = 100.0 * gb * hours
  152. disk1.Cost = 9.0
  153. disk1.SetAdjustment(1.0)
  154. if disk1.Bytes() != 100.0*gb {
  155. t.Fatalf("Disk.Add: expected %f; got %f", 100.0*gb, disk1.Bytes())
  156. }
  157. disk2 := NewDisk("disk2", "cluster1", "disk2", *windows[0].start, *windows[0].end, windows[0])
  158. disk2.ByteHours = 60.0 * gb * hours
  159. disk2.Cost = 4.0
  160. disk2.Local = 1.0
  161. disk2.SetAdjustment(1.0)
  162. if disk2.Bytes() != 60.0*gb {
  163. t.Fatalf("Disk.Add: expected %f; got %f", 60.0*gb, disk2.Bytes())
  164. }
  165. diskT := disk1.Add(disk2).(*Disk)
  166. // Check that the sums and properties are correct
  167. if diskT.TotalCost() != 15.0 {
  168. t.Fatalf("Disk.Add: expected %f; got %f", 15.0, diskT.TotalCost())
  169. }
  170. if diskT.Adjustment != 2.0 {
  171. t.Fatalf("Disk.Add: expected %f; got %f", 2.0, diskT.Adjustment)
  172. }
  173. if diskT.Properties.Cluster != "cluster1" {
  174. t.Fatalf("Disk.Add: expected %s; got %s", "cluster1", diskT.Properties.Cluster)
  175. }
  176. if diskT.Type() != DiskAssetType {
  177. t.Fatalf("Disk.Add: expected %s; got %s", AnyAssetType, diskT.Type())
  178. }
  179. if diskT.Properties.ProviderID != "" {
  180. t.Fatalf("Disk.Add: expected %s; got %s", "", diskT.Properties.ProviderID)
  181. }
  182. if diskT.Properties.Name != "" {
  183. t.Fatalf("Disk.Add: expected %s; got %s", "", diskT.Properties.Name)
  184. }
  185. if diskT.Bytes() != 160.0*gb {
  186. t.Fatalf("Disk.Add: expected %f; got %f", 160.0*gb, diskT.Bytes())
  187. }
  188. if !util.IsApproximately(diskT.Local, 0.333333) {
  189. t.Fatalf("Disk.Add: expected %f; got %f", 0.333333, diskT.Local)
  190. }
  191. // Check that the original assets are unchanged
  192. if disk1.TotalCost() != 10.0 {
  193. t.Fatalf("Disk.Add: expected %f; got %f", 10.0, disk1.TotalCost())
  194. }
  195. if disk1.Adjustment != 1.0 {
  196. t.Fatalf("Disk.Add: expected %f; got %f", 1.0, disk1.Adjustment)
  197. }
  198. if disk1.Local != 0.0 {
  199. t.Fatalf("Disk.Add: expected %f; got %f", 0.0, disk1.Local)
  200. }
  201. if disk2.TotalCost() != 5.0 {
  202. t.Fatalf("Disk.Add: expected %f; got %f", 5.0, disk2.TotalCost())
  203. }
  204. if disk2.Adjustment != 1.0 {
  205. t.Fatalf("Disk.Add: expected %f; got %f", 1.0, disk2.Adjustment)
  206. }
  207. if disk2.Local != 1.0 {
  208. t.Fatalf("Disk.Add: expected %f; got %f", 1.0, disk2.Local)
  209. }
  210. disk3 := NewDisk("disk3", "cluster1", "disk3", *windows[0].start, *windows[0].end, windows[0])
  211. disk3.ByteHours = 0.0 * hours
  212. disk3.Cost = 0.0
  213. disk3.Local = 0.0
  214. disk3.SetAdjustment(0.0)
  215. disk4 := NewDisk("disk4", "cluster1", "disk4", *windows[0].start, *windows[0].end, windows[0])
  216. disk4.ByteHours = 0.0 * hours
  217. disk4.Cost = 0.0
  218. disk4.Local = 1.0
  219. disk4.SetAdjustment(0.0)
  220. diskT = disk3.Add(disk4).(*Disk)
  221. if diskT.TotalCost() != 0.0 {
  222. t.Fatalf("Disk.Add: expected %f; got %f", 0.0, diskT.TotalCost())
  223. }
  224. if diskT.Local != 0.5 {
  225. t.Fatalf("Disk.Add: expected %f; got %f", 0.5, diskT.Local)
  226. }
  227. // Accumulate: one disks, two windows
  228. diskA1 := NewDisk("diskA1", "cluster1", "diskA1", *windows[0].start, *windows[0].end, windows[0])
  229. diskA1.ByteHours = 100 * gb * hours
  230. diskA1.Cost = 9.0
  231. diskA1.SetAdjustment(1.0)
  232. diskA2 := NewDisk("diskA2", "cluster1", "diskA2", *windows[1].start, *windows[1].end, windows[1])
  233. diskA2.ByteHours = 100 * gb * hours
  234. diskA2.Cost = 9.0
  235. diskA2.SetAdjustment(1.0)
  236. diskAT := diskA1.Add(diskA2).(*Disk)
  237. // Check that the sums and properties are correct
  238. if diskAT.TotalCost() != 20.0 {
  239. t.Fatalf("Disk.Add: expected %f; got %f", 20.0, diskAT.TotalCost())
  240. }
  241. if diskAT.Adjustment != 2.0 {
  242. t.Fatalf("Disk.Add: expected %f; got %f", 2.0, diskAT.Adjustment)
  243. }
  244. if diskAT.Properties.Cluster != "cluster1" {
  245. t.Fatalf("Disk.Add: expected %s; got %s", "cluster1", diskAT.Properties.Cluster)
  246. }
  247. if diskAT.Type() != DiskAssetType {
  248. t.Fatalf("Disk.Add: expected %s; got %s", AnyAssetType, diskAT.Type())
  249. }
  250. if diskAT.Properties.ProviderID != "" {
  251. t.Fatalf("Disk.Add: expected %s; got %s", "", diskAT.Properties.ProviderID)
  252. }
  253. if diskAT.Properties.Name != "" {
  254. t.Fatalf("Disk.Add: expected %s; got %s", "", diskAT.Properties.Name)
  255. }
  256. if diskAT.Bytes() != 100.0*gb {
  257. t.Fatalf("Disk.Add: expected %f; got %f", 100.0*gb, diskT.Bytes())
  258. }
  259. if diskAT.Local != 0.0 {
  260. t.Fatalf("Disk.Add: expected %f; got %f", 0.0, diskAT.Local)
  261. }
  262. // Check that the original assets are unchanged
  263. if diskA1.TotalCost() != 10.0 {
  264. t.Fatalf("Disk.Add: expected %f; got %f", 10.0, diskA1.TotalCost())
  265. }
  266. if diskA1.Adjustment != 1.0 {
  267. t.Fatalf("Disk.Add: expected %f; got %f", 1.0, diskA1.Adjustment)
  268. }
  269. if diskA1.Local != 0.0 {
  270. t.Fatalf("Disk.Add: expected %f; got %f", 0.0, diskA1.Local)
  271. }
  272. if diskA2.TotalCost() != 10.0 {
  273. t.Fatalf("Disk.Add: expected %f; got %f", 10.0, diskA2.TotalCost())
  274. }
  275. if diskA2.Adjustment != 1.0 {
  276. t.Fatalf("Disk.Add: expected %f; got %f", 1.0, diskA2.Adjustment)
  277. }
  278. if diskA2.Local != 0.0 {
  279. t.Fatalf("Disk.Add: expected %f; got %f", 0.0, diskA2.Local)
  280. }
  281. }
  282. func TestDisk_Clone(t *testing.T) {
  283. disk1 := NewDisk("disk1", "cluster1", "disk1", *windows[0].start, *windows[0].end, windows[0])
  284. disk1.Local = 0.0
  285. disk1.Cost = 9.0
  286. disk1.SetAdjustment(1.0)
  287. disk2 := disk1.Clone().(*Disk)
  288. disk2.Local = 1.0
  289. disk1.Cost = 18.0
  290. disk1.SetAdjustment(2.0)
  291. // disk2 should match disk1, even after mutating disk1
  292. if disk2.TotalCost() != 10.0 {
  293. t.Fatalf("Any.Clone: expected %f; got %f", 10.0, disk2.TotalCost())
  294. }
  295. if disk2.Adjustment != 1.0 {
  296. t.Fatalf("Any.Clone: expected %f; got %f", 1.0, disk2.Adjustment)
  297. }
  298. if disk2.Local != 1.0 {
  299. t.Fatalf("Disk.Add: expected %f; got %f", 1.0, disk2.Local)
  300. }
  301. }
  302. func TestDisk_MarshalJSON(t *testing.T) {
  303. disk := NewDisk("disk", "cluster", "providerID", *windows[0].start, *windows[0].end, windows[0])
  304. disk.SetLabels(AssetLabels{
  305. "label": "value",
  306. })
  307. disk.Cost = 9.0
  308. disk.SetAdjustment(1.0)
  309. _, err := json.Marshal(disk)
  310. if err != nil {
  311. t.Fatalf("Disk.MarshalJSON: unexpected error: %s", err)
  312. }
  313. }
  314. func TestNode_Add(t *testing.T) {
  315. // 1. aggregate: add size, local
  316. // 2. accumulate: don't add size, local
  317. hours := windows[0].Duration().Hours()
  318. // Aggregate: two nodes, one window
  319. node1 := NewNode("node1", "cluster1", "node1", *windows[0].start, *windows[0].end, windows[0])
  320. node1.CPUCoreHours = 1.0 * hours
  321. node1.RAMByteHours = 2.0 * gb * hours
  322. node1.GPUHours = 0.0 * hours
  323. node1.GPUCost = 0.0
  324. node1.CPUCost = 8.0
  325. node1.RAMCost = 4.0
  326. node1.Discount = 0.3
  327. node1.CPUBreakdown = &Breakdown{
  328. Idle: 0.6,
  329. System: 0.2,
  330. User: 0.2,
  331. Other: 0.0,
  332. }
  333. node1.RAMBreakdown = &Breakdown{
  334. Idle: 0.6,
  335. System: 0.2,
  336. User: 0.2,
  337. Other: 0.0,
  338. }
  339. node1.SetAdjustment(1.6)
  340. node2 := NewNode("node2", "cluster1", "node2", *windows[0].start, *windows[0].end, windows[0])
  341. node2.CPUCoreHours = 1.0 * hours
  342. node2.RAMByteHours = 2.0 * gb * hours
  343. node2.GPUHours = 0.0 * hours
  344. node2.GPUCost = 0.0
  345. node2.CPUCost = 3.0
  346. node2.RAMCost = 1.0
  347. node2.Discount = 0.0
  348. node1.CPUBreakdown = &Breakdown{
  349. Idle: 0.9,
  350. System: 0.05,
  351. User: 0.0,
  352. Other: 0.05,
  353. }
  354. node1.RAMBreakdown = &Breakdown{
  355. Idle: 0.9,
  356. System: 0.05,
  357. User: 0.0,
  358. Other: 0.05,
  359. }
  360. node2.SetAdjustment(1.0)
  361. nodeT := node1.Add(node2).(*Node)
  362. // Check that the sums and properties are correct
  363. if !util.IsApproximately(nodeT.TotalCost(), 15.0) {
  364. t.Fatalf("Node.Add: expected %f; got %f", 15.0, nodeT.TotalCost())
  365. }
  366. if nodeT.Adjustment != 2.6 {
  367. t.Fatalf("Node.Add: expected %f; got %f", 2.6, nodeT.Adjustment)
  368. }
  369. if nodeT.Properties.Cluster != "cluster1" {
  370. t.Fatalf("Node.Add: expected %s; got %s", "cluster1", nodeT.Properties.Cluster)
  371. }
  372. if nodeT.Type() != NodeAssetType {
  373. t.Fatalf("Node.Add: expected %s; got %s", AnyAssetType, nodeT.Type())
  374. }
  375. if nodeT.Properties.ProviderID != "" {
  376. t.Fatalf("Node.Add: expected %s; got %s", "", nodeT.Properties.ProviderID)
  377. }
  378. if nodeT.Properties.Name != "" {
  379. t.Fatalf("Node.Add: expected %s; got %s", "", nodeT.Properties.Name)
  380. }
  381. if nodeT.CPUCores() != 2.0 {
  382. t.Fatalf("Node.Add: expected %f; got %f", 2.0, nodeT.CPUCores())
  383. }
  384. if nodeT.RAMBytes() != 4.0*gb {
  385. t.Fatalf("Node.Add: expected %f; got %f", 4.0*gb, nodeT.RAMBytes())
  386. }
  387. // Check that the original assets are unchanged
  388. if !util.IsApproximately(node1.TotalCost(), 10.0) {
  389. t.Fatalf("Node.Add: expected %f; got %f", 10.0, node1.TotalCost())
  390. }
  391. if node1.Adjustment != 1.6 {
  392. t.Fatalf("Node.Add: expected %f; got %f", 1.0, node1.Adjustment)
  393. }
  394. if !util.IsApproximately(node2.TotalCost(), 5.0) {
  395. t.Fatalf("Node.Add: expected %f; got %f", 5.0, node2.TotalCost())
  396. }
  397. if node2.Adjustment != 1.0 {
  398. t.Fatalf("Node.Add: expected %f; got %f", 1.0, node2.Adjustment)
  399. }
  400. // Check that we don't divide by zero computing Local
  401. node3 := NewNode("node3", "cluster1", "node3", *windows[0].start, *windows[0].end, windows[0])
  402. node3.CPUCoreHours = 0 * hours
  403. node3.RAMByteHours = 0 * hours
  404. node3.GPUHours = 0.0 * hours
  405. node3.GPUCost = 0
  406. node3.CPUCost = 0.0
  407. node3.RAMCost = 0.0
  408. node3.Discount = 0.3
  409. node3.SetAdjustment(0.0)
  410. node4 := NewNode("node4", "cluster1", "node4", *windows[0].start, *windows[0].end, windows[0])
  411. node4.CPUCoreHours = 0 * hours
  412. node4.RAMByteHours = 0 * hours
  413. node4.GPUHours = 0.0 * hours
  414. node4.GPUCost = 0
  415. node4.CPUCost = 0.0
  416. node4.RAMCost = 0.0
  417. node4.Discount = 0.1
  418. node4.SetAdjustment(0.0)
  419. nodeT = node3.Add(node4).(*Node)
  420. // Check that the sums and properties are correct and without NaNs
  421. if nodeT.TotalCost() != 0.0 {
  422. t.Fatalf("Node.Add: expected %f; got %f", 0.0, nodeT.TotalCost())
  423. }
  424. if nodeT.Discount != 0.2 {
  425. t.Fatalf("Node.Add: expected %f; got %f", 0.2, nodeT.Discount)
  426. }
  427. // Accumulate: one nodes, two window
  428. nodeA1 := NewNode("nodeA1", "cluster1", "nodeA1", *windows[0].start, *windows[0].end, windows[0])
  429. nodeA1.CPUCoreHours = 1.0 * hours
  430. nodeA1.RAMByteHours = 2.0 * gb * hours
  431. nodeA1.GPUHours = 0.0 * hours
  432. nodeA1.GPUCost = 0.0
  433. nodeA1.CPUCost = 8.0
  434. nodeA1.RAMCost = 4.0
  435. nodeA1.Discount = 0.3
  436. nodeA1.SetAdjustment(1.6)
  437. nodeA2 := NewNode("nodeA2", "cluster1", "nodeA2", *windows[1].start, *windows[1].end, windows[1])
  438. nodeA2.CPUCoreHours = 1.0 * hours
  439. nodeA2.RAMByteHours = 2.0 * gb * hours
  440. nodeA2.GPUHours = 0.0 * hours
  441. nodeA2.GPUCost = 0.0
  442. nodeA2.CPUCost = 3.0
  443. nodeA2.RAMCost = 1.0
  444. nodeA2.Discount = 0.0
  445. nodeA2.SetAdjustment(1.0)
  446. nodeAT := nodeA1.Add(nodeA2).(*Node)
  447. // Check that the sums and properties are correct
  448. if !util.IsApproximately(nodeAT.TotalCost(), 15.0) {
  449. t.Fatalf("Node.Add: expected %f; got %f", 15.0, nodeAT.TotalCost())
  450. }
  451. if nodeAT.Adjustment != 2.6 {
  452. t.Fatalf("Node.Add: expected %f; got %f", 2.6, nodeAT.Adjustment)
  453. }
  454. if nodeAT.Properties.Cluster != "cluster1" {
  455. t.Fatalf("Node.Add: expected %s; got %s", "cluster1", nodeAT.Properties.Cluster)
  456. }
  457. if nodeAT.Type() != NodeAssetType {
  458. t.Fatalf("Node.Add: expected %s; got %s", AnyAssetType, nodeAT.Type())
  459. }
  460. if nodeAT.Properties.ProviderID != "" {
  461. t.Fatalf("Node.Add: expected %s; got %s", "", nodeAT.Properties.ProviderID)
  462. }
  463. if nodeAT.Properties.Name != "" {
  464. t.Fatalf("Node.Add: expected %s; got %s", "", nodeAT.Properties.Name)
  465. }
  466. if nodeAT.CPUCores() != 1.0 {
  467. t.Fatalf("Node.Add: expected %f; got %f", 1.0, nodeAT.CPUCores())
  468. }
  469. if nodeAT.RAMBytes() != 2.0*gb {
  470. t.Fatalf("Node.Add: expected %f; got %f", 2.0*gb, nodeAT.RAMBytes())
  471. }
  472. if nodeAT.GPUs() != 0.0 {
  473. t.Fatalf("Node.Add: expected %f; got %f", 0.0, nodeAT.GPUs())
  474. }
  475. // Check that the original assets are unchanged
  476. if !util.IsApproximately(nodeA1.TotalCost(), 10.0) {
  477. t.Fatalf("Node.Add: expected %f; got %f", 10.0, nodeA1.TotalCost())
  478. }
  479. if nodeA1.Adjustment != 1.6 {
  480. t.Fatalf("Node.Add: expected %f; got %f", 1.0, nodeA1.Adjustment)
  481. }
  482. if !util.IsApproximately(nodeA2.TotalCost(), 5.0) {
  483. t.Fatalf("Node.Add: expected %f; got %f", 5.0, nodeA2.TotalCost())
  484. }
  485. if nodeA2.Adjustment != 1.0 {
  486. t.Fatalf("Node.Add: expected %f; got %f", 1.0, nodeA2.Adjustment)
  487. }
  488. }
  489. func TestNode_Clone(t *testing.T) {
  490. // TODO
  491. }
  492. func TestNode_MarshalJSON(t *testing.T) {
  493. node := NewNode("node", "cluster", "providerID", *windows[0].start, *windows[0].end, windows[0])
  494. node.SetLabels(AssetLabels{
  495. "label": "value",
  496. })
  497. node.CPUCost = 9.0
  498. node.RAMCost = 0.0
  499. node.RAMCost = 21.0
  500. node.CPUCoreHours = 123.0
  501. node.RAMByteHours = 13323.0
  502. node.GPUHours = 123.0
  503. node.SetAdjustment(1.0)
  504. _, err := json.Marshal(node)
  505. if err != nil {
  506. t.Fatalf("Node.MarshalJSON: unexpected error: %s", err)
  507. }
  508. }
  509. func TestClusterManagement_Add(t *testing.T) {
  510. cm1 := NewClusterManagement(GCPProvider, "cluster1", windows[0])
  511. cm1.Cost = 9.0
  512. cm2 := NewClusterManagement(GCPProvider, "cluster1", windows[0])
  513. cm2.Cost = 4.0
  514. cm3 := cm1.Add(cm2)
  515. // Check that the sums and properties are correct
  516. if cm3.TotalCost() != 13.0 {
  517. t.Fatalf("ClusterManagement.Add: expected %f; got %f", 13.0, cm3.TotalCost())
  518. }
  519. if cm3.GetProperties().Cluster != "cluster1" {
  520. t.Fatalf("ClusterManagement.Add: expected %s; got %s", "cluster1", cm3.GetProperties().Cluster)
  521. }
  522. if cm3.Type() != ClusterManagementAssetType {
  523. t.Fatalf("ClusterManagement.Add: expected %s; got %s", ClusterManagementAssetType, cm3.Type())
  524. }
  525. // Check that the original assets are unchanged
  526. if cm1.TotalCost() != 9.0 {
  527. t.Fatalf("ClusterManagement.Add: expected %f; got %f", 9.0, cm1.TotalCost())
  528. }
  529. if cm2.TotalCost() != 4.0 {
  530. t.Fatalf("ClusterManagement.Add: expected %f; got %f", 4.0, cm2.TotalCost())
  531. }
  532. }
  533. func TestClusterManagement_Clone(t *testing.T) {
  534. // TODO
  535. }
  536. func TestCloudAny_Add(t *testing.T) {
  537. ca1 := NewCloud(ComputeCategory, "ca1", *windows[0].start, *windows[0].end, windows[0])
  538. ca1.Cost = 9.0
  539. ca1.SetAdjustment(1.0)
  540. ca2 := NewCloud(StorageCategory, "ca2", *windows[0].start, *windows[0].end, windows[0])
  541. ca2.Cost = 4.0
  542. ca2.SetAdjustment(1.0)
  543. ca3 := ca1.Add(ca2)
  544. // Check that the sums and properties are correct
  545. if ca3.TotalCost() != 15.0 {
  546. t.Fatalf("Any.Add: expected %f; got %f", 15.0, ca3.TotalCost())
  547. }
  548. if ca3.GetAdjustment() != 2.0 {
  549. t.Fatalf("Any.Add: expected %f; got %f", 2.0, ca3.GetAdjustment())
  550. }
  551. if ca3.Type() != CloudAssetType {
  552. t.Fatalf("Any.Add: expected %s; got %s", CloudAssetType, ca3.Type())
  553. }
  554. // Check that the original assets are unchanged
  555. if ca1.TotalCost() != 10.0 {
  556. t.Fatalf("Any.Add: expected %f; got %f", 10.0, ca1.TotalCost())
  557. }
  558. if ca1.Adjustment != 1.0 {
  559. t.Fatalf("Any.Add: expected %f; got %f", 1.0, ca1.Adjustment)
  560. }
  561. if ca2.TotalCost() != 5.0 {
  562. t.Fatalf("Any.Add: expected %f; got %f", 5.0, ca2.TotalCost())
  563. }
  564. if ca2.Adjustment != 1.0 {
  565. t.Fatalf("Any.Add: expected %f; got %f", 1.0, ca2.Adjustment)
  566. }
  567. }
  568. func TestCloudAny_Clone(t *testing.T) {
  569. // TODO
  570. }
  571. func TestAssetSet_AggregateBy(t *testing.T) {
  572. endYesterday := time.Now().UTC().Truncate(day)
  573. startYesterday := endYesterday.Add(-day)
  574. window := NewWindow(&startYesterday, &endYesterday)
  575. // Scenarios to test:
  576. // 1 Single-aggregation
  577. // 1a []AssetProperty=[Cluster]
  578. // 1b []AssetProperty=[Type]
  579. // 1c []AssetProperty=[Nil]
  580. // 1d []AssetProperty=nil
  581. // 1e aggregateBy []string=["label:test"]
  582. // 2 Multi-aggregation
  583. // 2a []AssetProperty=[Cluster,Type]
  584. // 3 Share resources
  585. // 3a Shared hourly cost > 0.0
  586. // Definitions and set-up:
  587. var as *AssetSet
  588. var err error
  589. // Tests:
  590. // 1 Single-aggregation
  591. // 1a []AssetProperty=[Cluster]
  592. as = GenerateMockAssetSet(startYesterday)
  593. err = as.AggregateBy([]string{string(AssetClusterProp)}, nil)
  594. if err != nil {
  595. t.Fatalf("AssetSet.AggregateBy: unexpected error: %s", err)
  596. }
  597. assertAssetSet(t, as, "1a", window, map[string]float64{
  598. "cluster1": 26.0,
  599. "cluster2": 15.0,
  600. "cluster3": 19.0,
  601. }, nil)
  602. // 1b []AssetProperty=[Type]
  603. as = GenerateMockAssetSet(startYesterday)
  604. err = as.AggregateBy([]string{string(AssetTypeProp)}, nil)
  605. if err != nil {
  606. t.Fatalf("AssetSet.AggregateBy: unexpected error: %s", err)
  607. }
  608. assertAssetSet(t, as, "1b", window, map[string]float64{
  609. "Node": 49.0,
  610. "Disk": 8.0,
  611. "ClusterManagement": 3.0,
  612. }, nil)
  613. // 1c []AssetProperty=[Nil]
  614. as = GenerateMockAssetSet(startYesterday)
  615. err = as.AggregateBy([]string{}, nil)
  616. if err != nil {
  617. t.Fatalf("AssetSet.AggregateBy: unexpected error: %s", err)
  618. }
  619. assertAssetSet(t, as, "1c", window, map[string]float64{
  620. "": 60.0,
  621. }, nil)
  622. // 1d []AssetProperty=nil
  623. as = GenerateMockAssetSet(startYesterday)
  624. err = as.AggregateBy(nil, nil)
  625. if err != nil {
  626. t.Fatalf("AssetSet.AggregateBy: unexpected error: %s", err)
  627. }
  628. assertAssetSet(t, as, "1d", window, map[string]float64{
  629. "__undefined__/__undefined__/__undefined__/Compute/cluster1/Node/Kubernetes/gcp-node1/node1": 7.00,
  630. "__undefined__/__undefined__/__undefined__/Compute/cluster1/Node/Kubernetes/gcp-node2/node2": 5.50,
  631. "__undefined__/__undefined__/__undefined__/Compute/cluster1/Node/Kubernetes/gcp-node3/node3": 6.50,
  632. "__undefined__/__undefined__/__undefined__/Storage/cluster1/Disk/Kubernetes/gcp-disk1/disk1": 2.50,
  633. "__undefined__/__undefined__/__undefined__/Storage/cluster1/Disk/Kubernetes/gcp-disk2/disk2": 1.50,
  634. "GCP/__undefined__/__undefined__/Management/cluster1/ClusterManagement/Kubernetes/__undefined__/__undefined__": 3.00,
  635. "__undefined__/__undefined__/__undefined__/Compute/cluster2/Node/Kubernetes/gcp-node4/node4": 11.00,
  636. "__undefined__/__undefined__/__undefined__/Storage/cluster2/Disk/Kubernetes/gcp-disk3/disk3": 2.50,
  637. "__undefined__/__undefined__/__undefined__/Storage/cluster2/Disk/Kubernetes/gcp-disk4/disk4": 1.50,
  638. "GCP/__undefined__/__undefined__/Management/cluster2/ClusterManagement/Kubernetes/__undefined__/__undefined__": 0.00,
  639. "__undefined__/__undefined__/__undefined__/Compute/cluster3/Node/Kubernetes/aws-node5/node5": 19.00,
  640. }, nil)
  641. // 1e aggregateBy []string=["label:test"]
  642. as = GenerateMockAssetSet(startYesterday)
  643. err = as.AggregateBy([]string{"label:test"}, nil)
  644. if err != nil {
  645. t.Fatalf("AssetSet.AggregateBy: unexpected error: %s", err)
  646. }
  647. assertAssetSet(t, as, "1e", window, map[string]float64{
  648. "__undefined__": 53.00,
  649. "test=test": 7.00,
  650. }, nil)
  651. // 2 Multi-aggregation
  652. // 2a []AssetProperty=[Cluster,Type]
  653. as = GenerateMockAssetSet(startYesterday)
  654. err = as.AggregateBy([]string{string(AssetClusterProp), string(AssetTypeProp)}, nil)
  655. if err != nil {
  656. t.Fatalf("AssetSet.AggregateBy: unexpected error: %s", err)
  657. }
  658. assertAssetSet(t, as, "2a", window, map[string]float64{
  659. "cluster1/Node": 19.0,
  660. "cluster1/Disk": 4.0,
  661. "cluster1/ClusterManagement": 3.0,
  662. "cluster2/Node": 11.0,
  663. "cluster2/Disk": 4.0,
  664. "cluster2/ClusterManagement": 0.0,
  665. "cluster3/Node": 19.0,
  666. }, nil)
  667. // 3 Share resources
  668. // 3a Shared hourly cost > 0.0
  669. as = GenerateMockAssetSet(startYesterday)
  670. err = as.AggregateBy([]string{string(AssetTypeProp)}, &AssetAggregationOptions{
  671. SharedHourlyCosts: map[string]float64{"shared1": 0.5},
  672. })
  673. if err != nil {
  674. t.Fatalf("AssetSet.AggregateBy: unexpected error: %s", err)
  675. }
  676. assertAssetSet(t, as, "1a", window, map[string]float64{
  677. "Node": 49.0,
  678. "Disk": 8.0,
  679. "ClusterManagement": 3.0,
  680. "Shared": 12.0,
  681. }, nil)
  682. }
  683. func TestAssetSet_FindMatch(t *testing.T) {
  684. endYesterday := time.Now().UTC().Truncate(day)
  685. startYesterday := endYesterday.Add(-day)
  686. s, e := startYesterday, endYesterday
  687. w := NewWindow(&s, &e)
  688. var query, match Asset
  689. var as *AssetSet
  690. var err error
  691. // Assert success of a simple match of Type and ProviderID
  692. as = GenerateMockAssetSet(startYesterday)
  693. query = NewNode("", "", "gcp-node3", s, e, w)
  694. match, err = as.FindMatch(query, []string{string(AssetTypeProp), string(AssetProviderIDProp)})
  695. if err != nil {
  696. t.Fatalf("AssetSet.FindMatch: unexpected error: %s", err)
  697. }
  698. // Assert error of a simple non-match of Type and ProviderID
  699. as = GenerateMockAssetSet(startYesterday)
  700. query = NewNode("", "", "aws-node3", s, e, w)
  701. match, err = as.FindMatch(query, []string{string(AssetTypeProp), string(AssetProviderIDProp)})
  702. if err == nil {
  703. t.Fatalf("AssetSet.FindMatch: expected error (no match); found %s", match)
  704. }
  705. // Assert error of matching ProviderID, but not Type
  706. as = GenerateMockAssetSet(startYesterday)
  707. query = NewCloud(ComputeCategory, "gcp-node3", s, e, w)
  708. match, err = as.FindMatch(query, []string{string(AssetTypeProp), string(AssetProviderIDProp)})
  709. if err == nil {
  710. t.Fatalf("AssetSet.FindMatch: expected error (no match); found %s", match)
  711. }
  712. }
  713. // Asserts that all Assets within an AssetSet have a Window that
  714. // matches that of the AssetSet.
  715. func TestAssetSet_InsertMatchingWindow(t *testing.T) {
  716. setStart := time.Now().Round(time.Hour)
  717. setEnd := setStart.Add(1 * time.Hour)
  718. a1WindowStart := setStart.Add(5 * time.Minute)
  719. a1WindowEnd := setStart.Add(50 * time.Minute)
  720. a2WindowStart := setStart.Add(17 * time.Minute)
  721. a2WindowEnd := setStart.Add(34 * time.Minute)
  722. a1 := &Node{}
  723. a1.SetProperties(&AssetProperties{
  724. Name: "asset-1",
  725. })
  726. a1.Window = NewClosedWindow(a1WindowStart, a1WindowEnd)
  727. a2 := &Disk{}
  728. a2.SetProperties(&AssetProperties{
  729. Name: "asset-2",
  730. })
  731. a2.Window = NewClosedWindow(a2WindowStart, a2WindowEnd)
  732. as := NewAssetSet(setStart, setEnd)
  733. as.Insert(a1)
  734. as.Insert(a2)
  735. if as.Length() != 2 {
  736. t.Errorf("AS length got %d, expected %d", as.Length(), 2)
  737. }
  738. for _, a := range as.Assets {
  739. if !(*a.GetWindow().Start()).Equal(setStart) {
  740. t.Errorf("Asset %s window start is %s, expected %s", a.GetProperties().Name, *a.GetWindow().Start(), setStart)
  741. }
  742. if !(*a.GetWindow().End()).Equal(setEnd) {
  743. t.Errorf("Asset %s window end is %s, expected %s", a.GetProperties().Name, *a.GetWindow().End(), setEnd)
  744. }
  745. }
  746. }
  747. func TestAssetSet_ReconciliationMatchMap(t *testing.T) {
  748. endYesterday := time.Now().UTC().Truncate(day)
  749. startYesterday := endYesterday.Add(-day)
  750. as := GenerateMockAssetSet(startYesterday)
  751. matchMap := as.ReconciliationMatchMap()
  752. // Determine the number of assets by provider ID
  753. assetCountByProviderId := make(map[string]int, len(matchMap))
  754. for _, a := range as.Assets {
  755. if a == nil || a.GetProperties() == nil || a.GetProperties().ProviderID == "" {
  756. return
  757. }
  758. if _, ok := assetCountByProviderId[a.GetProperties().ProviderID]; !ok {
  759. assetCountByProviderId[a.GetProperties().ProviderID] = 0
  760. }
  761. assetCountByProviderId[a.GetProperties().ProviderID] += 1
  762. }
  763. for k, count := range assetCountByProviderId {
  764. if len(matchMap[k]) != count {
  765. t.Errorf("AssetSet.ReconciliationMatchMap: incorrect asset count for provider id: %s", k)
  766. }
  767. }
  768. }
  769. func TestAssetSetRange_Accumulate(t *testing.T) {
  770. endYesterday := time.Now().UTC().Truncate(day)
  771. startYesterday := endYesterday.Add(-day)
  772. startD2 := startYesterday
  773. startD1 := startD2.Add(-day)
  774. startD0 := startD1.Add(-day)
  775. window := NewWindow(&startD0, &endYesterday)
  776. var asr *AssetSetRange
  777. var as *AssetSet
  778. var err error
  779. asr = NewAssetSetRange(
  780. GenerateMockAssetSet(startD0),
  781. GenerateMockAssetSet(startD1),
  782. GenerateMockAssetSet(startD2),
  783. )
  784. err = asr.AggregateBy(nil, nil)
  785. as, err = asr.Accumulate()
  786. if err != nil {
  787. t.Fatalf("AssetSetRange.AggregateBy: unexpected error: %s", err)
  788. }
  789. assertAssetSet(t, as, "1a", window, map[string]float64{
  790. "__undefined__/__undefined__/__undefined__/Compute/cluster1/Node/Kubernetes/gcp-node1/node1": 21.00,
  791. "__undefined__/__undefined__/__undefined__/Compute/cluster1/Node/Kubernetes/gcp-node2/node2": 16.50,
  792. "__undefined__/__undefined__/__undefined__/Compute/cluster1/Node/Kubernetes/gcp-node3/node3": 19.50,
  793. "__undefined__/__undefined__/__undefined__/Storage/cluster1/Disk/Kubernetes/gcp-disk1/disk1": 7.50,
  794. "__undefined__/__undefined__/__undefined__/Storage/cluster1/Disk/Kubernetes/gcp-disk2/disk2": 4.50,
  795. "GCP/__undefined__/__undefined__/Management/cluster1/ClusterManagement/Kubernetes/__undefined__/__undefined__": 9.00,
  796. "__undefined__/__undefined__/__undefined__/Compute/cluster2/Node/Kubernetes/gcp-node4/node4": 33.00,
  797. "__undefined__/__undefined__/__undefined__/Storage/cluster2/Disk/Kubernetes/gcp-disk3/disk3": 7.50,
  798. "__undefined__/__undefined__/__undefined__/Storage/cluster2/Disk/Kubernetes/gcp-disk4/disk4": 4.50,
  799. "GCP/__undefined__/__undefined__/Management/cluster2/ClusterManagement/Kubernetes/__undefined__/__undefined__": 0.00,
  800. "__undefined__/__undefined__/__undefined__/Compute/cluster3/Node/Kubernetes/aws-node5/node5": 57.00,
  801. }, nil)
  802. asr = NewAssetSetRange(
  803. GenerateMockAssetSet(startD0),
  804. GenerateMockAssetSet(startD1),
  805. GenerateMockAssetSet(startD2),
  806. )
  807. err = asr.AggregateBy([]string{}, nil)
  808. as, err = asr.Accumulate()
  809. if err != nil {
  810. t.Fatalf("AssetSetRange.AggregateBy: unexpected error: %s", err)
  811. }
  812. assertAssetSet(t, as, "1b", window, map[string]float64{
  813. "": 180.00,
  814. }, nil)
  815. asr = NewAssetSetRange(
  816. GenerateMockAssetSet(startD0),
  817. GenerateMockAssetSet(startD1),
  818. GenerateMockAssetSet(startD2),
  819. )
  820. err = asr.AggregateBy([]string{string(AssetTypeProp)}, nil)
  821. if err != nil {
  822. t.Fatalf("AssetSetRange.AggregateBy: unexpected error: %s", err)
  823. }
  824. as, err = asr.Accumulate()
  825. if err != nil {
  826. t.Fatalf("AssetSetRange.AggregateBy: unexpected error: %s", err)
  827. }
  828. assertAssetSet(t, as, "1c", window, map[string]float64{
  829. "Node": 147.0,
  830. "Disk": 24.0,
  831. "ClusterManagement": 9.0,
  832. }, nil)
  833. asr = NewAssetSetRange(
  834. GenerateMockAssetSet(startD0),
  835. GenerateMockAssetSet(startD1),
  836. GenerateMockAssetSet(startD2),
  837. )
  838. err = asr.AggregateBy([]string{string(AssetClusterProp)}, nil)
  839. if err != nil {
  840. t.Fatalf("AssetSetRange.AggregateBy: unexpected error: %s", err)
  841. }
  842. as, err = asr.Accumulate()
  843. if err != nil {
  844. t.Fatalf("AssetSetRange.AggregateBy: unexpected error: %s", err)
  845. }
  846. assertAssetSet(t, as, "1c", window, map[string]float64{
  847. "cluster1": 78.0,
  848. "cluster2": 45.0,
  849. "cluster3": 57.0,
  850. }, nil)
  851. // Accumulation with aggregation should work, even when the first AssetSet
  852. // is empty (this was previously an issue)
  853. asr = NewAssetSetRange(
  854. NewAssetSet(startD0, startD1),
  855. GenerateMockAssetSet(startD1),
  856. GenerateMockAssetSet(startD2),
  857. )
  858. err = asr.AggregateBy([]string{string(AssetTypeProp)}, nil)
  859. as, err = asr.Accumulate()
  860. if err != nil {
  861. t.Fatalf("AssetSetRange.AggregateBy: unexpected error: %s", err)
  862. }
  863. assertAssetSet(t, as, "1d", window, map[string]float64{
  864. "Node": 98.00,
  865. "Disk": 16.00,
  866. "ClusterManagement": 6.00,
  867. }, nil)
  868. }
  869. func TestAssetToExternalAllocation(t *testing.T) {
  870. var asset Asset
  871. var alloc *Allocation
  872. var err error
  873. labelConfig := NewLabelConfig()
  874. _, err = AssetToExternalAllocation(asset, []string{"namespace"}, labelConfig)
  875. if err == nil {
  876. t.Fatalf("expected error due to nil asset; no error returned")
  877. }
  878. // Consider this Asset:
  879. // Cloud {
  880. // TotalCost: 10.00,
  881. // Labels{
  882. // "kubernetes_namespace":"monitoring",
  883. // "env":"prod"
  884. // }
  885. // }
  886. cloud := NewCloud(ComputeCategory, "abc123", start1, start2, windows[0])
  887. cloud.SetLabels(map[string]string{
  888. "kubernetes_namespace": "monitoring",
  889. "env": "prod",
  890. "app": "cost-analyzer",
  891. "kubernetes_label_app": "app",
  892. "kubernetes_label_department": "department",
  893. "kubernetes_label_env": "env",
  894. "kubernetes_label_owner": "owner",
  895. "kubernetes_label_team": "team",
  896. })
  897. cloud.Cost = 10.00
  898. asset = cloud
  899. _, err = AssetToExternalAllocation(asset, []string{"namespace"}, nil)
  900. if err != nil {
  901. t.Fatalf("unexpected error: %s", err)
  902. }
  903. _, err = AssetToExternalAllocation(asset, nil, nil)
  904. if err == nil {
  905. t.Fatalf("expected error due to nil aggregateBy; no error returned")
  906. }
  907. // Given the following parameters, we expect to return:
  908. //
  909. // 1) single-prop full match
  910. // aggregateBy = ["namespace"]
  911. // allocationPropertyLabels = {"namespace":"kubernetes_namespace"}
  912. // => Allocation{Name: "monitoring", ExternalCost: 10.00, TotalCost: 10.00}, nil
  913. //
  914. // 2) multi-prop full match
  915. // aggregateBy = ["namespace", "label:env"]
  916. // allocationPropertyLabels = {"namespace":"kubernetes_namespace"}
  917. // => Allocation{Name: "monitoring/env=prod", ExternalCost: 10.00, TotalCost: 10.00}, nil
  918. //
  919. // 3) multi-prop partial match
  920. // aggregateBy = ["namespace", "label:foo"]
  921. // allocationPropertyLabels = {"namespace":"kubernetes_namespace"}
  922. // => Allocation{Name: "monitoring/__unallocated__", ExternalCost: 10.00, TotalCost: 10.00}, nil
  923. //
  924. // 4) label alias match(es)
  925. // aggregateBy = ["product", "deployment", "environment", "owner", "team"]
  926. // allocationPropertyLabels = {"namespace":"kubernetes_namespace"}
  927. // => Allocation{Name: "app/department/env/owner/team", ExternalCost: 10.00, TotalCost: 10.00}, nil
  928. //
  929. // 5) no match
  930. // aggregateBy = ["cluster"]
  931. // allocationPropertyLabels = {"namespace":"kubernetes_namespace"}
  932. // => nil, err
  933. // 1) single-prop full match
  934. alloc, err = AssetToExternalAllocation(asset, []string{"namespace"}, nil)
  935. if err != nil {
  936. t.Fatalf("unexpected error: %s", err)
  937. }
  938. if alloc.Name != "monitoring/__external__" {
  939. t.Fatalf("expected external allocation with name '%s'; got '%s'", "monitoring/__external__", alloc.Name)
  940. }
  941. if ns := alloc.Properties.Namespace; ns != "monitoring" {
  942. t.Fatalf("expected external allocation with AllocationProperties.Namespace '%s'; got '%s'", "monitoring", ns)
  943. }
  944. if alloc.ExternalCost != 10.00 {
  945. t.Fatalf("expected external allocation with ExternalCost %f; got %f", 10.00, alloc.ExternalCost)
  946. }
  947. if alloc.TotalCost() != 10.00 {
  948. t.Fatalf("expected external allocation with TotalCost %f; got %f", 10.00, alloc.TotalCost())
  949. }
  950. // 2) multi-prop full match
  951. alloc, err = AssetToExternalAllocation(asset, []string{"namespace", "label:env"}, nil)
  952. if err != nil {
  953. t.Fatalf("unexpected error: %s", err)
  954. }
  955. if alloc.Name != "monitoring/env=prod/__external__" {
  956. t.Fatalf("expected external allocation with name '%s'; got '%s'", "monitoring/env=prod/__external__", alloc.Name)
  957. }
  958. if ns := alloc.Properties.Namespace; ns != "monitoring" {
  959. t.Fatalf("expected external allocation with AllocationProperties.Namespace '%s'; got '%s' (%s)", "monitoring", ns, err)
  960. }
  961. if ls := alloc.Properties.Labels; len(ls) == 0 || ls["env"] != "prod" {
  962. t.Fatalf("expected external allocation with AllocationProperties.Labels[\"env\"] '%s'; got '%s' (%s)", "prod", ls["env"], err)
  963. }
  964. if alloc.ExternalCost != 10.00 {
  965. t.Fatalf("expected external allocation with ExternalCost %f; got %f", 10.00, alloc.ExternalCost)
  966. }
  967. if alloc.TotalCost() != 10.00 {
  968. t.Fatalf("expected external allocation with TotalCost %f; got %f", 10.00, alloc.TotalCost())
  969. }
  970. // 3) multi-prop partial match
  971. alloc, err = AssetToExternalAllocation(asset, []string{"namespace", "label:foo"}, nil)
  972. if err != nil {
  973. t.Fatalf("unexpected error: %s", err)
  974. }
  975. if alloc.Name != "monitoring/__unallocated__/__external__" {
  976. t.Fatalf("expected external allocation with name '%s'; got '%s'", "monitoring/__unallocated__/__external__", alloc.Name)
  977. }
  978. if ns := alloc.Properties.Namespace; ns != "monitoring" {
  979. t.Fatalf("expected external allocation with AllocationProperties.Namespace '%s'; got '%s' (%s)", "monitoring", ns, err)
  980. }
  981. if alloc.ExternalCost != 10.00 {
  982. t.Fatalf("expected external allocation with ExternalCost %f; got %f", 10.00, alloc.ExternalCost)
  983. }
  984. if alloc.TotalCost() != 10.00 {
  985. t.Fatalf("expected external allocation with TotalCost %f; got %f", 10.00, alloc.TotalCost())
  986. }
  987. // 4) label alias match(es)
  988. alloc, err = AssetToExternalAllocation(asset, []string{"product", "department", "environment", "owner", "team"}, nil)
  989. if err != nil {
  990. t.Fatalf("unexpected error: %s", err)
  991. }
  992. if alloc.Name != "app/department/env/owner/team/__external__" {
  993. t.Fatalf("expected external allocation with name '%s'; got '%s'", "app/department/env/owner/team/__external__", alloc.Name)
  994. }
  995. if alloc.Properties.Labels[labelConfig.ProductLabel] != "app" {
  996. t.Fatalf("expected external allocation with label %s equal to %s; got %s", labelConfig.ProductLabel, "app", alloc.Properties.Labels[labelConfig.ProductLabel])
  997. }
  998. if alloc.Properties.Labels[labelConfig.DepartmentLabel] != "department" {
  999. t.Fatalf("expected external allocation with label %s equal to %s; got %s", labelConfig.DepartmentLabel, "department", alloc.Properties.Labels[labelConfig.DepartmentLabel])
  1000. }
  1001. if alloc.Properties.Labels[labelConfig.EnvironmentLabel] != "env" {
  1002. t.Fatalf("expected external allocation with label %s equal to %s; got %s", labelConfig.EnvironmentLabel, "env", alloc.Properties.Labels[labelConfig.EnvironmentLabel])
  1003. }
  1004. if alloc.Properties.Labels[labelConfig.OwnerLabel] != "owner" {
  1005. t.Fatalf("expected external allocation with label %s equal to %s; got %s", labelConfig.OwnerLabel, "owner", alloc.Properties.Labels[labelConfig.OwnerLabel])
  1006. }
  1007. if alloc.Properties.Labels[labelConfig.TeamLabel] != "team" {
  1008. t.Fatalf("expected external allocation with label %s equal to %s; got %s", labelConfig.TeamLabel, "team", alloc.Properties.Labels[labelConfig.TeamLabel])
  1009. }
  1010. if alloc.ExternalCost != 10.00 {
  1011. t.Fatalf("expected external allocation with ExternalCost %f; got %f", 10.00, alloc.ExternalCost)
  1012. }
  1013. if alloc.TotalCost() != 10.00 {
  1014. t.Fatalf("expected external allocation with TotalCost %f; got %f", 10.00, alloc.TotalCost())
  1015. }
  1016. // 5) no match
  1017. _, err = AssetToExternalAllocation(asset, []string{"cluster"}, nil)
  1018. if err == nil {
  1019. t.Fatalf("expected 'no match' error")
  1020. }
  1021. // other cases
  1022. alloc, err = AssetToExternalAllocation(asset, []string{"namespace", "label:app"}, nil)
  1023. if err != nil {
  1024. t.Fatalf("unexpected error: %s", err)
  1025. }
  1026. if alloc.ExternalCost != 10.00 {
  1027. t.Fatalf("expected external allocation with ExternalCost %f; got %f", 10.00, alloc.ExternalCost)
  1028. }
  1029. if alloc.TotalCost() != 10.00 {
  1030. t.Fatalf("expected external allocation with TotalCost %f; got %f", 10.00, alloc.TotalCost())
  1031. }
  1032. }
  1033. func TestAssetSetRange_Start(t *testing.T) {
  1034. tests := []struct {
  1035. name string
  1036. arg *AssetSetRange
  1037. expectError bool
  1038. expected time.Time
  1039. }{
  1040. {
  1041. name: "Empty ASR",
  1042. arg: nil,
  1043. expectError: true,
  1044. },
  1045. {
  1046. name: "Single asset",
  1047. arg: &AssetSetRange{
  1048. Assets: []*AssetSet{
  1049. {
  1050. Assets: map[string]Asset{
  1051. "a": &Node{
  1052. Start: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  1053. },
  1054. },
  1055. },
  1056. },
  1057. },
  1058. expected: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  1059. },
  1060. {
  1061. name: "Two assets",
  1062. arg: &AssetSetRange{
  1063. Assets: []*AssetSet{
  1064. {
  1065. Assets: map[string]Asset{
  1066. "a": &Node{
  1067. Start: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  1068. },
  1069. "b": &Node{
  1070. Start: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  1071. },
  1072. },
  1073. },
  1074. },
  1075. },
  1076. expected: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  1077. },
  1078. {
  1079. name: "Two AssetSets",
  1080. arg: &AssetSetRange{
  1081. Assets: []*AssetSet{
  1082. {
  1083. Assets: map[string]Asset{
  1084. "a": &Node{
  1085. Start: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  1086. },
  1087. },
  1088. },
  1089. {
  1090. Assets: map[string]Asset{
  1091. "b": &Node{
  1092. Start: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  1093. },
  1094. },
  1095. },
  1096. },
  1097. },
  1098. expected: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  1099. },
  1100. }
  1101. for _, test := range tests {
  1102. result, err := test.arg.Start()
  1103. if test.expectError && err != nil {
  1104. continue
  1105. }
  1106. if test.expectError && err == nil {
  1107. t.Errorf("%s: expected error and got none", test.name)
  1108. } else if result != test.expected {
  1109. t.Errorf("%s: expected %s but got %s", test.name, test.expected, result)
  1110. }
  1111. }
  1112. }
  1113. func TestAssetSetRange_End(t *testing.T) {
  1114. tests := []struct {
  1115. name string
  1116. arg *AssetSetRange
  1117. expectError bool
  1118. expected time.Time
  1119. }{
  1120. {
  1121. name: "Empty ASR",
  1122. arg: nil,
  1123. expectError: true,
  1124. },
  1125. {
  1126. name: "Single asset",
  1127. arg: &AssetSetRange{
  1128. Assets: []*AssetSet{
  1129. {
  1130. Assets: map[string]Asset{
  1131. "a": &Node{
  1132. End: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  1133. },
  1134. },
  1135. },
  1136. },
  1137. },
  1138. expected: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  1139. },
  1140. {
  1141. name: "Two assets",
  1142. arg: &AssetSetRange{
  1143. Assets: []*AssetSet{
  1144. {
  1145. Assets: map[string]Asset{
  1146. "a": &Node{
  1147. End: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  1148. },
  1149. "b": &Node{
  1150. End: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  1151. },
  1152. },
  1153. },
  1154. },
  1155. },
  1156. expected: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  1157. },
  1158. {
  1159. name: "Two AssetSets",
  1160. arg: &AssetSetRange{
  1161. Assets: []*AssetSet{
  1162. {
  1163. Assets: map[string]Asset{
  1164. "a": &Node{
  1165. End: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  1166. },
  1167. },
  1168. },
  1169. {
  1170. Assets: map[string]Asset{
  1171. "b": &Node{
  1172. End: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  1173. },
  1174. },
  1175. },
  1176. },
  1177. },
  1178. expected: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  1179. },
  1180. }
  1181. for _, test := range tests {
  1182. result, err := test.arg.End()
  1183. if test.expectError && err != nil {
  1184. continue
  1185. }
  1186. if test.expectError && err == nil {
  1187. t.Errorf("%s: expected error and got none", test.name)
  1188. } else if result != test.expected {
  1189. t.Errorf("%s: expected %s but got %s", test.name, test.expected, result)
  1190. }
  1191. }
  1192. }
  1193. func TestAssetSetRange_Minutes(t *testing.T) {
  1194. tests := []struct {
  1195. name string
  1196. arg *AssetSetRange
  1197. expected float64
  1198. }{
  1199. {
  1200. name: "Empty ASR",
  1201. arg: nil,
  1202. expected: 0,
  1203. },
  1204. {
  1205. name: "Single asset",
  1206. arg: &AssetSetRange{
  1207. Assets: []*AssetSet{
  1208. {
  1209. Assets: map[string]Asset{
  1210. "a": &Node{
  1211. Start: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  1212. End: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  1213. },
  1214. },
  1215. },
  1216. },
  1217. },
  1218. expected: 24 * 60,
  1219. },
  1220. {
  1221. name: "Two assets",
  1222. arg: &AssetSetRange{
  1223. Assets: []*AssetSet{
  1224. {
  1225. Assets: map[string]Asset{
  1226. "a": &Node{
  1227. Start: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  1228. End: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  1229. },
  1230. "b": &Node{
  1231. Start: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  1232. End: time.Date(1970, 1, 3, 0, 0, 0, 0, time.UTC),
  1233. },
  1234. },
  1235. },
  1236. },
  1237. },
  1238. expected: 2 * 24 * 60,
  1239. },
  1240. {
  1241. name: "Two AssetSets",
  1242. arg: &AssetSetRange{
  1243. Assets: []*AssetSet{
  1244. {
  1245. Assets: map[string]Asset{
  1246. "a": &Node{
  1247. Start: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  1248. End: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  1249. },
  1250. },
  1251. },
  1252. {
  1253. Assets: map[string]Asset{
  1254. "b": &Node{
  1255. Start: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  1256. End: time.Date(1970, 1, 3, 0, 0, 0, 0, time.UTC),
  1257. },
  1258. },
  1259. },
  1260. },
  1261. },
  1262. expected: 2 * 24 * 60,
  1263. },
  1264. }
  1265. for _, test := range tests {
  1266. result := test.arg.Minutes()
  1267. if result != test.expected {
  1268. t.Errorf("%s: expected %f but got %f", test.name, test.expected, result)
  1269. }
  1270. }
  1271. }
  1272. func TestAssetSetRange_MarshalJSON(t *testing.T) {
  1273. tests := []struct {
  1274. name string
  1275. arg *AssetSetRange
  1276. expected *AssetSetRange
  1277. }{
  1278. {
  1279. name: "Nil ASR",
  1280. arg: nil,
  1281. },
  1282. {
  1283. name: "Nil AS in ASR",
  1284. arg: NewAssetSetRange(nil),
  1285. },
  1286. {
  1287. name: "Normal ASR",
  1288. arg: &AssetSetRange{
  1289. Assets: []*AssetSet{
  1290. {
  1291. Assets: map[string]Asset{
  1292. "a": &Any{
  1293. Start: time.Now().UTC().Truncate(day),
  1294. },
  1295. },
  1296. },
  1297. },
  1298. },
  1299. },
  1300. }
  1301. for _, test := range tests {
  1302. bytes, err := json.Marshal(test.arg)
  1303. if err != nil {
  1304. t.Fatalf("ASR Marshal: test %s, unexpected error: %s", test.name, err)
  1305. }
  1306. var testASR []*AssetSet
  1307. marshaled := &testASR
  1308. err = json.Unmarshal(bytes, marshaled)
  1309. if err != nil {
  1310. t.Fatalf("ASR Unmarshal: test %s: unexpected error: %s", test.name, err)
  1311. }
  1312. if test.arg.Length() != len(testASR) {
  1313. t.Fatalf("ASR Unmarshal: test %s: length mutated in encoding: expected %d but got %d", test.name, test.arg.Length(), len(testASR))
  1314. }
  1315. // asset don't unmarshal back from json
  1316. }
  1317. }