@@ -177,3 +177,133 @@ func TestDNSProvider_Present(t *testing.T) {
177177 err := provider .Present (domain , "" , keyAuth )
178178 require .NoError (t , err , "Expected Present to return no error" )
179179}
180+
181+ func TestCreateSession (t * testing.T ) {
182+ testCases := []struct {
183+ desc string
184+ env map [string ]string
185+ config * Config
186+ wantCreds credentials.Value
187+ wantDefaultChain bool
188+ wantRegion string
189+ wantErr string
190+ }{
191+ {
192+ desc : "config is nil" ,
193+ wantErr : "config is nil" ,
194+ },
195+ {
196+ desc : "session token without access key id or secret access key" ,
197+ config : & Config {SessionToken : "foo" },
198+ wantErr : "SessionToken must be supplied with AccessKeyID and SecretAccessKey" ,
199+ },
200+ {
201+ desc : "access key id without secret access key" ,
202+ config : & Config {AccessKeyID : "foo" },
203+ wantErr : "AccessKeyID and SecretAccessKey must be supplied together" ,
204+ },
205+ {
206+ desc : "access key id without secret access key" ,
207+ config : & Config {SecretAccessKey : "foo" },
208+ wantErr : "AccessKeyID and SecretAccessKey must be supplied together" ,
209+ },
210+ {
211+ desc : "credentials from default chain" ,
212+ config : & Config {},
213+ wantDefaultChain : true ,
214+ },
215+ {
216+ desc : "static credentials" ,
217+ config : & Config {
218+ AccessKeyID : "one" ,
219+ SecretAccessKey : "two" ,
220+ },
221+ wantCreds : credentials.Value {
222+ AccessKeyID : "one" ,
223+ SecretAccessKey : "two" ,
224+ SessionToken : "" ,
225+ ProviderName : credentials .StaticProviderName ,
226+ },
227+ },
228+ {
229+ desc : "static credentials with session token" ,
230+ config : & Config {
231+ AccessKeyID : "one" ,
232+ SecretAccessKey : "two" ,
233+ SessionToken : "three" ,
234+ },
235+ wantCreds : credentials.Value {
236+ AccessKeyID : "one" ,
237+ SecretAccessKey : "two" ,
238+ SessionToken : "three" ,
239+ ProviderName : credentials .StaticProviderName ,
240+ },
241+ },
242+ {
243+ desc : "region from env" ,
244+ config : & Config {},
245+ env : map [string ]string {
246+ "AWS_REGION" : "foo" ,
247+ },
248+ wantDefaultChain : true ,
249+ wantRegion : "foo" ,
250+ },
251+ {
252+ desc : "static region" ,
253+ config : & Config {
254+ Region : "one" ,
255+ },
256+ env : map [string ]string {
257+ "AWS_REGION" : "foo" ,
258+ },
259+ wantDefaultChain : true ,
260+ wantRegion : "one" ,
261+ },
262+ }
263+
264+ for _ , test := range testCases {
265+ t .Run (test .desc , func (t * testing.T ) {
266+ defer envTest .RestoreEnv ()
267+ envTest .ClearEnv ()
268+
269+ envTest .Apply (test .env )
270+
271+ sess , err := createSession (test .config )
272+ requireErr (t , err , test .wantErr )
273+
274+ if err != nil {
275+ return
276+ }
277+
278+ gotCreds , err := sess .Config .Credentials .Get ()
279+
280+ if test .wantDefaultChain {
281+ assert .NotEqual (t , credentials .StaticProviderName , gotCreds .ProviderName )
282+ } else {
283+ require .NoError (t , err )
284+ assert .Equal (t , test .wantCreds , gotCreds )
285+ }
286+
287+ if test .wantRegion != "" {
288+ assert .Equal (t , test .wantRegion , aws .StringValue (sess .Config .Region ))
289+ }
290+ })
291+ }
292+ }
293+
294+ func requireErr (t * testing.T , err error , wantErr string ) {
295+ t .Helper ()
296+
297+ switch {
298+ case err != nil && wantErr == "" :
299+ // force the assertion error.
300+ require .NoError (t , err )
301+
302+ case err == nil && wantErr != "" :
303+ // force the assertion error.
304+ require .EqualError (t , err , wantErr )
305+
306+ case err != nil && wantErr != "" :
307+ require .EqualError (t , err , wantErr )
308+ }
309+ }
0 commit comments