2
0

mock_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package pricing
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "testing"
  8. "github.com/opencost/opencost/core/pkg/reader"
  9. )
  10. func TestMockPricingModule(t *testing.T) {
  11. var source PricingSource
  12. pricingModule, err := NewMockPricingModule()
  13. if err != nil {
  14. t.Fatalf("unexpected error initializing mock repository: %s", err)
  15. }
  16. source = pricingModule
  17. // Simple example of a sink for pricing data (will be database tables in reality)
  18. bufferSize := 10
  19. ingestor := newMockIngestor(bufferSize)
  20. // Test ingestion of mock node reader
  21. nodePricingReader, err := source.NewNodePricingReader(t.Context())
  22. if err != nil {
  23. t.Errorf("unexpected error initializing node reader: %s", err)
  24. }
  25. n, err := ingestor.ingestNodePricing(context.Background(), nodePricingReader)
  26. if err != nil {
  27. t.Errorf("unexpected error ingesting node pricing: %s", err)
  28. }
  29. if n != 39 {
  30. t.Errorf("expected to ingest %d node pricing records; ingested %d", 39, n)
  31. }
  32. nodePricingCount := ingestor.countNodePricing()
  33. if nodePricingCount != 39 {
  34. t.Errorf("expected %d node pricing records; received %d", 39, nodePricingCount)
  35. }
  36. // Test ingestion of mock persistent volume reader
  37. volumePricingReader, err := source.NewPersistentVolumePricingReader(t.Context())
  38. if err != nil {
  39. t.Errorf("unexpected error initializing volume reader: %s", err)
  40. }
  41. n, err = ingestor.ingestPersistentVolumePricing(context.Background(), volumePricingReader)
  42. if err != nil {
  43. t.Errorf("unexpected error ingesting volume pricing: %s", err)
  44. }
  45. if n != 20 {
  46. t.Errorf("expected to ingest %d volume pricing records; ingested %d", 20, n)
  47. }
  48. volumePricingCount := ingestor.countVolumePricing()
  49. if volumePricingCount != 20 {
  50. t.Errorf("expected %d volume pricing records; received %d", 20, volumePricingCount)
  51. }
  52. }
  53. // newMock is a helper that constructs a fresh MockPricingModule and fails the
  54. // test if construction errors.
  55. func newMock(t *testing.T) *MockPricingModule {
  56. t.Helper()
  57. mpm, err := NewMockPricingModule()
  58. if err != nil {
  59. t.Fatalf("unexpected error initializing mock pricing module: %v", err)
  60. }
  61. return mpm
  62. }
  63. type mockPricingIngestor struct {
  64. bufferSize int
  65. clusterPricing []*ClusterPricing
  66. networkPricing []*NetworkPricing
  67. nodePricing []*NodePricing
  68. persistentVolumePricing []*PersistentVolumePricing
  69. servicePricing []*ServicePricing
  70. }
  71. func newMockIngestor(bufferSize int) *mockPricingIngestor {
  72. if bufferSize == 0 {
  73. bufferSize = 100
  74. }
  75. return &mockPricingIngestor{
  76. bufferSize: bufferSize,
  77. clusterPricing: []*ClusterPricing{},
  78. networkPricing: []*NetworkPricing{},
  79. nodePricing: []*NodePricing{},
  80. persistentVolumePricing: []*PersistentVolumePricing{},
  81. servicePricing: []*ServicePricing{},
  82. }
  83. }
  84. func (ing *mockPricingIngestor) countNodePricing() int {
  85. return len(ing.nodePricing)
  86. }
  87. func (ing *mockPricingIngestor) ingestNodePricing(ctx context.Context, pricingReader reader.Reader[*NodePricing]) (int, error) {
  88. defer pricingReader.Close()
  89. nodeBuf := make([]*NodePricing, ing.bufferSize)
  90. totalCount := 0
  91. for {
  92. n, err := pricingReader.Read(ctx, nodeBuf)
  93. if n > 0 {
  94. ing.nodePricing = append(ing.nodePricing, nodeBuf[:n]...)
  95. totalCount += n
  96. }
  97. if errors.Is(err, io.EOF) {
  98. break
  99. }
  100. if err != nil {
  101. return totalCount, fmt.Errorf("unexpected error reading node pricing: %s", err)
  102. }
  103. }
  104. return totalCount, nil
  105. }
  106. func (ing *mockPricingIngestor) countVolumePricing() int {
  107. return len(ing.persistentVolumePricing)
  108. }
  109. func (ing *mockPricingIngestor) ingestPersistentVolumePricing(ctx context.Context, pricingReader reader.Reader[*PersistentVolumePricing]) (int, error) {
  110. defer pricingReader.Close()
  111. volBuf := make([]*PersistentVolumePricing, ing.bufferSize)
  112. totalCount := 0
  113. for {
  114. n, err := pricingReader.Read(ctx, volBuf)
  115. if n > 0 {
  116. ing.persistentVolumePricing = append(ing.persistentVolumePricing, volBuf[:n]...)
  117. totalCount += n
  118. }
  119. if errors.Is(err, io.EOF) {
  120. break
  121. }
  122. if err != nil {
  123. return totalCount, fmt.Errorf("unexpected error reading volume pricing: %s", err)
  124. }
  125. }
  126. return totalCount, nil
  127. }