configurations_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. CURVersion: "2.0",
  111. },
  112. },
  113. },
  114. }
  115. AWSAthenaAssumeRoleServiceAccountMultiCloudConfig = MultiCloudConfig{
  116. AWSConfigs: []aws.AwsAthenaInfo{
  117. {
  118. AthenaBucketName: "bucket",
  119. AthenaRegion: "region",
  120. AthenaDatabase: "database",
  121. AthenaTable: "table",
  122. AthenaWorkgroup: "workgroup",
  123. AccountID: "account",
  124. MasterPayerARN: "roleArn",
  125. },
  126. },
  127. }
  128. AWSAthenaAssumeRoleServiceAccountConfigurations = &Configurations{
  129. AWS: &AWSConfigs{
  130. Athena: []*aws.AthenaConfiguration{
  131. {
  132. Bucket: "bucket",
  133. Region: "region",
  134. Database: "database",
  135. Table: "table",
  136. Workgroup: "workgroup",
  137. Account: "account",
  138. Authorizer: &aws.AssumeRole{
  139. Authorizer: &aws.ServiceAccount{},
  140. RoleARN: "roleArn",
  141. },
  142. CURVersion: "2.0",
  143. },
  144. },
  145. },
  146. }
  147. AWSS3ServiceAccountMultiCloudConfig = MultiCloudConfig{
  148. AWSConfigs: []aws.AwsAthenaInfo{
  149. {
  150. AthenaBucketName: "bucket",
  151. AthenaRegion: "region",
  152. AccountID: "account",
  153. MasterPayerARN: "",
  154. },
  155. },
  156. }
  157. AWSS3ServiceAccountConfigurations = &Configurations{
  158. AWS: &AWSConfigs{
  159. S3: []*aws.S3Configuration{
  160. {
  161. Bucket: "bucket",
  162. Region: "region",
  163. Account: "account",
  164. Authorizer: &aws.ServiceAccount{},
  165. },
  166. },
  167. },
  168. }
  169. AWSS3AssumeRoleAccessKeyMultiCloudConfig = MultiCloudConfig{
  170. AWSConfigs: []aws.AwsAthenaInfo{
  171. {
  172. AthenaBucketName: "bucket",
  173. AthenaRegion: "region",
  174. AccountID: "account",
  175. ServiceKeyName: "id",
  176. ServiceKeySecret: "secret",
  177. MasterPayerARN: "roleARN",
  178. },
  179. },
  180. }
  181. AWSS3AssumeRoleAccessKeyConfigurations = &Configurations{
  182. AWS: &AWSConfigs{
  183. S3: []*aws.S3Configuration{
  184. {
  185. Bucket: "bucket",
  186. Region: "region",
  187. Account: "account",
  188. Authorizer: &aws.AssumeRole{
  189. Authorizer: &aws.AccessKey{
  190. ID: "id",
  191. Secret: "secret",
  192. },
  193. RoleARN: "roleARN",
  194. },
  195. },
  196. },
  197. },
  198. }
  199. )
  200. func TestConfigurations_UnmarshalJSON(t *testing.T) {
  201. tests := map[string]struct {
  202. input any
  203. expected *Configurations
  204. }{
  205. "Azure Storage SharedKeyCredential": {
  206. input: azureConfiguration,
  207. expected: azureConfiguration,
  208. },
  209. "Azure Storage SharedKeyCredential Conversion": {
  210. input: azureMultiCloudConf,
  211. expected: azureConfiguration,
  212. },
  213. "GCP BigQuery ServiceAccountKey": {
  214. input: GCPKeyConfigurations,
  215. expected: &GCPKeyConfigurations,
  216. },
  217. "GCP BigQuery ServiceAccountKey Conversion": {
  218. input: GCPKeyMultiCloudConf,
  219. expected: &GCPKeyConfigurations,
  220. },
  221. "GCP BigQuery Workload Identity ": {
  222. input: &GCPWIConfigurations,
  223. expected: &GCPWIConfigurations,
  224. },
  225. "GCP BigQuery Workload Identity Conversion": {
  226. input: GCPWIMultiCloudConf,
  227. expected: &GCPWIConfigurations,
  228. },
  229. "AWS Athena Access Key": {
  230. input: AWSAthenaKeyConfigurations,
  231. expected: AWSAthenaKeyConfigurations,
  232. },
  233. "AWS Athena Access Key Conversion": {
  234. input: AWSAthenaKeyMultiCloudConfig,
  235. expected: AWSAthenaKeyConfigurations,
  236. },
  237. "AWS Athena Assume Role Service Account": {
  238. input: AWSAthenaAssumeRoleServiceAccountConfigurations,
  239. expected: AWSAthenaAssumeRoleServiceAccountConfigurations,
  240. },
  241. "AWS Athena Assume Role Service Account Conversion": {
  242. input: AWSAthenaAssumeRoleServiceAccountMultiCloudConfig,
  243. expected: AWSAthenaAssumeRoleServiceAccountConfigurations,
  244. },
  245. "AWS S3 Service Account": {
  246. input: AWSS3ServiceAccountConfigurations,
  247. expected: AWSS3ServiceAccountConfigurations,
  248. },
  249. "AWS S3 Service Account Conversion": {
  250. input: AWSS3ServiceAccountMultiCloudConfig,
  251. expected: AWSS3ServiceAccountConfigurations,
  252. },
  253. "AWS S3 Assume Role Access Key": {
  254. input: AWSS3AssumeRoleAccessKeyConfigurations,
  255. expected: AWSS3AssumeRoleAccessKeyConfigurations,
  256. },
  257. "AWS S3 Assume Role Service Access Key": {
  258. input: AWSS3AssumeRoleAccessKeyMultiCloudConfig,
  259. expected: AWSS3AssumeRoleAccessKeyConfigurations,
  260. },
  261. }
  262. for name, tt := range tests {
  263. t.Run(name, func(t *testing.T) {
  264. b, err := json.Marshal(tt.input)
  265. if err != nil {
  266. t.Fatalf("failed to marshal input")
  267. }
  268. actual := &Configurations{}
  269. err = json.Unmarshal(b, actual)
  270. if err != nil && tt.expected != nil {
  271. t.Fatalf("Unmarshal failed with error %s", err.Error())
  272. }
  273. if !tt.expected.Equals(actual) {
  274. t.Fatalf("actual Configuration did not match expected")
  275. }
  276. })
  277. }
  278. }