configurations_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. package config
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "github.com/opencost/opencost/pkg/cloud/aws"
  6. "github.com/opencost/opencost/pkg/cloud/azure"
  7. "github.com/opencost/opencost/pkg/cloud/gcp"
  8. )
  9. var (
  10. azureMultiCloudConf = MultiCloudConfig{
  11. AzureConfigs: []azure.AzureStorageConfig{
  12. {
  13. SubscriptionId: "subscriptionID",
  14. AccountName: "accountName",
  15. AccessKey: "accessKey",
  16. ContainerName: "containerName",
  17. ContainerPath: "containerPath",
  18. AzureCloud: "azureCloud",
  19. },
  20. },
  21. }
  22. azureConfiguration = &Configurations{
  23. Azure: &AzureConfigs{
  24. Storage: []*azure.StorageConfiguration{
  25. {
  26. SubscriptionID: "subscriptionID",
  27. Account: "accountName",
  28. Container: "containerName",
  29. Path: "containerPath",
  30. Cloud: "azureCloud",
  31. Authorizer: &azure.SharedKeyCredential{
  32. AccessKey: "accessKey",
  33. Account: "accountName",
  34. },
  35. },
  36. },
  37. },
  38. }
  39. GCPKeyMultiCloudConf = MultiCloudConfig{
  40. GCPConfigs: []gcp.BigQueryConfig{
  41. {
  42. ProjectID: "projectID",
  43. BillingDataDataset: "dataset.table",
  44. Key: map[string]string{
  45. "key": "value",
  46. },
  47. },
  48. },
  49. }
  50. GCPKeyConfigurations = Configurations{
  51. GCP: &GCPConfigs{BigQuery: []*gcp.BigQueryConfiguration{{
  52. ProjectID: "projectID",
  53. Dataset: "dataset",
  54. Table: "table",
  55. Authorizer: &gcp.ServiceAccountKey{
  56. Key: map[string]string{
  57. "key": "value",
  58. },
  59. },
  60. },
  61. }},
  62. }
  63. GCPWIMultiCloudConf = MultiCloudConfig{
  64. GCPConfigs: []gcp.BigQueryConfig{
  65. {
  66. ProjectID: "projectID",
  67. BillingDataDataset: "dataset.table",
  68. Key: nil,
  69. },
  70. },
  71. }
  72. GCPWIConfigurations = Configurations{
  73. GCP: &GCPConfigs{BigQuery: []*gcp.BigQueryConfiguration{{
  74. ProjectID: "projectID",
  75. Dataset: "dataset",
  76. Table: "table",
  77. Authorizer: &gcp.WorkloadIdentity{},
  78. },
  79. }},
  80. }
  81. AWSAthenaKeyMultiCloudConfig = MultiCloudConfig{
  82. AWSConfigs: []aws.AwsAthenaInfo{
  83. {
  84. AthenaBucketName: "bucket",
  85. AthenaRegion: "region",
  86. AthenaDatabase: "database",
  87. AthenaTable: "table",
  88. AthenaWorkgroup: "workgroup",
  89. ServiceKeyName: "id",
  90. ServiceKeySecret: "secret",
  91. AccountID: "account",
  92. MasterPayerARN: "",
  93. },
  94. },
  95. }
  96. AWSAthenaKeyConfigurations = &Configurations{
  97. AWS: &AWSConfigs{
  98. Athena: []*aws.AthenaConfiguration{
  99. {
  100. Bucket: "bucket",
  101. Region: "region",
  102. Database: "database",
  103. Table: "table",
  104. Workgroup: "workgroup",
  105. Account: "account",
  106. Authorizer: &aws.AccessKey{
  107. ID: "id",
  108. Secret: "secret",
  109. },
  110. },
  111. },
  112. },
  113. }
  114. AWSAthenaAssumeRoleServiceAccountMultiCloudConfig = MultiCloudConfig{
  115. AWSConfigs: []aws.AwsAthenaInfo{
  116. {
  117. AthenaBucketName: "bucket",
  118. AthenaRegion: "region",
  119. AthenaDatabase: "database",
  120. AthenaTable: "table",
  121. AthenaWorkgroup: "workgroup",
  122. AccountID: "account",
  123. MasterPayerARN: "roleArn",
  124. },
  125. },
  126. }
  127. AWSAthenaAssumeRoleServiceAccountConfigurations = &Configurations{
  128. AWS: &AWSConfigs{
  129. Athena: []*aws.AthenaConfiguration{
  130. {
  131. Bucket: "bucket",
  132. Region: "region",
  133. Database: "database",
  134. Table: "table",
  135. Workgroup: "workgroup",
  136. Account: "account",
  137. Authorizer: &aws.AssumeRole{
  138. Authorizer: &aws.ServiceAccount{},
  139. RoleARN: "roleArn",
  140. },
  141. },
  142. },
  143. },
  144. }
  145. AWSS3ServiceAccountMultiCloudConfig = MultiCloudConfig{
  146. AWSConfigs: []aws.AwsAthenaInfo{
  147. {
  148. AthenaBucketName: "bucket",
  149. AthenaRegion: "region",
  150. AccountID: "account",
  151. MasterPayerARN: "",
  152. },
  153. },
  154. }
  155. AWSS3ServiceAccountConfigurations = &Configurations{
  156. AWS: &AWSConfigs{
  157. S3: []*aws.S3Configuration{
  158. {
  159. Bucket: "bucket",
  160. Region: "region",
  161. Account: "account",
  162. Authorizer: &aws.ServiceAccount{},
  163. },
  164. },
  165. },
  166. }
  167. AWSS3AssumeRoleAccessKeyMultiCloudConfig = MultiCloudConfig{
  168. AWSConfigs: []aws.AwsAthenaInfo{
  169. {
  170. AthenaBucketName: "bucket",
  171. AthenaRegion: "region",
  172. AccountID: "account",
  173. ServiceKeyName: "id",
  174. ServiceKeySecret: "secret",
  175. MasterPayerARN: "roleARN",
  176. },
  177. },
  178. }
  179. AWSS3AssumeRoleAccessKeyConfigurations = &Configurations{
  180. AWS: &AWSConfigs{
  181. S3: []*aws.S3Configuration{
  182. {
  183. Bucket: "bucket",
  184. Region: "region",
  185. Account: "account",
  186. Authorizer: &aws.AssumeRole{
  187. Authorizer: &aws.AccessKey{
  188. ID: "id",
  189. Secret: "secret",
  190. },
  191. RoleARN: "roleARN",
  192. },
  193. },
  194. },
  195. },
  196. }
  197. )
  198. func TestConfigurations_UnmarshalJSON(t *testing.T) {
  199. tests := map[string]struct {
  200. input any
  201. expected *Configurations
  202. }{
  203. "Azure Storage SharedKeyCredential": {
  204. input: azureConfiguration,
  205. expected: azureConfiguration,
  206. },
  207. "Azure Storage SharedKeyCredential Conversion": {
  208. input: azureMultiCloudConf,
  209. expected: azureConfiguration,
  210. },
  211. "GCP BigQuery ServiceAccountKey": {
  212. input: GCPKeyConfigurations,
  213. expected: &GCPKeyConfigurations,
  214. },
  215. "GCP BigQuery ServiceAccountKey Conversion": {
  216. input: GCPKeyMultiCloudConf,
  217. expected: &GCPKeyConfigurations,
  218. },
  219. "GCP BigQuery Workload Identity ": {
  220. input: &GCPWIConfigurations,
  221. expected: &GCPWIConfigurations,
  222. },
  223. "GCP BigQuery Workload Identity Conversion": {
  224. input: GCPWIMultiCloudConf,
  225. expected: &GCPWIConfigurations,
  226. },
  227. "AWS Athena Access Key": {
  228. input: AWSAthenaKeyConfigurations,
  229. expected: AWSAthenaKeyConfigurations,
  230. },
  231. "AWS Athena Access Key Conversion": {
  232. input: AWSAthenaKeyMultiCloudConfig,
  233. expected: AWSAthenaKeyConfigurations,
  234. },
  235. "AWS Athena Assume Role Service Account": {
  236. input: AWSAthenaAssumeRoleServiceAccountConfigurations,
  237. expected: AWSAthenaAssumeRoleServiceAccountConfigurations,
  238. },
  239. "AWS Athena Assume Role Service Account Conversion": {
  240. input: AWSAthenaAssumeRoleServiceAccountMultiCloudConfig,
  241. expected: AWSAthenaAssumeRoleServiceAccountConfigurations,
  242. },
  243. "AWS S3 Service Account": {
  244. input: AWSS3ServiceAccountConfigurations,
  245. expected: AWSS3ServiceAccountConfigurations,
  246. },
  247. "AWS S3 Service Account Conversion": {
  248. input: AWSS3ServiceAccountMultiCloudConfig,
  249. expected: AWSS3ServiceAccountConfigurations,
  250. },
  251. "AWS S3 Assume Role Access Key": {
  252. input: AWSS3AssumeRoleAccessKeyConfigurations,
  253. expected: AWSS3AssumeRoleAccessKeyConfigurations,
  254. },
  255. "AWS S3 Assume Role Service Access Key": {
  256. input: AWSS3AssumeRoleAccessKeyMultiCloudConfig,
  257. expected: AWSS3AssumeRoleAccessKeyConfigurations,
  258. },
  259. }
  260. for name, tt := range tests {
  261. t.Run(name, func(t *testing.T) {
  262. b, err := json.Marshal(tt.input)
  263. if err != nil {
  264. t.Fatalf("failed to marshal input")
  265. }
  266. actual := &Configurations{}
  267. err = json.Unmarshal(b, actual)
  268. if err != nil && tt.expected != nil {
  269. t.Fatalf("Unmarshal failed with error %s", err.Error())
  270. }
  271. if !tt.expected.Equals(actual) {
  272. t.Fatalf("actual Configuration did not match expected")
  273. }
  274. })
  275. }
  276. }