controller_handlers_test.go 4.5 KB

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