|
| 1 | +import { CloudWatchLogs } from "@aws-sdk/client-cloudwatch-logs"; |
| 2 | +import { AwsJson1_1Protocol, AwsSmithyRpcV2CborProtocol } from "@aws-sdk/core/protocols"; |
| 3 | +import type { IncomingMessage } from "node:http"; |
| 4 | +import { describe, expect, test as it } from "vitest"; |
| 5 | + |
| 6 | +describe( |
| 7 | + CloudWatchLogs.name, |
| 8 | + () => { |
| 9 | + const cwlDefault = new CloudWatchLogs({ |
| 10 | + region: "us-west-2", |
| 11 | + protocol: new AwsJson1_1Protocol({ |
| 12 | + defaultNamespace: "com.amazonaws.cloudwatchlogs", |
| 13 | + serviceTarget: "Logs_20140328", |
| 14 | + awsQueryCompatible: false, |
| 15 | + }), |
| 16 | + }); |
| 17 | + const cwlCustom = new CloudWatchLogs({ |
| 18 | + region: "us-west-2", |
| 19 | + protocol: new AwsSmithyRpcV2CborProtocol({ defaultNamespace: "com.amazonaws.cloudwatchlogs" }), |
| 20 | + }); |
| 21 | + |
| 22 | + it("should be able to make requests with runtime protocol selection", async () => { |
| 23 | + for (const cwl of [cwlDefault, cwlCustom]) { |
| 24 | + const logGroups = await cwl.listLogGroups(); |
| 25 | + |
| 26 | + expect(logGroups).toMatchObject({ |
| 27 | + $metadata: { |
| 28 | + httpStatusCode: 200, |
| 29 | + }, |
| 30 | + logGroups: expect.any(Array), |
| 31 | + }); |
| 32 | + expect(logGroups.nextToken ?? "").toBeTypeOf("string"); |
| 33 | + } |
| 34 | + }); |
| 35 | + |
| 36 | + it("should be able to use an event stream to tail logs", async () => { |
| 37 | + for (const cwl of [cwlDefault, cwlCustom]) { |
| 38 | + const logGroups = await cwl.listLogGroups({ |
| 39 | + limit: 1, |
| 40 | + }); |
| 41 | + |
| 42 | + const groupArn = logGroups.logGroups?.[0].logGroupArn; |
| 43 | + |
| 44 | + if (groupArn) { |
| 45 | + const liveTail = await cwl.startLiveTail({ |
| 46 | + logGroupIdentifiers: [groupArn], |
| 47 | + }); |
| 48 | + |
| 49 | + let pagesRead = 0; |
| 50 | + |
| 51 | + for await (const page of liveTail.responseStream ?? []) { |
| 52 | + pagesRead += 1; |
| 53 | + |
| 54 | + if (pagesRead === 1) { |
| 55 | + expect(page.sessionStart?.requestId).toBeTypeOf("string"); |
| 56 | + expect(page.sessionStart?.sessionId).toBeTypeOf("string"); |
| 57 | + expect(page.sessionStart?.logGroupIdentifiers).toEqual([groupArn]); |
| 58 | + } else if (pagesRead === 2) { |
| 59 | + expect(page.sessionUpdate?.sessionMetadata).toMatchObject({ |
| 60 | + sampled: expect.any(Boolean), |
| 61 | + }); |
| 62 | + } else if (pagesRead > 2) { |
| 63 | + break; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + cwl.destroy(); |
| 68 | + } |
| 69 | + } |
| 70 | + }); |
| 71 | + }, |
| 72 | + 120_000 |
| 73 | +); |
0 commit comments