|
| 1 | +import { |
| 2 | + awsCliCommandName, |
| 3 | + awslocalExecEnv, |
| 4 | + buildsClientUrl, |
| 5 | + containerEnvValue, |
| 6 | + containerGatewayEndpoint, |
| 7 | + containerGatewayPort, |
| 8 | +} from "./awslocal-endpoint.logic"; |
| 9 | + |
| 10 | +describe("containerGatewayEndpoint", () => { |
| 11 | + test("default: no GATEWAY_LISTEN means the gateway on 4566", () => { |
| 12 | + expect(containerGatewayEndpoint(undefined)).toBe("http://localhost:4566"); |
| 13 | + expect(containerGatewayEndpoint(["PATH=/usr/bin", "DEBUG=1"])).toBe("http://localhost:4566"); |
| 14 | + }); |
| 15 | + |
| 16 | + test("the server's own container spec (:4566,:443) keeps 4566", () => { |
| 17 | + expect(containerGatewayEndpoint(["GATEWAY_LISTEN=:4566,:443"])).toBe("http://localhost:4566"); |
| 18 | + }); |
| 19 | + |
| 20 | + test("mapped host port: LOCALSTACK_HOST names the host side and is ignored", () => { |
| 21 | + const env = ["LOCALSTACK_HOST=localhost.localstack.cloud:4700", "PATH=/usr/bin"]; |
| 22 | + expect(containerGatewayEndpoint(env)).toBe("http://localhost:4566"); |
| 23 | + }); |
| 24 | + |
| 25 | + test("mapped host port with the server's GATEWAY_LISTEN still targets the container port", () => { |
| 26 | + const env = ["LOCALSTACK_HOST=localhost.localstack.cloud:4700", "GATEWAY_LISTEN=:4566,:443"]; |
| 27 | + expect(containerGatewayEndpoint(env)).toBe("http://localhost:4566"); |
| 28 | + }); |
| 29 | + |
| 30 | + test("custom GATEWAY_LISTEN: the first entry's port", () => { |
| 31 | + expect(containerGatewayEndpoint(["GATEWAY_LISTEN=0.0.0.0:4700"])).toBe("http://localhost:4700"); |
| 32 | + expect(containerGatewayEndpoint(["GATEWAY_LISTEN=:5000,:4566"])).toBe("http://localhost:5000"); |
| 33 | + expect(containerGatewayEndpoint(["GATEWAY_LISTEN= 127.0.0.1:4800 , :443"])).toBe( |
| 34 | + "http://localhost:4800" |
| 35 | + ); |
| 36 | + }); |
| 37 | + |
| 38 | + test("custom GATEWAY_LISTEN together with LOCALSTACK_HOST on another port", () => { |
| 39 | + const env = ["GATEWAY_LISTEN=0.0.0.0:4700", "LOCALSTACK_HOST=localhost.localstack.cloud:5700"]; |
| 40 | + expect(containerGatewayEndpoint(env)).toBe("http://localhost:4700"); |
| 41 | + }); |
| 42 | + |
| 43 | + test("IPv6 entries", () => { |
| 44 | + expect(containerGatewayPort(["GATEWAY_LISTEN=[::]:4700"])).toBe(4700); |
| 45 | + expect(containerGatewayPort(["GATEWAY_LISTEN=[::1]"])).toBe(4566); |
| 46 | + }); |
| 47 | + |
| 48 | + test("a host without a port keeps the default port, as LocalStack reads it", () => { |
| 49 | + expect(containerGatewayPort(["GATEWAY_LISTEN=0.0.0.0"])).toBe(4566); |
| 50 | + }); |
| 51 | + |
| 52 | + test("LOCALSTACK_GATEWAY_LISTEN wins, since the entrypoint exports it as GATEWAY_LISTEN", () => { |
| 53 | + const env = ["GATEWAY_LISTEN=:4566", "LOCALSTACK_GATEWAY_LISTEN=:4900"]; |
| 54 | + expect(containerGatewayPort(env)).toBe(4900); |
| 55 | + }); |
| 56 | + |
| 57 | + test("empty or unreadable values fall back to 4566", () => { |
| 58 | + expect(containerGatewayPort(["GATEWAY_LISTEN="])).toBe(4566); |
| 59 | + expect(containerGatewayPort(["GATEWAY_LISTEN=:abc"])).toBe(4566); |
| 60 | + expect(containerGatewayPort(["GATEWAY_LISTEN=:0"])).toBe(4566); |
| 61 | + expect(containerGatewayPort(["GATEWAY_LISTEN=:70000"])).toBe(4566); |
| 62 | + expect(containerGatewayPort(["GATEWAY_LISTEN=[::1"])).toBe(4566); |
| 63 | + }); |
| 64 | + |
| 65 | + test("a variable whose name only starts with GATEWAY_LISTEN is not read", () => { |
| 66 | + expect(containerGatewayPort(["GATEWAY_LISTEN_EXTRA=:4700"])).toBe(4566); |
| 67 | + }); |
| 68 | +}); |
| 69 | + |
| 70 | +describe("containerEnvValue", () => { |
| 71 | + test("keeps '=' inside the value", () => { |
| 72 | + expect(containerEnvValue(["A=b=c"], "A")).toBe("b=c"); |
| 73 | + expect(containerEnvValue(["A=1"], "B")).toBeUndefined(); |
| 74 | + }); |
| 75 | +}); |
| 76 | + |
| 77 | +describe("awslocalExecEnv", () => { |
| 78 | + const mapped = ["LOCALSTACK_HOST=localhost.localstack.cloud:4700", "GATEWAY_LISTEN=:4600"]; |
| 79 | + |
| 80 | + test("sets only AWS_ENDPOINT_URL, to the container-side gateway", () => { |
| 81 | + expect(awslocalExecEnv(mapped, ["sqs", "list-queues"])).toEqual([ |
| 82 | + "AWS_ENDPOINT_URL=http://localhost:4600", |
| 83 | + ]); |
| 84 | + }); |
| 85 | + |
| 86 | + test.each([ |
| 87 | + [["s3", "presign", "s3://bucket/key"]], |
| 88 | + [["s3", "presign", "s3://bucket/key", "--expires-in", "60"]], |
| 89 | + [["--region", "eu-west-1", "s3", "presign", "s3://bucket/key"]], |
| 90 | + [["rds", "generate-db-auth-token", "--hostname", "db", "--port", "5432", "--username", "u"]], |
| 91 | + [["eks", "get-token", "--cluster-name", "c1"]], |
| 92 | + [["dsql", "generate-db-connect-auth-token", "--hostname", "h"]], |
| 93 | + [["dsql", "generate-db-connect-admin-auth-token", "--hostname", "h"]], |
| 94 | + ])("%j builds a URL for the caller, so it keeps awslocal's LOCALSTACK_HOST endpoint", (args) => { |
| 95 | + expect(awslocalExecEnv(mapped, args)).toEqual([]); |
| 96 | + }); |
| 97 | + |
| 98 | + test.each([ |
| 99 | + [["s3", "ls"]], |
| 100 | + [["s3", "cp", "presign", "s3://bucket/presign"]], |
| 101 | + [["s3api", "get-object", "--bucket", "presign", "--key", "k", "out"]], |
| 102 | + [["rds", "describe-db-instances"]], |
| 103 | + [["eks", "list-clusters"]], |
| 104 | + [["--output", "s3", "sqs", "list-queues"]], |
| 105 | + ])("%j calls the gateway, so it gets the container-side endpoint", (args) => { |
| 106 | + expect(awslocalExecEnv(mapped, args)).toEqual(["AWS_ENDPOINT_URL=http://localhost:4600"]); |
| 107 | + }); |
| 108 | +}); |
| 109 | + |
| 110 | +describe("awsCliCommandName", () => { |
| 111 | + test("skips global options and their values", () => { |
| 112 | + expect(awsCliCommandName(["--debug", "--profile", "p", "S3", "Presign", "x"])).toBe( |
| 113 | + "s3 presign" |
| 114 | + ); |
| 115 | + expect(awsCliCommandName(["--region=us-east-1", "sqs", "list-queues"])).toBe("sqs list-queues"); |
| 116 | + expect(awsCliCommandName(["help"])).toBe("help"); |
| 117 | + expect(buildsClientUrl([])).toBe(false); |
| 118 | + }); |
| 119 | +}); |
0 commit comments