controller_handlers_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package config
  2. import (
  3. "bytes"
  4. "reflect"
  5. "strings"
  6. "testing"
  7. "github.com/opencost/opencost/core/pkg/util/json"
  8. "github.com/opencost/opencost/pkg/cloud/aws"
  9. "github.com/opencost/opencost/pkg/cloud/azure"
  10. "github.com/opencost/opencost/pkg/cloud/gcp"
  11. )
  12. func Test_ParseConfig_InvalidType(t *testing.T) {
  13. body := strings.NewReader("{}")
  14. _, err := ParseConfig("invalid_type", body)
  15. if err == nil {
  16. t.Fatalf("expected error, got none")
  17. }
  18. }
  19. func Test_ParseConfig_S3(t *testing.T) {
  20. config := &aws.S3Configuration{
  21. Bucket: "bucket",
  22. Region: "region",
  23. Account: "account",
  24. Authorizer: &aws.AccessKey{
  25. ID: "id",
  26. Secret: "secret",
  27. },
  28. }
  29. configBytes, err := json.Marshal(config)
  30. if err != nil {
  31. t.Fatalf("failed to marshal config: %v", err)
  32. }
  33. parsedConfig, err := ParseConfig(S3ConfigType, bytes.NewReader(configBytes))
  34. if err != nil {
  35. t.Fatalf("failed to parse config: %v", err)
  36. }
  37. if !reflect.DeepEqual(config, parsedConfig) {
  38. t.Fatalf("parsed config does not match original config:\n%+v\n%+v", parsedConfig, config)
  39. }
  40. }
  41. func Test_ParseConfig_Athena(t *testing.T) {
  42. config := &aws.AthenaConfiguration{
  43. Bucket: "bucket",
  44. Region: "region",
  45. Database: "database",
  46. Catalog: "catalog",
  47. Table: "table",
  48. Workgroup: "workgroup",
  49. Account: "account",
  50. Authorizer: &aws.AccessKey{
  51. ID: "id",
  52. Secret: "secret",
  53. },
  54. CURVersion: "curversion",
  55. }
  56. configBytes, err := json.Marshal(config)
  57. if err != nil {
  58. t.Fatalf("failed to marshal config: %v", err)
  59. }
  60. parsedConfig, err := ParseConfig(AthenaConfigType, bytes.NewReader(configBytes))
  61. if err != nil {
  62. t.Fatalf("failed to parse config: %v", err)
  63. }
  64. if !reflect.DeepEqual(config, parsedConfig) {
  65. t.Fatalf("parsed config does not match original config:\n%+v\n%+v", parsedConfig, config)
  66. }
  67. }
  68. func Test_ParseConfig_BigQuery(t *testing.T) {
  69. config := &gcp.BigQueryConfiguration{
  70. ProjectID: "projectid",
  71. Dataset: "dataset",
  72. Table: "table",
  73. ExcludePartitionTime: false,
  74. Authorizer: &gcp.ServiceAccountKey{
  75. Key: map[string]string{
  76. "key": "value",
  77. },
  78. },
  79. }
  80. configBytes, err := json.Marshal(config)
  81. if err != nil {
  82. t.Fatalf("failed to marshal config: %v", err)
  83. }
  84. parsedConfig, err := ParseConfig(BigQueryConfigType, bytes.NewReader(configBytes))
  85. if err != nil {
  86. t.Fatalf("failed to parse config: %v", err)
  87. }
  88. if !reflect.DeepEqual(config, parsedConfig) {
  89. t.Fatalf("parsed config does not match original config:\n%+v\n%+v", parsedConfig, config)
  90. }
  91. }
  92. func Test_ParseConfig_Azure(t *testing.T) {
  93. config := &azure.StorageConfiguration{
  94. SubscriptionID: "subscriptionid",
  95. Account: "account",
  96. Container: "container",
  97. Path: "path",
  98. Cloud: "cloud",
  99. Authorizer: &azure.SharedKeyCredential{
  100. AccessKey: "accesskey",
  101. Account: "account",
  102. },
  103. }
  104. configBytes, err := json.Marshal(config)
  105. if err != nil {
  106. t.Fatalf("failed to marshal config: %v", err)
  107. }
  108. parsedConfig, err := ParseConfig(AzureStorageConfigType, bytes.NewReader(configBytes))
  109. if err != nil {
  110. t.Fatalf("failed to parse config: %v", err)
  111. }
  112. if !reflect.DeepEqual(config, parsedConfig) {
  113. t.Fatalf("parsed config does not match original config:\n%+v\n%+v", parsedConfig, config)
  114. }
  115. }