cloudcostaggregate_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. package kubecost
  2. import (
  3. "github.com/opencost/opencost/pkg/util/timeutil"
  4. "testing"
  5. "time"
  6. )
  7. var ccaProperties1 = CloudCostAggregateProperties{
  8. Provider: "provider1",
  9. WorkGroupID: "workgroup1",
  10. BillingID: "billing1",
  11. Service: "service1",
  12. LabelValue: "labelValue1",
  13. }
  14. // TestCloudCostAggregate_LoadCloudCostAggregate checks that loaded CloudCostAggregates end up in the correct set in the
  15. // correct proportions
  16. func TestCloudCostAggregate_LoadCloudCostAggregate(t *testing.T) {
  17. // create values for 3 day Range tests
  18. end := RoundBack(time.Now().UTC(), timeutil.Day)
  19. start := end.Add(-3 * timeutil.Day)
  20. dayWindows, _ := GetWindows(start, end, timeutil.Day)
  21. emtpyCASSR, _ := NewCloudCostAggregateSetRange(start, end, timeutil.Day, "integration", "label")
  22. testCases := map[string]struct {
  23. cca []*CloudCostAggregate
  24. windows []Window
  25. ccasr *CloudCostAggregateSetRange
  26. expected []*CloudCostAggregateSet
  27. }{
  28. "Load Single Day On Grid": {
  29. cca: []*CloudCostAggregate{
  30. {
  31. Properties: ccaProperties1,
  32. KubernetesPercent: 1,
  33. Cost: 100,
  34. NetCost: 80,
  35. },
  36. },
  37. windows: []Window{
  38. dayWindows[0],
  39. },
  40. ccasr: emtpyCASSR.Clone(),
  41. expected: []*CloudCostAggregateSet{
  42. {
  43. Integration: "integration",
  44. LabelName: "label",
  45. Window: dayWindows[0],
  46. CloudCostAggregates: map[string]*CloudCostAggregate{
  47. ccaProperties1.Key(nil): {
  48. Properties: ccaProperties1,
  49. KubernetesPercent: 1,
  50. Cost: 100,
  51. NetCost: 80,
  52. },
  53. },
  54. },
  55. {
  56. Integration: "integration",
  57. LabelName: "label",
  58. Window: dayWindows[1],
  59. CloudCostAggregates: map[string]*CloudCostAggregate{},
  60. },
  61. {
  62. Integration: "integration",
  63. LabelName: "label",
  64. Window: dayWindows[2],
  65. CloudCostAggregates: map[string]*CloudCostAggregate{},
  66. },
  67. },
  68. },
  69. "Load Single Day Off Grid": {
  70. cca: []*CloudCostAggregate{
  71. {
  72. Properties: ccaProperties1,
  73. KubernetesPercent: 1,
  74. Cost: 100,
  75. NetCost: 80,
  76. },
  77. },
  78. windows: []Window{
  79. NewClosedWindow(start.Add(12*time.Hour), start.Add(36*time.Hour)),
  80. },
  81. ccasr: emtpyCASSR.Clone(),
  82. expected: []*CloudCostAggregateSet{
  83. {
  84. Integration: "integration",
  85. LabelName: "label",
  86. Window: dayWindows[0],
  87. CloudCostAggregates: map[string]*CloudCostAggregate{
  88. ccaProperties1.Key(nil): {
  89. Properties: ccaProperties1,
  90. KubernetesPercent: 1,
  91. Cost: 50,
  92. NetCost: 40,
  93. },
  94. },
  95. },
  96. {
  97. Integration: "integration",
  98. LabelName: "label",
  99. Window: dayWindows[1],
  100. CloudCostAggregates: map[string]*CloudCostAggregate{
  101. ccaProperties1.Key(nil): {
  102. Properties: ccaProperties1,
  103. KubernetesPercent: 1,
  104. Cost: 50,
  105. NetCost: 40,
  106. },
  107. },
  108. },
  109. {
  110. Integration: "integration",
  111. LabelName: "label",
  112. Window: dayWindows[2],
  113. CloudCostAggregates: map[string]*CloudCostAggregate{},
  114. },
  115. },
  116. },
  117. "Load Single Day Off Grid Before Range Window": {
  118. cca: []*CloudCostAggregate{
  119. {
  120. Properties: ccaProperties1,
  121. KubernetesPercent: 1,
  122. Cost: 100,
  123. NetCost: 80,
  124. },
  125. },
  126. windows: []Window{
  127. NewClosedWindow(start.Add(-12*time.Hour), start.Add(12*time.Hour)),
  128. },
  129. ccasr: emtpyCASSR.Clone(),
  130. expected: []*CloudCostAggregateSet{
  131. {
  132. Integration: "integration",
  133. LabelName: "label",
  134. Window: dayWindows[0],
  135. CloudCostAggregates: map[string]*CloudCostAggregate{
  136. ccaProperties1.Key(nil): {
  137. Properties: ccaProperties1,
  138. KubernetesPercent: 1,
  139. Cost: 50,
  140. NetCost: 40,
  141. },
  142. },
  143. },
  144. {
  145. Integration: "integration",
  146. LabelName: "label",
  147. Window: dayWindows[1],
  148. CloudCostAggregates: map[string]*CloudCostAggregate{},
  149. },
  150. {
  151. Integration: "integration",
  152. LabelName: "label",
  153. Window: dayWindows[2],
  154. CloudCostAggregates: map[string]*CloudCostAggregate{},
  155. },
  156. },
  157. },
  158. "Load Single Day Off Grid After Range Window": {
  159. cca: []*CloudCostAggregate{
  160. {
  161. Properties: ccaProperties1,
  162. KubernetesPercent: 1,
  163. Cost: 100,
  164. NetCost: 80,
  165. },
  166. },
  167. windows: []Window{
  168. NewClosedWindow(end.Add(-12*time.Hour), end.Add(12*time.Hour)),
  169. },
  170. ccasr: emtpyCASSR.Clone(),
  171. expected: []*CloudCostAggregateSet{
  172. {
  173. Integration: "integration",
  174. LabelName: "label",
  175. Window: dayWindows[0],
  176. CloudCostAggregates: map[string]*CloudCostAggregate{},
  177. },
  178. {
  179. Integration: "integration",
  180. LabelName: "label",
  181. Window: dayWindows[1],
  182. CloudCostAggregates: map[string]*CloudCostAggregate{},
  183. },
  184. {
  185. Integration: "integration",
  186. LabelName: "label",
  187. Window: dayWindows[2],
  188. CloudCostAggregates: map[string]*CloudCostAggregate{
  189. ccaProperties1.Key(nil): {
  190. Properties: ccaProperties1,
  191. KubernetesPercent: 1,
  192. Cost: 50,
  193. NetCost: 40,
  194. },
  195. },
  196. },
  197. },
  198. },
  199. "Single Day Kubecost Percent": {
  200. cca: []*CloudCostAggregate{
  201. {
  202. Properties: ccaProperties1,
  203. KubernetesPercent: 1,
  204. Cost: 75,
  205. NetCost: 60,
  206. },
  207. {
  208. Properties: ccaProperties1,
  209. KubernetesPercent: 0,
  210. Cost: 25,
  211. NetCost: 20,
  212. },
  213. },
  214. windows: []Window{
  215. dayWindows[1],
  216. dayWindows[1],
  217. },
  218. ccasr: emtpyCASSR.Clone(),
  219. expected: []*CloudCostAggregateSet{
  220. {
  221. Integration: "integration",
  222. LabelName: "label",
  223. Window: dayWindows[0],
  224. CloudCostAggregates: map[string]*CloudCostAggregate{},
  225. },
  226. {
  227. Integration: "integration",
  228. LabelName: "label",
  229. Window: dayWindows[1],
  230. CloudCostAggregates: map[string]*CloudCostAggregate{
  231. ccaProperties1.Key(nil): {
  232. Properties: ccaProperties1,
  233. KubernetesPercent: 0.75,
  234. Cost: 100,
  235. NetCost: 80,
  236. },
  237. },
  238. },
  239. {
  240. Integration: "integration",
  241. LabelName: "label",
  242. Window: dayWindows[2],
  243. CloudCostAggregates: map[string]*CloudCostAggregate{},
  244. },
  245. },
  246. },
  247. }
  248. for name, tc := range testCases {
  249. t.Run(name, func(t *testing.T) {
  250. // load Cloud Cost Aggregates
  251. for i, cca := range tc.cca {
  252. tc.ccasr.LoadCloudCostAggregate(tc.windows[i], cca)
  253. }
  254. if len(tc.ccasr.CloudCostAggregateSets) != len(tc.expected) {
  255. t.Errorf("the CloudCostAggregateSetRanges did not have the expected length")
  256. }
  257. for i, ccas := range tc.ccasr.CloudCostAggregateSets {
  258. if !ccas.Equal(tc.expected[i]) {
  259. t.Errorf("CloudCostAggregateSet at index: %d did not match expected", i)
  260. }
  261. }
  262. })
  263. }
  264. }