asset_test.go 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016
  1. package opencost
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "math"
  6. "reflect"
  7. "testing"
  8. "time"
  9. "github.com/opencost/opencost/core/pkg/util"
  10. )
  11. var start1 = time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)
  12. var start2 = start1.Add(day)
  13. var start3 = start2.Add(day)
  14. var start4 = start2.Add(day)
  15. var windows = []Window{
  16. NewWindow(&start1, &start2),
  17. NewWindow(&start2, &start3),
  18. NewWindow(&start3, &start4),
  19. }
  20. func assertAssetSet(t *testing.T, as *AssetSet, msg string, window Window, exps map[string]float64, err error) {
  21. if err != nil {
  22. t.Fatalf("AssetSet.AggregateBy[%s]: unexpected error: %s", msg, err)
  23. }
  24. if as.Length() != len(exps) {
  25. t.Fatalf("AssetSet.AggregateBy[%s]: expected set of length %d, actual %d", msg, len(exps), as.Length())
  26. }
  27. if !as.Window.Equal(window) {
  28. t.Fatalf("AssetSet.AggregateBy[%s]: expected window %s, actual %s", msg, window, as.Window)
  29. }
  30. for key, a := range as.Assets {
  31. if exp, ok := exps[key]; ok {
  32. if math.Round(a.TotalCost()*100) != math.Round(exp*100) {
  33. t.Fatalf("AssetSet.AggregateBy[%s]: key %s expected total cost %.2f, actual %.2f", msg, key, exp, a.TotalCost())
  34. }
  35. if !a.GetWindow().Equal(window) {
  36. t.Fatalf("AssetSet.AggregateBy[%s]: key %s expected window %s, actual %s", msg, key, window, a.GetWindow())
  37. }
  38. } else {
  39. t.Fatalf("AssetSet.AggregateBy[%s]: unexpected asset: %s", msg, key)
  40. }
  41. }
  42. }
  43. func printAssetSet(msg string, as *AssetSet) {
  44. fmt.Printf("--- %s ---\n", msg)
  45. for key, a := range as.Assets {
  46. fmt.Printf(" > %s: %s\n", key, a)
  47. }
  48. }
  49. func TestAny_Add(t *testing.T) {
  50. any1 := NewAsset(*windows[0].start, *windows[0].end, windows[0])
  51. any1.SetProperties(&AssetProperties{
  52. Name: "any1",
  53. Cluster: "cluster1",
  54. ProviderID: "any1",
  55. })
  56. any1.Cost = 9.0
  57. any1.SetAdjustment(1.0)
  58. any2 := NewAsset(*windows[0].start, *windows[0].end, windows[0])
  59. any2.SetProperties(&AssetProperties{
  60. Name: "any2",
  61. Cluster: "cluster1",
  62. ProviderID: "any2",
  63. })
  64. any2.Cost = 4.0
  65. any2.SetAdjustment(1.0)
  66. any3 := any1.Add(any2)
  67. // Check that the sums and properties are correct
  68. if any3.TotalCost() != 15.0 {
  69. t.Fatalf("Any.Add: expected %f; got %f", 15.0, any3.TotalCost())
  70. }
  71. if any3.GetAdjustment() != 2.0 {
  72. t.Fatalf("Any.Add: expected %f; got %f", 2.0, any3.GetAdjustment())
  73. }
  74. if any3.GetProperties().Cluster != "cluster1" {
  75. t.Fatalf("Any.Add: expected %s; got %s", "cluster1", any3.GetProperties().Cluster)
  76. }
  77. if any3.Type() != AnyAssetType {
  78. t.Fatalf("Any.Add: expected %s; got %s", AnyAssetType, any3.Type())
  79. }
  80. if any3.GetProperties().ProviderID != "" {
  81. t.Fatalf("Any.Add: expected %s; got %s", "", any3.GetProperties().ProviderID)
  82. }
  83. if any3.GetProperties().Name != "" {
  84. t.Fatalf("Any.Add: expected %s; got %s", "", any3.GetProperties().Name)
  85. }
  86. // Check that the original assets are unchanged
  87. if any1.TotalCost() != 10.0 {
  88. t.Fatalf("Any.Add: expected %f; got %f", 10.0, any1.TotalCost())
  89. }
  90. if any1.Adjustment != 1.0 {
  91. t.Fatalf("Any.Add: expected %f; got %f", 1.0, any1.Adjustment)
  92. }
  93. if any2.TotalCost() != 5.0 {
  94. t.Fatalf("Any.Add: expected %f; got %f", 5.0, any2.TotalCost())
  95. }
  96. if any2.Adjustment != 1.0 {
  97. t.Fatalf("Any.Add: expected %f; got %f", 1.0, any2.Adjustment)
  98. }
  99. }
  100. func TestAny_Clone(t *testing.T) {
  101. any1 := NewAsset(*windows[0].start, *windows[0].end, windows[0])
  102. any1.SetProperties(&AssetProperties{
  103. Name: "any1",
  104. Cluster: "cluster1",
  105. ProviderID: "any1",
  106. })
  107. any1.Cost = 9.0
  108. any1.SetAdjustment(1.0)
  109. any2 := any1.Clone()
  110. any1.Cost = 18.0
  111. any1.SetAdjustment(2.0)
  112. // any2 should match any1, even after mutating any1
  113. if any2.TotalCost() != 10.0 {
  114. t.Fatalf("Any.Clone: expected %f; got %f", 10.0, any2.TotalCost())
  115. }
  116. if any2.GetAdjustment() != 1.0 {
  117. t.Fatalf("Any.Clone: expected %f; got %f", 1.0, any2.GetAdjustment())
  118. }
  119. }
  120. func TestAny_MarshalJSON(t *testing.T) {
  121. any1 := NewAsset(*windows[0].start, *windows[0].end, windows[0])
  122. any1.SetProperties(&AssetProperties{
  123. Name: "any1",
  124. Cluster: "cluster1",
  125. ProviderID: "any1",
  126. })
  127. any1.Cost = 9.0
  128. any1.SetAdjustment(1.0)
  129. _, err := json.Marshal(any1)
  130. if err != nil {
  131. t.Fatalf("Any.MarshalJSON: unexpected error: %s", err)
  132. }
  133. any2 := NewAsset(*windows[0].start, *windows[0].end, windows[0])
  134. any2.SetProperties(&AssetProperties{
  135. Name: "any2",
  136. Cluster: "cluster1",
  137. ProviderID: "any2",
  138. })
  139. any2.Cost = math.NaN()
  140. any2.SetAdjustment(1.0)
  141. _, err = json.Marshal(any2)
  142. if err != nil {
  143. t.Fatalf("Any.MarshalJSON: unexpected error: %s", err)
  144. }
  145. }
  146. func TestDisk_Add(t *testing.T) {
  147. // 1. aggregate: add size, local
  148. // 2. accumulate: don't add size, local
  149. hours := windows[0].Duration().Hours()
  150. // Aggregate: two disks, one window
  151. disk1 := NewDisk("disk1", "cluster1", "disk1", *windows[0].start, *windows[0].end, windows[0])
  152. disk1.ByteHours = 100.0 * gb * hours
  153. disk1.Cost = 9.0
  154. disk1.SetAdjustment(1.0)
  155. if disk1.Bytes() != 100.0*gb {
  156. t.Fatalf("Disk.Add: expected %f; got %f", 100.0*gb, disk1.Bytes())
  157. }
  158. disk2 := NewDisk("disk2", "cluster1", "disk2", *windows[0].start, *windows[0].end, windows[0])
  159. disk2.ByteHours = 60.0 * gb * hours
  160. disk2.Cost = 4.0
  161. disk2.Local = 1.0
  162. disk2.SetAdjustment(1.0)
  163. if disk2.Bytes() != 60.0*gb {
  164. t.Fatalf("Disk.Add: expected %f; got %f", 60.0*gb, disk2.Bytes())
  165. }
  166. diskT := disk1.Add(disk2).(*Disk)
  167. // Check that the sums and properties are correct
  168. if diskT.TotalCost() != 15.0 {
  169. t.Fatalf("Disk.Add: expected %f; got %f", 15.0, diskT.TotalCost())
  170. }
  171. if diskT.Adjustment != 2.0 {
  172. t.Fatalf("Disk.Add: expected %f; got %f", 2.0, diskT.Adjustment)
  173. }
  174. if diskT.Properties.Cluster != "cluster1" {
  175. t.Fatalf("Disk.Add: expected %s; got %s", "cluster1", diskT.Properties.Cluster)
  176. }
  177. if diskT.Type() != DiskAssetType {
  178. t.Fatalf("Disk.Add: expected %s; got %s", AnyAssetType, diskT.Type())
  179. }
  180. if diskT.Properties.ProviderID != "" {
  181. t.Fatalf("Disk.Add: expected %s; got %s", "", diskT.Properties.ProviderID)
  182. }
  183. if diskT.Properties.Name != "" {
  184. t.Fatalf("Disk.Add: expected %s; got %s", "", diskT.Properties.Name)
  185. }
  186. if diskT.Bytes() != 160.0*gb {
  187. t.Fatalf("Disk.Add: expected %f; got %f", 160.0*gb, diskT.Bytes())
  188. }
  189. if !util.IsApproximately(diskT.Local, 0.333333) {
  190. t.Fatalf("Disk.Add: expected %f; got %f", 0.333333, diskT.Local)
  191. }
  192. // Check that the original assets are unchanged
  193. if disk1.TotalCost() != 10.0 {
  194. t.Fatalf("Disk.Add: expected %f; got %f", 10.0, disk1.TotalCost())
  195. }
  196. if disk1.Adjustment != 1.0 {
  197. t.Fatalf("Disk.Add: expected %f; got %f", 1.0, disk1.Adjustment)
  198. }
  199. if disk1.Local != 0.0 {
  200. t.Fatalf("Disk.Add: expected %f; got %f", 0.0, disk1.Local)
  201. }
  202. if disk2.TotalCost() != 5.0 {
  203. t.Fatalf("Disk.Add: expected %f; got %f", 5.0, disk2.TotalCost())
  204. }
  205. if disk2.Adjustment != 1.0 {
  206. t.Fatalf("Disk.Add: expected %f; got %f", 1.0, disk2.Adjustment)
  207. }
  208. if disk2.Local != 1.0 {
  209. t.Fatalf("Disk.Add: expected %f; got %f", 1.0, disk2.Local)
  210. }
  211. disk3 := NewDisk("disk3", "cluster1", "disk3", *windows[0].start, *windows[0].end, windows[0])
  212. disk3.ByteHours = 0.0 * hours
  213. disk3.Cost = 0.0
  214. disk3.Local = 0.0
  215. disk3.SetAdjustment(0.0)
  216. disk4 := NewDisk("disk4", "cluster1", "disk4", *windows[0].start, *windows[0].end, windows[0])
  217. disk4.ByteHours = 0.0 * hours
  218. disk4.Cost = 0.0
  219. disk4.Local = 1.0
  220. disk4.SetAdjustment(0.0)
  221. diskT = disk3.Add(disk4).(*Disk)
  222. if diskT.TotalCost() != 0.0 {
  223. t.Fatalf("Disk.Add: expected %f; got %f", 0.0, diskT.TotalCost())
  224. }
  225. if diskT.Local != 0.5 {
  226. t.Fatalf("Disk.Add: expected %f; got %f", 0.5, diskT.Local)
  227. }
  228. // Accumulate: one disks, two windows
  229. diskA1 := NewDisk("diskA1", "cluster1", "diskA1", *windows[0].start, *windows[0].end, windows[0])
  230. diskA1.ByteHours = 100 * gb * hours
  231. diskA1.Cost = 9.0
  232. diskA1.SetAdjustment(1.0)
  233. diskA2 := NewDisk("diskA2", "cluster1", "diskA2", *windows[1].start, *windows[1].end, windows[1])
  234. diskA2.ByteHours = 100 * gb * hours
  235. diskA2.Cost = 9.0
  236. diskA2.SetAdjustment(1.0)
  237. diskAT := diskA1.Add(diskA2).(*Disk)
  238. // Check that the sums and properties are correct
  239. if diskAT.TotalCost() != 20.0 {
  240. t.Fatalf("Disk.Add: expected %f; got %f", 20.0, diskAT.TotalCost())
  241. }
  242. if diskAT.Adjustment != 2.0 {
  243. t.Fatalf("Disk.Add: expected %f; got %f", 2.0, diskAT.Adjustment)
  244. }
  245. if diskAT.Properties.Cluster != "cluster1" {
  246. t.Fatalf("Disk.Add: expected %s; got %s", "cluster1", diskAT.Properties.Cluster)
  247. }
  248. if diskAT.Type() != DiskAssetType {
  249. t.Fatalf("Disk.Add: expected %s; got %s", AnyAssetType, diskAT.Type())
  250. }
  251. if diskAT.Properties.ProviderID != "" {
  252. t.Fatalf("Disk.Add: expected %s; got %s", "", diskAT.Properties.ProviderID)
  253. }
  254. if diskAT.Properties.Name != "" {
  255. t.Fatalf("Disk.Add: expected %s; got %s", "", diskAT.Properties.Name)
  256. }
  257. if diskAT.Bytes() != 100.0*gb {
  258. t.Fatalf("Disk.Add: expected %f; got %f", 100.0*gb, diskT.Bytes())
  259. }
  260. if diskAT.Local != 0.0 {
  261. t.Fatalf("Disk.Add: expected %f; got %f", 0.0, diskAT.Local)
  262. }
  263. // Check that the original assets are unchanged
  264. if diskA1.TotalCost() != 10.0 {
  265. t.Fatalf("Disk.Add: expected %f; got %f", 10.0, diskA1.TotalCost())
  266. }
  267. if diskA1.Adjustment != 1.0 {
  268. t.Fatalf("Disk.Add: expected %f; got %f", 1.0, diskA1.Adjustment)
  269. }
  270. if diskA1.Local != 0.0 {
  271. t.Fatalf("Disk.Add: expected %f; got %f", 0.0, diskA1.Local)
  272. }
  273. if diskA2.TotalCost() != 10.0 {
  274. t.Fatalf("Disk.Add: expected %f; got %f", 10.0, diskA2.TotalCost())
  275. }
  276. if diskA2.Adjustment != 1.0 {
  277. t.Fatalf("Disk.Add: expected %f; got %f", 1.0, diskA2.Adjustment)
  278. }
  279. if diskA2.Local != 0.0 {
  280. t.Fatalf("Disk.Add: expected %f; got %f", 0.0, diskA2.Local)
  281. }
  282. }
  283. func TestDisk_Clone(t *testing.T) {
  284. disk1 := NewDisk("disk1", "cluster1", "disk1", *windows[0].start, *windows[0].end, windows[0])
  285. disk1.Local = 0.0
  286. disk1.Cost = 9.0
  287. disk1.SetAdjustment(1.0)
  288. disk2 := disk1.Clone().(*Disk)
  289. disk2.Local = 1.0
  290. disk1.Cost = 18.0
  291. disk1.SetAdjustment(2.0)
  292. // disk2 should match disk1, even after mutating disk1
  293. if disk2.TotalCost() != 10.0 {
  294. t.Fatalf("Any.Clone: expected %f; got %f", 10.0, disk2.TotalCost())
  295. }
  296. if disk2.Adjustment != 1.0 {
  297. t.Fatalf("Any.Clone: expected %f; got %f", 1.0, disk2.Adjustment)
  298. }
  299. if disk2.Local != 1.0 {
  300. t.Fatalf("Disk.Add: expected %f; got %f", 1.0, disk2.Local)
  301. }
  302. }
  303. func TestDisk_MarshalJSON(t *testing.T) {
  304. disk := NewDisk("disk", "cluster", "providerID", *windows[0].start, *windows[0].end, windows[0])
  305. disk.SetLabels(AssetLabels{
  306. "label": "value",
  307. })
  308. disk.Cost = 9.0
  309. disk.SetAdjustment(1.0)
  310. _, err := json.Marshal(disk)
  311. if err != nil {
  312. t.Fatalf("Disk.MarshalJSON: unexpected error: %s", err)
  313. }
  314. }
  315. func TestNode_Add(t *testing.T) {
  316. // 1. aggregate: add size, local
  317. // 2. accumulate: don't add size, local
  318. hours := windows[0].Duration().Hours()
  319. // Aggregate: two nodes, one window
  320. node1 := NewNode("node1", "cluster1", "node1", *windows[0].start, *windows[0].end, windows[0])
  321. node1.CPUCoreHours = 1.0 * hours
  322. node1.RAMByteHours = 2.0 * gb * hours
  323. node1.GPUHours = 0.0 * hours
  324. node1.GPUCost = 0.0
  325. node1.CPUCost = 8.0
  326. node1.RAMCost = 4.0
  327. node1.Discount = 0.3
  328. node1.CPUBreakdown = &Breakdown{
  329. Idle: 0.6,
  330. System: 0.2,
  331. User: 0.2,
  332. Other: 0.0,
  333. }
  334. node1.RAMBreakdown = &Breakdown{
  335. Idle: 0.6,
  336. System: 0.2,
  337. User: 0.2,
  338. Other: 0.0,
  339. }
  340. node1.SetAdjustment(1.6)
  341. node1.Overhead = &NodeOverhead{
  342. CpuOverheadFraction: 1,
  343. RamOverheadFraction: 1,
  344. OverheadCostFraction: 1,
  345. }
  346. node2 := NewNode("node2", "cluster1", "node2", *windows[0].start, *windows[0].end, windows[0])
  347. node2.CPUCoreHours = 1.0 * hours
  348. node2.RAMByteHours = 2.0 * gb * hours
  349. node2.GPUHours = 0.0 * hours
  350. node2.GPUCost = 0.0
  351. node2.CPUCost = 3.0
  352. node2.RAMCost = 1.0
  353. node2.Discount = 0.0
  354. node1.CPUBreakdown = &Breakdown{
  355. Idle: 0.9,
  356. System: 0.05,
  357. User: 0.0,
  358. Other: 0.05,
  359. }
  360. node1.RAMBreakdown = &Breakdown{
  361. Idle: 0.9,
  362. System: 0.05,
  363. User: 0.0,
  364. Other: 0.05,
  365. }
  366. node2.SetAdjustment(1.0)
  367. node2.Overhead = &NodeOverhead{
  368. CpuOverheadFraction: 0.6,
  369. RamOverheadFraction: 0.75,
  370. OverheadCostFraction: 0.7,
  371. }
  372. nodeT := node1.Add(node2).(*Node)
  373. // Check that the sums and properties are correct
  374. if !util.IsApproximately(nodeT.TotalCost(), 15.0) {
  375. t.Fatalf("Node.Add: expected %f; got %f", 15.0, nodeT.TotalCost())
  376. }
  377. if nodeT.Adjustment != 2.6 {
  378. t.Fatalf("Node.Add: expected %f; got %f", 2.6, nodeT.Adjustment)
  379. }
  380. if nodeT.Properties.Cluster != "cluster1" {
  381. t.Fatalf("Node.Add: expected %s; got %s", "cluster1", nodeT.Properties.Cluster)
  382. }
  383. if nodeT.Type() != NodeAssetType {
  384. t.Fatalf("Node.Add: expected %s; got %s", AnyAssetType, nodeT.Type())
  385. }
  386. if nodeT.Properties.ProviderID != "" {
  387. t.Fatalf("Node.Add: expected %s; got %s", "", nodeT.Properties.ProviderID)
  388. }
  389. if nodeT.Properties.Name != "" {
  390. t.Fatalf("Node.Add: expected %s; got %s", "", nodeT.Properties.Name)
  391. }
  392. if nodeT.CPUCores() != 2.0 {
  393. t.Fatalf("Node.Add: expected %f; got %f", 2.0, nodeT.CPUCores())
  394. }
  395. if nodeT.RAMBytes() != 4.0*gb {
  396. t.Fatalf("Node.Add: expected %f; got %f", 4.0*gb, nodeT.RAMBytes())
  397. }
  398. if o := nodeT.Overhead; o == nil {
  399. t.Errorf("Node.Add (1 + 2): expected overhead to be non-nil")
  400. } else {
  401. if o.CpuOverheadFraction < 0 || o.CpuOverheadFraction > 1 {
  402. t.Errorf("CPU overhead must be within [0, 1], is: %f", o.CpuOverheadFraction)
  403. }
  404. if o.RamOverheadFraction < 0 || o.RamOverheadFraction > 1 {
  405. t.Errorf("RAM overhead must be within [0, 1], is: %f", o.RamOverheadFraction)
  406. }
  407. if o.OverheadCostFraction < 0 || o.OverheadCostFraction > 1 {
  408. t.Errorf("Cost-weighted overhead must be within [0, 1], is: %f", o.OverheadCostFraction)
  409. }
  410. }
  411. // Check that the original assets are unchanged
  412. if !util.IsApproximately(node1.TotalCost(), 10.0) {
  413. t.Fatalf("Node.Add: expected %f; got %f", 10.0, node1.TotalCost())
  414. }
  415. if node1.Adjustment != 1.6 {
  416. t.Fatalf("Node.Add: expected %f; got %f", 1.0, node1.Adjustment)
  417. }
  418. if !util.IsApproximately(node2.TotalCost(), 5.0) {
  419. t.Fatalf("Node.Add: expected %f; got %f", 5.0, node2.TotalCost())
  420. }
  421. if node2.Adjustment != 1.0 {
  422. t.Fatalf("Node.Add: expected %f; got %f", 1.0, node2.Adjustment)
  423. }
  424. // Check that we don't divide by zero computing Local
  425. node3 := NewNode("node3", "cluster1", "node3", *windows[0].start, *windows[0].end, windows[0])
  426. node3.CPUCoreHours = 0 * hours
  427. node3.RAMByteHours = 0 * hours
  428. node3.GPUHours = 0.0 * hours
  429. node3.GPUCost = 0
  430. node3.CPUCost = 0.0
  431. node3.RAMCost = 0.0
  432. node3.Discount = 0.3
  433. node3.SetAdjustment(0.0)
  434. node3.Overhead = &NodeOverhead{
  435. CpuOverheadFraction: 0.6,
  436. RamOverheadFraction: 0.75,
  437. OverheadCostFraction: 0.7,
  438. }
  439. node4 := NewNode("node4", "cluster1", "node4", *windows[0].start, *windows[0].end, windows[0])
  440. node4.CPUCoreHours = 0 * hours
  441. node4.RAMByteHours = 0 * hours
  442. node4.GPUHours = 0.0 * hours
  443. node4.GPUCost = 0
  444. node4.CPUCost = 0.0
  445. node4.RAMCost = 0.0
  446. node4.Discount = 0.1
  447. node4.SetAdjustment(0.0)
  448. node4.Overhead = nil
  449. nodeT = node3.Add(node4).(*Node)
  450. // Check that the sums and properties are correct and without NaNs
  451. if nodeT.TotalCost() != 0.0 {
  452. t.Fatalf("Node.Add: expected %f; got %f", 0.0, nodeT.TotalCost())
  453. }
  454. if nodeT.Discount != 0.2 {
  455. t.Fatalf("Node.Add: expected %f; got %f", 0.2, nodeT.Discount)
  456. }
  457. if nodeT.Overhead != nil {
  458. t.Errorf("Node.Add: adding a node with nil overhead should nil the resulting overhead")
  459. }
  460. // Accumulate: one nodes, two window
  461. nodeA1 := NewNode("nodeA1", "cluster1", "nodeA1", *windows[0].start, *windows[0].end, windows[0])
  462. nodeA1.CPUCoreHours = 1.0 * hours
  463. nodeA1.RAMByteHours = 2.0 * gb * hours
  464. nodeA1.GPUHours = 0.0 * hours
  465. nodeA1.GPUCost = 0.0
  466. nodeA1.CPUCost = 8.0
  467. nodeA1.RAMCost = 4.0
  468. nodeA1.Discount = 0.3
  469. nodeA1.SetAdjustment(1.6)
  470. nodeA2 := NewNode("nodeA2", "cluster1", "nodeA2", *windows[1].start, *windows[1].end, windows[1])
  471. nodeA2.CPUCoreHours = 1.0 * hours
  472. nodeA2.RAMByteHours = 2.0 * gb * hours
  473. nodeA2.GPUHours = 0.0 * hours
  474. nodeA2.GPUCost = 0.0
  475. nodeA2.CPUCost = 3.0
  476. nodeA2.RAMCost = 1.0
  477. nodeA2.Discount = 0.0
  478. nodeA2.SetAdjustment(1.0)
  479. nodeAT := nodeA1.Add(nodeA2).(*Node)
  480. // Check that the sums and properties are correct
  481. if !util.IsApproximately(nodeAT.TotalCost(), 15.0) {
  482. t.Fatalf("Node.Add: expected %f; got %f", 15.0, nodeAT.TotalCost())
  483. }
  484. if nodeAT.Adjustment != 2.6 {
  485. t.Fatalf("Node.Add: expected %f; got %f", 2.6, nodeAT.Adjustment)
  486. }
  487. if nodeAT.Properties.Cluster != "cluster1" {
  488. t.Fatalf("Node.Add: expected %s; got %s", "cluster1", nodeAT.Properties.Cluster)
  489. }
  490. if nodeAT.Type() != NodeAssetType {
  491. t.Fatalf("Node.Add: expected %s; got %s", AnyAssetType, nodeAT.Type())
  492. }
  493. if nodeAT.Properties.ProviderID != "" {
  494. t.Fatalf("Node.Add: expected %s; got %s", "", nodeAT.Properties.ProviderID)
  495. }
  496. if nodeAT.Properties.Name != "" {
  497. t.Fatalf("Node.Add: expected %s; got %s", "", nodeAT.Properties.Name)
  498. }
  499. if nodeAT.CPUCores() != 1.0 {
  500. t.Fatalf("Node.Add: expected %f; got %f", 1.0, nodeAT.CPUCores())
  501. }
  502. if nodeAT.RAMBytes() != 2.0*gb {
  503. t.Fatalf("Node.Add: expected %f; got %f", 2.0*gb, nodeAT.RAMBytes())
  504. }
  505. if nodeAT.GPUs() != 0.0 {
  506. t.Fatalf("Node.Add: expected %f; got %f", 0.0, nodeAT.GPUs())
  507. }
  508. // Check that the original assets are unchanged
  509. if !util.IsApproximately(nodeA1.TotalCost(), 10.0) {
  510. t.Fatalf("Node.Add: expected %f; got %f", 10.0, nodeA1.TotalCost())
  511. }
  512. if nodeA1.Adjustment != 1.6 {
  513. t.Fatalf("Node.Add: expected %f; got %f", 1.0, nodeA1.Adjustment)
  514. }
  515. if !util.IsApproximately(nodeA2.TotalCost(), 5.0) {
  516. t.Fatalf("Node.Add: expected %f; got %f", 5.0, nodeA2.TotalCost())
  517. }
  518. if nodeA2.Adjustment != 1.0 {
  519. t.Fatalf("Node.Add: expected %f; got %f", 1.0, nodeA2.Adjustment)
  520. }
  521. }
  522. func TestNode_Clone(t *testing.T) {
  523. cases := []struct {
  524. name string
  525. input *Node
  526. }{
  527. {
  528. name: "overhead nil",
  529. input: &Node{
  530. Overhead: nil,
  531. },
  532. },
  533. {
  534. name: "overhead non-nil",
  535. input: &Node{
  536. Overhead: &NodeOverhead{
  537. CpuOverheadFraction: 3,
  538. RamOverheadFraction: 7,
  539. OverheadCostFraction: 6,
  540. },
  541. },
  542. },
  543. }
  544. for _, c := range cases {
  545. t.Run(c.name, func(t *testing.T) {
  546. result := c.input.Clone()
  547. if !result.Equal(c.input) {
  548. t.Errorf("clone result doesn't equal input")
  549. }
  550. })
  551. }
  552. }
  553. func TestNode_MarshalJSON(t *testing.T) {
  554. node := NewNode("node", "cluster", "providerID", *windows[0].start, *windows[0].end, windows[0])
  555. node.SetLabels(AssetLabels{
  556. "label": "value",
  557. })
  558. node.CPUCost = 9.0
  559. node.RAMCost = 0.0
  560. node.RAMCost = 21.0
  561. node.CPUCoreHours = 123.0
  562. node.RAMByteHours = 13323.0
  563. node.GPUHours = 123.0
  564. node.SetAdjustment(1.0)
  565. _, err := json.Marshal(node)
  566. if err != nil {
  567. t.Fatalf("Node.MarshalJSON: unexpected error: %s", err)
  568. }
  569. }
  570. func TestClusterManagement_Add(t *testing.T) {
  571. cm1 := NewClusterManagement(GCPProvider, "cluster1", windows[0])
  572. cm1.Cost = 9.0
  573. cm2 := NewClusterManagement(GCPProvider, "cluster1", windows[0])
  574. cm2.Cost = 4.0
  575. cm3 := cm1.Add(cm2)
  576. // Check that the sums and properties are correct
  577. if cm3.TotalCost() != 13.0 {
  578. t.Fatalf("ClusterManagement.Add: expected %f; got %f", 13.0, cm3.TotalCost())
  579. }
  580. if cm3.GetProperties().Cluster != "cluster1" {
  581. t.Fatalf("ClusterManagement.Add: expected %s; got %s", "cluster1", cm3.GetProperties().Cluster)
  582. }
  583. if cm3.Type() != ClusterManagementAssetType {
  584. t.Fatalf("ClusterManagement.Add: expected %s; got %s", ClusterManagementAssetType, cm3.Type())
  585. }
  586. // Check that the original assets are unchanged
  587. if cm1.TotalCost() != 9.0 {
  588. t.Fatalf("ClusterManagement.Add: expected %f; got %f", 9.0, cm1.TotalCost())
  589. }
  590. if cm2.TotalCost() != 4.0 {
  591. t.Fatalf("ClusterManagement.Add: expected %f; got %f", 4.0, cm2.TotalCost())
  592. }
  593. }
  594. func TestClusterManagement_Clone(t *testing.T) {
  595. // TODO
  596. }
  597. func TestCloudAny_Add(t *testing.T) {
  598. ca1 := NewCloud(ComputeCategory, "ca1", *windows[0].start, *windows[0].end, windows[0])
  599. ca1.Cost = 9.0
  600. ca1.SetAdjustment(1.0)
  601. ca2 := NewCloud(StorageCategory, "ca2", *windows[0].start, *windows[0].end, windows[0])
  602. ca2.Cost = 4.0
  603. ca2.SetAdjustment(1.0)
  604. ca3 := ca1.Add(ca2)
  605. // Check that the sums and properties are correct
  606. if ca3.TotalCost() != 15.0 {
  607. t.Fatalf("Any.Add: expected %f; got %f", 15.0, ca3.TotalCost())
  608. }
  609. if ca3.GetAdjustment() != 2.0 {
  610. t.Fatalf("Any.Add: expected %f; got %f", 2.0, ca3.GetAdjustment())
  611. }
  612. if ca3.Type() != CloudAssetType {
  613. t.Fatalf("Any.Add: expected %s; got %s", CloudAssetType, ca3.Type())
  614. }
  615. // Check that the original assets are unchanged
  616. if ca1.TotalCost() != 10.0 {
  617. t.Fatalf("Any.Add: expected %f; got %f", 10.0, ca1.TotalCost())
  618. }
  619. if ca1.Adjustment != 1.0 {
  620. t.Fatalf("Any.Add: expected %f; got %f", 1.0, ca1.Adjustment)
  621. }
  622. if ca2.TotalCost() != 5.0 {
  623. t.Fatalf("Any.Add: expected %f; got %f", 5.0, ca2.TotalCost())
  624. }
  625. if ca2.Adjustment != 1.0 {
  626. t.Fatalf("Any.Add: expected %f; got %f", 1.0, ca2.Adjustment)
  627. }
  628. }
  629. func TestCloudAny_Clone(t *testing.T) {
  630. // TODO
  631. }
  632. func TestAssetSet_AggregateBy(t *testing.T) {
  633. endYesterday := time.Now().UTC().Truncate(day)
  634. startYesterday := endYesterday.Add(-day)
  635. window := NewWindow(&startYesterday, &endYesterday)
  636. // Scenarios to test:
  637. // 1 Single-aggregation
  638. // 1a []AssetProperty=[Cluster]
  639. // 1b []AssetProperty=[Type]
  640. // 1c []AssetProperty=[Nil]
  641. // 1d []AssetProperty=nil
  642. // 1e aggregateBy []string=["label:test"]
  643. // 2 Multi-aggregation
  644. // 2a []AssetProperty=[Cluster,Type]
  645. // 3 Share resources
  646. // 3a Shared hourly cost > 0.0
  647. // Definitions and set-up:
  648. var as *AssetSet
  649. var err error
  650. // Tests:
  651. // 1 Single-aggregation
  652. // 1a []AssetProperty=[Cluster]
  653. as = GenerateMockAssetSet(startYesterday, day)
  654. err = as.AggregateBy([]string{string(AssetClusterProp)}, nil)
  655. if err != nil {
  656. t.Fatalf("AssetSet.AggregateBy: unexpected error: %s", err)
  657. }
  658. assertAssetSet(t, as, "1a", window, map[string]float64{
  659. "cluster1": 26.0,
  660. "cluster2": 15.0,
  661. "cluster3": 19.0,
  662. }, nil)
  663. // 1b []AssetProperty=[Type]
  664. as = GenerateMockAssetSet(startYesterday, day)
  665. err = as.AggregateBy([]string{string(AssetTypeProp)}, nil)
  666. if err != nil {
  667. t.Fatalf("AssetSet.AggregateBy: unexpected error: %s", err)
  668. }
  669. assertAssetSet(t, as, "1b", window, map[string]float64{
  670. "Node": 49.0,
  671. "Disk": 8.0,
  672. "ClusterManagement": 3.0,
  673. }, nil)
  674. // 1c []AssetProperty=[Nil]
  675. as = GenerateMockAssetSet(startYesterday, day)
  676. err = as.AggregateBy([]string{}, nil)
  677. if err != nil {
  678. t.Fatalf("AssetSet.AggregateBy: unexpected error: %s", err)
  679. }
  680. assertAssetSet(t, as, "1c", window, map[string]float64{
  681. "": 60.0,
  682. }, nil)
  683. // 1d []AssetProperty=nil
  684. as = GenerateMockAssetSet(startYesterday, day)
  685. err = as.AggregateBy(nil, nil)
  686. if err != nil {
  687. t.Fatalf("AssetSet.AggregateBy: unexpected error: %s", err)
  688. }
  689. assertAssetSet(t, as, "1d", window, map[string]float64{
  690. "__undefined__/__undefined__/__undefined__/Compute/cluster1/Node/Kubernetes/gcp-node1/node1": 7.00,
  691. "__undefined__/__undefined__/__undefined__/Compute/cluster1/Node/Kubernetes/gcp-node2/node2": 5.50,
  692. "__undefined__/__undefined__/__undefined__/Compute/cluster1/Node/Kubernetes/gcp-node3/node3": 6.50,
  693. "__undefined__/__undefined__/__undefined__/Storage/cluster1/Disk/Kubernetes/gcp-disk1/disk1": 2.50,
  694. "__undefined__/__undefined__/__undefined__/Storage/cluster1/Disk/Kubernetes/gcp-disk2/disk2": 1.50,
  695. "GCP/__undefined__/__undefined__/Management/cluster1/ClusterManagement/Kubernetes/__undefined__/__undefined__": 3.00,
  696. "__undefined__/__undefined__/__undefined__/Compute/cluster2/Node/Kubernetes/gcp-node4/node4": 11.00,
  697. "__undefined__/__undefined__/__undefined__/Storage/cluster2/Disk/Kubernetes/gcp-disk3/disk3": 2.50,
  698. "__undefined__/__undefined__/__undefined__/Storage/cluster2/Disk/Kubernetes/gcp-disk4/disk4": 1.50,
  699. "GCP/__undefined__/__undefined__/Management/cluster2/ClusterManagement/Kubernetes/__undefined__/__undefined__": 0.00,
  700. "__undefined__/__undefined__/__undefined__/Compute/cluster3/Node/Kubernetes/aws-node5/node5": 19.00,
  701. }, nil)
  702. // 1e aggregateBy []string=["label:test"]
  703. as = GenerateMockAssetSet(startYesterday, day)
  704. err = as.AggregateBy([]string{"label:test"}, nil)
  705. if err != nil {
  706. t.Fatalf("AssetSet.AggregateBy: unexpected error: %s", err)
  707. }
  708. assertAssetSet(t, as, "1e", window, map[string]float64{
  709. "__undefined__": 53.00,
  710. "test=test": 7.00,
  711. }, nil)
  712. // 2 Multi-aggregation
  713. // 2a []AssetProperty=[Cluster,Type]
  714. as = GenerateMockAssetSet(startYesterday, day)
  715. err = as.AggregateBy([]string{string(AssetClusterProp), string(AssetTypeProp)}, nil)
  716. if err != nil {
  717. t.Fatalf("AssetSet.AggregateBy: unexpected error: %s", err)
  718. }
  719. assertAssetSet(t, as, "2a", window, map[string]float64{
  720. "cluster1/Node": 19.0,
  721. "cluster1/Disk": 4.0,
  722. "cluster1/ClusterManagement": 3.0,
  723. "cluster2/Node": 11.0,
  724. "cluster2/Disk": 4.0,
  725. "cluster2/ClusterManagement": 0.0,
  726. "cluster3/Node": 19.0,
  727. }, nil)
  728. // 3 Share resources
  729. // 3a Shared hourly cost > 0.0
  730. as = GenerateMockAssetSet(startYesterday, day)
  731. err = as.AggregateBy([]string{string(AssetTypeProp)}, &AssetAggregationOptions{
  732. SharedHourlyCosts: map[string]float64{"shared1": 0.5},
  733. })
  734. if err != nil {
  735. t.Fatalf("AssetSet.AggregateBy: unexpected error: %s", err)
  736. }
  737. assertAssetSet(t, as, "1a", window, map[string]float64{
  738. "Node": 49.0,
  739. "Disk": 8.0,
  740. "ClusterManagement": 3.0,
  741. "Shared": 12.0,
  742. }, nil)
  743. }
  744. func TestAssetSet_FindMatch(t *testing.T) {
  745. endYesterday := time.Now().UTC().Truncate(day)
  746. startYesterday := endYesterday.Add(-day)
  747. s, e := startYesterday, endYesterday
  748. w := NewWindow(&s, &e)
  749. var query, match Asset
  750. var as *AssetSet
  751. var err error
  752. // Assert success of a simple match of Type and ProviderID
  753. as = GenerateMockAssetSet(startYesterday, day)
  754. query = NewNode("", "", "gcp-node3", s, e, w)
  755. match, err = as.FindMatch(query, []string{string(AssetTypeProp), string(AssetProviderIDProp)}, nil)
  756. if err != nil {
  757. t.Fatalf("AssetSet.FindMatch: unexpected error: %s", err)
  758. }
  759. // Assert error of a simple non-match of Type and ProviderID
  760. as = GenerateMockAssetSet(startYesterday, day)
  761. query = NewNode("", "", "aws-node3", s, e, w)
  762. match, err = as.FindMatch(query, []string{string(AssetTypeProp), string(AssetProviderIDProp)}, nil)
  763. if err == nil {
  764. t.Fatalf("AssetSet.FindMatch: expected error (no match); found %s", match)
  765. }
  766. // Assert error of matching ProviderID, but not Type
  767. as = GenerateMockAssetSet(startYesterday, day)
  768. query = NewCloud(ComputeCategory, "gcp-node3", s, e, w)
  769. match, err = as.FindMatch(query, []string{string(AssetTypeProp), string(AssetProviderIDProp)}, nil)
  770. if err == nil {
  771. t.Fatalf("AssetSet.FindMatch: expected error (no match); found %s", match)
  772. }
  773. }
  774. // Asserts that all Assets within an AssetSet have a Window that
  775. // matches that of the AssetSet.
  776. func TestAssetSet_InsertMatchingWindow(t *testing.T) {
  777. setStart := time.Now().Round(time.Hour)
  778. setEnd := setStart.Add(1 * time.Hour)
  779. a1WindowStart := setStart.Add(5 * time.Minute)
  780. a1WindowEnd := setStart.Add(50 * time.Minute)
  781. a2WindowStart := setStart.Add(17 * time.Minute)
  782. a2WindowEnd := setStart.Add(34 * time.Minute)
  783. a1 := &Node{}
  784. a1.SetProperties(&AssetProperties{
  785. Name: "asset-1",
  786. })
  787. a1.Window = NewClosedWindow(a1WindowStart, a1WindowEnd)
  788. a2 := &Disk{}
  789. a2.SetProperties(&AssetProperties{
  790. Name: "asset-2",
  791. })
  792. a2.Window = NewClosedWindow(a2WindowStart, a2WindowEnd)
  793. as := NewAssetSet(setStart, setEnd)
  794. as.Insert(a1, nil)
  795. as.Insert(a2, nil)
  796. if as.Length() != 2 {
  797. t.Errorf("AS length got %d, expected %d", as.Length(), 2)
  798. }
  799. for _, a := range as.Assets {
  800. if !(*a.GetWindow().Start()).Equal(setStart) {
  801. t.Errorf("Asset %s window start is %s, expected %s", a.GetProperties().Name, *a.GetWindow().Start(), setStart)
  802. }
  803. if !(*a.GetWindow().End()).Equal(setEnd) {
  804. t.Errorf("Asset %s window end is %s, expected %s", a.GetProperties().Name, *a.GetWindow().End(), setEnd)
  805. }
  806. }
  807. }
  808. func TestAssetSet_ReconciliationMatchMap(t *testing.T) {
  809. endYesterday := time.Now().UTC().Truncate(day)
  810. startYesterday := endYesterday.Add(-day)
  811. as := GenerateMockAssetSet(startYesterday, day)
  812. matchMap := as.ReconciliationMatchMap()
  813. // Determine the number of assets by provider ID
  814. assetCountByProviderId := make(map[string]int, len(matchMap))
  815. for _, a := range as.Assets {
  816. if a == nil || a.GetProperties() == nil || a.GetProperties().ProviderID == "" {
  817. return
  818. }
  819. if _, ok := assetCountByProviderId[a.GetProperties().ProviderID]; !ok {
  820. assetCountByProviderId[a.GetProperties().ProviderID] = 0
  821. }
  822. assetCountByProviderId[a.GetProperties().ProviderID] += 1
  823. }
  824. for k, count := range assetCountByProviderId {
  825. if len(matchMap[k]) != count {
  826. t.Errorf("AssetSet.ReconciliationMatchMap: incorrect asset count for provider id: %s", k)
  827. }
  828. }
  829. }
  830. func TestAssetSetRange_AccumulateToAssetSet(t *testing.T) {
  831. endYesterday := time.Now().UTC().Truncate(day)
  832. startYesterday := endYesterday.Add(-day)
  833. startD2 := startYesterday
  834. startD1 := startD2.Add(-day)
  835. startD0 := startD1.Add(-day)
  836. window := NewWindow(&startD0, &endYesterday)
  837. var asr *AssetSetRange
  838. var as *AssetSet
  839. var err error
  840. asr = NewAssetSetRange(
  841. GenerateMockAssetSet(startD0, day),
  842. GenerateMockAssetSet(startD1, day),
  843. GenerateMockAssetSet(startD2, day),
  844. )
  845. err = asr.AggregateBy(nil, nil)
  846. if err != nil {
  847. t.Fatalf("AssetSetRange.AggregateBy: unexpected error: %s", err)
  848. }
  849. as, err = asr.AccumulateToAssetSet()
  850. if err != nil {
  851. t.Fatalf("AssetSetRange.AccumulateToAssetSet: unexpected error: %s", err)
  852. }
  853. assertAssetSet(t, as, "1a", window, map[string]float64{
  854. "__undefined__/__undefined__/__undefined__/Compute/cluster1/Node/Kubernetes/gcp-node1/node1": 21.00,
  855. "__undefined__/__undefined__/__undefined__/Compute/cluster1/Node/Kubernetes/gcp-node2/node2": 16.50,
  856. "__undefined__/__undefined__/__undefined__/Compute/cluster1/Node/Kubernetes/gcp-node3/node3": 19.50,
  857. "__undefined__/__undefined__/__undefined__/Storage/cluster1/Disk/Kubernetes/gcp-disk1/disk1": 7.50,
  858. "__undefined__/__undefined__/__undefined__/Storage/cluster1/Disk/Kubernetes/gcp-disk2/disk2": 4.50,
  859. "GCP/__undefined__/__undefined__/Management/cluster1/ClusterManagement/Kubernetes/__undefined__/__undefined__": 9.00,
  860. "__undefined__/__undefined__/__undefined__/Compute/cluster2/Node/Kubernetes/gcp-node4/node4": 33.00,
  861. "__undefined__/__undefined__/__undefined__/Storage/cluster2/Disk/Kubernetes/gcp-disk3/disk3": 7.50,
  862. "__undefined__/__undefined__/__undefined__/Storage/cluster2/Disk/Kubernetes/gcp-disk4/disk4": 4.50,
  863. "GCP/__undefined__/__undefined__/Management/cluster2/ClusterManagement/Kubernetes/__undefined__/__undefined__": 0.00,
  864. "__undefined__/__undefined__/__undefined__/Compute/cluster3/Node/Kubernetes/aws-node5/node5": 57.00,
  865. }, nil)
  866. asr = NewAssetSetRange(
  867. GenerateMockAssetSet(startD0, day),
  868. GenerateMockAssetSet(startD1, day),
  869. GenerateMockAssetSet(startD2, day),
  870. )
  871. err = asr.AggregateBy([]string{}, nil)
  872. if err != nil {
  873. t.Fatalf("AssetSetRange.AggregateBy: unexpected error: %s", err)
  874. }
  875. as, err = asr.AccumulateToAssetSet()
  876. if err != nil {
  877. t.Fatalf("AssetSetRange.AccumulateToAssetSet: unexpected error: %s", err)
  878. }
  879. assertAssetSet(t, as, "1b", window, map[string]float64{
  880. "": 180.00,
  881. }, nil)
  882. asr = NewAssetSetRange(
  883. GenerateMockAssetSet(startD0, day),
  884. GenerateMockAssetSet(startD1, day),
  885. GenerateMockAssetSet(startD2, day),
  886. )
  887. err = asr.AggregateBy([]string{string(AssetTypeProp)}, nil)
  888. if err != nil {
  889. t.Fatalf("AssetSetRange.AggregateBy: unexpected error: %s", err)
  890. }
  891. as, err = asr.AccumulateToAssetSet()
  892. if err != nil {
  893. t.Fatalf("AssetSetRange.AggregateBy: unexpected error: %s", err)
  894. }
  895. assertAssetSet(t, as, "1c", window, map[string]float64{
  896. "Node": 147.0,
  897. "Disk": 24.0,
  898. "ClusterManagement": 9.0,
  899. }, nil)
  900. asr = NewAssetSetRange(
  901. GenerateMockAssetSet(startD0, day),
  902. GenerateMockAssetSet(startD1, day),
  903. GenerateMockAssetSet(startD2, day),
  904. )
  905. err = asr.AggregateBy([]string{string(AssetClusterProp)}, nil)
  906. if err != nil {
  907. t.Fatalf("AssetSetRange.AggregateBy: unexpected error: %s", err)
  908. }
  909. as, err = asr.AccumulateToAssetSet()
  910. if err != nil {
  911. t.Fatalf("AssetSetRange.AggregateBy: unexpected error: %s", err)
  912. }
  913. assertAssetSet(t, as, "1c", window, map[string]float64{
  914. "cluster1": 78.0,
  915. "cluster2": 45.0,
  916. "cluster3": 57.0,
  917. }, nil)
  918. // Accumulation with aggregation should work, even when the first AssetSet
  919. // is empty (this was previously an issue)
  920. asr = NewAssetSetRange(
  921. NewAssetSet(startD0, startD1),
  922. GenerateMockAssetSet(startD1, day),
  923. GenerateMockAssetSet(startD2, day),
  924. )
  925. err = asr.AggregateBy([]string{string(AssetTypeProp)}, nil)
  926. if err != nil {
  927. t.Fatalf("AssetSetRange.AggregateBy: unexpected error: %s", err)
  928. }
  929. as, err = asr.AccumulateToAssetSet()
  930. if err != nil {
  931. t.Fatalf("AssetSetRange.AggregateBy: unexpected error: %s", err)
  932. }
  933. assertAssetSet(t, as, "1d", window, map[string]float64{
  934. "Node": 98.00,
  935. "Disk": 16.00,
  936. "ClusterManagement": 6.00,
  937. }, nil)
  938. }
  939. func TestAssetToExternalAllocation(t *testing.T) {
  940. var asset Asset
  941. var alloc *Allocation
  942. var err error
  943. labelConfig := NewLabelConfig()
  944. _, err = AssetToExternalAllocation(asset, []string{"namespace"}, labelConfig)
  945. if err == nil {
  946. t.Fatalf("expected error due to nil asset; no error returned")
  947. }
  948. // Consider this Asset:
  949. // Cloud {
  950. // TotalCost: 10.00,
  951. // Labels{
  952. // "kubernetes_namespace":"monitoring",
  953. // "env":"prod"
  954. // }
  955. // }
  956. cloud := NewCloud(ComputeCategory, "abc123", start1, start2, windows[0])
  957. cloud.SetLabels(map[string]string{
  958. "kubernetes_namespace": "monitoring",
  959. "env": "prod",
  960. "app": "cost-analyzer",
  961. "kubernetes_label_app": "app",
  962. "kubernetes_label_department": "department",
  963. "kubernetes_label_env": "env",
  964. "kubernetes_label_owner": "owner",
  965. "kubernetes_label_team": "team",
  966. })
  967. cloud.Cost = 10.00
  968. asset = cloud
  969. _, err = AssetToExternalAllocation(asset, []string{"namespace"}, nil)
  970. if err != nil {
  971. t.Fatalf("unexpected error: %s", err)
  972. }
  973. _, err = AssetToExternalAllocation(asset, nil, nil)
  974. if err == nil {
  975. t.Fatalf("expected error due to nil aggregateBy; no error returned")
  976. }
  977. // Given the following parameters, we expect to return:
  978. //
  979. // 1) single-prop full match
  980. // aggregateBy = ["namespace"]
  981. // allocationPropertyLabels = {"namespace":"kubernetes_namespace"}
  982. // => Allocation{Name: "monitoring", ExternalCost: 10.00, TotalCost: 10.00}, nil
  983. //
  984. // 2) multi-prop full match
  985. // aggregateBy = ["namespace", "label:env"]
  986. // allocationPropertyLabels = {"namespace":"kubernetes_namespace"}
  987. // => Allocation{Name: "monitoring/env=prod", ExternalCost: 10.00, TotalCost: 10.00}, nil
  988. //
  989. // 3) multi-prop partial match
  990. // aggregateBy = ["namespace", "label:foo"]
  991. // allocationPropertyLabels = {"namespace":"kubernetes_namespace"}
  992. // => Allocation{Name: "monitoring/__unallocated__", ExternalCost: 10.00, TotalCost: 10.00}, nil
  993. //
  994. // 4) label alias match(es)
  995. // aggregateBy = ["product", "deployment", "environment", "owner", "team"]
  996. // allocationPropertyLabels = {"namespace":"kubernetes_namespace"}
  997. // => Allocation{Name: "app/department/env/owner/team", ExternalCost: 10.00, TotalCost: 10.00}, nil
  998. //
  999. // 5) no match
  1000. // aggregateBy = ["cluster"]
  1001. // allocationPropertyLabels = {"namespace":"kubernetes_namespace"}
  1002. // => nil, err
  1003. // 1) single-prop full match
  1004. alloc, err = AssetToExternalAllocation(asset, []string{"namespace"}, nil)
  1005. if err != nil {
  1006. t.Fatalf("unexpected error: %s", err)
  1007. }
  1008. if alloc.Name != "monitoring/__external__" {
  1009. t.Fatalf("expected external allocation with name '%s'; got '%s'", "monitoring/__external__", alloc.Name)
  1010. }
  1011. if ns := alloc.Properties.Namespace; ns != "monitoring" {
  1012. t.Fatalf("expected external allocation with AllocationProperties.Namespace '%s'; got '%s'", "monitoring", ns)
  1013. }
  1014. if alloc.ExternalCost != 10.00 {
  1015. t.Fatalf("expected external allocation with ExternalCost %f; got %f", 10.00, alloc.ExternalCost)
  1016. }
  1017. if alloc.TotalCost() != 10.00 {
  1018. t.Fatalf("expected external allocation with TotalCost %f; got %f", 10.00, alloc.TotalCost())
  1019. }
  1020. // 2) multi-prop full match
  1021. alloc, err = AssetToExternalAllocation(asset, []string{"namespace", "label:env"}, nil)
  1022. if err != nil {
  1023. t.Fatalf("unexpected error: %s", err)
  1024. }
  1025. if alloc.Name != "monitoring/env=prod/__external__" {
  1026. t.Fatalf("expected external allocation with name '%s'; got '%s'", "monitoring/env=prod/__external__", alloc.Name)
  1027. }
  1028. if ns := alloc.Properties.Namespace; ns != "monitoring" {
  1029. t.Fatalf("expected external allocation with AllocationProperties.Namespace '%s'; got '%s' (%s)", "monitoring", ns, err)
  1030. }
  1031. if ls := alloc.Properties.Labels; len(ls) == 0 || ls["env"] != "prod" {
  1032. t.Fatalf("expected external allocation with AllocationProperties.Labels[\"env\"] '%s'; got '%s' (%s)", "prod", ls["env"], err)
  1033. }
  1034. if alloc.ExternalCost != 10.00 {
  1035. t.Fatalf("expected external allocation with ExternalCost %f; got %f", 10.00, alloc.ExternalCost)
  1036. }
  1037. if alloc.TotalCost() != 10.00 {
  1038. t.Fatalf("expected external allocation with TotalCost %f; got %f", 10.00, alloc.TotalCost())
  1039. }
  1040. // 3) multi-prop partial match
  1041. alloc, err = AssetToExternalAllocation(asset, []string{"namespace", "label:foo"}, nil)
  1042. if err != nil {
  1043. t.Fatalf("unexpected error: %s", err)
  1044. }
  1045. if alloc.Name != "monitoring/__unallocated__/__external__" {
  1046. t.Fatalf("expected external allocation with name '%s'; got '%s'", "monitoring/__unallocated__/__external__", alloc.Name)
  1047. }
  1048. if ns := alloc.Properties.Namespace; ns != "monitoring" {
  1049. t.Fatalf("expected external allocation with AllocationProperties.Namespace '%s'; got '%s' (%s)", "monitoring", ns, err)
  1050. }
  1051. if alloc.ExternalCost != 10.00 {
  1052. t.Fatalf("expected external allocation with ExternalCost %f; got %f", 10.00, alloc.ExternalCost)
  1053. }
  1054. if alloc.TotalCost() != 10.00 {
  1055. t.Fatalf("expected external allocation with TotalCost %f; got %f", 10.00, alloc.TotalCost())
  1056. }
  1057. // 4) label alias match(es)
  1058. alloc, err = AssetToExternalAllocation(asset, []string{"product", "department", "environment", "owner", "team"}, nil)
  1059. if err != nil {
  1060. t.Fatalf("unexpected error: %s", err)
  1061. }
  1062. if alloc.Name != "app/department/env/owner/team/__external__" {
  1063. t.Fatalf("expected external allocation with name '%s'; got '%s'", "app/department/env/owner/team/__external__", alloc.Name)
  1064. }
  1065. if alloc.Properties.Labels[labelConfig.ProductLabel] != "app" {
  1066. t.Fatalf("expected external allocation with label %s equal to %s; got %s", labelConfig.ProductLabel, "app", alloc.Properties.Labels[labelConfig.ProductLabel])
  1067. }
  1068. if alloc.Properties.Labels[labelConfig.DepartmentLabel] != "department" {
  1069. t.Fatalf("expected external allocation with label %s equal to %s; got %s", labelConfig.DepartmentLabel, "department", alloc.Properties.Labels[labelConfig.DepartmentLabel])
  1070. }
  1071. if alloc.Properties.Labels[labelConfig.EnvironmentLabel] != "env" {
  1072. t.Fatalf("expected external allocation with label %s equal to %s; got %s", labelConfig.EnvironmentLabel, "env", alloc.Properties.Labels[labelConfig.EnvironmentLabel])
  1073. }
  1074. if alloc.Properties.Labels[labelConfig.OwnerLabel] != "owner" {
  1075. t.Fatalf("expected external allocation with label %s equal to %s; got %s", labelConfig.OwnerLabel, "owner", alloc.Properties.Labels[labelConfig.OwnerLabel])
  1076. }
  1077. if alloc.Properties.Labels[labelConfig.TeamLabel] != "team" {
  1078. t.Fatalf("expected external allocation with label %s equal to %s; got %s", labelConfig.TeamLabel, "team", alloc.Properties.Labels[labelConfig.TeamLabel])
  1079. }
  1080. if alloc.ExternalCost != 10.00 {
  1081. t.Fatalf("expected external allocation with ExternalCost %f; got %f", 10.00, alloc.ExternalCost)
  1082. }
  1083. if alloc.TotalCost() != 10.00 {
  1084. t.Fatalf("expected external allocation with TotalCost %f; got %f", 10.00, alloc.TotalCost())
  1085. }
  1086. // 5) no match
  1087. _, err = AssetToExternalAllocation(asset, []string{"cluster"}, nil)
  1088. if err == nil {
  1089. t.Fatalf("expected 'no match' error")
  1090. }
  1091. // other cases
  1092. alloc, err = AssetToExternalAllocation(asset, []string{"namespace", "label:app"}, nil)
  1093. if err != nil {
  1094. t.Fatalf("unexpected error: %s", err)
  1095. }
  1096. if alloc.ExternalCost != 10.00 {
  1097. t.Fatalf("expected external allocation with ExternalCost %f; got %f", 10.00, alloc.ExternalCost)
  1098. }
  1099. if alloc.TotalCost() != 10.00 {
  1100. t.Fatalf("expected external allocation with TotalCost %f; got %f", 10.00, alloc.TotalCost())
  1101. }
  1102. }
  1103. func TestAssetSetRange_Start(t *testing.T) {
  1104. tests := []struct {
  1105. name string
  1106. arg *AssetSetRange
  1107. expectError bool
  1108. expected time.Time
  1109. }{
  1110. {
  1111. name: "Empty ASR",
  1112. arg: nil,
  1113. expectError: true,
  1114. },
  1115. {
  1116. name: "Single asset",
  1117. arg: &AssetSetRange{
  1118. Assets: []*AssetSet{
  1119. {
  1120. Assets: map[string]Asset{
  1121. "a": &Node{
  1122. Start: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  1123. },
  1124. },
  1125. },
  1126. },
  1127. },
  1128. expected: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  1129. },
  1130. {
  1131. name: "Two assets",
  1132. arg: &AssetSetRange{
  1133. Assets: []*AssetSet{
  1134. {
  1135. Assets: map[string]Asset{
  1136. "a": &Node{
  1137. Start: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  1138. },
  1139. "b": &Node{
  1140. Start: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  1141. },
  1142. },
  1143. },
  1144. },
  1145. },
  1146. expected: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  1147. },
  1148. {
  1149. name: "Two AssetSets",
  1150. arg: &AssetSetRange{
  1151. Assets: []*AssetSet{
  1152. {
  1153. Assets: map[string]Asset{
  1154. "a": &Node{
  1155. Start: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  1156. },
  1157. },
  1158. },
  1159. {
  1160. Assets: map[string]Asset{
  1161. "b": &Node{
  1162. Start: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  1163. },
  1164. },
  1165. },
  1166. },
  1167. },
  1168. expected: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  1169. },
  1170. }
  1171. for _, test := range tests {
  1172. result, err := test.arg.Start()
  1173. if test.expectError && err != nil {
  1174. continue
  1175. }
  1176. if test.expectError && err == nil {
  1177. t.Errorf("%s: expected error and got none", test.name)
  1178. } else if result != test.expected {
  1179. t.Errorf("%s: expected %s but got %s", test.name, test.expected, result)
  1180. }
  1181. }
  1182. }
  1183. func TestAssetSetRange_End(t *testing.T) {
  1184. tests := []struct {
  1185. name string
  1186. arg *AssetSetRange
  1187. expectError bool
  1188. expected time.Time
  1189. }{
  1190. {
  1191. name: "Empty ASR",
  1192. arg: nil,
  1193. expectError: true,
  1194. },
  1195. {
  1196. name: "Single asset",
  1197. arg: &AssetSetRange{
  1198. Assets: []*AssetSet{
  1199. {
  1200. Assets: map[string]Asset{
  1201. "a": &Node{
  1202. End: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  1203. },
  1204. },
  1205. },
  1206. },
  1207. },
  1208. expected: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  1209. },
  1210. {
  1211. name: "Two assets",
  1212. arg: &AssetSetRange{
  1213. Assets: []*AssetSet{
  1214. {
  1215. Assets: map[string]Asset{
  1216. "a": &Node{
  1217. End: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  1218. },
  1219. "b": &Node{
  1220. End: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  1221. },
  1222. },
  1223. },
  1224. },
  1225. },
  1226. expected: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  1227. },
  1228. {
  1229. name: "Two AssetSets",
  1230. arg: &AssetSetRange{
  1231. Assets: []*AssetSet{
  1232. {
  1233. Assets: map[string]Asset{
  1234. "a": &Node{
  1235. End: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  1236. },
  1237. },
  1238. },
  1239. {
  1240. Assets: map[string]Asset{
  1241. "b": &Node{
  1242. End: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  1243. },
  1244. },
  1245. },
  1246. },
  1247. },
  1248. expected: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  1249. },
  1250. }
  1251. for _, test := range tests {
  1252. result, err := test.arg.End()
  1253. if test.expectError && err != nil {
  1254. continue
  1255. }
  1256. if test.expectError && err == nil {
  1257. t.Errorf("%s: expected error and got none", test.name)
  1258. } else if result != test.expected {
  1259. t.Errorf("%s: expected %s but got %s", test.name, test.expected, result)
  1260. }
  1261. }
  1262. }
  1263. func TestAssetSetRange_Minutes(t *testing.T) {
  1264. tests := []struct {
  1265. name string
  1266. arg *AssetSetRange
  1267. expected float64
  1268. }{
  1269. {
  1270. name: "Empty ASR",
  1271. arg: nil,
  1272. expected: 0,
  1273. },
  1274. {
  1275. name: "Single asset",
  1276. arg: &AssetSetRange{
  1277. Assets: []*AssetSet{
  1278. {
  1279. Assets: map[string]Asset{
  1280. "a": &Node{
  1281. Start: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  1282. End: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  1283. },
  1284. },
  1285. },
  1286. },
  1287. },
  1288. expected: 24 * 60,
  1289. },
  1290. {
  1291. name: "Two assets",
  1292. arg: &AssetSetRange{
  1293. Assets: []*AssetSet{
  1294. {
  1295. Assets: map[string]Asset{
  1296. "a": &Node{
  1297. Start: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  1298. End: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  1299. },
  1300. "b": &Node{
  1301. Start: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  1302. End: time.Date(1970, 1, 3, 0, 0, 0, 0, time.UTC),
  1303. },
  1304. },
  1305. },
  1306. },
  1307. },
  1308. expected: 2 * 24 * 60,
  1309. },
  1310. {
  1311. name: "Two AssetSets",
  1312. arg: &AssetSetRange{
  1313. Assets: []*AssetSet{
  1314. {
  1315. Assets: map[string]Asset{
  1316. "a": &Node{
  1317. Start: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC),
  1318. End: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  1319. },
  1320. },
  1321. },
  1322. {
  1323. Assets: map[string]Asset{
  1324. "b": &Node{
  1325. Start: time.Date(1970, 1, 2, 0, 0, 0, 0, time.UTC),
  1326. End: time.Date(1970, 1, 3, 0, 0, 0, 0, time.UTC),
  1327. },
  1328. },
  1329. },
  1330. },
  1331. },
  1332. expected: 2 * 24 * 60,
  1333. },
  1334. }
  1335. for _, test := range tests {
  1336. result := test.arg.Minutes()
  1337. if result != test.expected {
  1338. t.Errorf("%s: expected %f but got %f", test.name, test.expected, result)
  1339. }
  1340. }
  1341. }
  1342. func TestAssetSetRange_MarshalJSON(t *testing.T) {
  1343. tests := []struct {
  1344. name string
  1345. arg *AssetSetRange
  1346. expected *AssetSetRange
  1347. }{
  1348. {
  1349. name: "Nil ASR",
  1350. arg: nil,
  1351. },
  1352. {
  1353. name: "Nil AS in ASR",
  1354. arg: NewAssetSetRange(nil),
  1355. },
  1356. {
  1357. name: "Normal ASR",
  1358. arg: &AssetSetRange{
  1359. Assets: []*AssetSet{
  1360. {
  1361. Assets: map[string]Asset{
  1362. "a": &Any{
  1363. Start: time.Now().UTC().Truncate(day),
  1364. },
  1365. },
  1366. },
  1367. },
  1368. },
  1369. },
  1370. }
  1371. for _, test := range tests {
  1372. bytes, err := json.Marshal(test.arg)
  1373. if err != nil {
  1374. t.Fatalf("ASR Marshal: test %s, unexpected error: %s", test.name, err)
  1375. }
  1376. var testASR []*AssetSet
  1377. marshaled := &testASR
  1378. err = json.Unmarshal(bytes, marshaled)
  1379. if err != nil {
  1380. t.Fatalf("ASR Unmarshal: test %s: unexpected error: %s", test.name, err)
  1381. }
  1382. if test.arg.Length() != len(testASR) {
  1383. t.Fatalf("ASR Unmarshal: test %s: length mutated in encoding: expected %d but got %d", test.name, test.arg.Length(), len(testASR))
  1384. }
  1385. // asset don't unmarshal back from json
  1386. }
  1387. }
  1388. func TestAssetSetRange_AccumulateBy_None(t *testing.T) {
  1389. ago4d := time.Now().UTC().Truncate(day).Add(-4 * day)
  1390. ago3d := time.Now().UTC().Truncate(day).Add(-3 * day)
  1391. ago2d := time.Now().UTC().Truncate(day).Add(-2 * day)
  1392. yesterday := time.Now().UTC().Truncate(day).Add(-day)
  1393. today := time.Now().UTC().Truncate(day)
  1394. ago4dAS := GenerateMockAssetSet(ago4d, day)
  1395. ago3dAS := GenerateMockAssetSet(ago3d, day)
  1396. ago2dAS := GenerateMockAssetSet(ago2d, day)
  1397. yesterdayAS := GenerateMockAssetSet(yesterday, day)
  1398. todayAS := GenerateMockAssetSet(today, day)
  1399. asr := NewAssetSetRange(ago4dAS, ago3dAS, ago2dAS, yesterdayAS, todayAS)
  1400. asr, err := asr.Accumulate(AccumulateOptionNone)
  1401. if err != nil {
  1402. t.Fatalf("unexpected error calling accumulateBy: %s", err)
  1403. }
  1404. if len(asr.Assets) != 5 {
  1405. t.Fatalf("expected 5 asset sets, got:%d", len(asr.Assets))
  1406. }
  1407. }
  1408. func TestAssetSetRange_AccumulateBy_All(t *testing.T) {
  1409. ago4d := time.Now().UTC().Truncate(day).Add(-4 * day)
  1410. ago3d := time.Now().UTC().Truncate(day).Add(-3 * day)
  1411. ago2d := time.Now().UTC().Truncate(day).Add(-2 * day)
  1412. yesterday := time.Now().UTC().Truncate(day).Add(-day)
  1413. today := time.Now().UTC().Truncate(day)
  1414. ago4dAS := GenerateMockAssetSet(ago4d, day)
  1415. ago3dAS := GenerateMockAssetSet(ago3d, day)
  1416. ago2dAS := GenerateMockAssetSet(ago2d, day)
  1417. yesterdayAS := GenerateMockAssetSet(yesterday, day)
  1418. todayAS := GenerateMockAssetSet(today, day)
  1419. asr := NewAssetSetRange(ago4dAS, ago3dAS, ago2dAS, yesterdayAS, todayAS)
  1420. asr, err := asr.Accumulate(AccumulateOptionAll)
  1421. if err != nil {
  1422. t.Fatalf("unexpected error calling accumulateBy: %s", err)
  1423. }
  1424. if len(asr.Assets) != 1 {
  1425. t.Fatalf("expected 1 asset set, got:%d", len(asr.Assets))
  1426. }
  1427. }
  1428. func TestAssetSetRange_AccumulateBy_Hour(t *testing.T) {
  1429. ago4h := time.Now().UTC().Truncate(time.Hour).Add(-4 * time.Hour)
  1430. ago3h := time.Now().UTC().Truncate(time.Hour).Add(-3 * time.Hour)
  1431. ago2h := time.Now().UTC().Truncate(time.Hour).Add(-2 * time.Hour)
  1432. ago1h := time.Now().UTC().Truncate(time.Hour).Add(-time.Hour)
  1433. currentHour := time.Now().UTC().Truncate(time.Hour)
  1434. ago4hAS := GenerateMockAssetSet(ago4h, time.Hour)
  1435. ago3hAS := GenerateMockAssetSet(ago3h, time.Hour)
  1436. ago2hAS := GenerateMockAssetSet(ago2h, time.Hour)
  1437. ago1hAS := GenerateMockAssetSet(ago1h, time.Hour)
  1438. currentHourAS := GenerateMockAssetSet(currentHour, time.Hour)
  1439. asr := NewAssetSetRange(ago4hAS, ago3hAS, ago2hAS, ago1hAS, currentHourAS)
  1440. asr, err := asr.Accumulate(AccumulateOptionHour)
  1441. if err != nil {
  1442. t.Fatalf("unexpected error calling accumulateBy: %s", err)
  1443. }
  1444. if len(asr.Assets) != 5 {
  1445. t.Fatalf("expected 5 asset sets, got:%d", len(asr.Assets))
  1446. }
  1447. allocMap := asr.Assets[0].Assets
  1448. alloc := allocMap["__undefined__/__undefined__/__undefined__/Storage/cluster2/Disk/Kubernetes/gcp-disk4/disk4"]
  1449. if alloc.Minutes() != 60.0 {
  1450. t.Errorf("accumulating asset set range: expected %f minutes; actual %f", 60.0, alloc.Minutes())
  1451. }
  1452. }
  1453. func TestAssetSetRange_AccumulateBy_Day_From_Day(t *testing.T) {
  1454. ago4d := time.Now().UTC().Truncate(day).Add(-4 * day)
  1455. ago3d := time.Now().UTC().Truncate(day).Add(-3 * day)
  1456. ago2d := time.Now().UTC().Truncate(day).Add(-2 * day)
  1457. yesterday := time.Now().UTC().Truncate(day).Add(-day)
  1458. today := time.Now().UTC().Truncate(day)
  1459. ago4dAS := GenerateMockAssetSet(ago4d, day)
  1460. ago3dAS := GenerateMockAssetSet(ago3d, day)
  1461. ago2dAS := GenerateMockAssetSet(ago2d, day)
  1462. yesterdayAS := GenerateMockAssetSet(yesterday, day)
  1463. todayAS := GenerateMockAssetSet(today, day)
  1464. asr := NewAssetSetRange(ago4dAS, ago3dAS, ago2dAS, yesterdayAS, todayAS)
  1465. asr, err := asr.Accumulate(AccumulateOptionDay)
  1466. if err != nil {
  1467. t.Fatalf("unexpected error calling accumulateBy: %s", err)
  1468. }
  1469. if len(asr.Assets) != 5 {
  1470. t.Fatalf("expected 5 asset sets, got:%d", len(asr.Assets))
  1471. }
  1472. allocMap := asr.Assets[0].Assets
  1473. alloc := allocMap["__undefined__/__undefined__/__undefined__/Storage/cluster2/Disk/Kubernetes/gcp-disk4/disk4"]
  1474. if alloc.Minutes() != 1440.0 {
  1475. t.Errorf("accumulating asset set range: expected %f minutes; actual %f", 1440.0, alloc.Minutes())
  1476. }
  1477. }
  1478. func TestAssetSetRange_AccumulateBy_Day_From_Hours(t *testing.T) {
  1479. ago4h := time.Now().UTC().Truncate(time.Hour).Add(-4 * time.Hour)
  1480. ago3h := time.Now().UTC().Truncate(time.Hour).Add(-3 * time.Hour)
  1481. ago2h := time.Now().UTC().Truncate(time.Hour).Add(-2 * time.Hour)
  1482. ago1h := time.Now().UTC().Truncate(time.Hour).Add(-time.Hour)
  1483. currentHour := time.Now().UTC().Truncate(time.Hour)
  1484. ago4hAS := GenerateMockAssetSet(ago4h, time.Hour)
  1485. ago3hAS := GenerateMockAssetSet(ago3h, time.Hour)
  1486. ago2hAS := GenerateMockAssetSet(ago2h, time.Hour)
  1487. ago1hAS := GenerateMockAssetSet(ago1h, time.Hour)
  1488. currentHourAS := GenerateMockAssetSet(currentHour, time.Hour)
  1489. asr := NewAssetSetRange(ago4hAS, ago3hAS, ago2hAS, ago1hAS, currentHourAS)
  1490. asr, err := asr.Accumulate(AccumulateOptionDay)
  1491. if err != nil {
  1492. t.Fatalf("unexpected error calling accumulateBy: %s", err)
  1493. }
  1494. if len(asr.Assets) != 1 && len(asr.Assets) != 2 {
  1495. t.Fatalf("expected 1 allocation set, got:%d", len(asr.Assets))
  1496. }
  1497. allocMap := asr.Assets[0].Assets
  1498. alloc := allocMap["__undefined__/__undefined__/__undefined__/Storage/cluster2/Disk/Kubernetes/gcp-disk4/disk4"]
  1499. if alloc.Minutes() > 300.0 {
  1500. t.Errorf("accumulating AllocationSetRange: expected %f or less minutes; actual %f", 300.0, alloc.Minutes())
  1501. }
  1502. }
  1503. func TestAssetSetRange_AccumulateBy_Week(t *testing.T) {
  1504. ago9d := time.Now().UTC().Truncate(day).Add(-9 * day)
  1505. ago8d := time.Now().UTC().Truncate(day).Add(-8 * day)
  1506. ago7d := time.Now().UTC().Truncate(day).Add(-7 * day)
  1507. ago6d := time.Now().UTC().Truncate(day).Add(-6 * day)
  1508. ago5d := time.Now().UTC().Truncate(day).Add(-5 * day)
  1509. ago4d := time.Now().UTC().Truncate(day).Add(-4 * day)
  1510. ago3d := time.Now().UTC().Truncate(day).Add(-3 * day)
  1511. ago2d := time.Now().UTC().Truncate(day).Add(-2 * day)
  1512. yesterday := time.Now().UTC().Truncate(day).Add(-day)
  1513. today := time.Now().UTC().Truncate(day)
  1514. ago9dAS := GenerateMockAssetSet(ago9d, day)
  1515. ago8dAS := GenerateMockAssetSet(ago8d, day)
  1516. ago7dAS := GenerateMockAssetSet(ago7d, day)
  1517. ago6dAS := GenerateMockAssetSet(ago6d, day)
  1518. ago5dAS := GenerateMockAssetSet(ago5d, day)
  1519. ago4dAS := GenerateMockAssetSet(ago4d, day)
  1520. ago3dAS := GenerateMockAssetSet(ago3d, day)
  1521. ago2dAS := GenerateMockAssetSet(ago2d, day)
  1522. yesterdayAS := GenerateMockAssetSet(yesterday, day)
  1523. todayAS := GenerateMockAssetSet(today, day)
  1524. asr := NewAssetSetRange(ago9dAS, ago8dAS, ago7dAS, ago6dAS, ago5dAS, ago4dAS, ago3dAS, ago2dAS, yesterdayAS, todayAS)
  1525. asr, err := asr.Accumulate(AccumulateOptionWeek)
  1526. if err != nil {
  1527. t.Fatalf("unexpected error calling accumulateBy: %s", err)
  1528. }
  1529. if len(asr.Assets) != 2 && len(asr.Assets) != 3 {
  1530. t.Fatalf("expected 2 or 3 asset sets, got:%d", len(asr.Assets))
  1531. }
  1532. for _, as := range asr.Assets {
  1533. if as.Window.Duration() < time.Hour*24 || as.Window.Duration() > time.Hour*24*7 {
  1534. t.Fatalf("expected window duration to be between 1 and 7 days, got:%s", as.Window.Duration().String())
  1535. }
  1536. }
  1537. }
  1538. func TestAssetSetRange_AccumulateBy_Month(t *testing.T) {
  1539. prevMonth1stDay := time.Date(2020, 01, 29, 0, 0, 0, 0, time.UTC)
  1540. prevMonth2ndDay := time.Date(2020, 01, 30, 0, 0, 0, 0, time.UTC)
  1541. prevMonth3ndDay := time.Date(2020, 01, 31, 0, 0, 0, 0, time.UTC)
  1542. nextMonth1stDay := time.Date(2020, 02, 01, 0, 0, 0, 0, time.UTC)
  1543. prev1AS := GenerateMockAssetSet(prevMonth1stDay, day)
  1544. prev2AS := GenerateMockAssetSet(prevMonth2ndDay, day)
  1545. prev3AS := GenerateMockAssetSet(prevMonth3ndDay, day)
  1546. nextAS := GenerateMockAssetSet(nextMonth1stDay, day)
  1547. asr := NewAssetSetRange(prev1AS, prev2AS, prev3AS, nextAS)
  1548. asr, err := asr.Accumulate(AccumulateOptionMonth)
  1549. if err != nil {
  1550. t.Fatalf("unexpected error calling accumulateBy: %s", err)
  1551. }
  1552. if len(asr.Assets) != 2 {
  1553. t.Fatalf("expected 2 assets sets, got:%d", len(asr.Assets))
  1554. }
  1555. for _, as := range asr.Assets {
  1556. if as.Window.Duration() < time.Hour*24 || as.Window.Duration() > time.Hour*24*31 {
  1557. t.Fatalf("expected window duration to be between 1 and 31 days, got:%s", as.Window.Duration().String())
  1558. }
  1559. }
  1560. }
  1561. func TestAny_SanitizeNaN(t *testing.T) {
  1562. any := getMockAny(math.NaN())
  1563. any.SanitizeNaN()
  1564. v := reflect.ValueOf(any)
  1565. checkAllFloat64sForNaN(t, v, "TestAny_SanitizeNaN")
  1566. }
  1567. func getMockAny(f float64) Any {
  1568. return Any{
  1569. Adjustment: f,
  1570. Cost: f,
  1571. }
  1572. }
  1573. func TestCloud_SanitizeNaN(t *testing.T) {
  1574. cloud := getMockCloud(math.NaN())
  1575. cloud.SanitizeNaN()
  1576. v := reflect.ValueOf(cloud)
  1577. checkAllFloat64sForNaN(t, v, "TestCloud_SanitizeNaN")
  1578. }
  1579. func getMockCloud(f float64) Cloud {
  1580. return Cloud{
  1581. Adjustment: f,
  1582. Cost: f,
  1583. Credit: f,
  1584. }
  1585. }
  1586. func TestClusterManagement_SanitizeNaN(t *testing.T) {
  1587. cm := getMockClusterManagement(math.NaN())
  1588. cm.SanitizeNaN()
  1589. v := reflect.ValueOf(cm)
  1590. checkAllFloat64sForNaN(t, v, "TestClusterManagement_SanitizeNaN")
  1591. }
  1592. func getMockClusterManagement(f float64) ClusterManagement {
  1593. return ClusterManagement{
  1594. Cost: f,
  1595. Adjustment: f,
  1596. }
  1597. }
  1598. func TestDisk_SanitizeNaN(t *testing.T) {
  1599. disk := getMockDisk(math.NaN())
  1600. disk.SanitizeNaN()
  1601. v := reflect.ValueOf(disk)
  1602. checkAllFloat64sForNaN(t, v, "TestDisk_SanitizeNaN")
  1603. vBreakdown := reflect.ValueOf(*disk.Breakdown)
  1604. checkAllFloat64sForNaN(t, vBreakdown, "TestDisk_SanitizeNaN")
  1605. }
  1606. func getMockDisk(f float64) Disk {
  1607. bhu := f
  1608. bum := f
  1609. breakdown := getMockBreakdown(f)
  1610. return Disk{
  1611. Adjustment: f,
  1612. Cost: f,
  1613. ByteHours: f,
  1614. Local: f,
  1615. Breakdown: &breakdown,
  1616. ByteHoursUsed: &bhu,
  1617. ByteUsageMax: &bum,
  1618. }
  1619. }
  1620. func TestBreakdown_SanitizeNaN(t *testing.T) {
  1621. b := getMockBreakdown(math.NaN())
  1622. b.SanitizeNaN()
  1623. v := reflect.ValueOf(b)
  1624. checkAllFloat64sForNaN(t, v, "TestBreakdown_SanitizeNaN")
  1625. }
  1626. func getMockBreakdown(f float64) Breakdown {
  1627. return Breakdown{
  1628. Idle: f,
  1629. Other: f,
  1630. System: f,
  1631. User: f,
  1632. }
  1633. }
  1634. func TestNetwork_SanitizeNaN(t *testing.T) {
  1635. n := getMockNetwork(math.NaN())
  1636. n.SanitizeNaN()
  1637. v := reflect.ValueOf(n)
  1638. checkAllFloat64sForNaN(t, v, "TestNetwork_SanitizeNaN")
  1639. }
  1640. func getMockNetwork(f float64) Network {
  1641. return Network{
  1642. Adjustment: f,
  1643. Cost: f,
  1644. }
  1645. }
  1646. func TestNodeOverhead_SanitizeNaN(t *testing.T) {
  1647. n := getMockNodeOverhead(math.NaN())
  1648. n.SanitizeNaN()
  1649. v := reflect.ValueOf(n)
  1650. checkAllFloat64sForNaN(t, v, "TestNodeOverhead_SanitizeNaN")
  1651. }
  1652. func getMockNodeOverhead(f float64) NodeOverhead {
  1653. return NodeOverhead{
  1654. CpuOverheadFraction: f,
  1655. RamOverheadFraction: f,
  1656. OverheadCostFraction: f,
  1657. }
  1658. }
  1659. func TestNode_SanitizeNaN(t *testing.T) {
  1660. n := getMockNode(math.NaN())
  1661. n.SanitizeNaN()
  1662. v := reflect.ValueOf(n)
  1663. checkAllFloat64sForNaN(t, v, "TestNode_SanitizeNaN")
  1664. vCpu := reflect.ValueOf(*n.CPUBreakdown)
  1665. checkAllFloat64sForNaN(t, vCpu, "TestNode_SanitizeNaN")
  1666. vRam := reflect.ValueOf(*n.RAMBreakdown)
  1667. checkAllFloat64sForNaN(t, vRam, "TestNode_SanitizeNaN")
  1668. vOverhead := reflect.ValueOf(*n.Overhead)
  1669. checkAllFloat64sForNaN(t, vOverhead, "TestNode_SanitizeNaN")
  1670. }
  1671. func getMockNode(f float64) Node {
  1672. cpuBreakdown := getMockBreakdown(f)
  1673. ramBreakdown := getMockBreakdown(f)
  1674. overhead := getMockNodeOverhead(f)
  1675. return Node{
  1676. Adjustment: f,
  1677. CPUCoreHours: f,
  1678. RAMByteHours: f,
  1679. GPUHours: f,
  1680. CPUBreakdown: &cpuBreakdown,
  1681. RAMBreakdown: &ramBreakdown,
  1682. CPUCost: f,
  1683. GPUCost: f,
  1684. GPUCount: f,
  1685. RAMCost: f,
  1686. Discount: f,
  1687. Preemptible: f,
  1688. Overhead: &overhead,
  1689. }
  1690. }
  1691. func TestLoadBalancer_SanitizeNaN(t *testing.T) {
  1692. lb := getMockLoadBalancer(math.NaN())
  1693. lb.SanitizeNaN()
  1694. v := reflect.ValueOf(lb)
  1695. checkAllFloat64sForNaN(t, v, "TestLoadBalancer_SanitizeNaN")
  1696. }
  1697. func getMockLoadBalancer(f float64) LoadBalancer {
  1698. return LoadBalancer{
  1699. Adjustment: f,
  1700. Cost: f,
  1701. }
  1702. }
  1703. func TestSharedAsset_SanitizeNaN(t *testing.T) {
  1704. sa := getMockSharedAsset(math.NaN())
  1705. sa.SanitizeNaN()
  1706. v := reflect.ValueOf(sa)
  1707. checkAllFloat64sForNaN(t, v, "TestSharedAsset_SanitizeNaN")
  1708. }
  1709. func getMockSharedAsset(f float64) SharedAsset {
  1710. return SharedAsset{
  1711. Cost: f,
  1712. }
  1713. }
  1714. func TestAssetSet_SanitizeNaN(t *testing.T) {
  1715. testCaseName := "TestAssetSet_SanitizeNaN"
  1716. as := getMockAssetSet(math.NaN())
  1717. as.SanitizeNaN()
  1718. v := reflect.ValueOf(as)
  1719. checkAllFloat64sForNaN(t, v, testCaseName)
  1720. for _, a := range as.Assets {
  1721. if math.IsNaN(a.TotalCost()) {
  1722. t.Fatalf("TestAssetSet_SanitizeNaN: Asset: expected not NaN for TotalCost(): expected NaN, got:%f", a.TotalCost())
  1723. }
  1724. if math.IsNaN(a.GetAdjustment()) {
  1725. t.Fatalf("TestAssetSet_SanitizeNaN: Asset: expected not NaN for GetAdjustment(): expected NaN, got:%f", a.GetAdjustment())
  1726. }
  1727. }
  1728. for _, any := range as.Any {
  1729. vAny := reflect.ValueOf(*any)
  1730. checkAllFloat64sForNaN(t, vAny, testCaseName)
  1731. }
  1732. for _, cloud := range as.Cloud {
  1733. vCloud := reflect.ValueOf(*cloud)
  1734. checkAllFloat64sForNaN(t, vCloud, testCaseName)
  1735. }
  1736. for _, cm := range as.ClusterManagement {
  1737. vCM := reflect.ValueOf(*cm)
  1738. checkAllFloat64sForNaN(t, vCM, testCaseName)
  1739. }
  1740. for _, disk := range as.Disks {
  1741. vDisk := reflect.ValueOf(*disk)
  1742. checkAllFloat64sForNaN(t, vDisk, testCaseName)
  1743. }
  1744. for _, network := range as.Network {
  1745. vNetwork := reflect.ValueOf(*network)
  1746. checkAllFloat64sForNaN(t, vNetwork, testCaseName)
  1747. }
  1748. for _, node := range as.Nodes {
  1749. vNode := reflect.ValueOf(*node)
  1750. checkAllFloat64sForNaN(t, vNode, testCaseName)
  1751. }
  1752. for _, sa := range as.SharedAssets {
  1753. vSA := reflect.ValueOf(*sa)
  1754. checkAllFloat64sForNaN(t, vSA, testCaseName)
  1755. }
  1756. }
  1757. func getMockAssetSet(f float64) AssetSet {
  1758. any := getMockAny(f)
  1759. cloud := getMockCloud(f)
  1760. cm := getMockClusterManagement(f)
  1761. disk := getMockDisk(f)
  1762. network := getMockNetwork(f)
  1763. node := getMockNode(f)
  1764. lb := getMockLoadBalancer(f)
  1765. sa := getMockSharedAsset(f)
  1766. assets := map[string]Asset{"any": &any, "cloud": &cloud}
  1767. as := AssetSet{
  1768. Assets: assets,
  1769. Any: map[string]*Any{"NaN": &any},
  1770. Cloud: map[string]*Cloud{"NaN": &cloud},
  1771. ClusterManagement: map[string]*ClusterManagement{"NaN": &cm},
  1772. Disks: map[string]*Disk{"NaN": &disk},
  1773. Network: map[string]*Network{"NaN": &network},
  1774. Nodes: map[string]*Node{"NaN": &node},
  1775. LoadBalancers: map[string]*LoadBalancer{"NaN": &lb},
  1776. SharedAssets: map[string]*SharedAsset{"NaN": &sa},
  1777. }
  1778. return as
  1779. }