Skip to content

Commit

Permalink
Merge pull request #34429 from hashicorp/b-config-file-parsing-tests
Browse files Browse the repository at this point in the history
Adds tests for shared config file parsing
  • Loading branch information
gdavison authored Nov 16, 2023
2 parents b9ec173 + 4465eae commit fc2b8ae
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
4 changes: 4 additions & 0 deletions internal/conns/awsclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ func (client *AWSClient) CredentialsProvider() aws_sdkv2.CredentialsProvider {
return client.awsConfig.Credentials
}

func (client *AWSClient) AwsConfig() aws_sdkv2.Config { // nosemgrep:ci.aws-in-func-name
return client.awsConfig.Copy()
}

// PartitionHostname returns a hostname with the provider domain suffix for the partition
// e.g. PREFIX.amazonaws.com
// The prefix should not contain a trailing period.
Expand Down
97 changes: 96 additions & 1 deletion internal/provider/provider_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/google/go-cmp/cmp"
configtesting "github.com/hashicorp/aws-sdk-go-base/v2/configtesting"
"github.com/hashicorp/aws-sdk-go-base/v2/servicemocks"
Expand All @@ -17,29 +18,117 @@ import (
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
"golang.org/x/exp/maps"
)

// TestSharedConfigFileParsing prevents regression in shared config file parsing
// * https://github.com/aws/aws-sdk-go-v2/issues/2349: indented keys
// * https://github.com/aws/aws-sdk-go-v2/issues/2363: leading whitespace
// * https://github.com/aws/aws-sdk-go-v2/issues/2369: trailing `#` in, e.g. SSO Start URLs
func TestSharedConfigFileParsing(t *testing.T) { //nolint:paralleltest
testcases := map[string]struct {
Config map[string]any
SharedConfigurationFile string
Check func(t *testing.T, meta *conns.AWSClient)
}{
"leading newline": {
SharedConfigurationFile: `
[default]
region = us-west-2
`, //lintignore:AWSAT003
Check: func(t *testing.T, meta *conns.AWSClient) {
//lintignore:AWSAT003
if a, e := meta.Region, "us-west-2"; a != e {
t.Errorf("expected region %q, got %q", e, a)
}
},
},

"leading whitespace": {
// Do not "fix" indentation!
SharedConfigurationFile: ` [default]
region = us-west-2
`, //lintignore:AWSAT003
Check: func(t *testing.T, meta *conns.AWSClient) {
//lintignore:AWSAT003
if a, e := meta.Region, "us-west-2"; a != e {
t.Errorf("expected region %q, got %q", e, a)
}
},
},

"leading newline and whitespace": {
// Do not "fix" indentation!
SharedConfigurationFile: `
[default]
region = us-west-2
`, //lintignore:AWSAT003
`, //lintignore:AWSAT003
Check: func(t *testing.T, meta *conns.AWSClient) {
//lintignore:AWSAT003
if a, e := meta.Region, "us-west-2"; a != e {
t.Errorf("expected region %q, got %q", e, a)
}
},
},

"named profile after leading newline and whitespace": {
Config: map[string]any{
"profile": "test",
},
// Do not "fix" indentation!
SharedConfigurationFile: `
[default]
region = us-west-2
[profile test]
region = us-east-1
`, //lintignore:AWSAT003
Check: func(t *testing.T, meta *conns.AWSClient) {
//lintignore:AWSAT003
if a, e := meta.Region, "us-east-1"; a != e {
t.Errorf("expected region %q, got %q", e, a)
}
},
},

"named profile": {
Config: map[string]any{
"profile": "test",
},
SharedConfigurationFile: `
[default]
region = us-west-2
[profile test]
region = us-east-1
`, //lintignore:AWSAT003
Check: func(t *testing.T, meta *conns.AWSClient) {
//lintignore:AWSAT003
if a, e := meta.Region, "us-east-1"; a != e {
t.Errorf("expected region %q, got %q", e, a)
}
},
},

"trailing hash": {
SharedConfigurationFile: `
[default]
region = us-west-2
sso_start_url = https://d-123456789a.awsapps.com/start#
`, //lintignore:AWSAT003
Check: func(t *testing.T, meta *conns.AWSClient) {
awsConfig := meta.AwsConfig()
var ssoStartUrl string
for _, source := range awsConfig.ConfigSources {
if shared, ok := source.(config.SharedConfig); ok {
ssoStartUrl = shared.SSOStartURL
}
}
if a, e := ssoStartUrl, "https://d-123456789a.awsapps.com/start#"; a != e {
t.Errorf("expected sso_start_url %q, got %q", e, a)
}
},
},
}

for name, tc := range testcases { //nolint:paralleltest
Expand All @@ -57,6 +146,8 @@ func TestSharedConfigFileParsing(t *testing.T) { //nolint:paralleltest
"skip_requesting_account_id": true,
}

maps.Copy(config, tc.Config)

if tc.SharedConfigurationFile != "" {
file, err := os.CreateTemp("", "aws-sdk-go-base-shared-configuration-file")

Expand Down Expand Up @@ -103,6 +194,10 @@ func TestSharedConfigFileParsing(t *testing.T) { //nolint:paralleltest
t.Errorf("unexpected diagnostics difference: %s", diff)
}

if diags.HasError() {
t.FailNow()
}

meta := p.Meta().(*conns.AWSClient)

tc.Check(t, meta)
Expand Down

0 comments on commit fc2b8ae

Please sign in to comment.