|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import assert from 'assert' |
| 7 | +import * as sinon from 'sinon' |
| 8 | +import * as vscode from 'vscode' |
| 9 | +import { LambdaFunctionNode } from '../../lambda/explorer/lambdaFunctionNode' |
| 10 | +import * as treeNodeUtils from '../../shared/utilities/treeNodeUtils' |
| 11 | +import * as resourceNode from '../../awsService/appBuilder/explorer/nodes/resourceNode' |
| 12 | +import * as invokeLambdaModule from '../../lambda/vue/remoteInvoke/invokeLambda' |
| 13 | +import * as tailLogGroupModule from '../../awsService/cloudWatchLogs/commands/tailLogGroup' |
| 14 | + |
| 15 | +describe('Lambda activation', () => { |
| 16 | + let sandbox: sinon.SinonSandbox |
| 17 | + let getSourceNodeStub: sinon.SinonStub |
| 18 | + let generateLambdaNodeFromResourceStub: sinon.SinonStub |
| 19 | + let invokeRemoteLambdaStub: sinon.SinonStub |
| 20 | + let tailLogGroupStub: sinon.SinonStub |
| 21 | + let isTreeNodeStub: sinon.SinonStub |
| 22 | + |
| 23 | + beforeEach(async () => { |
| 24 | + sandbox = sinon.createSandbox() |
| 25 | + getSourceNodeStub = sandbox.stub(treeNodeUtils, 'getSourceNode') |
| 26 | + generateLambdaNodeFromResourceStub = sandbox.stub(resourceNode, 'generateLambdaNodeFromResource') |
| 27 | + invokeRemoteLambdaStub = sandbox.stub(invokeLambdaModule, 'invokeRemoteLambda') |
| 28 | + tailLogGroupStub = sandbox.stub(tailLogGroupModule, 'tailLogGroup') |
| 29 | + isTreeNodeStub = sandbox.stub(require('../../shared/treeview/resourceTreeDataProvider'), 'isTreeNode') |
| 30 | + }) |
| 31 | + |
| 32 | + afterEach(() => { |
| 33 | + sandbox.restore() |
| 34 | + }) |
| 35 | + |
| 36 | + describe('aws.invokeLambda command', () => { |
| 37 | + it('should handle LambdaFunctionNode directly from AWS Explorer', async () => { |
| 38 | + const mockLambdaNode: LambdaFunctionNode = { |
| 39 | + functionName: 'testFunction', |
| 40 | + regionCode: 'us-west-2', |
| 41 | + configuration: { |
| 42 | + FunctionName: 'testFunction', |
| 43 | + FunctionArn: 'arn:aws:lambda:us-west-2:123456789012:function:testFunction', |
| 44 | + }, |
| 45 | + } as LambdaFunctionNode |
| 46 | + |
| 47 | + isTreeNodeStub.returns(false) |
| 48 | + invokeRemoteLambdaStub.resolves() |
| 49 | + |
| 50 | + await vscode.commands.executeCommand('aws.invokeLambda', mockLambdaNode) |
| 51 | + |
| 52 | + assert(invokeRemoteLambdaStub.calledOnce) |
| 53 | + const callArgs = invokeRemoteLambdaStub.getCall(0).args |
| 54 | + assert.strictEqual(callArgs[1].source, 'AwsExplorerRemoteInvoke') |
| 55 | + assert.strictEqual(callArgs[1].functionNode, mockLambdaNode) |
| 56 | + }) |
| 57 | + |
| 58 | + it('should generate LambdaFunctionNode from TreeNode when coming from AppBuilder', async () => { |
| 59 | + const mockGeneratedLambdaNode: LambdaFunctionNode = { |
| 60 | + functionName: 'generatedFunction', |
| 61 | + regionCode: 'us-east-1', |
| 62 | + configuration: { |
| 63 | + FunctionName: 'generatedFunction', |
| 64 | + FunctionArn: 'arn:aws:lambda:us-east-1:123456789012:function:generatedFunction', |
| 65 | + }, |
| 66 | + } as LambdaFunctionNode |
| 67 | + |
| 68 | + const mockTreeNode = { |
| 69 | + resource: { |
| 70 | + deployedResource: { LogicalResourceId: 'TestFunction' }, |
| 71 | + region: 'us-east-1', |
| 72 | + stackName: 'TestStack', |
| 73 | + resource: { Id: 'TestFunction', Type: 'AWS::Serverless::Function' }, |
| 74 | + }, |
| 75 | + } |
| 76 | + |
| 77 | + isTreeNodeStub.returns(true) |
| 78 | + getSourceNodeStub.returns(undefined) |
| 79 | + generateLambdaNodeFromResourceStub.resolves(mockGeneratedLambdaNode) |
| 80 | + invokeRemoteLambdaStub.resolves() |
| 81 | + |
| 82 | + await vscode.commands.executeCommand('aws.invokeLambda', mockTreeNode) |
| 83 | + |
| 84 | + assert(generateLambdaNodeFromResourceStub.calledOnce) |
| 85 | + assert(generateLambdaNodeFromResourceStub.calledWith(mockTreeNode.resource)) |
| 86 | + assert(invokeRemoteLambdaStub.calledOnce) |
| 87 | + const callArgs = invokeRemoteLambdaStub.getCall(0).args |
| 88 | + assert.strictEqual(callArgs[1].source, 'AppBuilderRemoteInvoke') |
| 89 | + assert.strictEqual(callArgs[1].functionNode, mockGeneratedLambdaNode) |
| 90 | + }) |
| 91 | + |
| 92 | + it('should handle existing LambdaFunctionNode from TreeNode', async () => { |
| 93 | + const mockLambdaNode: LambdaFunctionNode = { |
| 94 | + functionName: 'existingFunction', |
| 95 | + regionCode: 'us-west-2', |
| 96 | + configuration: { |
| 97 | + FunctionName: 'existingFunction', |
| 98 | + FunctionArn: 'arn:aws:lambda:us-west-2:123456789012:function:existingFunction', |
| 99 | + }, |
| 100 | + } as LambdaFunctionNode |
| 101 | + |
| 102 | + const mockTreeNode = { |
| 103 | + resource: {}, |
| 104 | + } |
| 105 | + |
| 106 | + isTreeNodeStub.returns(true) |
| 107 | + getSourceNodeStub.returns(mockLambdaNode) |
| 108 | + invokeRemoteLambdaStub.resolves() |
| 109 | + |
| 110 | + await vscode.commands.executeCommand('aws.invokeLambda', mockTreeNode) |
| 111 | + |
| 112 | + assert(generateLambdaNodeFromResourceStub.notCalled) |
| 113 | + assert(invokeRemoteLambdaStub.calledOnce) |
| 114 | + const callArgs = invokeRemoteLambdaStub.getCall(0).args |
| 115 | + assert.strictEqual(callArgs[1].source, 'AppBuilderRemoteInvoke') |
| 116 | + assert.strictEqual(callArgs[1].functionNode, mockLambdaNode) |
| 117 | + }) |
| 118 | + }) |
| 119 | + |
| 120 | + describe('aws.appBuilder.tailLogs command', () => { |
| 121 | + it('should handle LambdaFunctionNode directly', async () => { |
| 122 | + const mockLambdaNode: LambdaFunctionNode = { |
| 123 | + functionName: 'testFunction', |
| 124 | + regionCode: 'us-west-2', |
| 125 | + configuration: { |
| 126 | + FunctionName: 'testFunction', |
| 127 | + LoggingConfig: { |
| 128 | + LogGroup: '/aws/lambda/custom-log-group', |
| 129 | + }, |
| 130 | + }, |
| 131 | + } as LambdaFunctionNode |
| 132 | + |
| 133 | + isTreeNodeStub.returns(false) |
| 134 | + getSourceNodeStub.returns(mockLambdaNode) |
| 135 | + tailLogGroupStub.resolves() |
| 136 | + |
| 137 | + await vscode.commands.executeCommand('aws.appBuilder.tailLogs', mockLambdaNode) |
| 138 | + |
| 139 | + assert(tailLogGroupStub.calledOnce) |
| 140 | + const callArgs = tailLogGroupStub.getCall(0).args |
| 141 | + assert.strictEqual(callArgs[1], 'AwsExplorerLambdaNode') |
| 142 | + assert.deepStrictEqual(callArgs[3], { |
| 143 | + regionName: 'us-west-2', |
| 144 | + groupName: '/aws/lambda/custom-log-group', |
| 145 | + }) |
| 146 | + assert.deepStrictEqual(callArgs[4], { type: 'all' }) |
| 147 | + }) |
| 148 | + |
| 149 | + it('should generate LambdaFunctionNode from TreeNode when getSourceNode returns undefined', async () => { |
| 150 | + const mockGeneratedLambdaNode: LambdaFunctionNode = { |
| 151 | + functionName: 'generatedFunction', |
| 152 | + regionCode: 'us-east-1', |
| 153 | + configuration: { |
| 154 | + FunctionName: 'generatedFunction', |
| 155 | + }, |
| 156 | + } as LambdaFunctionNode |
| 157 | + |
| 158 | + const mockTreeNode = { |
| 159 | + resource: { |
| 160 | + deployedResource: { LogicalResourceId: 'TestFunction' }, |
| 161 | + region: 'us-east-1', |
| 162 | + stackName: 'TestStack', |
| 163 | + resource: { Id: 'TestFunction', Type: 'AWS::Serverless::Function' }, |
| 164 | + }, |
| 165 | + } |
| 166 | + |
| 167 | + isTreeNodeStub.returns(true) |
| 168 | + getSourceNodeStub.returns(undefined) |
| 169 | + generateLambdaNodeFromResourceStub.resolves(mockGeneratedLambdaNode) |
| 170 | + tailLogGroupStub.resolves() |
| 171 | + |
| 172 | + await vscode.commands.executeCommand('aws.appBuilder.tailLogs', mockTreeNode) |
| 173 | + |
| 174 | + assert(generateLambdaNodeFromResourceStub.calledOnce) |
| 175 | + assert(generateLambdaNodeFromResourceStub.calledWith(mockTreeNode.resource)) |
| 176 | + assert(tailLogGroupStub.calledOnce) |
| 177 | + const callArgs = tailLogGroupStub.getCall(0).args |
| 178 | + assert.strictEqual(callArgs[1], 'AppBuilder') |
| 179 | + assert.deepStrictEqual(callArgs[3], { |
| 180 | + regionName: 'us-east-1', |
| 181 | + groupName: '/aws/lambda/generatedFunction', |
| 182 | + }) |
| 183 | + assert.deepStrictEqual(callArgs[4], { type: 'all' }) |
| 184 | + }) |
| 185 | + |
| 186 | + it('should use correct source for TreeNode', async () => { |
| 187 | + const mockLambdaNode: LambdaFunctionNode = { |
| 188 | + functionName: 'testFunction', |
| 189 | + regionCode: 'us-west-2', |
| 190 | + configuration: { |
| 191 | + FunctionName: 'testFunction', |
| 192 | + }, |
| 193 | + } as LambdaFunctionNode |
| 194 | + |
| 195 | + const mockTreeNode = { |
| 196 | + resource: {}, |
| 197 | + } |
| 198 | + |
| 199 | + isTreeNodeStub.returns(true) |
| 200 | + getSourceNodeStub.returns(mockLambdaNode) |
| 201 | + tailLogGroupStub.resolves() |
| 202 | + |
| 203 | + await vscode.commands.executeCommand('aws.appBuilder.tailLogs', mockTreeNode) |
| 204 | + |
| 205 | + assert(tailLogGroupStub.calledOnce) |
| 206 | + const callArgs = tailLogGroupStub.getCall(0).args |
| 207 | + assert.strictEqual(callArgs[1], 'AppBuilder') |
| 208 | + }) |
| 209 | + }) |
| 210 | +}) |
0 commit comments