mock_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package pricing
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "testing"
  7. "github.com/opencost/opencost/core/pkg/reader"
  8. )
  9. func TestMockPricingRepository(t *testing.T) {
  10. var repo PricingRepository
  11. mockRepo, err := NewMockPricingRepository()
  12. if err != nil {
  13. t.Fatalf("unexpected error initializing mock repository: %s", err)
  14. }
  15. repo = mockRepo
  16. // Simple example of a sink for pricing data (will be database tables in reality)
  17. bufferSize := 10
  18. ingestor := newMockIngestor(bufferSize)
  19. // Test ingestion of mock node reader
  20. nodePricingReader, err := repo.NewNodePricingReader(t.Context())
  21. if err != nil {
  22. t.Errorf("unexpected error initializing node reader: %s", err)
  23. }
  24. n, err := ingestor.IngestNodePricing(context.Background(), nodePricingReader)
  25. if err != nil {
  26. t.Errorf("unexpected error ingesting node pricing: %s", err)
  27. }
  28. if n != 39 {
  29. t.Errorf("expected to ingest %d node pricing records; ingested %d", 39, n)
  30. }
  31. nodePricingCount := ingestor.CountNodePricing()
  32. if nodePricingCount != 39 {
  33. t.Errorf("expected %d node pricing records; received %d", 39, nodePricingCount)
  34. }
  35. // Test ingestion of mock volume reader
  36. volumePricingReader, err := repo.NewVolumePricingReader(t.Context())
  37. if err != nil {
  38. t.Errorf("unexpected error initializing volume reader: %s", err)
  39. }
  40. n, err = ingestor.IngestVolumePricing(context.Background(), volumePricingReader)
  41. if err != nil {
  42. t.Errorf("unexpected error ingesting volume pricing: %s", err)
  43. }
  44. if n != 20 {
  45. t.Errorf("expected to ingest %d volume pricing records; ingested %d", 20, n)
  46. }
  47. volumePricingCount := ingestor.CountVolumePricing()
  48. if volumePricingCount != 20 {
  49. t.Errorf("expected %d volume pricing records; received %d", 20, volumePricingCount)
  50. }
  51. }
  52. type mockPricingIngestor struct {
  53. bufferSize int
  54. nodePricing []*NodePricing
  55. volumePricing []*VolumePricing
  56. }
  57. func newMockIngestor(bufferSize int) *mockPricingIngestor {
  58. if bufferSize == 0 {
  59. bufferSize = 100
  60. }
  61. return &mockPricingIngestor{
  62. bufferSize: bufferSize,
  63. nodePricing: []*NodePricing{},
  64. volumePricing: []*VolumePricing{},
  65. }
  66. }
  67. func (ing *mockPricingIngestor) CountNodePricing() int {
  68. return len(ing.nodePricing)
  69. }
  70. func (ing *mockPricingIngestor) IngestNodePricing(ctx context.Context, pricingReader reader.Reader[*NodePricing]) (int, error) {
  71. defer pricingReader.Close()
  72. nodeBuf := make([]*NodePricing, ing.bufferSize)
  73. totalCount := 0
  74. for {
  75. n, err := pricingReader.Read(ctx, nodeBuf)
  76. if n > 0 {
  77. ing.nodePricing = append(ing.nodePricing, nodeBuf[:n]...)
  78. }
  79. if errors.Is(err, reader.Done) {
  80. break
  81. }
  82. if err != nil {
  83. return totalCount, fmt.Errorf("unexpected error reading node pricing: %s", err)
  84. }
  85. totalCount += n
  86. }
  87. return totalCount, nil
  88. }
  89. func (ing *mockPricingIngestor) CountVolumePricing() int {
  90. return len(ing.volumePricing)
  91. }
  92. func (ing *mockPricingIngestor) IngestVolumePricing(ctx context.Context, pricingReader reader.Reader[*VolumePricing]) (int, error) {
  93. defer pricingReader.Close()
  94. volBuf := make([]*VolumePricing, ing.bufferSize)
  95. totalCount := 0
  96. for {
  97. n, err := pricingReader.Read(ctx, volBuf)
  98. if n > 0 {
  99. ing.volumePricing = append(ing.volumePricing, volBuf[:n]...)
  100. }
  101. if errors.Is(err, reader.Done) {
  102. break
  103. }
  104. if err != nil {
  105. return totalCount, fmt.Errorf("unexpected error reading volume pricing: %s", err)
  106. }
  107. totalCount += n
  108. }
  109. return totalCount, nil
  110. }