storage_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package pricing
  2. import (
  3. "sync"
  4. "testing"
  5. "github.com/opencost/opencost/core/pkg/storage"
  6. )
  7. // TestStorageNewValidation verifies the constructor's argument validation and
  8. // that a non-existent path is auto-initialized with an empty pricing set.
  9. func TestStorageNewValidation(t *testing.T) {
  10. if _, err := NewStoragePricingStore(t.Context(), nil, "pricing.json"); err == nil {
  11. t.Error("expected error for nil storage")
  12. }
  13. if _, err := NewStoragePricingStore(t.Context(), storage.NewMemoryStorage(), ""); err == nil {
  14. t.Error("expected error for empty path")
  15. }
  16. store := storage.NewMemoryStorage()
  17. sps, err := NewStoragePricingStore(t.Context(), store, "pricing.json")
  18. if err != nil {
  19. t.Fatalf("unexpected error: %v", err)
  20. }
  21. exists, err := store.Exists("pricing.json")
  22. if err != nil {
  23. t.Fatalf("unexpected error: %v", err)
  24. }
  25. if !exists {
  26. t.Error("expected constructor to initialize the pricing path")
  27. }
  28. ps, err := sps.GetPricingSet(t.Context())
  29. if err != nil {
  30. t.Fatalf("unexpected error: %v", err)
  31. }
  32. if !ps.IsEmpty() {
  33. t.Errorf("expected auto-initialized set to be empty, got %+v", ps)
  34. }
  35. }
  36. // TestStorageRoundTrip verifies that a set survives a Set/Get round trip through
  37. // the backing storage.
  38. func TestStorageRoundTrip(t *testing.T) {
  39. sps, err := NewStoragePricingStore(t.Context(), storage.NewMemoryStorage(), "pricing.json")
  40. if err != nil {
  41. t.Fatalf("unexpected error: %v", err)
  42. }
  43. in := fullPricingSet()
  44. if err := sps.SetPricingSet(t.Context(), in); err != nil {
  45. t.Fatalf("unexpected error: %v", err)
  46. }
  47. out, err := sps.GetPricingSet(t.Context())
  48. if err != nil {
  49. t.Fatalf("unexpected error: %v", err)
  50. }
  51. csIn, err := in.Checksum()
  52. if err != nil {
  53. t.Fatalf("unexpected error: %v", err)
  54. }
  55. csOut, err := out.Checksum()
  56. if err != nil {
  57. t.Fatalf("unexpected error: %v", err)
  58. }
  59. if csIn != csOut {
  60. t.Errorf("round-trip changed contents: %q -> %q", csIn, csOut)
  61. }
  62. }
  63. // TestStorageSetNil verifies that Set rejects a nil pricing set.
  64. func TestStorageSetNil(t *testing.T) {
  65. sps, err := NewStoragePricingStore(t.Context(), storage.NewMemoryStorage(), "pricing.json")
  66. if err != nil {
  67. t.Fatalf("unexpected error: %v", err)
  68. }
  69. if err := sps.SetPricingSet(t.Context(), nil); err == nil {
  70. t.Error("expected error for nil pricing set")
  71. }
  72. }
  73. // TestStorageGetMissingPath verifies that Get surfaces a read error when the
  74. // backing path does not exist. The store is constructed directly to bypass the
  75. // constructor's auto-initialization.
  76. func TestStorageGetMissingPath(t *testing.T) {
  77. sps := &StoragePricingStore{
  78. store: storage.NewMemoryStorage(),
  79. path: "missing.json",
  80. }
  81. if _, err := sps.GetPricingSet(t.Context()); err == nil {
  82. t.Error("expected error reading a missing path")
  83. }
  84. }
  85. // TestStorageGetMalformedData verifies that Get surfaces a decode error when the
  86. // backing data is not valid JSON.
  87. func TestStorageGetMalformedData(t *testing.T) {
  88. store := storage.NewMemoryStorage()
  89. if err := store.Write("pricing.json", []byte("not json")); err != nil {
  90. t.Fatalf("unexpected error: %v", err)
  91. }
  92. sps := &StoragePricingStore{store: store, path: "pricing.json"}
  93. if _, err := sps.GetPricingSet(t.Context()); err == nil {
  94. t.Error("expected error decoding malformed pricing data")
  95. }
  96. }
  97. // TestStorageConcurrentAccess exercises the store under concurrent readers and
  98. // writers; run with -race to catch data races.
  99. func TestStorageConcurrentAccess(t *testing.T) {
  100. sps, err := NewStoragePricingStore(t.Context(), storage.NewMemoryStorage(), "pricing.json")
  101. if err != nil {
  102. t.Fatalf("unexpected error: %v", err)
  103. }
  104. var wg sync.WaitGroup
  105. for i := 0; i < 50; i++ {
  106. wg.Add(2)
  107. go func() {
  108. defer wg.Done()
  109. _ = sps.SetPricingSet(t.Context(), fullPricingSet())
  110. }()
  111. go func() {
  112. defer wg.Done()
  113. if _, err := sps.GetPricingSet(t.Context()); err != nil {
  114. t.Errorf("unexpected error: %v", err)
  115. }
  116. }()
  117. }
  118. wg.Wait()
  119. }