aggregation_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package costmodel
  2. import (
  3. "testing"
  4. "github.com/opencost/opencost/core/pkg/opencost"
  5. "github.com/opencost/opencost/core/pkg/util"
  6. )
  7. func TestScaleHourlyCostData(t *testing.T) {
  8. costData := map[string]*CostData{}
  9. start := 1570000000
  10. oneHour := 60 * 60
  11. generateVectorSeries := func(start, count, interval int, value float64) []*util.Vector {
  12. vs := []*util.Vector{}
  13. for i := 0; i < count; i++ {
  14. v := &util.Vector{
  15. Timestamp: float64(start + (i * oneHour)),
  16. Value: value,
  17. }
  18. vs = append(vs, v)
  19. }
  20. return vs
  21. }
  22. costData["default"] = &CostData{
  23. RAMReq: generateVectorSeries(start, 100, oneHour, 1.0),
  24. RAMUsed: generateVectorSeries(start, 0, oneHour, 1.0),
  25. RAMAllocation: generateVectorSeries(start, 100, oneHour, 107.226),
  26. CPUReq: generateVectorSeries(start, 100, oneHour, 0.00317),
  27. CPUUsed: generateVectorSeries(start, 95, oneHour, 1.0),
  28. CPUAllocation: generateVectorSeries(start, 2, oneHour, 123.456),
  29. PVCData: []*PersistentVolumeClaimData{
  30. {Values: generateVectorSeries(start, 100, oneHour, 1.34)},
  31. },
  32. }
  33. compressedData := ScaleHourlyCostData(costData, 10)
  34. act, ok := compressedData["default"]
  35. if !ok {
  36. t.Errorf("compressed data should have key \"default\"")
  37. }
  38. // RAMReq
  39. if len(act.RAMReq) != 10 {
  40. t.Errorf("expected RAMReq to have length %d, was actually %d", 10, len(act.RAMReq))
  41. }
  42. for _, val := range act.RAMReq {
  43. if val.Value != 10.0 {
  44. t.Errorf("expected each RAMReq Vector to have Value %f, was actually %f", 10.0, val.Value)
  45. }
  46. }
  47. // RAMUsed
  48. if len(act.RAMUsed) != 0 {
  49. t.Errorf("expected RAMUsed to have length %d, was actually %d", 0, len(act.RAMUsed))
  50. }
  51. // RAMAllocation
  52. if len(act.RAMAllocation) != 10 {
  53. t.Errorf("expected RAMAllocation to have length %d, was actually %d", 10, len(act.RAMAllocation))
  54. }
  55. for _, val := range act.RAMAllocation {
  56. if val.Value != 1072.26 {
  57. t.Errorf("expected each RAMAllocation Vector to have Value %f, was actually %f", 1072.26, val.Value)
  58. }
  59. }
  60. // CPUReq
  61. if len(act.CPUReq) != 10 {
  62. t.Errorf("expected CPUReq to have length %d, was actually %d", 10, len(act.CPUReq))
  63. }
  64. for _, val := range act.CPUReq {
  65. if val.Value != 0.0317 {
  66. t.Errorf("expected each CPUReq Vector to have Value %f, was actually %f", 0.0317, val.Value)
  67. }
  68. }
  69. // CPUUsed
  70. if len(act.CPUUsed) != 10 {
  71. t.Errorf("expected CPUUsed to have length %d, was actually %d", 10, len(act.CPUUsed))
  72. }
  73. for _, val := range act.CPUUsed[:len(act.CPUUsed)-1] {
  74. if val.Value != 10.0 {
  75. t.Errorf("expected each CPUUsed Vector to have Value %f, was actually %f", 10.0, val.Value)
  76. }
  77. }
  78. if act.CPUUsed[len(act.CPUUsed)-1].Value != 5.0 {
  79. t.Errorf("expected each CPUUsed Vector to have Value %f, was actually %f", 5.0, act.CPUUsed[len(act.CPUUsed)-1].Value)
  80. }
  81. // CPUAllocation
  82. if len(act.CPUAllocation) != 1 {
  83. t.Errorf("expected CPUAllocation to have length %d, was actually %d", 1, len(act.CPUAllocation))
  84. }
  85. if act.CPUAllocation[0].Value != 246.912 {
  86. t.Errorf("expected each CPUAllocation Vector to have Value %f, was actually %f", 246.912, act.CPUAllocation[len(act.CPUAllocation)-1].Value)
  87. }
  88. // PVCData
  89. if len(act.PVCData[0].Values) != 10 {
  90. t.Errorf("expected PVCData[0] to have length %d, was actually %d", 10, len(act.PVCData[0].Values))
  91. }
  92. for _, val := range act.PVCData[0].Values {
  93. if val.Value != 13.4 {
  94. t.Errorf("expected each PVCData[0] Vector to have Value %f, was actually %f", 13.4, val.Value)
  95. }
  96. }
  97. costData["default"] = &CostData{
  98. RAMReq: generateVectorSeries(start, 100, oneHour, 1.0),
  99. RAMUsed: generateVectorSeries(start, 0, oneHour, 1.0),
  100. RAMAllocation: generateVectorSeries(start, 100, oneHour, 107.226),
  101. CPUReq: generateVectorSeries(start, 100, oneHour, 0.00317),
  102. CPUUsed: generateVectorSeries(start, 95, oneHour, 1.0),
  103. CPUAllocation: generateVectorSeries(start, 2, oneHour, 124.6),
  104. PVCData: []*PersistentVolumeClaimData{
  105. {Values: generateVectorSeries(start, 100, oneHour, 1.34)},
  106. },
  107. }
  108. scaledData := ScaleHourlyCostData(costData, 0.1)
  109. act, ok = scaledData["default"]
  110. if !ok {
  111. t.Errorf("scaled data should have key \"default\"")
  112. }
  113. // RAMReq
  114. if len(act.RAMReq) != 100 {
  115. t.Errorf("expected RAMReq to have length %d, was actually %d", 100, len(act.RAMReq))
  116. }
  117. for _, val := range act.RAMReq {
  118. if val.Value != 0.1 {
  119. t.Errorf("expected each RAMReq Vector to have Value %f, was actually %f", 0.1, val.Value)
  120. }
  121. }
  122. // RAMUsed
  123. if len(act.RAMUsed) != 0 {
  124. t.Errorf("expected RAMUsed to have length %d, was actually %d", 0, len(act.RAMUsed))
  125. }
  126. // RAMAllocation
  127. if len(act.RAMAllocation) != 100 {
  128. t.Errorf("expected RAMAllocation to have length %d, was actually %d", 100, len(act.RAMAllocation))
  129. }
  130. for _, val := range act.RAMAllocation {
  131. if val.Value != 10.7226 {
  132. t.Errorf("expected each RAMAllocation Vector to have Value %f, was actually %f", 10.7226, val.Value)
  133. }
  134. }
  135. // CPUReq
  136. if len(act.CPUReq) != 100 {
  137. t.Errorf("expected CPUReq to have length %d, was actually %d", 100, len(act.CPUReq))
  138. }
  139. for _, val := range act.CPUReq {
  140. if val.Value != 0.000317 {
  141. t.Errorf("expected each CPUReq Vector to have Value %f, was actually %f", 0.000317, val.Value)
  142. }
  143. }
  144. // CPUUsed
  145. if len(act.CPUUsed) != 95 {
  146. t.Errorf("expected CPUUsed to have length %d, was actually %d", 95, len(act.CPUUsed))
  147. }
  148. for _, val := range act.CPUUsed {
  149. if val.Value != 0.1 {
  150. t.Errorf("expected each CPUUsed Vector to have Value %f, was actually %f", 0.1, val.Value)
  151. }
  152. }
  153. // CPUAllocation
  154. if len(act.CPUAllocation) != 2 {
  155. t.Errorf("expected CPUAllocation to have length %d, was actually %d", 2, len(act.CPUAllocation))
  156. }
  157. for _, val := range act.CPUAllocation {
  158. if val.Value != 12.46 {
  159. t.Errorf("expected each CPUAllocation Vector to have Value %f, was actually %f", 12.46, val.Value)
  160. }
  161. }
  162. // PVCData
  163. if len(act.PVCData[0].Values) != 100 {
  164. t.Errorf("expected PVCData[0] to have length %d, was actually %d", 100, len(act.PVCData[0].Values))
  165. }
  166. for _, val := range act.PVCData[0].Values {
  167. if val.Value != .134 {
  168. t.Errorf("expected each PVCData[0] Vector to have Value %f, was actually %f", .134, val.Value)
  169. }
  170. }
  171. }
  172. func TestParseAggregationProperties_Default(t *testing.T) {
  173. got, err := ParseAggregationProperties([]string{})
  174. expected := []string{
  175. opencost.AllocationClusterProp,
  176. opencost.AllocationNodeProp,
  177. opencost.AllocationNamespaceProp,
  178. opencost.AllocationPodProp,
  179. opencost.AllocationContainerProp,
  180. }
  181. if err != nil {
  182. t.Fatalf("TestParseAggregationPropertiesDefault: unexpected error: %s", err)
  183. }
  184. if len(expected) != len(got) {
  185. t.Fatalf("TestParseAggregationPropertiesDefault: expected length of %d, got: %d", len(expected), len(got))
  186. }
  187. for i := range got {
  188. if got[i] != expected[i] {
  189. t.Fatalf("TestParseAggregationPropertiesDefault: expected[i] should be %s, got[i]:%s", expected[i], got[i])
  190. }
  191. }
  192. }
  193. func TestParseAggregationProperties_All(t *testing.T) {
  194. got, err := ParseAggregationProperties([]string{"all"})
  195. if err != nil {
  196. t.Fatalf("TestParseAggregationPropertiesDefault: unexpected error: %s", err)
  197. }
  198. if len(got) != 0 {
  199. t.Fatalf("TestParseAggregationPropertiesDefault: expected length of 0, got: %d", len(got))
  200. }
  201. }