allocation_json_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package kubecost
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "time"
  6. )
  7. func TestAllocation_MarshalJSON(t *testing.T) {
  8. start := time.Date(2021, time.January, 1, 0, 0, 0, 0, time.UTC)
  9. end := time.Date(2021, time.January, 2, 0, 0, 0, 0, time.UTC)
  10. hrs := 24.0
  11. gib := 1024.0 * 1024.0 * 1024.0
  12. cpuPrice := 0.02
  13. gpuPrice := 2.00
  14. ramPrice := 0.01
  15. pvPrice := 0.00005
  16. before := &Allocation{
  17. Name: "cluster1/namespace1/node1/pod1/container1",
  18. Properties: &AllocationProperties{
  19. Cluster: "cluster1",
  20. Node: "node1",
  21. Namespace: "namespace1",
  22. Pod: "pod1",
  23. Container: "container1",
  24. },
  25. Window: NewWindow(&start, &end),
  26. Start: start,
  27. End: end,
  28. CPUCoreHours: 2.0 * hrs,
  29. CPUCoreRequestAverage: 2.0,
  30. CPUCoreUsageAverage: 1.0,
  31. CPUCost: 2.0 * hrs * cpuPrice,
  32. CPUCostAdjustment: 3.0,
  33. GPUHours: 1.0 * hrs,
  34. GPUCost: 1.0 * hrs * gpuPrice,
  35. GPUCostAdjustment: 2.0,
  36. NetworkCost: 0.05,
  37. LoadBalancerCost: 0.02,
  38. PVs: PVAllocations{
  39. disk: {
  40. ByteHours: 100.0 * gib * hrs,
  41. Cost: 100.0 * hrs * pvPrice,
  42. },
  43. },
  44. PVCostAdjustment: 4.0,
  45. RAMByteHours: 8.0 * gib * hrs,
  46. RAMBytesRequestAverage: 8.0 * gib,
  47. RAMBytesUsageAverage: 4.0 * gib,
  48. RAMCost: 8.0 * hrs * ramPrice,
  49. RAMCostAdjustment: 1.0,
  50. SharedCost: 2.00,
  51. ExternalCost: 1.00,
  52. RawAllocationOnly: &RawAllocationOnlyData{},
  53. }
  54. data, err := json.Marshal(before)
  55. if err != nil {
  56. t.Fatalf("Allocation.MarshalJSON: unexpected error: %s", err)
  57. }
  58. after := &Allocation{}
  59. err = json.Unmarshal(data, after)
  60. if err != nil {
  61. t.Fatalf("Allocation.UnmarshalJSON: unexpected error: %s", err)
  62. }
  63. // TODO:CLEANUP fix json marshaling of Window so that all of this works.
  64. // In the meantime, just set the Window so that we can test the rest.
  65. after.Window = before.Window.Clone()
  66. if !after.Equal(before) {
  67. t.Fatalf("Allocation.MarshalJSON: before and after are not equal")
  68. }
  69. }
  70. func TestPVAllocations_MarshalJSON(t *testing.T) {
  71. testCases := map[string]PVAllocations{
  72. "empty": {},
  73. "single": {
  74. {
  75. Cluster: "cluster1",
  76. Name: "pv1",
  77. }: {
  78. ByteHours: 100,
  79. Cost: 1,
  80. },
  81. },
  82. "multi": {
  83. {
  84. Cluster: "cluster1",
  85. Name: "pv1",
  86. }: {
  87. ByteHours: 100,
  88. Cost: 1,
  89. },
  90. {
  91. Cluster: "cluster1",
  92. Name: "pv2",
  93. }: {
  94. ByteHours: 200,
  95. Cost: 2,
  96. },
  97. },
  98. "emptyPV": {
  99. {
  100. Cluster: "cluster1",
  101. Name: "pv1",
  102. }: {},
  103. },
  104. "emptyKey": {
  105. {}: {
  106. ByteHours: 100,
  107. Cost: 1,
  108. },
  109. },
  110. }
  111. for name, before := range testCases {
  112. t.Run(name, func(t *testing.T) {
  113. data, err := json.Marshal(before)
  114. if err != nil {
  115. t.Fatalf("PVAllocations.MarshalJSON: unexpected error: %s", err)
  116. }
  117. after := PVAllocations{}
  118. err = json.Unmarshal(data, &after)
  119. if err != nil {
  120. t.Fatalf("PVAllocations.UnmarshalJSON: unexpected error: %s", err)
  121. }
  122. if len(before) != len(after) {
  123. t.Fatalf("PVAllocations.MarshalJSON: before and after are not equal")
  124. }
  125. for pvKey, beforePV := range before {
  126. afterPV, ok := after[pvKey]
  127. if !ok {
  128. t.Fatalf("PVAllocations.MarshalJSON: after missing PVKey %s", pvKey)
  129. }
  130. if beforePV.Cost != afterPV.Cost {
  131. t.Fatalf("PVAllocations.MarshalJSON: PVAllocation Cost not equal for PVKey %s", pvKey)
  132. }
  133. if beforePV.ByteHours != afterPV.ByteHours {
  134. t.Fatalf("PVAllocations.MarshalJSON: PVAllocation ByteHours not equal for PVKey %s", pvKey)
  135. }
  136. }
  137. })
  138. }
  139. }