Skip to content

Commit b00b062

Browse files
author
Keegan Irby
committed
Add LiveTailSession unit tests for start and stopLiveTail
1 parent 500f94b commit b00b062

File tree

2 files changed

+88
-27
lines changed

2 files changed

+88
-27
lines changed

packages/core/src/test/awsService/cloudWatchLogs/commands/tailLogGroup.test.ts

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ import * as vscode from 'vscode'
99

1010
import assert from 'assert'
1111
import { clearDocument, closeSession, tailLogGroup } from '../../../../awsService/cloudWatchLogs/commands/tailLogGroup'
12-
import { LiveTailSessionLogEvent, StartLiveTailResponseStream } from '@aws-sdk/client-cloudwatch-logs'
1312
import { LiveTailSessionRegistry } from '../../../../awsService/cloudWatchLogs/registry/liveTailSessionRegistry'
1413
import { LiveTailSession } from '../../../../awsService/cloudWatchLogs/registry/liveTailSession'
15-
import { asyncGenerator } from '../../../../shared/utilities/collectionUtils'
1614
import {
1715
TailLogGroupWizard,
1816
TailLogGroupWizardResponse,
@@ -21,6 +19,7 @@ import { getTestWindow } from '../../../shared/vscode/window'
2119
import { CloudWatchLogsSettings, uriToKey } from '../../../../awsService/cloudWatchLogs/cloudWatchLogsUtils'
2220
import { installFakeClock } from '../../../testUtil'
2321
import { DefaultAwsContext, ToolkitError } from '../../../../shared'
22+
import { getTestResponseStream } from '../registry/liveTailSession.test'
2423

2524
describe('TailLogGroup', function () {
2625
const testLogGroup = 'test-log-group'
@@ -68,7 +67,7 @@ describe('TailLogGroup', function () {
6867
startLiveTailSessionSpy = sandbox
6968
.stub(LiveTailSession.prototype, 'startLiveTailSession')
7069
.callsFake(async function () {
71-
return getTestResponseStream([
70+
return getTestResponseStream(testLogGroup, [
7271
{
7372
message: testMessage,
7473
timestamp: 876830400000,
@@ -195,27 +194,4 @@ describe('TailLogGroup', function () {
195194
},
196195
}
197196
}
198-
199-
//Creates a test response stream. Each log event provided will be its own "frame" of the input stream.
200-
function getTestResponseStream(logEvents: LiveTailSessionLogEvent[]): AsyncIterable<StartLiveTailResponseStream> {
201-
const sessionStartFrame: StartLiveTailResponseStream = {
202-
sessionStart: {
203-
logGroupIdentifiers: [testLogGroup],
204-
},
205-
sessionUpdate: undefined,
206-
}
207-
208-
const updateFrames: StartLiveTailResponseStream[] = logEvents.map((event) => {
209-
return {
210-
sessionUpdate: {
211-
sessionMetadata: {
212-
sampled: false,
213-
},
214-
sessionResults: [event],
215-
},
216-
}
217-
})
218-
219-
return asyncGenerator([sessionStartFrame, ...updateFrames])
220-
}
221197
})

packages/core/src/test/awsService/cloudWatchLogs/registry/liveTailSession.test.ts

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,35 @@
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'
56
import assert from 'assert'
67
import { 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'
815
import { LogStreamFilterResponse } from '../../../../awsService/cloudWatchLogs/wizard/liveTailLogStreamSubmenu'
16+
import { asyncGenerator } from '../../../../shared/utilities/collectionUtils'
917

1018
describe('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

Comments
 (0)