|
| 1 | +import { MetadataService } from "@aws-sdk/ec2-metadata-service"; |
| 2 | +import { loadSharedConfigFiles } from "@smithy/shared-ini-file-loader"; |
| 3 | +import { afterEach, beforeEach, describe, expect, test as it, vi } from "vitest"; |
| 4 | + |
| 5 | +import { getRegionFromIni, regionFromMetadataService, resolveAwsCliV2Region } from "./resolveAwsCliV2Region"; |
| 6 | + |
| 7 | +//Mock the dependencies |
| 8 | +vi.mock("@aws-sdk/ec2-metadata-service"); |
| 9 | +vi.mock("@smithy/shared-ini-file-loader", () => ({ |
| 10 | + loadSharedConfigFiles: vi.fn(), |
| 11 | +})); |
| 12 | + |
| 13 | +describe("AWS Region Resolution", () => { |
| 14 | + // Store original environment variables to restore them later |
| 15 | + const originalEnv = process.env; |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + // Reset environment variables before each test |
| 19 | + process.env = { ...originalEnv }; |
| 20 | + // Clear all mock data before each test |
| 21 | + vi.clearAllMocks(); |
| 22 | + }); |
| 23 | + |
| 24 | + afterEach(() => { |
| 25 | + // Restore environment variables after each test |
| 26 | + process.env = originalEnv; |
| 27 | + }); |
| 28 | + |
| 29 | + describe("resolveAwsCliV2Region", () => { |
| 30 | + it("should use AWS_REGION from environment variables", async () => { |
| 31 | + process.env.AWS_REGION = "us-west-2"; |
| 32 | + const region = await resolveAwsCliV2Region({}); |
| 33 | + expect(region).toBe("us-west-2"); |
| 34 | + }); |
| 35 | + |
| 36 | + it("should fall back to AWS_DEFAULT_REGION if AWS_REGION is empty", async () => { |
| 37 | + process.env.AWS_REGION = ""; |
| 38 | + process.env.AWS_DEFAULT_REGION = "eu-west-1"; |
| 39 | + const region = await resolveAwsCliV2Region({}); |
| 40 | + expect(region).toBe("eu-west-1"); |
| 41 | + }); |
| 42 | + |
| 43 | + it("should use defaultRegion when no other source is available", async () => { |
| 44 | + const consoleSpy = vi.spyOn(console, "warn"); |
| 45 | + const region = await resolveAwsCliV2Region({ defaultRegion: "ap-southeast-1" }); |
| 46 | + expect(region).toBe("ap-southeast-1"); |
| 47 | + expect(consoleSpy).toHaveBeenCalled(); |
| 48 | + }); |
| 49 | + |
| 50 | + it("should return undefined when no region is available and no default region is provided", async () => { |
| 51 | + const consoleSpy = vi.spyOn(console, "warn"); |
| 52 | + const region = await resolveAwsCliV2Region({}); |
| 53 | + expect(region).toBeUndefined(); |
| 54 | + expect(consoleSpy).toHaveBeenCalled(); |
| 55 | + }); |
| 56 | + |
| 57 | + it("should use specified profile", async () => { |
| 58 | + vi.mocked(loadSharedConfigFiles).mockResolvedValue({ |
| 59 | + configFile: { |
| 60 | + "custom-profile": { |
| 61 | + region: "us-east-1", |
| 62 | + }, |
| 63 | + }, |
| 64 | + credentialsFile: {}, |
| 65 | + }); |
| 66 | + |
| 67 | + const region = await resolveAwsCliV2Region({ profile: "custom-profile" }); |
| 68 | + expect(region).toBe("us-east-1"); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + describe("getRegionFromIni", () => { |
| 73 | + it("should get region from config file", async () => { |
| 74 | + vi.mocked(loadSharedConfigFiles).mockResolvedValue({ |
| 75 | + configFile: { |
| 76 | + default: { |
| 77 | + region: "us-east-2", |
| 78 | + }, |
| 79 | + }, |
| 80 | + credentialsFile: {}, |
| 81 | + }); |
| 82 | + |
| 83 | + const region = await getRegionFromIni("default"); |
| 84 | + expect(region).toBe("us-east-2"); |
| 85 | + }); |
| 86 | + |
| 87 | + it("should fall back to credentials file", async () => { |
| 88 | + vi.mocked(loadSharedConfigFiles).mockResolvedValue({ |
| 89 | + configFile: {}, |
| 90 | + credentialsFile: { |
| 91 | + default: { |
| 92 | + region: "eu-central-1", |
| 93 | + }, |
| 94 | + }, |
| 95 | + }); |
| 96 | + |
| 97 | + const region = await getRegionFromIni("default"); |
| 98 | + expect(region).toBe("eu-central-1"); |
| 99 | + }); |
| 100 | + |
| 101 | + it("should return undefined when no region is found", async () => { |
| 102 | + vi.mocked(loadSharedConfigFiles).mockResolvedValue({ |
| 103 | + configFile: {}, |
| 104 | + credentialsFile: {}, |
| 105 | + }); |
| 106 | + |
| 107 | + const region = await getRegionFromIni("default"); |
| 108 | + expect(region).toBeUndefined(); |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + describe("regionFromMetadataService", () => { |
| 113 | + it("should get region from EC2 metadata service", async () => { |
| 114 | + vi.mocked(MetadataService).mockImplementation( |
| 115 | + () => |
| 116 | + ({ |
| 117 | + request: vi.fn().mockResolvedValue(JSON.stringify({ region: "us-west-1" })), |
| 118 | + } as any) |
| 119 | + ); |
| 120 | + |
| 121 | + const region = await regionFromMetadataService(); |
| 122 | + expect(region).toBe("us-west-1"); |
| 123 | + }); |
| 124 | + |
| 125 | + it("should handle metadata service errors", async () => { |
| 126 | + vi.mocked(MetadataService).mockImplementation( |
| 127 | + () => |
| 128 | + ({ |
| 129 | + request: vi.fn().mockRejectedValue(new Error("IMDS not available")), |
| 130 | + } as any) |
| 131 | + ); |
| 132 | + |
| 133 | + const consoleSpy = vi.spyOn(console, "warn"); |
| 134 | + const region = await regionFromMetadataService(); |
| 135 | + |
| 136 | + expect(region).toBeUndefined(); |
| 137 | + expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("IMDS not available")); |
| 138 | + }); |
| 139 | + }); |
| 140 | +}); |
0 commit comments