|
| 1 | +import { CloudWatchLogs } from "@aws-sdk/client-cloudwatch-logs"; |
| 2 | +import { GetCallerIdentityCommandOutput, STS } from "@aws-sdk/client-sts"; |
| 3 | +import { describe, expect, test as it } from "vitest"; |
| 4 | + |
| 5 | +describe( |
| 6 | + CloudWatchLogs.name, |
| 7 | + { |
| 8 | + timeout: 120_000, |
| 9 | + retry: 4, |
| 10 | + }, |
| 11 | + () => { |
| 12 | + const cwlDefault = new CloudWatchLogs({ |
| 13 | + region: "us-west-2", |
| 14 | + }); |
| 15 | + |
| 16 | + it("should be able to use an event stream to tail logs", async () => { |
| 17 | + const sts = new STS({ region: "us-west-2" }); |
| 18 | + const id: GetCallerIdentityCommandOutput = await sts.getCallerIdentity(); |
| 19 | + const accountId = id.Account; |
| 20 | + |
| 21 | + for (const cwl of [cwlDefault]) { |
| 22 | + const testLogGroupName = `/jsv3-e2e-${accountId}`; |
| 23 | + |
| 24 | + let logGroups = await cwl.listLogGroups({ |
| 25 | + logGroupNamePattern: `^${testLogGroupName}`, |
| 26 | + limit: 1, |
| 27 | + }); |
| 28 | + |
| 29 | + if (!logGroups.logGroups?.length) { |
| 30 | + await cwl.createLogGroup({ |
| 31 | + logGroupName: testLogGroupName, |
| 32 | + }); |
| 33 | + logGroups = await cwl.listLogGroups({ |
| 34 | + logGroupNamePattern: `^${testLogGroupName}`, |
| 35 | + limit: 1, |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + const groupArn = logGroups.logGroups?.[0]?.logGroupArn; |
| 40 | + |
| 41 | + if (groupArn) { |
| 42 | + const liveTail = await cwl.startLiveTail({ |
| 43 | + logGroupIdentifiers: [groupArn], |
| 44 | + }); |
| 45 | + |
| 46 | + let pagesRead = 0; |
| 47 | + |
| 48 | + for await (const page of liveTail.responseStream ?? []) { |
| 49 | + pagesRead += 1; |
| 50 | + |
| 51 | + if (pagesRead === 1) { |
| 52 | + expect(page.sessionStart?.requestId).toBeTypeOf("string"); |
| 53 | + expect(page.sessionStart?.sessionId).toBeTypeOf("string"); |
| 54 | + expect(page.sessionStart?.logGroupIdentifiers).toEqual([groupArn]); |
| 55 | + } else if (pagesRead === 2) { |
| 56 | + expect(page.sessionUpdate?.sessionMetadata).toMatchObject({ |
| 57 | + sampled: expect.any(Boolean), |
| 58 | + }); |
| 59 | + } else if (pagesRead > 2) { |
| 60 | + break; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + cwl.destroy(); |
| 65 | + } |
| 66 | + } |
| 67 | + }); |
| 68 | + } |
| 69 | +); |
0 commit comments