mock.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package pricing
  2. import (
  3. "context"
  4. "embed"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "path/filepath"
  9. "strings"
  10. "github.com/opencost/opencost/core/pkg/reader"
  11. "gopkg.in/yaml.v3"
  12. )
  13. // MockPricingModule must satisfy the PricingModule interface
  14. var _ PricingModule = (*MockPricingModule)(nil)
  15. type MockPricingModule struct {
  16. ClusterPricing []*ClusterPricing
  17. NetworkPricing []*NetworkPricing
  18. NodePricing []*NodePricing
  19. PersistentVolumePricing []*PersistentVolumePricing
  20. ServicePricing []*ServicePricing
  21. }
  22. func NewMockPricingModule() (*MockPricingModule, error) {
  23. mpm := &MockPricingModule{
  24. ClusterPricing: []*ClusterPricing{},
  25. NetworkPricing: []*NetworkPricing{},
  26. NodePricing: []*NodePricing{},
  27. PersistentVolumePricing: []*PersistentVolumePricing{},
  28. ServicePricing: []*ServicePricing{},
  29. }
  30. // Default
  31. err := mpm.loadTestFile("default.yaml")
  32. if err != nil {
  33. return nil, fmt.Errorf("error loading test default pricing: %w", err)
  34. }
  35. // AWS
  36. err = mpm.loadTestFile("aws.yaml")
  37. if err != nil {
  38. return nil, fmt.Errorf("error loading test AWS pricing: %w", err)
  39. }
  40. // Azure
  41. err = mpm.loadTestFile("azure.yaml")
  42. if err != nil {
  43. return nil, fmt.Errorf("error loading test Azure pricing: %w", err)
  44. }
  45. // GCP
  46. err = mpm.loadTestFile("gcp.yaml")
  47. if err != nil {
  48. return nil, fmt.Errorf("error loading test GCP pricing: %w", err)
  49. }
  50. return mpm, nil
  51. }
  52. func (mpm *MockPricingModule) GetClusterPricing(ctx context.Context, props ClusterPricingProperties) (*ClusterPricing, error) {
  53. if err := ctx.Err(); err != nil {
  54. return nil, err
  55. }
  56. // Search through the mock data for a matching cluster pricing entry
  57. for _, cp := range mpm.ClusterPricing {
  58. if cp.Properties.Provider == props.Provider {
  59. return cp, nil
  60. }
  61. }
  62. return nil, fmt.Errorf("cluster pricing not found for provider=%s", props.Provider)
  63. }
  64. func (mpm *MockPricingModule) NewClusterPricingReader(ctx context.Context) (reader.Reader[*ClusterPricing], error) {
  65. return reader.NewSliceReader(mpm.ClusterPricing), nil
  66. }
  67. func (mpm *MockPricingModule) GetNetworkPricing(ctx context.Context, props NetworkPricingProperties) (*NetworkPricing, error) {
  68. if err := ctx.Err(); err != nil {
  69. return nil, err
  70. }
  71. // Search through the mock data for a matching network pricing entry
  72. for _, np := range mpm.NetworkPricing {
  73. if np.Properties.Provider == props.Provider {
  74. return np, nil
  75. }
  76. }
  77. return nil, fmt.Errorf("network pricing not found for provider=%s", props.Provider)
  78. }
  79. func (mpm *MockPricingModule) NewNetworkPricingReader(ctx context.Context) (reader.Reader[*NetworkPricing], error) {
  80. return reader.NewSliceReader(mpm.NetworkPricing), nil
  81. }
  82. func (mpm *MockPricingModule) GetNodePricing(ctx context.Context, props NodePricingProperties) (*NodePricing, error) {
  83. if err := ctx.Err(); err != nil {
  84. return nil, err
  85. }
  86. // Search through the mock data for a matching node pricing entry
  87. for _, np := range mpm.NodePricing {
  88. if np.Properties.Provider == props.Provider &&
  89. np.Properties.Region == props.Region &&
  90. np.Properties.InstanceType == props.InstanceType &&
  91. np.Properties.Provisioning == props.Provisioning &&
  92. np.Properties.Commitment == props.Commitment {
  93. return np, nil
  94. }
  95. }
  96. return nil, fmt.Errorf("node pricing not found for provider=%s, region=%s, instanceType=%s, provisioning=%s, commitment=%s",
  97. props.Provider, props.Region, props.InstanceType, props.Provisioning, props.Commitment)
  98. }
  99. func (mpm *MockPricingModule) NewNodePricingReader(ctx context.Context) (reader.Reader[*NodePricing], error) {
  100. return reader.NewSliceReader(mpm.NodePricing), nil
  101. }
  102. func (mpm *MockPricingModule) GetPersistentVolumePricing(ctx context.Context, props PersistentVolumePricingProperties) (*PersistentVolumePricing, error) {
  103. if err := ctx.Err(); err != nil {
  104. return nil, err
  105. }
  106. // Search through the mock data for a matching volume pricing entry
  107. for _, vp := range mpm.PersistentVolumePricing {
  108. if vp.Properties.Provider == props.Provider &&
  109. vp.Properties.Region == props.Region &&
  110. vp.Properties.VolumeType == props.VolumeType {
  111. return vp, nil
  112. }
  113. }
  114. return nil, fmt.Errorf("volume pricing not found for provider=%s, region=%s, volumeType=%s", props.Provider, props.Region, props.VolumeType)
  115. }
  116. func (mpm *MockPricingModule) NewPersistentVolumePricingReader(ctx context.Context) (reader.Reader[*PersistentVolumePricing], error) {
  117. return reader.NewSliceReader(mpm.PersistentVolumePricing), nil
  118. }
  119. func (mpm *MockPricingModule) GetServicePricing(ctx context.Context, props ServicePricingProperties) (*ServicePricing, error) {
  120. if err := ctx.Err(); err != nil {
  121. return nil, err
  122. }
  123. // Search through the mock data for a matching service pricing entry
  124. for _, sp := range mpm.ServicePricing {
  125. if sp.Properties.Provider == props.Provider &&
  126. sp.Properties.Region == props.Region {
  127. return sp, nil
  128. }
  129. }
  130. return nil, fmt.Errorf("service pricing not found for provider=%s, region=%s", props.Provider, props.Region)
  131. }
  132. func (mpm *MockPricingModule) NewServicePricingReader(ctx context.Context) (reader.Reader[*ServicePricing], error) {
  133. return reader.NewSliceReader(mpm.ServicePricing), nil
  134. }
  135. func (mpm *MockPricingModule) GetPricingSet(ctx context.Context) (*PricingSet, error) {
  136. ps := &PricingSet{
  137. ClusterPricing: mpm.ClusterPricing,
  138. NetworkPricing: mpm.NetworkPricing,
  139. NodePricing: mpm.NodePricing,
  140. PersistentVolumePricing: mpm.PersistentVolumePricing,
  141. ServicePricing: mpm.ServicePricing,
  142. }
  143. return ps, nil
  144. }
  145. func (mpm *MockPricingModule) SourceKind() string {
  146. return "test"
  147. }
  148. func (mpm *MockPricingModule) SourceName() string {
  149. return "mock"
  150. }
  151. func (mpm *MockPricingModule) Checksum(ctx context.Context) (string, error) {
  152. ps, err := mpm.GetPricingSet(ctx)
  153. if err != nil {
  154. return "", fmt.Errorf("getting pricing set: %w", err)
  155. }
  156. return ps.Checksum()
  157. }
  158. //go:embed test/*
  159. var pricingTestFS embed.FS
  160. func (mpm *MockPricingModule) loadTestFile(filename string) error {
  161. path := filepath.Join("test", filename)
  162. bs, err := pricingTestFS.ReadFile(path)
  163. if err != nil {
  164. return fmt.Errorf("failed to read embedded pricing file: %w", err)
  165. }
  166. var set *PricingSet
  167. // Detect file format based on extension
  168. ext := strings.ToLower(filepath.Ext(filename))
  169. switch ext {
  170. case ".json":
  171. err = json.Unmarshal(bs, &set)
  172. if err != nil {
  173. return fmt.Errorf("failed to parse json: %w", err)
  174. }
  175. case ".yaml", ".yml":
  176. err = yaml.Unmarshal(bs, &set)
  177. if err != nil {
  178. return fmt.Errorf("failed to parse yaml: %w", err)
  179. }
  180. default:
  181. return fmt.Errorf("unsupported file format: %s (expected .json, .yaml, or .yml)", ext)
  182. }
  183. if set == nil {
  184. return errors.New("nil set")
  185. }
  186. mpm.ClusterPricing = append(mpm.ClusterPricing, set.ClusterPricing...)
  187. mpm.NetworkPricing = append(mpm.NetworkPricing, set.NetworkPricing...)
  188. mpm.NodePricing = append(mpm.NodePricing, set.NodePricing...)
  189. mpm.PersistentVolumePricing = append(mpm.PersistentVolumePricing, set.PersistentVolumePricing...)
  190. mpm.ServicePricing = append(mpm.ServicePricing, set.ServicePricing...)
  191. return nil
  192. }