Skip to content

Commit 8c51abb

Browse files
committed
dynamic test skipping
1 parent c551568 commit 8c51abb

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

packages/credential-provider-imds/src/fromInstanceMetadata.e2e.spec.ts

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { CredentialsProviderError } from "@smithy/property-provider";
21
import { afterEach, beforeEach, describe, expect, test as it } from "vitest";
32

43
import { fromInstanceMetadata, getMetadataToken } from "./fromInstanceMetadata";
@@ -24,8 +23,11 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
2423
process.env = { ...originalEnv };
2524
});
2625

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+
2931
const options = {
3032
path: "/latest/api/token",
3133
method: "PUT",
@@ -40,8 +42,10 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
4042
expect(token.length).toBeGreaterThan(0);
4143
});
4244

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+
}
4549

4650
const provider = fromInstanceMetadata({ timeout: 1000, maxRetries: 2 });
4751
const credentials = await provider();
@@ -52,24 +56,20 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
5256
expect(typeof credentials.secretAccessKey).toBe("string");
5357
});
5458

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+
}
5763

5864
const provider = fromInstanceMetadata({ timeout: 1000, maxRetries: 2 });
5965
const credentials = await provider();
6066

6167
if (!credentials.accountId) {
62-
it.skip("account ID test skipped - not an allowlisted instance", () => {});
68+
context.skip();
6369
}
6470

6571
expect(credentials.accountId).toBeDefined();
6672
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-
});
7373
});
7474

7575
it("IMDS access disabled via AWS_EC2_METADATA_DISABLED", async () => {
@@ -88,23 +88,25 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
8888
await expect(provider()).rejects.toThrow();
8989
});
9090

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+
}
9395

9496
const provider = fromInstanceMetadata({ timeout: 1000 });
9597

9698
try {
9799
const credentials = await provider();
98100
expect(credentials).toHaveProperty("accessKeyId");
99-
console.log("Used configured profile name from env.");
100101
} catch (error) {
101102
expect(error).toBeDefined();
102-
console.log("Profile test completed (profile may not exist).");
103103
}
104104
});
105105

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+
}
108110

109111
const provider = fromInstanceMetadata({ timeout: 1000 });
110112
const creds1 = await provider();
@@ -113,12 +115,12 @@ describe("fromInstanceMetadata (Live EC2 E2E Tests)", () => {
113115
expect(creds1.accessKeyId).toBeTruthy();
114116
expect(creds2.accessKeyId).toBeTruthy();
115117
expect(creds1.accessKeyId).toBe(creds2.accessKeyId);
116-
117-
console.log("Stable credentials returned across calls.");
118118
});
119119

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+
}
122124
const provider = fromInstanceMetadata({ timeout: 1 });
123125

124126
await expect(provider()).rejects.toThrow(/timeout|timed out|TimeoutError/i);

0 commit comments

Comments
 (0)