configurations_test.go 7.6 KB

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