2
0

allocation_csv_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package costmodel
  2. import (
  3. "context"
  4. "io"
  5. "os"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/require"
  9. "github.com/opencost/opencost/pkg/kubecost"
  10. )
  11. //go:generate moq -out moq_cloud_storage_test.go . CloudStorage:CloudStorageMock
  12. //go:generate moq -out moq_allocation_model_test.go . AllocationModel:AllocationModelMock
  13. func Test_UpdateCSV(t *testing.T) {
  14. t.Run("previous data doesn't exist, upload new data", func(t *testing.T) {
  15. var csv string
  16. storage := &CloudStorageMock{
  17. FileExistsFunc: func(ctx context.Context, path string) (bool, error) {
  18. return false, nil
  19. },
  20. FileReplaceFunc: func(ctx context.Context, f *os.File, path string) error {
  21. data, err := io.ReadAll(f)
  22. csv = string(data)
  23. require.NoError(t, err)
  24. return nil
  25. },
  26. }
  27. model := &AllocationModelMock{
  28. ComputeAllocationFunc: func(start time.Time, end time.Time, resolution time.Duration) (*kubecost.AllocationSet, error) {
  29. return &kubecost.AllocationSet{
  30. Allocations: map[string]*kubecost.Allocation{
  31. "test": {
  32. Name: "test",
  33. CPUCoreUsageAverage: 0.1,
  34. CPUCoreRequestAverage: 0.2,
  35. CPUCost: 0.3,
  36. RAMBytesUsageAverage: 0.4,
  37. RAMBytesRequestAverage: 0.5,
  38. RAMCost: 0.6,
  39. },
  40. },
  41. }, nil
  42. },
  43. }
  44. err := UpdateCSV(context.TODO(), storage, model, "/test.csv", time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC))
  45. require.NoError(t, err)
  46. // uploaded a single file with the data
  47. require.Len(t, storage.FileReplaceCalls(), 1)
  48. require.Equal(t, `Date,Name,CPUCoreUsageAverage,CPUCoreRequestAverage,CPUCost,RAMBytesUsageAverage,RAMBytesRequestAverage,RAMCost
  49. 2021-01-01,test,0.1,0.2,0.3,0.4,0.5,0.6
  50. `, csv)
  51. })
  52. t.Run("merge new data with previous data", func(t *testing.T) {
  53. var csv string
  54. storage := &CloudStorageMock{
  55. FileExistsFunc: func(ctx context.Context, path string) (bool, error) {
  56. return true, nil
  57. },
  58. FileDownloadFunc: func(ctx context.Context, path string) (*os.File, error) {
  59. f, err := os.CreateTemp("", "")
  60. require.NoError(t, err)
  61. _, err = f.WriteString(`Date,Name,CPUCoreUsageAverage,CPUCoreRequestAverage,CPUCost,RAMBytesUsageAverage,RAMBytesRequestAverage,RAMCost
  62. 2021-01-01,test,0.1,0.2,0.3,0.4,0.5,0.6
  63. `)
  64. _, err = f.Seek(0, io.SeekStart)
  65. require.NoError(t, err)
  66. return f, err
  67. },
  68. FileReplaceFunc: func(ctx context.Context, f *os.File, path string) error {
  69. data, err := io.ReadAll(f)
  70. csv = string(data)
  71. require.NoError(t, err)
  72. return nil
  73. },
  74. }
  75. model := &AllocationModelMock{
  76. ComputeAllocationFunc: func(start time.Time, end time.Time, resolution time.Duration) (*kubecost.AllocationSet, error) {
  77. return &kubecost.AllocationSet{
  78. Allocations: map[string]*kubecost.Allocation{
  79. "test": {
  80. Name: "test",
  81. CPUCoreUsageAverage: 1,
  82. CPUCoreRequestAverage: 2,
  83. CPUCost: 3,
  84. RAMBytesUsageAverage: 4,
  85. RAMBytesRequestAverage: 5,
  86. RAMCost: 6,
  87. },
  88. },
  89. }, nil
  90. },
  91. }
  92. err := UpdateCSV(context.TODO(), storage, model, "/test.csv", time.Date(2021, 1, 2, 0, 0, 0, 0, time.UTC))
  93. require.NoError(t, err)
  94. // uploaded a single file with the data
  95. require.Len(t, storage.FileReplaceCalls(), 1)
  96. require.Equal(t, `Date,Name,CPUCoreUsageAverage,CPUCoreRequestAverage,CPUCost,RAMBytesUsageAverage,RAMBytesRequestAverage,RAMCost
  97. 2021-01-01,test,0.1,0.2,0.3,0.4,0.5,0.6
  98. 2021-01-02,test,1,2,3,4,5,6
  99. `, csv)
  100. })
  101. }