-
Notifications
You must be signed in to change notification settings - Fork 34
/
file_parsing.go
169 lines (141 loc) · 3.87 KB
/
file_parsing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package configtesting
import (
"context"
"os"
"testing"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/hashicorp/aws-sdk-go-base/v2/servicemocks"
)
// haredConfigFileParsing prevents regression in shared config file parsing
// * https://github.com/aws/aws-sdk-go-v2/issues/2349: indented keys
func SharedConfigFileParsing(t *testing.T, driver TestDriver) {
t.Helper()
driver.Init(TestModeLocal)
testcases := map[string]struct {
Configuration []ConfigFunc
SharedConfigurationFile string
Check func(t *testing.T, thing Thing)
}{
"leading newline": {
SharedConfigurationFile: `
[default]
region = us-west-2
`,
Check: func(t *testing.T, thing Thing) {
region := thing.GetRegion()
if a, e := 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
`,
Check: func(t *testing.T, thing Thing) {
region := thing.GetRegion()
if a, e := 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
`,
Check: func(t *testing.T, thing Thing) {
region := thing.GetRegion()
if a, e := region, "us-west-2"; a != e {
t.Errorf("expected region %q, got %q", e, a)
}
},
},
"named profile after leading newline and whitespace": {
Configuration: []ConfigFunc{
WithProfile("test"),
},
// Do not "fix" indentation!
SharedConfigurationFile: `
[default]
region = us-west-2
[profile test]
region = us-east-1
`,
Check: func(t *testing.T, thing Thing) {
region := thing.GetRegion()
if a, e := region, "us-east-1"; a != e {
t.Errorf("expected region %q, got %q", e, a)
}
},
},
"named profile": {
Configuration: []ConfigFunc{
WithProfile("test"),
},
SharedConfigurationFile: `
[default]
region = us-west-2
[profile test]
region = us-east-1
`,
Check: func(t *testing.T, thing Thing) {
region := thing.GetRegion()
if a, e := region, "us-east-1"; a != e {
t.Errorf("expected region %q, got %q", e, a)
}
},
},
"trailing hash": {
SharedConfigurationFile: `
[default]
sso_start_url = https://d-123456789a.awsapps.com/start#
`,
Check: func(t *testing.T, thing Thing) {
ct, ok := thing.(AwsConfigThing)
if !ok {
t.Skipf("Not an AwsConfigThing")
}
awsConfig := ct.GetAwsConfig()
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 {
tc := tc
t.Run(name, func(t *testing.T) {
ctx := context.TODO()
caseDriver := driver.TestCase()
servicemocks.InitSessionTestEnv(t)
config := caseDriver.Configuration(tc.Configuration)
config.SetAccessKey(servicemocks.MockStaticAccessKey)
config.SetSecretKey(servicemocks.MockStaticSecretKey)
if tc.SharedConfigurationFile != "" {
file, err := os.CreateTemp("", "aws-sdk-go-base-shared-configuration-file")
if err != nil {
t.Fatalf("unexpected error creating temporary shared configuration file: %s", err)
}
defer os.Remove(file.Name())
err = os.WriteFile(file.Name(), []byte(tc.SharedConfigurationFile), 0600) //nolint:mnd
if err != nil {
t.Fatalf("unexpected error writing shared configuration file: %s", err)
}
config.AddSharedConfigFile(file.Name())
}
_, thing := caseDriver.Apply(ctx, t)
tc.Check(t, thing)
})
}
}