customprovider_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package provider
  2. import (
  3. "fmt"
  4. "strings"
  5. "testing"
  6. "github.com/opencost/opencost/pkg/cloud/models"
  7. "github.com/opencost/opencost/pkg/config"
  8. )
  9. type fakeProviderConfig struct {
  10. customPricing *models.CustomPricing
  11. }
  12. func (f *fakeProviderConfig) GetCustomPricingData() (*models.CustomPricing, error) {
  13. if f.customPricing != nil {
  14. return f.customPricing, nil
  15. }
  16. return nil, fmt.Errorf("no config")
  17. }
  18. func (f *fakeProviderConfig) Update(func(*models.CustomPricing) error) (*models.CustomPricing, error) {
  19. return nil, fmt.Errorf("no config")
  20. }
  21. func (f *fakeProviderConfig) UpdateFromMap(map[string]string) (*models.CustomPricing, error) {
  22. return nil, fmt.Errorf("no config")
  23. }
  24. func (f *fakeProviderConfig) ConfigFileManager() *config.ConfigFileManager { return nil }
  25. func TestCustomProviderLoadBalancerPricing(t *testing.T) {
  26. cases := map[string]struct {
  27. pricing *models.CustomPricing
  28. expectedCost float64
  29. expectErr string
  30. }{
  31. "unset fields default to zero cost": {
  32. pricing: &models.CustomPricing{},
  33. expectedCost: 0.0,
  34. },
  35. "forwarding rule cost is used when set": {
  36. pricing: &models.CustomPricing{
  37. FirstFiveForwardingRulesCost: "0.025",
  38. AdditionalForwardingRuleCost: "0.01",
  39. LBIngressDataCost: "0.008",
  40. },
  41. expectedCost: 0.025,
  42. },
  43. "defaultLBPrice is used when forwarding rule cost is unset": {
  44. pricing: &models.CustomPricing{
  45. DefaultLBPrice: "0.05",
  46. },
  47. expectedCost: 0.05,
  48. },
  49. "forwarding rule cost takes precedence over defaultLBPrice": {
  50. pricing: &models.CustomPricing{
  51. FirstFiveForwardingRulesCost: "0.025",
  52. DefaultLBPrice: "0.05",
  53. },
  54. expectedCost: 0.025,
  55. },
  56. "malformed value returns an error naming the field": {
  57. pricing: &models.CustomPricing{
  58. FirstFiveForwardingRulesCost: "not-a-number",
  59. },
  60. expectErr: "firstFiveForwardingRulesCost",
  61. },
  62. }
  63. for name, tc := range cases {
  64. t.Run(name, func(t *testing.T) {
  65. cp := &CustomProvider{Config: &fakeProviderConfig{customPricing: tc.pricing}}
  66. lb, err := cp.LoadBalancerPricing()
  67. if tc.expectErr != "" {
  68. if err == nil {
  69. t.Fatalf("expected error containing %q, got nil", tc.expectErr)
  70. }
  71. if !strings.Contains(err.Error(), tc.expectErr) {
  72. t.Fatalf("expected error containing %q, got %q", tc.expectErr, err.Error())
  73. }
  74. return
  75. }
  76. if err != nil {
  77. t.Fatalf("LoadBalancerPricing returned error: %v", err)
  78. }
  79. if lb.Cost != tc.expectedCost {
  80. t.Fatalf("expected cost %f, got %f", tc.expectedCost, lb.Cost)
  81. }
  82. })
  83. }
  84. }
  85. func TestCustomProviderClusterInfoName(t *testing.T) {
  86. cases := map[string]struct {
  87. clusterName string
  88. clusterIDEnv string
  89. expectedName string
  90. }{
  91. "configured cluster name is used": {
  92. clusterName: "my-cluster",
  93. clusterIDEnv: "cluster-id",
  94. expectedName: "my-cluster",
  95. },
  96. "falls back to CLUSTER_ID when cluster name is unset": {
  97. clusterIDEnv: "cluster-id",
  98. expectedName: "cluster-id",
  99. },
  100. "falls back to default when cluster name and CLUSTER_ID are unset": {
  101. expectedName: "Custom Cluster",
  102. },
  103. }
  104. for name, tc := range cases {
  105. t.Run(name, func(t *testing.T) {
  106. t.Setenv("CLUSTER_ID", tc.clusterIDEnv)
  107. cp := &CustomProvider{Config: &fakeProviderConfig{customPricing: &models.CustomPricing{ClusterName: tc.clusterName}}}
  108. info, err := cp.ClusterInfo()
  109. if err != nil {
  110. t.Fatalf("ClusterInfo returned error: %v", err)
  111. }
  112. if info["name"] != tc.expectedName {
  113. t.Fatalf("expected cluster name %q, got %q", tc.expectedName, info["name"])
  114. }
  115. })
  116. }
  117. }