usageconfiguration_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package ibm
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/opencost/opencost/core/pkg/opencost"
  6. "github.com/opencost/opencost/core/pkg/util/json"
  7. "github.com/opencost/opencost/pkg/cloud"
  8. )
  9. func TestUsageConfiguration_Validate(t *testing.T) {
  10. validAuth := &APIKey{Key: "ibm-api-key"}
  11. testCases := map[string]struct {
  12. config UsageConfiguration
  13. expected error
  14. }{
  15. "valid": {
  16. config: UsageConfiguration{
  17. AccountID: "account-id",
  18. Authorizer: validAuth,
  19. },
  20. expected: nil,
  21. },
  22. "missing authorizer": {
  23. config: UsageConfiguration{
  24. AccountID: "account-id",
  25. Authorizer: nil,
  26. },
  27. expected: fmt.Errorf("UsageConfiguration: missing Authorizer"),
  28. },
  29. "invalid authorizer": {
  30. config: UsageConfiguration{
  31. AccountID: "account-id",
  32. Authorizer: &APIKey{Key: ""},
  33. },
  34. expected: fmt.Errorf("UsageConfiguration: APIKey: missing apiKey"),
  35. },
  36. "missing accountID": {
  37. config: UsageConfiguration{
  38. AccountID: "",
  39. Authorizer: validAuth,
  40. },
  41. expected: fmt.Errorf("UsageConfiguration: missing accountID"),
  42. },
  43. }
  44. for name, tc := range testCases {
  45. t.Run(name, func(t *testing.T) {
  46. actual := tc.config.Validate()
  47. actualString := "nil"
  48. if actual != nil {
  49. actualString = actual.Error()
  50. }
  51. expectedString := "nil"
  52. if tc.expected != nil {
  53. expectedString = tc.expected.Error()
  54. }
  55. if actualString != expectedString {
  56. t.Errorf("errors do not match: Actual: '%s', Expected: '%s'", actualString, expectedString)
  57. }
  58. })
  59. }
  60. }
  61. func TestUsageConfiguration_Equals(t *testing.T) {
  62. auth := &APIKey{Key: "key"}
  63. testCases := map[string]struct {
  64. left UsageConfiguration
  65. right cloud.Config
  66. expected bool
  67. }{
  68. "matching": {
  69. left: UsageConfiguration{
  70. AccountID: "acct",
  71. Authorizer: auth,
  72. },
  73. right: &UsageConfiguration{
  74. AccountID: "acct",
  75. Authorizer: &APIKey{Key: "key"},
  76. },
  77. expected: true,
  78. },
  79. "different account": {
  80. left: UsageConfiguration{
  81. AccountID: "acct",
  82. Authorizer: auth,
  83. },
  84. right: &UsageConfiguration{
  85. AccountID: "other",
  86. Authorizer: &APIKey{Key: "key"},
  87. },
  88. expected: false,
  89. },
  90. "different authorizer": {
  91. left: UsageConfiguration{
  92. AccountID: "acct",
  93. Authorizer: auth,
  94. },
  95. right: &UsageConfiguration{
  96. AccountID: "acct",
  97. Authorizer: &APIKey{Key: "other"},
  98. },
  99. expected: false,
  100. },
  101. "both nil authorizer": {
  102. left: UsageConfiguration{
  103. AccountID: "acct",
  104. Authorizer: nil,
  105. },
  106. right: &UsageConfiguration{
  107. AccountID: "acct",
  108. Authorizer: nil,
  109. },
  110. expected: true,
  111. },
  112. "left nil authorizer": {
  113. left: UsageConfiguration{
  114. AccountID: "acct",
  115. Authorizer: nil,
  116. },
  117. right: &UsageConfiguration{
  118. AccountID: "acct",
  119. Authorizer: auth,
  120. },
  121. expected: false,
  122. },
  123. }
  124. for name, tc := range testCases {
  125. t.Run(name, func(t *testing.T) {
  126. if tc.left.Equals(tc.right) != tc.expected {
  127. t.Errorf("incorrect Equals result")
  128. }
  129. })
  130. }
  131. }
  132. func TestUsageConfiguration_KeyProviderSanitize(t *testing.T) {
  133. cfg := &UsageConfiguration{
  134. AccountID: "acct-123",
  135. Authorizer: &APIKey{Key: "secret"},
  136. }
  137. if cfg.Key() != "acct-123" {
  138. t.Errorf("Key() = %q, want acct-123", cfg.Key())
  139. }
  140. slashCfg := &UsageConfiguration{AccountID: "a/acct-123"}
  141. if slashCfg.Key() != "acct-123" {
  142. t.Errorf("Key() with a/ prefix = %q, want acct-123", slashCfg.Key())
  143. }
  144. if cfg.Provider() != opencost.IBMProvider {
  145. t.Errorf("Provider() = %q, want %q", cfg.Provider(), opencost.IBMProvider)
  146. }
  147. sanitized := cfg.Sanitize().(*UsageConfiguration)
  148. if sanitized.Authorizer.(*APIKey).Key != cloud.Redacted {
  149. t.Errorf("Sanitize did not redact api key")
  150. }
  151. }
  152. func TestUsageConfiguration_JSON(t *testing.T) {
  153. cfg := UsageConfiguration{
  154. AccountID: "acct-123",
  155. Authorizer: &APIKey{Key: "secret"},
  156. }
  157. b, err := json.Marshal(&cfg)
  158. if err != nil {
  159. t.Fatalf("marshal: %v", err)
  160. }
  161. unmarshalled := &UsageConfiguration{}
  162. if err := json.Unmarshal(b, unmarshalled); err != nil {
  163. t.Fatalf("unmarshal: %v", err)
  164. }
  165. if !cfg.Equals(unmarshalled) {
  166. t.Error("config does not equal unmarshalled config")
  167. }
  168. }