|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import * as sinon from 'sinon' |
| 7 | +import * as vscode from 'vscode' |
| 8 | + |
| 9 | +import assert from 'assert' |
| 10 | +import { clearDocument, closeSession, tailLogGroup } from '../../../../awsService/cloudWatchLogs/commands/tailLogGroup' |
| 11 | +import { StartLiveTailResponseStream } from '@aws-sdk/client-cloudwatch-logs' |
| 12 | +import { LiveTailSessionRegistry } from '../../../../awsService/cloudWatchLogs/registry/liveTailSessionRegistry' |
| 13 | +import { LiveTailSession } from '../../../../awsService/cloudWatchLogs/registry/liveTailSession' |
| 14 | +import { asyncGenerator } from '../../../../shared/utilities/collectionUtils' |
| 15 | +import { |
| 16 | + TailLogGroupWizard, |
| 17 | + TailLogGroupWizardResponse, |
| 18 | +} from '../../../../awsService/cloudWatchLogs/wizard/tailLogGroupWizard' |
| 19 | +import { getTestWindow } from '../../../shared/vscode/window' |
| 20 | + |
| 21 | +describe('TailLogGroup', function () { |
| 22 | + const testLogGroup = 'test-log-group' |
| 23 | + const testRegion = 'test-region' |
| 24 | + const testMessage = 'test-message' |
| 25 | + |
| 26 | + let sandbox: sinon.SinonSandbox |
| 27 | + let registry: LiveTailSessionRegistry |
| 28 | + let startLiveTailSessionSpy: sinon.SinonSpy |
| 29 | + let stopLiveTailSessionSpy: sinon.SinonSpy |
| 30 | + let wizardSpy: sinon.SinonSpy |
| 31 | + |
| 32 | + beforeEach(function () { |
| 33 | + sandbox = sinon.createSandbox() |
| 34 | + registry = new LiveTailSessionRegistry() |
| 35 | + }) |
| 36 | + |
| 37 | + afterEach(function () { |
| 38 | + sandbox.restore() |
| 39 | + }) |
| 40 | + |
| 41 | + it('starts LiveTailSession and writes to document. Closes tab and asserts session gets closed.', async function () { |
| 42 | + wizardSpy = sandbox.stub(TailLogGroupWizard.prototype, 'run').callsFake(async function () { |
| 43 | + return getTestWizardResponse() |
| 44 | + }) |
| 45 | + startLiveTailSessionSpy = sandbox |
| 46 | + .stub(LiveTailSession.prototype, 'startLiveTailSession') |
| 47 | + .callsFake(async function () { |
| 48 | + return getTestResponseStream() |
| 49 | + }) |
| 50 | + stopLiveTailSessionSpy = sandbox |
| 51 | + .stub(LiveTailSession.prototype, 'stopLiveTailSession') |
| 52 | + .callsFake(async function () { |
| 53 | + return |
| 54 | + }) |
| 55 | + await tailLogGroup(registry, { |
| 56 | + groupName: testLogGroup, |
| 57 | + regionName: testRegion, |
| 58 | + }) |
| 59 | + assert.strictEqual(wizardSpy.calledOnce, true) |
| 60 | + assert.strictEqual(startLiveTailSessionSpy.calledOnce, true) |
| 61 | + assert.strictEqual(registry.size, 1) |
| 62 | + |
| 63 | + //registry is asserted to have only one entry, so this is assumed to be the session that was |
| 64 | + //started in this test. |
| 65 | + let sessionUri: vscode.Uri | undefined |
| 66 | + registry.forEach((session) => (sessionUri = session.uri)) |
| 67 | + if (sessionUri === undefined) { |
| 68 | + throw Error |
| 69 | + } |
| 70 | + const document = getTestWindow().activeTextEditor?.document |
| 71 | + assert.strictEqual(sessionUri.toString(), document?.uri.toString()) |
| 72 | + assert.strictEqual(document?.getText(), `12:00:00\t${testMessage}\n`) |
| 73 | + |
| 74 | + //Test that closing all tabs the session's document is open in will cause the session to close |
| 75 | + const window = getTestWindow() |
| 76 | + window.tabGroups.all.forEach(async (tabGroup) => |
| 77 | + tabGroup.tabs.forEach(async (tab) => { |
| 78 | + if (tab.input instanceof vscode.TabInputText) { |
| 79 | + if (sessionUri!.toString() === tab.input.uri.toString()) { |
| 80 | + await window.tabGroups.close(tab) |
| 81 | + } |
| 82 | + } |
| 83 | + }) |
| 84 | + ) |
| 85 | + assert.strictEqual(registry.size, 0) |
| 86 | + assert.strictEqual(stopLiveTailSessionSpy.calledOnce, true) |
| 87 | + }) |
| 88 | + |
| 89 | + it('closeSession removes session from registry and calls underlying stopLiveTailSession function.', function () { |
| 90 | + stopLiveTailSessionSpy = sandbox |
| 91 | + .stub(LiveTailSession.prototype, 'stopLiveTailSession') |
| 92 | + .callsFake(async function () { |
| 93 | + return |
| 94 | + }) |
| 95 | + const session = new LiveTailSession({ |
| 96 | + logGroupName: testLogGroup, |
| 97 | + region: testRegion, |
| 98 | + }) |
| 99 | + registry.set(session.uri, session) |
| 100 | + |
| 101 | + closeSession(session.uri, registry) |
| 102 | + assert.strictEqual(0, registry.size) |
| 103 | + assert.strictEqual(true, stopLiveTailSessionSpy.calledOnce) |
| 104 | + }) |
| 105 | + |
| 106 | + it('clearDocument clears all text from document', async function () { |
| 107 | + const session = new LiveTailSession({ |
| 108 | + logGroupName: testLogGroup, |
| 109 | + region: testRegion, |
| 110 | + }) |
| 111 | + const testData = 'blah blah blah' |
| 112 | + const document = await vscode.workspace.openTextDocument(session.uri) |
| 113 | + const edit = new vscode.WorkspaceEdit() |
| 114 | + edit.insert(document.uri, new vscode.Position(0, 0), testData) |
| 115 | + await vscode.workspace.applyEdit(edit) |
| 116 | + assert.strictEqual(document.getText(), testData) |
| 117 | + |
| 118 | + await clearDocument(document) |
| 119 | + assert.strictEqual(document.getText(), '') |
| 120 | + }) |
| 121 | + |
| 122 | + function getTestWizardResponse(): TailLogGroupWizardResponse { |
| 123 | + return { |
| 124 | + regionLogGroupSubmenuResponse: { |
| 125 | + region: testRegion, |
| 126 | + data: testLogGroup, |
| 127 | + }, |
| 128 | + filterPattern: '', |
| 129 | + logStreamFilter: { |
| 130 | + type: 'all', |
| 131 | + }, |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + function getTestResponseStream(): AsyncIterable<StartLiveTailResponseStream> { |
| 136 | + const sessionStartFrame: StartLiveTailResponseStream = { |
| 137 | + sessionStart: { |
| 138 | + logGroupIdentifiers: [testLogGroup], |
| 139 | + }, |
| 140 | + sessionUpdate: undefined, |
| 141 | + } |
| 142 | + const sessionUpdateFrame: StartLiveTailResponseStream = { |
| 143 | + sessionUpdate: { |
| 144 | + sessionResults: [ |
| 145 | + { |
| 146 | + message: testMessage, |
| 147 | + timestamp: 876830400000, |
| 148 | + }, |
| 149 | + ], |
| 150 | + }, |
| 151 | + } |
| 152 | + return asyncGenerator([sessionStartFrame, sessionUpdateFrame]) |
| 153 | + } |
| 154 | +}) |
0 commit comments