@@ -10,6 +10,17 @@ jest.mock("@aws-sdk/credential-provider-env", () => {
10
10
} ) ;
11
11
import { fromEnv } from "@aws-sdk/credential-provider-env" ;
12
12
13
+ const loadedConfig = {
14
+ credentialsFile : {
15
+ foo : { aws_access_key_id : "key" , aws_secret_access_key : "secret" } ,
16
+ } ,
17
+ configFile : { bar : { aws_access_key_id : "key" , aws_secret_access_key : "secret" } } ,
18
+ } ;
19
+ jest . mock ( "@aws-sdk/shared-ini-file-loader" , ( ) => ( {
20
+ loadSharedConfigFiles : jest . fn ( ) . mockReturnValue ( loadedConfig ) ,
21
+ } ) ) ;
22
+ import { loadSharedConfigFiles } from "@aws-sdk/shared-ini-file-loader" ;
23
+
13
24
jest . mock ( "@aws-sdk/credential-provider-ini" , ( ) => {
14
25
const iniProvider = jest . fn ( ) ;
15
26
return {
@@ -39,6 +50,7 @@ jest.mock("@aws-sdk/credential-provider-imds", () => {
39
50
ENV_CMDS_RELATIVE_URI : "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI" ,
40
51
} ;
41
52
} ) ;
53
+
42
54
import {
43
55
ENV_CMDS_FULL_URI ,
44
56
ENV_CMDS_RELATIVE_URI ,
@@ -78,6 +90,7 @@ beforeEach(() => {
78
90
( fromProcess as any ) . mockClear ( ) ;
79
91
( fromContainerMetadata as any ) . mockClear ( ) ;
80
92
( fromInstanceMetadata as any ) . mockClear ( ) ;
93
+ ( loadSharedConfigFiles as any ) . mockClear ( ) ;
81
94
} ) ;
82
95
83
96
afterAll ( ( ) => {
@@ -200,6 +213,23 @@ describe("defaultProvider", () => {
200
213
expect ( ( fromInstanceMetadata ( ) as any ) . mock . calls . length ) . toBe ( 0 ) ;
201
214
} ) ;
202
215
216
+ it ( "should read config files only once for all providers" , async ( ) => {
217
+ const creds = {
218
+ accessKeyId : "foo" ,
219
+ secretAccessKey : "bar" ,
220
+ } ;
221
+
222
+ ( fromEnv ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Keep moving!" ) ) ) ;
223
+ ( fromIni ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nothing here!" ) ) ) ;
224
+ ( fromProcess ( ) as any ) . mockImplementation ( ( ) => Promise . reject ( new ProviderError ( "Nor here!" ) ) ) ;
225
+ ( fromInstanceMetadata ( ) as any ) . mockImplementation ( ( ) => Promise . resolve ( creds ) ) ;
226
+
227
+ await expect ( defaultProvider ( ) ( ) ) . resolves ;
228
+ expect ( ( loadSharedConfigFiles as any ) . mock . calls . length ) . toBe ( 1 ) ;
229
+ expect ( ( fromIni as any ) . mock . calls [ 1 ] [ 0 ] ) . toMatchObject ( { loadedConfig : loadSharedConfigFiles ( ) } ) ;
230
+ expect ( ( fromProcess as any ) . mock . calls [ 1 ] [ 0 ] ) . toMatchObject ( { loadedConfig : loadSharedConfigFiles ( ) } ) ;
231
+ } ) ;
232
+
203
233
it ( "should pass configuration on to the ini provider" , async ( ) => {
204
234
const iniConfig : FromIniInit = {
205
235
profile : "foo" ,
@@ -226,7 +256,7 @@ describe("defaultProvider", () => {
226
256
await expect ( defaultProvider ( iniConfig ) ( ) ) . resolves ;
227
257
228
258
expect ( ( fromIni as any ) . mock . calls . length ) . toBe ( 1 ) ;
229
- expect ( ( fromIni as any ) . mock . calls [ 0 ] [ 0 ] ) . toEqual ( iniConfig ) ;
259
+ expect ( ( fromIni as any ) . mock . calls [ 0 ] [ 0 ] ) . toEqual ( { ... iniConfig , loadedConfig } ) ;
230
260
} ) ;
231
261
232
262
it ( "should pass configuration on to the process provider" , async ( ) => {
@@ -249,7 +279,7 @@ describe("defaultProvider", () => {
249
279
await expect ( defaultProvider ( processConfig ) ( ) ) . resolves ;
250
280
expect ( ( fromProcess as any ) . mock . calls . length ) . toBe ( 1 ) ;
251
281
expect ( ( fromProcess as any ) . mock . calls . length ) . toBe ( 1 ) ;
252
- expect ( ( fromProcess as any ) . mock . calls [ 0 ] [ 0 ] ) . toEqual ( processConfig ) ;
282
+ expect ( ( fromProcess as any ) . mock . calls [ 0 ] [ 0 ] ) . toEqual ( { ... processConfig , loadedConfig } ) ;
253
283
} ) ;
254
284
255
285
it ( "should pass configuration on to the IMDS provider" , async ( ) => {
@@ -273,7 +303,7 @@ describe("defaultProvider", () => {
273
303
await expect ( defaultProvider ( imdsConfig ) ( ) ) . resolves ;
274
304
275
305
expect ( ( fromInstanceMetadata as any ) . mock . calls . length ) . toBe ( 1 ) ;
276
- expect ( ( fromInstanceMetadata as any ) . mock . calls [ 0 ] [ 0 ] ) . toEqual ( imdsConfig ) ;
306
+ expect ( ( fromInstanceMetadata as any ) . mock . calls [ 0 ] [ 0 ] ) . toEqual ( { ... imdsConfig , loadedConfig } ) ;
277
307
} ) ;
278
308
279
309
it ( "should pass configuration on to the ECS IMDS provider" , async ( ) => {
@@ -298,7 +328,7 @@ describe("defaultProvider", () => {
298
328
await expect ( defaultProvider ( ecsImdsConfig ) ( ) ) . resolves ;
299
329
300
330
expect ( ( fromContainerMetadata as any ) . mock . calls . length ) . toBe ( 1 ) ;
301
- expect ( ( fromContainerMetadata as any ) . mock . calls [ 0 ] [ 0 ] ) . toEqual ( ecsImdsConfig ) ;
331
+ expect ( ( fromContainerMetadata as any ) . mock . calls [ 0 ] [ 0 ] ) . toEqual ( { ... ecsImdsConfig , loadedConfig } ) ;
302
332
} ) ;
303
333
304
334
it ( "should return the same promise across invocations" , async ( ) => {
0 commit comments