22 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33 * SPDX-License-Identifier: Apache-2.0
44 */
5+ import * as sinon from 'sinon'
56import assert from 'assert'
67import { LiveTailSession } from '../../../../awsService/cloudWatchLogs/registry/liveTailSession'
7- import { StartLiveTailCommand } from '@aws-sdk/client-cloudwatch-logs'
8+ import {
9+ CloudWatchLogsClient ,
10+ LiveTailSessionLogEvent ,
11+ StartLiveTailCommand ,
12+ StartLiveTailResponse ,
13+ StartLiveTailResponseStream ,
14+ } from '@aws-sdk/client-cloudwatch-logs'
815import { LogStreamFilterResponse } from '../../../../awsService/cloudWatchLogs/wizard/liveTailLogStreamSubmenu'
16+ import { asyncGenerator } from '../../../../shared/utilities/collectionUtils'
917
1018describe ( 'LiveTailSession' , async function ( ) {
1119 const testLogGroupArn = 'arn:aws:test-log-group'
1220 const testRegion = 'test-region'
1321 const testFilter = 'test-filter'
1422 const testAwsCredentials = { } as any as AWS . Credentials
1523
24+ let sandbox : sinon . SinonSandbox
25+
26+ beforeEach ( function ( ) {
27+ sandbox = sinon . createSandbox ( )
28+ } )
29+
30+ afterEach ( function ( ) {
31+ sandbox . restore ( )
32+ } )
33+
1634 it ( 'builds StartLiveTailCommand: no stream Filter, no event filter.' , function ( ) {
1735 const session = buildLiveTailSession ( { type : 'all' } , undefined )
1836 assert . deepStrictEqual (
@@ -65,6 +83,36 @@ describe('LiveTailSession', async function () {
6583 )
6684 } )
6785
86+ it ( 'startLiveTail sends command to CWL client with AbortSignal' , async function ( ) {
87+ const sendCommandMock = sandbox . stub ( CloudWatchLogsClient . prototype , 'send' ) . callsFake ( function ( ) {
88+ return buildLiveTailCommandOutput ( testLogGroupArn )
89+ } )
90+ const session = buildLiveTailSession ( { type : 'all' } , undefined )
91+ const stream = await session . startLiveTailSession ( )
92+
93+ assert . strictEqual ( sendCommandMock . calledOnce , true )
94+ assert . strictEqual (
95+ sendCommandMock . calledWith (
96+ sinon . match . any ,
97+ sinon . match ( {
98+ abortSignal : sinon . match . defined ,
99+ } )
100+ ) ,
101+ true
102+ )
103+ assert . strictEqual ( session . isAborted , false )
104+ assert . deepEqual ( stream , buildLiveTailCommandOutput ( testLogGroupArn ) . responseStream )
105+ } )
106+
107+ it ( 'stopLiveTail fires AbortSignal and destroys CWL client' , function ( ) {
108+ const destroyClientSpy = sandbox . spy ( CloudWatchLogsClient . prototype , 'destroy' )
109+ const session = buildLiveTailSession ( { type : 'all' } , undefined )
110+ session . stopLiveTailSession ( )
111+
112+ assert . strictEqual ( session . isAborted , true )
113+ assert . strictEqual ( destroyClientSpy . calledOnce , true )
114+ } )
115+
68116 function buildLiveTailSession (
69117 logStreamFilter : LogStreamFilterResponse ,
70118 logEventFilterPattern : string | undefined
@@ -78,3 +126,40 @@ describe('LiveTailSession', async function () {
78126 } )
79127 }
80128} )
129+
130+ export function buildLiveTailCommandOutput ( logGroupArn : string ) : StartLiveTailResponse {
131+ return {
132+ responseStream : getTestResponseStream ( logGroupArn , [
133+ {
134+ message : 'testMessage' ,
135+ timestamp : 876830400000 ,
136+ } ,
137+ ] ) ,
138+ }
139+ }
140+
141+ //Creates a test response stream. Each log event provided will be its own "frame" of the input stream.
142+ export function getTestResponseStream (
143+ logGroupIdentifier : string ,
144+ logEvents : LiveTailSessionLogEvent [ ]
145+ ) : AsyncIterable < StartLiveTailResponseStream > {
146+ const sessionStartFrame : StartLiveTailResponseStream = {
147+ sessionStart : {
148+ logGroupIdentifiers : [ logGroupIdentifier ] ,
149+ } ,
150+ sessionUpdate : undefined ,
151+ }
152+
153+ const updateFrames : StartLiveTailResponseStream [ ] = logEvents . map ( ( event ) => {
154+ return {
155+ sessionUpdate : {
156+ sessionMetadata : {
157+ sampled : false ,
158+ } ,
159+ sessionResults : [ event ] ,
160+ } ,
161+ }
162+ } )
163+
164+ return asyncGenerator ( [ sessionStartFrame , ...updateFrames ] )
165+ }
0 commit comments