11import { afterEach , beforeEach , describe , expect , test as it } from "vitest" ;
22
33import { fromInstanceMetadata , getMetadataToken } from "./fromInstanceMetadata" ;
4+ import { getInstanceMetadataEndpoint } from "./utils/getInstanceMetadataEndpoint" ;
45
56describe ( "fromInstanceMetadata (Live EC2 E2E Tests)" , ( ) => {
67 const originalEnv = { ...process . env } ;
78 let imdsAvailable = false ;
89
910 beforeEach ( async ( ) => {
10- process . env = { ...originalEnv , AWS_EC2_INSTANCE_PROFILE_NAME : "foo-profile" } ;
11+ process . env = { ...originalEnv } ;
1112
1213 // Check IMDS availability
1314 try {
14- const testProvider = fromInstanceMetadata ( { timeout : 1000 , maxRetries : 0 } ) ;
15+ const testProvider = fromInstanceMetadata ( { timeout : 9000 } ) ;
1516 await testProvider ( ) ;
1617 imdsAvailable = true ;
1718 } catch ( err ) {
@@ -28,15 +29,8 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
2829 return context . skip ( ) ;
2930 }
3031
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 ) ;
4034 expect ( token ) . toBeDefined ( ) ;
4135 expect ( typeof token ) . toBe ( "string" ) ;
4236 expect ( token . length ) . toBeGreaterThan ( 0 ) ;
@@ -47,7 +41,7 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
4741 return context . skip ( ) ;
4842 }
4943
50- const provider = fromInstanceMetadata ( { timeout : 1000 , maxRetries : 2 } ) ;
44+ const provider = fromInstanceMetadata ( ) ;
5145 const credentials = await provider ( ) ;
5246
5347 expect ( credentials ) . toHaveProperty ( "accessKeyId" ) ;
@@ -61,7 +55,7 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
6155 return context . skip ( ) ;
6256 }
6357
64- const provider = fromInstanceMetadata ( { timeout : 1000 , maxRetries : 2 } ) ;
58+ const provider = fromInstanceMetadata ( ) ;
6559 const credentials = await provider ( ) ;
6660
6761 if ( ! credentials . accountId ) {
@@ -75,15 +69,15 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
7569 it ( "IMDS access disabled via AWS_EC2_METADATA_DISABLED" , async ( ) => {
7670 process . env . AWS_EC2_METADATA_DISABLED = "true" ;
7771
78- const provider = fromInstanceMetadata ( { timeout : 1000 } ) ;
72+ const provider = fromInstanceMetadata ( ) ;
7973
8074 await expect ( provider ( ) ) . rejects . toThrow ( "IMDS credential fetching is disabled" ) ;
8175 } ) ;
8276
8377 it ( "Empty configured profile name should throw error" , async ( ) => {
8478 process . env . AWS_EC2_INSTANCE_PROFILE_NAME = " " ;
8579
86- const provider = fromInstanceMetadata ( { timeout : 1000 } ) ;
80+ const provider = fromInstanceMetadata ( ) ;
8781
8882 await expect ( provider ( ) ) . rejects . toThrow ( ) ;
8983 } ) ;
@@ -93,7 +87,7 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
9387 return context . skip ( ) ;
9488 }
9589
96- const provider = fromInstanceMetadata ( { timeout : 1000 } ) ;
90+ const provider = fromInstanceMetadata ( ) ;
9791
9892 try {
9993 const credentials = await provider ( ) ;
@@ -108,7 +102,7 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
108102 return context . skip ( ) ;
109103 }
110104
111- const provider = fromInstanceMetadata ( { timeout : 1000 } ) ;
105+ const provider = fromInstanceMetadata ( ) ;
112106 const creds1 = await provider ( ) ;
113107 const creds2 = await provider ( ) ;
114108
@@ -117,7 +111,11 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
117111 expect ( creds1 . accessKeyId ) . toBe ( creds2 . accessKeyId ) ;
118112 } ) ;
119113
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 ) => {
121119 if ( ! imdsAvailable ) {
122120 return context . skip ( ) ;
123121 }
0 commit comments