@@ -5,13 +5,12 @@ import { afterEach, beforeEach, describe, expect, test as it, vi } from "vitest"
5
5
import { InstanceMetadataV1FallbackError } from "./error/InstanceMetadataV1FallbackError" ;
6
6
import {
7
7
fromInstanceMetadata ,
8
- getConfiguredProfileName ,
8
+ getEc2InstanceProfileName ,
9
9
getImdsProfile ,
10
10
throwIfImdsTurnedOff ,
11
11
} from "./fromInstanceMetadata" ;
12
12
import { httpRequest } from "./remoteProvider/httpRequest" ;
13
13
import { fromImdsCredentials , isImdsCredentials } from "./remoteProvider/ImdsCredentials" ;
14
- import { providerConfigFromInit } from "./remoteProvider/RemoteProviderInit" ;
15
14
import { retry } from "./remoteProvider/retry" ;
16
15
import { getInstanceMetadataEndpoint } from "./utils/getInstanceMetadataEndpoint" ;
17
16
import { staticStabilityProvider } from "./utils/staticStabilityProvider" ;
@@ -73,10 +72,6 @@ describe("fromInstanceMetadata", () => {
73
72
vi . mocked ( loadConfig ) . mockReturnValue ( ( ) => Promise . resolve ( false ) ) ;
74
73
vi . spyOn ( { throwIfImdsTurnedOff } , "throwIfImdsTurnedOff" ) . mockResolvedValue ( undefined ) ;
75
74
( isImdsCredentials as unknown as any ) . mockReturnValue ( true ) ;
76
- vi . mocked ( providerConfigFromInit ) . mockReturnValue ( {
77
- timeout : mockTimeout ,
78
- maxRetries : mockMaxRetries ,
79
- } ) ;
80
75
} ) ;
81
76
82
77
afterEach ( ( ) => {
@@ -98,31 +93,36 @@ describe("fromInstanceMetadata", () => {
98
93
) ;
99
94
const provider = fromInstanceMetadata ( { } ) ;
100
95
101
- await expect ( provider ( ) ) . rejects . toEqual ( new CredentialsProviderError ( "IMDS credential fetching is disabled" ) ) ;
96
+ await expect ( provider ( ) ) . rejects . toEqual ( new CredentialsProviderError ( "IMDS credential fetching is disabled" , { } ) ) ;
102
97
expect ( httpRequest ) . not . toHaveBeenCalled ( ) ;
103
98
} ) ;
104
99
105
100
it ( "returns valid credentials with account ID when ec2InstanceProfileName is provided" , async ( ) => {
106
- const profileName = "my-profile-0002" ;
101
+ const ec2InstanceProfileName = "my-profile-0002" ;
107
102
108
103
vi . mocked ( httpRequest )
104
+ . mockImplementation ( ( ( ...args : any [ ] ) => {
105
+ console . log ( ...args ) ;
106
+ return mockToken ;
107
+ } ) as any )
109
108
. mockResolvedValueOnce ( mockToken as any )
109
+ . mockResolvedValueOnce ( JSON . stringify ( mockImdsCreds ) as any )
110
110
. mockResolvedValueOnce ( JSON . stringify ( mockImdsCreds ) as any ) ;
111
111
112
112
vi . mocked ( retry ) . mockImplementation ( ( fn : any ) => fn ( ) ) ;
113
113
vi . mocked ( fromImdsCredentials ) . mockReturnValue ( mockCreds ) ;
114
114
115
- const credentials = await fromInstanceMetadata ( { ec2InstanceProfileName : profileName } ) ( ) ;
115
+ const credentials = await fromInstanceMetadata ( { ec2InstanceProfileName : ec2InstanceProfileName } ) ( ) ;
116
116
117
117
expect ( credentials ) . toEqual ( mockCreds ) ;
118
118
expect ( credentials . accountId ) . toBe ( mockCreds . accountId ) ;
119
119
120
- expect ( httpRequest ) . toHaveBeenCalledTimes ( 2 ) ;
121
120
expect ( httpRequest ) . toHaveBeenNthCalledWith ( 1 , mockTokenRequestOptions ) ;
122
121
expect ( httpRequest ) . toHaveBeenNthCalledWith ( 2 , {
123
122
...mockProfileRequestOptions ,
124
- path : `${ mockProfileRequestOptions . path } ${ profileName } ` ,
123
+ path : `${ mockProfileRequestOptions . path } ${ ec2InstanceProfileName } ` ,
125
124
} ) ;
125
+ expect ( httpRequest ) . toHaveBeenCalledTimes ( 2 ) ;
126
126
} ) ;
127
127
128
128
it ( "returns valid credentials with account ID when profile is discovered from IMDS" , async ( ) => {
@@ -177,7 +177,6 @@ describe("fromInstanceMetadata", () => {
177
177
178
178
vi . mocked ( retry ) . mockImplementation ( ( fn : any ) => fn ( ) ) ;
179
179
vi . mocked ( fromImdsCredentials ) . mockReturnValue ( mockCreds ) ;
180
- vi . spyOn ( { throwIfImdsTurnedOff } , "throwIfImdsTurnedOff" ) . mockResolvedValue ( ) ;
181
180
182
181
await expect ( fromInstanceMetadata ( ) ( ) ) . resolves . toEqual ( mockCreds ) ;
183
182
expect ( httpRequest ) . toHaveBeenNthCalledWith ( 3 , {
@@ -186,34 +185,6 @@ describe("fromInstanceMetadata", () => {
186
185
} ) ;
187
186
} ) ;
188
187
189
- it ( "passes {} to providerConfigFromInit if init not defined" , async ( ) => {
190
- vi . mocked ( retry ) . mockResolvedValueOnce ( mockProfile ) . mockResolvedValueOnce ( mockCreds ) ;
191
- vi . mocked ( loadConfig ) . mockReturnValueOnce ( ( ) => Promise . resolve ( false ) ) ;
192
-
193
- await expect ( fromInstanceMetadata ( ) ( ) ) . resolves . toEqual ( mockCreds ) ;
194
- expect ( providerConfigFromInit ) . toHaveBeenCalledTimes ( 1 ) ;
195
- expect ( providerConfigFromInit ) . toHaveBeenCalledWith ( { } ) ;
196
- } ) ;
197
-
198
- it ( "passes init to providerConfigFromInit" , async ( ) => {
199
- vi . mocked ( retry ) . mockResolvedValueOnce ( mockProfile ) . mockResolvedValueOnce ( mockCreds ) ;
200
- vi . mocked ( loadConfig ) . mockReturnValueOnce ( ( ) => Promise . resolve ( false ) ) ;
201
-
202
- const init = { maxRetries : 5 , timeout : 1213 } ;
203
- await expect ( fromInstanceMetadata ( init ) ( ) ) . resolves . toEqual ( mockCreds ) ;
204
- expect ( providerConfigFromInit ) . toHaveBeenCalledTimes ( 1 ) ;
205
- expect ( providerConfigFromInit ) . toHaveBeenCalledWith ( init ) ;
206
- } ) ;
207
-
208
- it ( "passes maxRetries returned from providerConfigFromInit to retry" , async ( ) => {
209
- vi . mocked ( retry ) . mockResolvedValueOnce ( mockProfile ) . mockResolvedValueOnce ( mockCreds ) ;
210
-
211
- await expect ( fromInstanceMetadata ( ) ( ) ) . resolves . toEqual ( mockCreds ) ;
212
- expect ( retry ) . toHaveBeenCalledTimes ( 2 ) ;
213
- expect ( vi . mocked ( retry ) . mock . calls [ 0 ] [ 1 ] ) . toBe ( mockMaxRetries ) ;
214
- expect ( vi . mocked ( retry ) . mock . calls [ 1 ] [ 1 ] ) . toBe ( mockMaxRetries ) ;
215
- } ) ;
216
-
217
188
it ( "throws CredentialsProviderError if credentials returned are incorrect" , async ( ) => {
218
189
vi . mocked ( httpRequest )
219
190
. mockResolvedValueOnce ( mockToken as any )
@@ -305,9 +276,13 @@ describe("fromInstanceMetadata", () => {
305
276
const profileName = "profile-from-init" ;
306
277
const options = { hostname } ;
307
278
308
- vi . spyOn ( { getConfiguredProfileName } , "getConfiguredProfileName" ) . mockResolvedValueOnce ( profileName ) ;
279
+ vi . spyOn (
280
+ { getConfiguredProfileName : getEc2InstanceProfileName } ,
281
+ "getConfiguredProfileName"
282
+ ) . mockResolvedValueOnce ( profileName ) ;
309
283
310
- const credentials = await getImdsProfile ( options , mockMaxRetries , {
284
+ const credentials = await getImdsProfile ( options , {
285
+ maxRetries : mockMaxRetries ,
311
286
ec2InstanceProfileName : profileName ,
312
287
} ) ;
313
288
@@ -321,7 +296,7 @@ describe("fromInstanceMetadata", () => {
321
296
322
297
vi . mocked ( loadConfig ) . mockReturnValue ( ( ) => Promise . resolve ( envProfileName ) ) ;
323
298
324
- const credentials = await getImdsProfile ( options , mockMaxRetries , { } ) ;
299
+ const credentials = await getImdsProfile ( options , { maxRetries : mockMaxRetries } ) ;
325
300
326
301
expect ( credentials ) . toBe ( envProfileName ) ;
327
302
expect ( httpRequest ) . not . toHaveBeenCalled ( ) ;
@@ -334,7 +309,7 @@ describe("fromInstanceMetadata", () => {
334
309
335
310
vi . mocked ( loadConfig ) . mockReturnValue ( ( ) => Promise . resolve ( configProfileName ) ) ;
336
311
337
- let credentials = await getImdsProfile ( options , mockMaxRetries , { } ) ;
312
+ let credentials = await getImdsProfile ( options , { maxRetries : mockMaxRetries } ) ;
338
313
expect ( credentials ) . toBe ( configProfileName ) ;
339
314
expect ( httpRequest ) . not . toHaveBeenCalled ( ) ;
340
315
@@ -343,7 +318,7 @@ describe("fromInstanceMetadata", () => {
343
318
. mockRejectedValueOnce ( Object . assign ( new Error ( ) , { statusCode : 404 } ) )
344
319
. mockResolvedValueOnce ( legacyProfileName as any ) ;
345
320
346
- credentials = await getImdsProfile ( options , mockMaxRetries , { } ) ;
321
+ credentials = await getImdsProfile ( options , { maxRetries : mockMaxRetries } ) ;
347
322
expect ( credentials ) . toBe ( legacyProfileName ) ;
348
323
expect ( httpRequest ) . toHaveBeenCalledTimes ( 2 ) ;
349
324
expect ( httpRequest ) . toHaveBeenNthCalledWith ( 1 , {
0 commit comments