| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- package config
- import (
- "encoding/json"
- "testing"
- "github.com/opencost/opencost/pkg/cloud/aws"
- "github.com/opencost/opencost/pkg/cloud/azure"
- "github.com/opencost/opencost/pkg/cloud/gcp"
- )
- var (
- azureMultiCloudConf = MultiCloudConfig{
- AzureConfigs: []azure.AzureStorageConfig{
- {
- SubscriptionId: "subscriptionID",
- AccountName: "accountName",
- AccessKey: "accessKey",
- ContainerName: "containerName",
- ContainerPath: "containerPath",
- AzureCloud: "azureCloud",
- },
- },
- }
- azureConfiguration = &Configurations{
- Azure: &AzureConfigs{
- Storage: []*azure.StorageConfiguration{
- {
- SubscriptionID: "subscriptionID",
- Account: "accountName",
- Container: "containerName",
- Path: "containerPath",
- Cloud: "azureCloud",
- Authorizer: &azure.SharedKeyCredential{
- AccessKey: "accessKey",
- Account: "accountName",
- },
- },
- },
- },
- }
- GCPKeyMultiCloudConf = MultiCloudConfig{
- GCPConfigs: []gcp.BigQueryConfig{
- {
- ProjectID: "projectID",
- BillingDataDataset: "dataset.table",
- Key: map[string]string{
- "key": "value",
- },
- },
- },
- }
- GCPKeyConfigurations = Configurations{
- GCP: &GCPConfigs{BigQuery: []*gcp.BigQueryConfiguration{{
- ProjectID: "projectID",
- Dataset: "dataset",
- Table: "table",
- Authorizer: &gcp.ServiceAccountKey{
- Key: map[string]string{
- "key": "value",
- },
- },
- },
- }},
- }
- GCPWIMultiCloudConf = MultiCloudConfig{
- GCPConfigs: []gcp.BigQueryConfig{
- {
- ProjectID: "projectID",
- BillingDataDataset: "dataset.table",
- Key: nil,
- },
- },
- }
- GCPWIConfigurations = Configurations{
- GCP: &GCPConfigs{BigQuery: []*gcp.BigQueryConfiguration{{
- ProjectID: "projectID",
- Dataset: "dataset",
- Table: "table",
- Authorizer: &gcp.WorkloadIdentity{},
- },
- }},
- }
- AWSAthenaKeyMultiCloudConfig = MultiCloudConfig{
- AWSConfigs: []aws.AwsAthenaInfo{
- {
- AthenaBucketName: "bucket",
- AthenaRegion: "region",
- AthenaDatabase: "database",
- AthenaTable: "table",
- AthenaWorkgroup: "workgroup",
- ServiceKeyName: "id",
- ServiceKeySecret: "secret",
- AccountID: "account",
- MasterPayerARN: "",
- },
- },
- }
- AWSAthenaKeyConfigurations = &Configurations{
- AWS: &AWSConfigs{
- Athena: []*aws.AthenaConfiguration{
- {
- Bucket: "bucket",
- Region: "region",
- Database: "database",
- Table: "table",
- Workgroup: "workgroup",
- Account: "account",
- Authorizer: &aws.AccessKey{
- ID: "id",
- Secret: "secret",
- },
- },
- },
- },
- }
- AWSAthenaAssumeRoleServiceAccountMultiCloudConfig = MultiCloudConfig{
- AWSConfigs: []aws.AwsAthenaInfo{
- {
- AthenaBucketName: "bucket",
- AthenaRegion: "region",
- AthenaDatabase: "database",
- AthenaTable: "table",
- AthenaWorkgroup: "workgroup",
- AccountID: "account",
- MasterPayerARN: "roleArn",
- },
- },
- }
- AWSAthenaAssumeRoleServiceAccountConfigurations = &Configurations{
- AWS: &AWSConfigs{
- Athena: []*aws.AthenaConfiguration{
- {
- Bucket: "bucket",
- Region: "region",
- Database: "database",
- Table: "table",
- Workgroup: "workgroup",
- Account: "account",
- Authorizer: &aws.AssumeRole{
- Authorizer: &aws.ServiceAccount{},
- RoleARN: "roleArn",
- },
- },
- },
- },
- }
- AWSS3ServiceAccountMultiCloudConfig = MultiCloudConfig{
- AWSConfigs: []aws.AwsAthenaInfo{
- {
- AthenaBucketName: "bucket",
- AthenaRegion: "region",
- AccountID: "account",
- MasterPayerARN: "",
- },
- },
- }
- AWSS3ServiceAccountConfigurations = &Configurations{
- AWS: &AWSConfigs{
- S3: []*aws.S3Configuration{
- {
- Bucket: "bucket",
- Region: "region",
- Account: "account",
- Authorizer: &aws.ServiceAccount{},
- },
- },
- },
- }
- AWSS3AssumeRoleAccessKeyMultiCloudConfig = MultiCloudConfig{
- AWSConfigs: []aws.AwsAthenaInfo{
- {
- AthenaBucketName: "bucket",
- AthenaRegion: "region",
- AccountID: "account",
- ServiceKeyName: "id",
- ServiceKeySecret: "secret",
- MasterPayerARN: "roleARN",
- },
- },
- }
- AWSS3AssumeRoleAccessKeyConfigurations = &Configurations{
- AWS: &AWSConfigs{
- S3: []*aws.S3Configuration{
- {
- Bucket: "bucket",
- Region: "region",
- Account: "account",
- Authorizer: &aws.AssumeRole{
- Authorizer: &aws.AccessKey{
- ID: "id",
- Secret: "secret",
- },
- RoleARN: "roleARN",
- },
- },
- },
- },
- }
- )
- func TestConfigurations_UnmarshalJSON(t *testing.T) {
- tests := map[string]struct {
- input any
- expected *Configurations
- }{
- "Azure Storage SharedKeyCredential": {
- input: azureConfiguration,
- expected: azureConfiguration,
- },
- "Azure Storage SharedKeyCredential Conversion": {
- input: azureMultiCloudConf,
- expected: azureConfiguration,
- },
- "GCP BigQuery ServiceAccountKey": {
- input: GCPKeyConfigurations,
- expected: &GCPKeyConfigurations,
- },
- "GCP BigQuery ServiceAccountKey Conversion": {
- input: GCPKeyMultiCloudConf,
- expected: &GCPKeyConfigurations,
- },
- "GCP BigQuery Workload Identity ": {
- input: &GCPWIConfigurations,
- expected: &GCPWIConfigurations,
- },
- "GCP BigQuery Workload Identity Conversion": {
- input: GCPWIMultiCloudConf,
- expected: &GCPWIConfigurations,
- },
- "AWS Athena Access Key": {
- input: AWSAthenaKeyConfigurations,
- expected: AWSAthenaKeyConfigurations,
- },
- "AWS Athena Access Key Conversion": {
- input: AWSAthenaKeyMultiCloudConfig,
- expected: AWSAthenaKeyConfigurations,
- },
- "AWS Athena Assume Role Service Account": {
- input: AWSAthenaAssumeRoleServiceAccountConfigurations,
- expected: AWSAthenaAssumeRoleServiceAccountConfigurations,
- },
- "AWS Athena Assume Role Service Account Conversion": {
- input: AWSAthenaAssumeRoleServiceAccountMultiCloudConfig,
- expected: AWSAthenaAssumeRoleServiceAccountConfigurations,
- },
- "AWS S3 Service Account": {
- input: AWSS3ServiceAccountConfigurations,
- expected: AWSS3ServiceAccountConfigurations,
- },
- "AWS S3 Service Account Conversion": {
- input: AWSS3ServiceAccountMultiCloudConfig,
- expected: AWSS3ServiceAccountConfigurations,
- },
- "AWS S3 Assume Role Access Key": {
- input: AWSS3AssumeRoleAccessKeyConfigurations,
- expected: AWSS3AssumeRoleAccessKeyConfigurations,
- },
- "AWS S3 Assume Role Service Access Key": {
- input: AWSS3AssumeRoleAccessKeyMultiCloudConfig,
- expected: AWSS3AssumeRoleAccessKeyConfigurations,
- },
- }
- for name, tt := range tests {
- t.Run(name, func(t *testing.T) {
- b, err := json.Marshal(tt.input)
- if err != nil {
- t.Fatalf("failed to marshal input")
- }
- actual := &Configurations{}
- err = json.Unmarshal(b, actual)
- if err != nil && tt.expected != nil {
- t.Fatalf("Unmarshal failed with error %s", err.Error())
- }
- if !tt.expected.Equals(actual) {
- t.Fatalf("actual Configuration did not match expected")
- }
- })
- }
- }
|