module_test.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. package basic
  2. import (
  3. "context"
  4. "os"
  5. "testing"
  6. "github.com/opencost/opencost/core/pkg/pricing"
  7. "github.com/opencost/opencost/core/pkg/reader"
  8. "github.com/opencost/opencost/core/pkg/storage"
  9. "github.com/stretchr/testify/require"
  10. )
  11. // TestNewBasicPricingModuleEmptyStore verifies the constructor populates a
  12. // default pricing set when given an empty store.
  13. func TestNewBasicPricingModuleEmptyStore(t *testing.T) {
  14. store := pricing.NewMemoryPricingStore()
  15. pm, err := NewBasicPricingModule(store)
  16. require.NoError(t, err)
  17. ps, err := store.GetPricingSet(t.Context())
  18. require.NoError(t, err)
  19. require.False(t, ps.IsEmpty())
  20. np, err := pm.getNodePricing(t.Context())
  21. require.NoError(t, err)
  22. require.NotNil(t, np)
  23. }
  24. func TestPricingModule(t *testing.T) {
  25. memoryPricingStore := pricing.NewMemoryPricingStore()
  26. filePricingStore, err := pricing.NewStoragePricingStore(t.Context(), newFileStorage(t), "pricing.json")
  27. require.NoError(t, err)
  28. stores := map[string]pricing.PricingStore{
  29. "MemoryPricingStore": memoryPricingStore,
  30. "StoragePricingStore": filePricingStore,
  31. }
  32. for name, store := range stores {
  33. t.Run(name, testPricingModuleWithStore(store))
  34. }
  35. }
  36. func testPricingModuleWithStore(store pricing.PricingStore) func(t *testing.T) {
  37. return func(t *testing.T) {
  38. ctx := t.Context()
  39. pm, err := NewBasicPricingModule(store)
  40. require.NoError(t, err)
  41. t.Run("DefaultPricing", func(t *testing.T) {
  42. testDefaultPricing(t, ctx, pm)
  43. })
  44. t.Run("SetNodePricePerCPUCoreHour", func(t *testing.T) {
  45. testSetNodePricePerCPUCoreHour(t, ctx, pm)
  46. })
  47. t.Run("SetNodePricePerRAMGiBHour", func(t *testing.T) {
  48. testSetNodePricePerRAMGiBHour(t, ctx, pm)
  49. })
  50. t.Run("SetNodePricePerGPUHour", func(t *testing.T) {
  51. testSetNodePricePerGPUHour(t, ctx, pm)
  52. })
  53. t.Run("SetNodePricePerLocalDiskGiBHour", func(t *testing.T) {
  54. testSetNodePricePerLocalDiskGiBHour(t, ctx, pm)
  55. })
  56. t.Run("SetVolumePricePerStorageGiBHour", func(t *testing.T) {
  57. testSetVolumePricePerStorageGiBHour(t, ctx, pm)
  58. })
  59. t.Run("NewNodePricingReader", func(t *testing.T) {
  60. testNewNodePricingReader(t, ctx, pm)
  61. })
  62. t.Run("NewVolumePricingReader", func(t *testing.T) {
  63. testNewVolumePricingReader(t, ctx, pm)
  64. })
  65. t.Run("ModulePersistence", func(t *testing.T) {
  66. // Create a new PricingModule with the same store
  67. pm2, err := NewBasicPricingModule(store)
  68. require.NoError(t, err)
  69. // Verify that pricing persists
  70. np, err := pm2.getNodePricing(ctx)
  71. if err != nil {
  72. t.Fatalf("Failed to get node pricing: %v", err)
  73. }
  74. if np == nil {
  75. t.Fatal("Expected node pricing to be persisted")
  76. }
  77. })
  78. }
  79. }
  80. // testDefaultPricing verifies that a freshly created PricingModule contains default pricing
  81. func testDefaultPricing(t *testing.T, ctx context.Context, pm *PricingModule) {
  82. // Test default node pricing
  83. np, err := pm.getNodePricing(ctx)
  84. if err != nil {
  85. t.Fatalf("Failed to get node pricing: %v", err)
  86. }
  87. if np == nil {
  88. t.Fatal("Expected node pricing to exist")
  89. }
  90. // Prices are keyed by Resource. RAM and local disk share the GiB-hr unit,
  91. // so the Resource key is what distinguishes them.
  92. nodeChecks := []struct {
  93. resource pricing.Resource
  94. want float64
  95. }{
  96. {pricing.ResourceCPU, DefaultNodePricePerVCPUHour},
  97. {pricing.ResourceRAM, DefaultNodePricePerRAMGiBHour},
  98. {pricing.ResourceGPU, DefaultNodePricePerGPUHour},
  99. {pricing.ResourceStorage, DefaultNodePricePerLocalDiskGiBHour},
  100. }
  101. for _, c := range nodeChecks {
  102. price, ok := np.Prices[c.resource]
  103. if !ok {
  104. t.Errorf("Expected to find %s pricing", c.resource)
  105. continue
  106. }
  107. if price.Price != c.want {
  108. t.Errorf("Expected %s price to be %f, got %f", c.resource, c.want, price.Price)
  109. }
  110. }
  111. // Test default volume pricing
  112. vp, err := pm.getPersistentVolumePricing(ctx)
  113. if err != nil {
  114. t.Fatalf("Failed to get volume pricing: %v", err)
  115. }
  116. if vp == nil {
  117. t.Fatal("Expected volume pricing to exist")
  118. }
  119. volumePrice, ok := vp.Prices[pricing.ResourceStorage]
  120. if !ok {
  121. t.Fatal("Expected to find volume storage pricing")
  122. }
  123. if volumePrice.Price != DefaultPersistentVolumePricePerGiBHour {
  124. t.Errorf("Expected volume price to be %f, got %f", DefaultPersistentVolumePricePerGiBHour, volumePrice.Price)
  125. }
  126. }
  127. // testSetNodePricePerCPUCoreHour tests the SetNodePricePerCPUCoreHour function
  128. func testSetNodePricePerCPUCoreHour(t *testing.T, ctx context.Context, pm *PricingModule) {
  129. newPrice := 0.075
  130. err := pm.SetNodePricePerCPUCoreHour(ctx, newPrice)
  131. if err != nil {
  132. t.Fatalf("Failed to set CPU price: %v", err)
  133. }
  134. // Verify the price was set
  135. np, err := pm.getNodePricing(ctx)
  136. if err != nil {
  137. t.Fatalf("Failed to get node pricing: %v", err)
  138. }
  139. price, ok := np.Prices[pricing.ResourceCPU]
  140. if !ok {
  141. t.Fatal("Expected to find CPU pricing")
  142. }
  143. if price.Price != newPrice {
  144. t.Errorf("Expected CPU price to be %f, got %f", newPrice, price.Price)
  145. }
  146. }
  147. // testSetNodePricePerRAMGiBHour tests the SetNodePricePerRAMGiBHour function
  148. func testSetNodePricePerRAMGiBHour(t *testing.T, ctx context.Context, pm *PricingModule) {
  149. newPrice := 0.008
  150. err := pm.SetNodePricePerRAMGiBHour(ctx, newPrice)
  151. if err != nil {
  152. t.Fatalf("Failed to set RAM price: %v", err)
  153. }
  154. // Verify the price was set
  155. np, err := pm.getNodePricing(ctx)
  156. if err != nil {
  157. t.Fatalf("Failed to get node pricing: %v", err)
  158. }
  159. price, ok := np.Prices[pricing.ResourceRAM]
  160. if !ok {
  161. t.Fatal("Expected to find RAM pricing")
  162. }
  163. if price.Price != newPrice {
  164. t.Errorf("Expected RAM price to be %f, got %f", newPrice, price.Price)
  165. }
  166. }
  167. // testSetNodePricePerGPUHour tests the SetNodePricePerGPUHour function
  168. func testSetNodePricePerGPUHour(t *testing.T, ctx context.Context, pm *PricingModule) {
  169. newPrice := 2.0
  170. err := pm.SetNodePricePerGPUHour(ctx, newPrice)
  171. if err != nil {
  172. t.Fatalf("Failed to set GPU price: %v", err)
  173. }
  174. // Verify the price was set
  175. np, err := pm.getNodePricing(ctx)
  176. if err != nil {
  177. t.Fatalf("Failed to get node pricing: %v", err)
  178. }
  179. price, ok := np.Prices[pricing.ResourceGPU]
  180. if !ok {
  181. t.Fatal("Expected to find GPU pricing")
  182. }
  183. if price.Price != newPrice {
  184. t.Errorf("Expected GPU price to be %f, got %f", newPrice, price.Price)
  185. }
  186. }
  187. // testSetNodePricePerLocalDiskGiBHour tests the SetNodePricePerLocalDiskGiBHour function
  188. func testSetNodePricePerLocalDiskGiBHour(t *testing.T, ctx context.Context, pm *PricingModule) {
  189. newPrice := 0.0007
  190. err := pm.SetNodePricePerLocalDiskGiBHour(ctx, newPrice)
  191. if err != nil {
  192. t.Fatalf("Failed to set local disk price: %v", err)
  193. }
  194. // Verify the price was set
  195. np, err := pm.getNodePricing(ctx)
  196. if err != nil {
  197. t.Fatalf("Failed to get node pricing: %v", err)
  198. }
  199. price, ok := np.Prices[pricing.ResourceStorage]
  200. if !ok {
  201. t.Fatal("Expected to find local disk pricing")
  202. }
  203. if price.Price != newPrice {
  204. t.Errorf("Expected local disk price to be %f, got %f", newPrice, price.Price)
  205. }
  206. }
  207. // testSetVolumePricePerStorageGiBHour tests the SetVolumePricePerStorageGiBHour function
  208. func testSetVolumePricePerStorageGiBHour(t *testing.T, ctx context.Context, pm *PricingModule) {
  209. newPrice := 0.0003
  210. err := pm.SetVolumePricePerStorageGiBHour(ctx, newPrice)
  211. if err != nil {
  212. t.Fatalf("Failed to set volume storage price: %v", err)
  213. }
  214. // Verify the price was set
  215. vp, err := pm.getPersistentVolumePricing(ctx)
  216. if err != nil {
  217. t.Fatalf("Failed to get volume pricing: %v", err)
  218. }
  219. price, ok := vp.Prices[pricing.ResourceStorage]
  220. if !ok {
  221. t.Fatal("Expected to find volume storage pricing")
  222. }
  223. if price.Price != newPrice {
  224. t.Errorf("Expected volume storage price to be %f, got %f", newPrice, price.Price)
  225. }
  226. }
  227. // testNewNodePricingReader tests the NewNodePricingReader function
  228. func testNewNodePricingReader(t *testing.T, ctx context.Context, pm *PricingModule) {
  229. // Test that NewNodePricingReader always produces a reader
  230. rdr, err := pm.NewNodePricingReader(ctx)
  231. if err != nil {
  232. t.Fatalf("Failed to create node pricing reader: %v", err)
  233. }
  234. if rdr == nil {
  235. t.Fatal("Expected reader to be non-nil")
  236. }
  237. // Test that the reader produces precisely one *NodePricing struct
  238. dst := make([]*pricing.NodePricing, 10) // Buffer larger than expected
  239. count := 0
  240. for {
  241. n, err := rdr.Read(ctx, dst)
  242. count += n
  243. // Verify all read items are non-nil
  244. for i := 0; i < n; i++ {
  245. if dst[i] == nil {
  246. t.Error("Expected non-nil NodePricing")
  247. }
  248. }
  249. if err == reader.Done {
  250. break
  251. }
  252. if err != nil {
  253. t.Fatalf("Reader error: %v", err)
  254. }
  255. }
  256. if count != 1 {
  257. t.Errorf("Expected reader to produce exactly 1 NodePricing, got %d", count)
  258. }
  259. // Clean up
  260. if err := rdr.Close(); err != nil {
  261. t.Errorf("Failed to close reader: %v", err)
  262. }
  263. }
  264. // testNewVolumePricingReader tests the NewVolumePricingReader function
  265. func testNewVolumePricingReader(t *testing.T, ctx context.Context, pm *PricingModule) {
  266. // Test that NewVolumePricingReader always produces a reader
  267. rdr, err := pm.NewPersistentVolumePricingReader(ctx)
  268. if err != nil {
  269. t.Fatalf("Failed to create volume pricing reader: %v", err)
  270. }
  271. if rdr == nil {
  272. t.Fatal("Expected reader to be non-nil")
  273. }
  274. // Test that the reader produces precisely one *VolumePricing struct
  275. dst := make([]*pricing.PersistentVolumePricing, 10) // Buffer larger than expected
  276. count := 0
  277. for {
  278. n, err := rdr.Read(ctx, dst)
  279. count += n
  280. // Verify all read items are non-nil
  281. for i := 0; i < n; i++ {
  282. if dst[i] == nil {
  283. t.Error("Expected non-nil VolumePricing")
  284. }
  285. }
  286. if err == reader.Done {
  287. break
  288. }
  289. if err != nil {
  290. t.Fatalf("Reader error: %v", err)
  291. }
  292. }
  293. if count != 1 {
  294. t.Errorf("Expected reader to produce exactly 1 VolumePricing, got %d", count)
  295. }
  296. // Clean up
  297. if err := rdr.Close(); err != nil {
  298. t.Errorf("Failed to close reader: %v", err)
  299. }
  300. }
  301. func newFileStorage(t *testing.T) storage.Storage {
  302. tempDir, err := os.MkdirTemp("", "pricing-test-*")
  303. if err != nil {
  304. t.Fatalf("Failed to create temp directory: %v", err)
  305. }
  306. defer os.RemoveAll(tempDir)
  307. return storage.NewFileStorage(tempDir)
  308. }