-
Notifications
You must be signed in to change notification settings - Fork 438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test(cwl): Add LiveTailSession unit tests for start and stopLiveTail #6023
base: feature/cwltail
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,17 +2,35 @@ | |
* 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' | ||
const testRegion = 'test-region' | ||
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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of mocking many things just to test that parameters are sent, can you stub fake responses and let the code actually work on the response. that tests the full codepath, instead of just tiny parts of it (and requiring lots of test code) |
||
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') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why does this need a spy? test the actual state. the client can expose some (readonly) state if needed. do not use mocks and spies except as a last resort. it looks like it's the first resort in this tests, instead of the last resort. |
||
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<StartLiveTailResponseStream> { | ||
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]) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like a lot of code to test very little. The test description sounds like it's testing that a parameter is passed somewhere.