1
- import { CredentialsProviderError } from "@smithy/property-provider" ;
2
1
import { afterEach , beforeEach , describe , expect , test as it } from "vitest" ;
3
2
4
3
import { fromInstanceMetadata , getMetadataToken } from "./fromInstanceMetadata" ;
@@ -24,8 +23,11 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
24
23
process . env = { ...originalEnv } ;
25
24
} ) ;
26
25
27
- const test = imdsAvailable ? it : it . skip ;
28
- test ( "should fetch metadata token successfully" , async ( ) => {
26
+ it ( "should fetch metadata token successfully" , async ( context ) => {
27
+ if ( ! imdsAvailable ) {
28
+ return context . skip ( ) ;
29
+ }
30
+
29
31
const options = {
30
32
path : "/latest/api/token" ,
31
33
method : "PUT" ,
@@ -40,8 +42,10 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
40
42
expect ( token . length ) . toBeGreaterThan ( 0 ) ;
41
43
} ) ;
42
44
43
- it ( "retrieves credentials successfully" , async ( ) => {
44
- if ( ! imdsAvailable ) return ;
45
+ it ( "retrieves credentials successfully" , async ( context ) => {
46
+ if ( ! imdsAvailable ) {
47
+ return context . skip ( ) ;
48
+ }
45
49
46
50
const provider = fromInstanceMetadata ( { timeout : 1000 , maxRetries : 2 } ) ;
47
51
const credentials = await provider ( ) ;
@@ -52,24 +56,20 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
52
56
expect ( typeof credentials . secretAccessKey ) . toBe ( "string" ) ;
53
57
} ) ;
54
58
55
- it ( "retrieves credentials with account ID on allowlisted instances" , async ( ) => {
56
- if ( ! imdsAvailable ) return ;
59
+ it ( "retrieves credentials with account ID on allowlisted instances" , async ( context ) => {
60
+ if ( ! imdsAvailable ) {
61
+ return context . skip ( ) ;
62
+ }
57
63
58
64
const provider = fromInstanceMetadata ( { timeout : 1000 , maxRetries : 2 } ) ;
59
65
const credentials = await provider ( ) ;
60
66
61
67
if ( ! credentials . accountId ) {
62
- it . skip ( "account ID test skipped - not an allowlisted instance" , ( ) => { } ) ;
68
+ context . skip ( ) ;
63
69
}
64
70
65
71
expect ( credentials . accountId ) . toBeDefined ( ) ;
66
72
expect ( typeof credentials . accountId ) . toBe ( "string" ) ;
67
-
68
- console . log ( "IMDSv2 Credentials with Account ID:" , {
69
- accessKeyId : credentials . accessKeyId ,
70
- sessionToken : credentials . sessionToken ?. slice ( 0 , 10 ) + "..." ,
71
- accountId : credentials . accountId ,
72
- } ) ;
73
73
} ) ;
74
74
75
75
it ( "IMDS access disabled via AWS_EC2_METADATA_DISABLED" , async ( ) => {
@@ -88,23 +88,25 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
88
88
await expect ( provider ( ) ) . rejects . toThrow ( ) ;
89
89
} ) ;
90
90
91
- it ( "Uses configured profile name from env" , async ( ) => {
92
- if ( ! imdsAvailable ) return ;
91
+ it ( "Uses configured profile name from env" , async ( context ) => {
92
+ if ( ! imdsAvailable ) {
93
+ return context . skip ( ) ;
94
+ }
93
95
94
96
const provider = fromInstanceMetadata ( { timeout : 1000 } ) ;
95
97
96
98
try {
97
99
const credentials = await provider ( ) ;
98
100
expect ( credentials ) . toHaveProperty ( "accessKeyId" ) ;
99
- console . log ( "Used configured profile name from env." ) ;
100
101
} catch ( error ) {
101
102
expect ( error ) . toBeDefined ( ) ;
102
- console . log ( "Profile test completed (profile may not exist)." ) ;
103
103
}
104
104
} ) ;
105
105
106
- it ( "Multiple calls return stable results" , async ( ) => {
107
- if ( ! imdsAvailable ) return ;
106
+ it ( "Multiple calls return stable results" , async ( context ) => {
107
+ if ( ! imdsAvailable ) {
108
+ return context . skip ( ) ;
109
+ }
108
110
109
111
const provider = fromInstanceMetadata ( { timeout : 1000 } ) ;
110
112
const creds1 = await provider ( ) ;
@@ -113,12 +115,12 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
113
115
expect ( creds1 . accessKeyId ) . toBeTruthy ( ) ;
114
116
expect ( creds2 . accessKeyId ) . toBeTruthy ( ) ;
115
117
expect ( creds1 . accessKeyId ) . toBe ( creds2 . accessKeyId ) ;
116
-
117
- console . log ( "Stable credentials returned across calls." ) ;
118
118
} ) ;
119
119
120
- it ( "should timeout as expected when a request exceeds the specified duration" , async ( ) => {
121
- if ( ! imdsAvailable ) return ;
120
+ it ( "should timeout as expected when a request exceeds the specified duration" , async ( context ) => {
121
+ if ( ! imdsAvailable ) {
122
+ return context . skip ( ) ;
123
+ }
122
124
const provider = fromInstanceMetadata ( { timeout : 1 } ) ;
123
125
124
126
await expect ( provider ( ) ) . rejects . toThrow ( / t i m e o u t | t i m e d o u t | T i m e o u t E r r o r / i) ;
0 commit comments