2
0

controller_handlers_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. CURVersion: "curversion",
  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_GetAddConfigHandler(t *testing.T) {
  119. controller := &Controller{
  120. storage: &MemoryControllerStorage{},
  121. }
  122. handler := controller.GetAddConfigHandler()
  123. if handler == nil {
  124. t.Fatalf("expected handler, got nil")
  125. }
  126. // Test no type param
  127. req := httptest.NewRequest("GET", "/", nil)
  128. w := httptest.NewRecorder()
  129. handler(w, req, nil)
  130. if w.Code != http.StatusBadRequest {
  131. t.Fatalf("expected 400 status code, got %v: %v", w.Code, w.Body.String())
  132. }
  133. // Test no config body
  134. req = httptest.NewRequest("GET", "/?type="+S3ConfigType, nil)
  135. w = httptest.NewRecorder()
  136. handler(w, req, nil)
  137. if w.Code != http.StatusBadRequest {
  138. t.Fatalf("expected 400 status code, got %v: %v", w.Code, w.Body.String())
  139. }
  140. // Test with config body
  141. mockConfig := aws.S3Configuration{
  142. Bucket: "bucket",
  143. Region: "region",
  144. Account: "account",
  145. Authorizer: &aws.AccessKey{
  146. ID: "id",
  147. Secret: "secret",
  148. },
  149. }
  150. configBytes, err := json.Marshal(mockConfig)
  151. if err != nil {
  152. t.Fatalf("failed to marshal config: %v", err)
  153. }
  154. req = httptest.NewRequest("GET", "/?type="+S3ConfigType, bytes.NewReader(configBytes))
  155. w = httptest.NewRecorder()
  156. handler(w, req, nil)
  157. if w.Code != http.StatusOK {
  158. t.Fatalf("expected 200 status code, got %v: %v", w.Code, w.Body.String())
  159. }
  160. }