1
1
import { afterEach , beforeEach , describe , expect , test as it } from "vitest" ;
2
2
3
3
import { fromInstanceMetadata , getMetadataToken } from "./fromInstanceMetadata" ;
4
+ import { getInstanceMetadataEndpoint } from "./utils/getInstanceMetadataEndpoint" ;
4
5
5
6
describe ( "fromInstanceMetadata (Live EC2 E2E Tests)" , ( ) => {
6
7
const originalEnv = { ...process . env } ;
7
8
let imdsAvailable = false ;
8
9
9
10
beforeEach ( async ( ) => {
10
- process . env = { ...originalEnv , AWS_EC2_INSTANCE_PROFILE_NAME : "foo-profile" } ;
11
+ process . env = { ...originalEnv } ;
11
12
12
13
// Check IMDS availability
13
14
try {
14
- const testProvider = fromInstanceMetadata ( { timeout : 1000 , maxRetries : 0 } ) ;
15
+ const testProvider = fromInstanceMetadata ( { timeout : 9000 } ) ;
15
16
await testProvider ( ) ;
16
17
imdsAvailable = true ;
17
18
} catch ( err ) {
@@ -28,15 +29,8 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
28
29
return context . skip ( ) ;
29
30
}
30
31
31
- const options = {
32
- path : "/latest/api/token" ,
33
- method : "PUT" ,
34
- timeout : 1000 ,
35
- headers : {
36
- "x-aws-ec2-metadata-token-ttl-seconds" : "21600" ,
37
- } ,
38
- } ;
39
- const token = await getMetadataToken ( options ) ;
32
+ const endpoint = await getInstanceMetadataEndpoint ( ) ;
33
+ const token = await getMetadataToken ( endpoint ) ;
40
34
expect ( token ) . toBeDefined ( ) ;
41
35
expect ( typeof token ) . toBe ( "string" ) ;
42
36
expect ( token . length ) . toBeGreaterThan ( 0 ) ;
@@ -47,7 +41,7 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
47
41
return context . skip ( ) ;
48
42
}
49
43
50
- const provider = fromInstanceMetadata ( { timeout : 1000 , maxRetries : 2 } ) ;
44
+ const provider = fromInstanceMetadata ( ) ;
51
45
const credentials = await provider ( ) ;
52
46
53
47
expect ( credentials ) . toHaveProperty ( "accessKeyId" ) ;
@@ -61,7 +55,7 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
61
55
return context . skip ( ) ;
62
56
}
63
57
64
- const provider = fromInstanceMetadata ( { timeout : 1000 , maxRetries : 2 } ) ;
58
+ const provider = fromInstanceMetadata ( ) ;
65
59
const credentials = await provider ( ) ;
66
60
67
61
if ( ! credentials . accountId ) {
@@ -75,15 +69,15 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
75
69
it ( "IMDS access disabled via AWS_EC2_METADATA_DISABLED" , async ( ) => {
76
70
process . env . AWS_EC2_METADATA_DISABLED = "true" ;
77
71
78
- const provider = fromInstanceMetadata ( { timeout : 1000 } ) ;
72
+ const provider = fromInstanceMetadata ( ) ;
79
73
80
74
await expect ( provider ( ) ) . rejects . toThrow ( "IMDS credential fetching is disabled" ) ;
81
75
} ) ;
82
76
83
77
it ( "Empty configured profile name should throw error" , async ( ) => {
84
78
process . env . AWS_EC2_INSTANCE_PROFILE_NAME = " " ;
85
79
86
- const provider = fromInstanceMetadata ( { timeout : 1000 } ) ;
80
+ const provider = fromInstanceMetadata ( ) ;
87
81
88
82
await expect ( provider ( ) ) . rejects . toThrow ( ) ;
89
83
} ) ;
@@ -93,7 +87,7 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
93
87
return context . skip ( ) ;
94
88
}
95
89
96
- const provider = fromInstanceMetadata ( { timeout : 1000 } ) ;
90
+ const provider = fromInstanceMetadata ( ) ;
97
91
98
92
try {
99
93
const credentials = await provider ( ) ;
@@ -108,7 +102,7 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
108
102
return context . skip ( ) ;
109
103
}
110
104
111
- const provider = fromInstanceMetadata ( { timeout : 1000 } ) ;
105
+ const provider = fromInstanceMetadata ( ) ;
112
106
const creds1 = await provider ( ) ;
113
107
const creds2 = await provider ( ) ;
114
108
@@ -117,7 +111,11 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
117
111
expect ( creds1 . accessKeyId ) . toBe ( creds2 . accessKeyId ) ;
118
112
} ) ;
119
113
120
- it ( "should timeout as expected when a request exceeds the specified duration" , async ( context ) => {
114
+ /**
115
+ * The IMDS may respond too quickly to test this,
116
+ * even with 1ms timeout.
117
+ */
118
+ it . skip ( "should timeout as expected when a request exceeds the specified duration" , async ( context ) => {
121
119
if ( ! imdsAvailable ) {
122
120
return context . skip ( ) ;
123
121
}
0 commit comments