|
| 1 | +import { |
| 2 | + getLambdaClient, |
| 3 | + invokeLogUploader, |
| 4 | + MissingAwsCredentialsError, |
| 5 | +} from "lib/lambda"; |
| 6 | + |
| 7 | +describe("getLambdaClient", () => { |
| 8 | + const saved = { |
| 9 | + id: process.env.OUR_AWS_ACCESS_KEY_ID, |
| 10 | + secret: process.env.OUR_AWS_SECRET_ACCESS_KEY, |
| 11 | + }; |
| 12 | + |
| 13 | + afterEach(() => { |
| 14 | + process.env.OUR_AWS_ACCESS_KEY_ID = saved.id; |
| 15 | + process.env.OUR_AWS_SECRET_ACCESS_KEY = saved.secret; |
| 16 | + }); |
| 17 | + |
| 18 | + function setCredentials(id?: string, secret?: string) { |
| 19 | + if (id === undefined) { |
| 20 | + delete process.env.OUR_AWS_ACCESS_KEY_ID; |
| 21 | + } else { |
| 22 | + process.env.OUR_AWS_ACCESS_KEY_ID = id; |
| 23 | + } |
| 24 | + if (secret === undefined) { |
| 25 | + delete process.env.OUR_AWS_SECRET_ACCESS_KEY; |
| 26 | + } else { |
| 27 | + process.env.OUR_AWS_SECRET_ACCESS_KEY = secret; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + test.each([ |
| 32 | + ["neither is set", undefined, undefined], |
| 33 | + ["only the key id is set", "AKIA", undefined], |
| 34 | + ["only the secret is set", undefined, "shh"], |
| 35 | + ["the key id is empty", "", "shh"], |
| 36 | + ])("throws a named error when %s", (_label, id, secret) => { |
| 37 | + setCredentials(id, secret); |
| 38 | + expect(() => getLambdaClient()).toThrow(MissingAwsCredentialsError); |
| 39 | + }); |
| 40 | + |
| 41 | + test("the error names the variables to set", () => { |
| 42 | + setCredentials(undefined, undefined); |
| 43 | + // The SDK's own message is "Resolved credential object is not valid", which |
| 44 | + // gives whoever is paged nothing to act on. |
| 45 | + expect(() => getLambdaClient()).toThrow(/OUR_AWS_ACCESS_KEY_ID/); |
| 46 | + }); |
| 47 | + |
| 48 | + test("builds a client when both are set", () => { |
| 49 | + setCredentials("AKIA", "shh"); |
| 50 | + expect(getLambdaClient()).toBeDefined(); |
| 51 | + }); |
| 52 | + |
| 53 | + test("bounds retries and socket waits", async () => { |
| 54 | + setCredentials("AKIA", "shh"); |
| 55 | + const config = getLambdaClient().config; |
| 56 | + expect(await config.maxAttempts()).toBe(2); |
| 57 | + }); |
| 58 | + |
| 59 | + test("invokeLogUploader rejects rather than hanging with no credentials", async () => { |
| 60 | + setCredentials(undefined, undefined); |
| 61 | + await expect( |
| 62 | + invokeLogUploader({ repo: "pytorch/pytorch", job_id: 1 }) |
| 63 | + ).rejects.toThrow(MissingAwsCredentialsError); |
| 64 | + }); |
| 65 | +}); |
0 commit comments