controller_handlers_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package config
  2. import (
  3. "bytes"
  4. "net/http"
  5. "net/http/httptest"
  6. "reflect"
  7. "strings"
  8. "testing"
  9. "github.com/opencost/opencost/core/pkg/util/json"
  10. "github.com/opencost/opencost/pkg/cloud/aws"
  11. "github.com/opencost/opencost/pkg/cloud/azure"
  12. "github.com/opencost/opencost/pkg/cloud/gcp"
  13. "github.com/opencost/opencost/pkg/cloud/ibm"
  14. )
  15. func Test_ParseConfig_InvalidType(t *testing.T) {
  16. body := strings.NewReader("{}")
  17. _, err := ParseConfig("invalid_type", body)
  18. if err == nil {
  19. t.Fatalf("expected error, got none")
  20. }
  21. }
  22. func Test_ParseConfig_S3(t *testing.T) {
  23. config := &aws.S3Configuration{
  24. Bucket: "bucket",
  25. Region: "region",
  26. Account: "account",
  27. Authorizer: &aws.AccessKey{
  28. ID: "id",
  29. Secret: "secret",
  30. },
  31. }
  32. configBytes, err := json.Marshal(config)
  33. if err != nil {
  34. t.Fatalf("failed to marshal config: %v", err)
  35. }
  36. parsedConfig, err := ParseConfig(S3ConfigType, bytes.NewReader(configBytes))
  37. if err != nil {
  38. t.Fatalf("failed to parse config: %v", err)
  39. }
  40. if !reflect.DeepEqual(config, parsedConfig) {
  41. t.Fatalf("parsed config does not match original config:\n%+v\n%+v", parsedConfig, config)
  42. }
  43. }
  44. func Test_ParseConfig_Athena(t *testing.T) {
  45. config := &aws.AthenaConfiguration{
  46. Bucket: "bucket",
  47. Region: "region",
  48. Database: "database",
  49. Catalog: "catalog",
  50. Table: "table",
  51. Workgroup: "workgroup",
  52. Account: "account",
  53. Authorizer: &aws.AccessKey{
  54. ID: "id",
  55. Secret: "secret",
  56. },
  57. }
  58. configBytes, err := json.Marshal(config)
  59. if err != nil {
  60. t.Fatalf("failed to marshal config: %v", err)
  61. }
  62. parsedConfig, err := ParseConfig(AthenaConfigType, bytes.NewReader(configBytes))
  63. if err != nil {
  64. t.Fatalf("failed to parse config: %v", err)
  65. }
  66. if !reflect.DeepEqual(config, parsedConfig) {
  67. t.Fatalf("parsed config does not match original config:\n%+v\n%+v", parsedConfig, config)
  68. }
  69. }
  70. func Test_ParseConfig_BigQuery(t *testing.T) {
  71. config := &gcp.BigQueryConfiguration{
  72. ProjectID: "projectid",
  73. Dataset: "dataset",
  74. Table: "table",
  75. ExcludePartitionTime: false,
  76. Authorizer: &gcp.ServiceAccountKey{
  77. Key: map[string]string{
  78. "key": "value",
  79. },
  80. },
  81. }
  82. configBytes, err := json.Marshal(config)
  83. if err != nil {
  84. t.Fatalf("failed to marshal config: %v", err)
  85. }
  86. parsedConfig, err := ParseConfig(BigQueryConfigType, bytes.NewReader(configBytes))
  87. if err != nil {
  88. t.Fatalf("failed to parse config: %v", err)
  89. }
  90. if !reflect.DeepEqual(config, parsedConfig) {
  91. t.Fatalf("parsed config does not match original config:\n%+v\n%+v", parsedConfig, config)
  92. }
  93. }
  94. func Test_ParseConfig_Azure(t *testing.T) {
  95. config := &azure.StorageConfiguration{
  96. SubscriptionID: "subscriptionid",
  97. Account: "account",
  98. Container: "container",
  99. Path: "path",
  100. Cloud: "cloud",
  101. Authorizer: &azure.SharedKeyCredential{
  102. AccessKey: "accesskey",
  103. Account: "account",
  104. },
  105. }
  106. configBytes, err := json.Marshal(config)
  107. if err != nil {
  108. t.Fatalf("failed to marshal config: %v", err)
  109. }
  110. parsedConfig, err := ParseConfig(AzureStorageConfigType, bytes.NewReader(configBytes))
  111. if err != nil {
  112. t.Fatalf("failed to parse config: %v", err)
  113. }
  114. if !reflect.DeepEqual(config, parsedConfig) {
  115. t.Fatalf("parsed config does not match original config:\n%+v\n%+v", parsedConfig, config)
  116. }
  117. }
  118. func Test_ParseConfig_IBM(t *testing.T) {
  119. config := &ibm.UsageConfiguration{
  120. AccountID: "account-id",
  121. Authorizer: &ibm.APIKey{
  122. Key: "api-key",
  123. },
  124. }
  125. configBytes, err := json.Marshal(config)
  126. if err != nil {
  127. t.Fatalf("failed to marshal config: %v", err)
  128. }
  129. parsedConfig, err := ParseConfig(IBMUsageConfigType, bytes.NewReader(configBytes))
  130. if err != nil {
  131. t.Fatalf("failed to parse config: %v", err)
  132. }
  133. if !config.Equals(parsedConfig) {
  134. t.Fatalf("parsed config does not match original config:\n%+v\n%+v", parsedConfig, config)
  135. }
  136. }
  137. func Test_GetAddConfigHandler(t *testing.T) {
  138. controller := &Controller{
  139. storage: &MemoryControllerStorage{},
  140. }
  141. handler := controller.GetAddConfigHandler()
  142. if handler == nil {
  143. t.Fatalf("expected handler, got nil")
  144. }
  145. // Test no type param
  146. req := httptest.NewRequest("GET", "/", nil)
  147. w := httptest.NewRecorder()
  148. handler(w, req, nil)
  149. if w.Code != http.StatusBadRequest {
  150. t.Fatalf("expected 400 status code, got %v: %v", w.Code, w.Body.String())
  151. }
  152. // Test no config body
  153. req = httptest.NewRequest("GET", "/?type="+S3ConfigType, nil)
  154. w = httptest.NewRecorder()
  155. handler(w, req, nil)
  156. if w.Code != http.StatusBadRequest {
  157. t.Fatalf("expected 400 status code, got %v: %v", w.Code, w.Body.String())
  158. }
  159. // Test with config body
  160. mockConfig := aws.S3Configuration{
  161. Bucket: "bucket",
  162. Region: "region",
  163. Account: "account",
  164. Authorizer: &aws.AccessKey{
  165. ID: "id",
  166. Secret: "secret",
  167. },
  168. }
  169. configBytes, err := json.Marshal(mockConfig)
  170. if err != nil {
  171. t.Fatalf("failed to marshal config: %v", err)
  172. }
  173. req = httptest.NewRequest("GET", "/?type="+S3ConfigType, bytes.NewReader(configBytes))
  174. w = httptest.NewRecorder()
  175. handler(w, req, nil)
  176. if w.Code != http.StatusOK {
  177. t.Fatalf("expected 200 status code, got %v: %v", w.Code, w.Body.String())
  178. }
  179. }