From b00b0629b3604f11bbf4742144fff93aec874d69 Mon Sep 17 00:00:00 2001 From: Keegan Irby Date: Thu, 14 Nov 2024 13:55:48 -0800 Subject: [PATCH] Add LiveTailSession unit tests for start and stopLiveTail --- .../commands/tailLogGroup.test.ts | 28 +----- .../registry/liveTailSession.test.ts | 87 ++++++++++++++++++- 2 files changed, 88 insertions(+), 27 deletions(-) diff --git a/packages/core/src/test/awsService/cloudWatchLogs/commands/tailLogGroup.test.ts b/packages/core/src/test/awsService/cloudWatchLogs/commands/tailLogGroup.test.ts index 56819d86a8a..6bc0b81eaf8 100644 --- a/packages/core/src/test/awsService/cloudWatchLogs/commands/tailLogGroup.test.ts +++ b/packages/core/src/test/awsService/cloudWatchLogs/commands/tailLogGroup.test.ts @@ -9,10 +9,8 @@ import * as vscode from 'vscode' import assert from 'assert' import { clearDocument, closeSession, tailLogGroup } from '../../../../awsService/cloudWatchLogs/commands/tailLogGroup' -import { LiveTailSessionLogEvent, StartLiveTailResponseStream } from '@aws-sdk/client-cloudwatch-logs' import { LiveTailSessionRegistry } from '../../../../awsService/cloudWatchLogs/registry/liveTailSessionRegistry' import { LiveTailSession } from '../../../../awsService/cloudWatchLogs/registry/liveTailSession' -import { asyncGenerator } from '../../../../shared/utilities/collectionUtils' import { TailLogGroupWizard, TailLogGroupWizardResponse, @@ -21,6 +19,7 @@ import { getTestWindow } from '../../../shared/vscode/window' import { CloudWatchLogsSettings, uriToKey } from '../../../../awsService/cloudWatchLogs/cloudWatchLogsUtils' import { installFakeClock } from '../../../testUtil' import { DefaultAwsContext, ToolkitError } from '../../../../shared' +import { getTestResponseStream } from '../registry/liveTailSession.test' describe('TailLogGroup', function () { const testLogGroup = 'test-log-group' @@ -68,7 +67,7 @@ describe('TailLogGroup', function () { startLiveTailSessionSpy = sandbox .stub(LiveTailSession.prototype, 'startLiveTailSession') .callsFake(async function () { - return getTestResponseStream([ + return getTestResponseStream(testLogGroup, [ { message: testMessage, timestamp: 876830400000, @@ -195,27 +194,4 @@ describe('TailLogGroup', function () { }, } } - - //Creates a test response stream. Each log event provided will be its own "frame" of the input stream. - function getTestResponseStream(logEvents: LiveTailSessionLogEvent[]): AsyncIterable { - const sessionStartFrame: StartLiveTailResponseStream = { - sessionStart: { - logGroupIdentifiers: [testLogGroup], - }, - sessionUpdate: undefined, - } - - const updateFrames: StartLiveTailResponseStream[] = logEvents.map((event) => { - return { - sessionUpdate: { - sessionMetadata: { - sampled: false, - }, - sessionResults: [event], - }, - } - }) - - return asyncGenerator([sessionStartFrame, ...updateFrames]) - } }) diff --git a/packages/core/src/test/awsService/cloudWatchLogs/registry/liveTailSession.test.ts b/packages/core/src/test/awsService/cloudWatchLogs/registry/liveTailSession.test.ts index 07bab07983a..b8c0b8cd4be 100644 --- a/packages/core/src/test/awsService/cloudWatchLogs/registry/liveTailSession.test.ts +++ b/packages/core/src/test/awsService/cloudWatchLogs/registry/liveTailSession.test.ts @@ -2,10 +2,18 @@ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ +import * as sinon from 'sinon' import assert from 'assert' import { LiveTailSession } from '../../../../awsService/cloudWatchLogs/registry/liveTailSession' -import { StartLiveTailCommand } from '@aws-sdk/client-cloudwatch-logs' +import { + CloudWatchLogsClient, + LiveTailSessionLogEvent, + StartLiveTailCommand, + StartLiveTailResponse, + StartLiveTailResponseStream, +} from '@aws-sdk/client-cloudwatch-logs' import { LogStreamFilterResponse } from '../../../../awsService/cloudWatchLogs/wizard/liveTailLogStreamSubmenu' +import { asyncGenerator } from '../../../../shared/utilities/collectionUtils' describe('LiveTailSession', async function () { const testLogGroupArn = 'arn:aws:test-log-group' @@ -13,6 +21,16 @@ describe('LiveTailSession', async function () { const testFilter = 'test-filter' const testAwsCredentials = {} as any as AWS.Credentials + let sandbox: sinon.SinonSandbox + + beforeEach(function () { + sandbox = sinon.createSandbox() + }) + + afterEach(function () { + sandbox.restore() + }) + it('builds StartLiveTailCommand: no stream Filter, no event filter.', function () { const session = buildLiveTailSession({ type: 'all' }, undefined) assert.deepStrictEqual( @@ -65,6 +83,36 @@ describe('LiveTailSession', async function () { ) }) + it('startLiveTail sends command to CWL client with AbortSignal', async function () { + const sendCommandMock = sandbox.stub(CloudWatchLogsClient.prototype, 'send').callsFake(function () { + return buildLiveTailCommandOutput(testLogGroupArn) + }) + const session = buildLiveTailSession({ type: 'all' }, undefined) + const stream = await session.startLiveTailSession() + + assert.strictEqual(sendCommandMock.calledOnce, true) + assert.strictEqual( + sendCommandMock.calledWith( + sinon.match.any, + sinon.match({ + abortSignal: sinon.match.defined, + }) + ), + true + ) + assert.strictEqual(session.isAborted, false) + assert.deepEqual(stream, buildLiveTailCommandOutput(testLogGroupArn).responseStream) + }) + + it('stopLiveTail fires AbortSignal and destroys CWL client', function () { + const destroyClientSpy = sandbox.spy(CloudWatchLogsClient.prototype, 'destroy') + const session = buildLiveTailSession({ type: 'all' }, undefined) + session.stopLiveTailSession() + + assert.strictEqual(session.isAborted, true) + assert.strictEqual(destroyClientSpy.calledOnce, true) + }) + function buildLiveTailSession( logStreamFilter: LogStreamFilterResponse, logEventFilterPattern: string | undefined @@ -78,3 +126,40 @@ describe('LiveTailSession', async function () { }) } }) + +export function buildLiveTailCommandOutput(logGroupArn: string): StartLiveTailResponse { + return { + responseStream: getTestResponseStream(logGroupArn, [ + { + message: 'testMessage', + timestamp: 876830400000, + }, + ]), + } +} + +//Creates a test response stream. Each log event provided will be its own "frame" of the input stream. +export function getTestResponseStream( + logGroupIdentifier: string, + logEvents: LiveTailSessionLogEvent[] +): AsyncIterable { + const sessionStartFrame: StartLiveTailResponseStream = { + sessionStart: { + logGroupIdentifiers: [logGroupIdentifier], + }, + sessionUpdate: undefined, + } + + const updateFrames: StartLiveTailResponseStream[] = logEvents.map((event) => { + return { + sessionUpdate: { + sessionMetadata: { + sampled: false, + }, + sessionResults: [event], + }, + } + }) + + return asyncGenerator([sessionStartFrame, ...updateFrames]) +}