2022-09-19 12:00:09 -04:00
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
2023-10-02 12:43:28 -04:00
import { describe , it , beforeAll , afterAll , expect } from "vitest" ;
2023-01-05 10:54:06 -05:00
import {
IAMClient ,
CreateRoleCommand ,
AttachRolePolicyCommand ,
DeleteRoleCommand ,
DetachRolePolicyCommand ,
waitUntilRoleExists ,
} from "@aws-sdk/client-iam" ;
2024-10-22 09:41:55 -04:00
import { logger } from "@aws-doc-sdk-examples/lib/utils/util-log.js" ;
2024-02-22 10:32:44 -05:00
import { retry } from "@aws-doc-sdk-examples/lib/utils/util-timers.js" ;
2023-01-05 10:54:06 -05:00
2022-09-19 12:00:09 -04:00
import {
waitForFunctionActive ,
waitForFunctionUpdated ,
} from "../../lambda/waiters/index.js" ;
2023-10-02 12:43:28 -04:00
import { createFunction } from "../actions/create-function.js" ;
import { deleteFunction } from "../actions/delete-function.js" ;
import { getFunction } from "../actions/get-function.js" ;
import { invoke } from "../actions/invoke.js" ;
import { listFunctions } from "../actions/list-functions.js" ;
import { updateFunctionCode } from "../actions/update-function-code.js" ;
import { updateFunctionConfiguration } from "../actions/update-function-configuration.js" ;
import { helloLambda } from "../hello.js" ;
2022-09-19 12:00:09 -04:00
describe ( "Creating, getting, invoking, listing, updating, and deleting" , ( ) => {
2024-01-08 09:06:33 -05:00
const iamClient = new IAMClient ( { } ) ;
2022-09-19 12:00:09 -04:00
const roleName = "test-lambda-actions-role-name" ;
const rolePolicyArn =
"arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" ;
const funcName = "func-math" ;
let roleArn ;
beforeAll ( async ( ) => {
2023-01-05 10:54:06 -05:00
const createRoleCommand = new CreateRoleCommand ( {
2023-10-11 11:30:41 -04:00
AssumeRolePolicyDocument : JSON . stringify ( {
2023-01-05 10:54:06 -05:00
Version : "2012-10-17" ,
Statement : [
{
Effect : "Allow" ,
Principal : {
Service : "lambda.amazonaws.com" ,
2022-09-19 12:00:09 -04:00
} ,
2023-01-05 10:54:06 -05:00
Action : "sts:AssumeRole" ,
} ,
] ,
} ) ,
RoleName : roleName ,
} ) ;
try {
const response = await iamClient . send ( createRoleCommand ) ;
2023-10-11 11:30:41 -04:00
roleArn = response . Role ? response . Role . Arn : null ;
2023-01-05 10:54:06 -05:00
await waitUntilRoleExists (
{
client : iamClient ,
maxWaitTime : 15 ,
} ,
2023-10-02 12:43:28 -04:00
{ RoleName : roleName } ,
2023-01-05 10:54:06 -05:00
) ;
const attachRolePolicyCommand = new AttachRolePolicyCommand ( {
PolicyArn : rolePolicyArn ,
2022-09-19 12:00:09 -04:00
RoleName : roleName ,
} ) ;
2023-01-05 10:54:06 -05:00
await iamClient . send ( attachRolePolicyCommand ) ;
2022-09-19 12:00:09 -04:00
} catch ( err ) {
2024-10-22 09:41:55 -04:00
logger . error ( err ) ;
2022-09-19 12:00:09 -04:00
throw err ;
}
} ) ;
afterAll ( async ( ) => {
try {
2023-01-05 10:54:06 -05:00
const detachRolePolicyCommand = new DetachRolePolicyCommand ( {
PolicyArn : rolePolicyArn ,
RoleName : roleName ,
} ) ;
await iamClient . send ( detachRolePolicyCommand ) ;
2023-10-02 12:43:28 -04:00
const deleteRoleCommand = new DeleteRoleCommand ( { RoleName : roleName } ) ;
2023-01-05 10:54:06 -05:00
await iamClient . send ( deleteRoleCommand ) ;
2022-09-19 12:00:09 -04:00
} catch ( err ) {
2024-10-22 09:41:55 -04:00
logger . error ( err ) ;
2022-09-19 12:00:09 -04:00
throw err ;
}
try {
// Ensure function gets deleted even if the test fails
// before the deletion step.
await deleteFunction ( funcName ) ;
} catch ( err ) {
2024-10-22 09:41:55 -04:00
logger . error ( err ) ;
2022-09-19 12:00:09 -04:00
}
} ) ;
const testCreateFunction = async ( ) => {
// A role goes into a busy state after attaching a policy
// and there's no explicit waiter available for this.
2023-10-02 12:43:28 -04:00
await retry ( { intervalInMs : 2000 , maxRetries : 15 } , ( ) =>
createFunction ( funcName , roleArn ) ,
2023-04-07 14:07:21 -04:00
) ;
2022-09-19 12:00:09 -04:00
2023-10-02 12:43:28 -04:00
const response = await retry ( { intervalInMs : 2000 , maxRetries : 15 } , ( ) =>
getFunction ( funcName ) ,
2023-04-07 14:07:21 -04:00
) ;
2023-10-11 11:30:41 -04:00
expect ( response . Configuration . FunctionName ) . toBe ( funcName ) ;
2022-09-19 12:00:09 -04:00
} ;
2023-08-24 09:56:09 -04:00
const testHello = async ( ) => {
const funcs = await helloLambda ( ) ;
expect ( funcs ) . toContain ( funcName ) ;
} ;
2022-09-19 12:00:09 -04:00
const testInvokeFunction = async ( ) => {
// Verify 'invoke-function' works.
2023-10-02 12:43:28 -04:00
const { result } = await invoke ( funcName , "1" ) ;
2022-09-19 12:00:09 -04:00
expect ( result ) . toBe ( "2" ) ;
} ;
const testDeleteFunction = async ( ) => {
try {
await deleteFunction ( funcName ) ;
await getFunction ( funcName ) ;
} catch ( err ) {
2023-10-11 11:30:41 -04:00
expect ( err . name ) . toBe ( "ResourceNotFoundException" ) ;
2022-09-19 12:00:09 -04:00
}
} ;
const testListFunctions = async ( ) => {
2023-10-11 11:30:41 -04:00
const getFunctionNames = async ( ) => {
const response = await listFunctions ( ) ;
return response . Functions . map ( ( func ) => func . FunctionName ) ;
} ;
2022-09-19 12:00:09 -04:00
const functionNames = await getFunctionNames ( ) ;
2023-10-11 11:30:41 -04:00
expect ( functionNames ) . toContain ( funcName ) ;
2022-09-19 12:00:09 -04:00
} ;
const testUpdateFunction = async ( ) => {
await updateFunctionCode ( funcName , "func-update" ) ;
2023-10-02 12:43:28 -04:00
await waitForFunctionUpdated ( { FunctionName : funcName } ) ;
const { result } = await invoke ( funcName , [ "add" , "1" , "1" ] ) ;
2022-09-19 12:00:09 -04:00
expect ( result ) . toBe ( "2" ) ;
} ;
// Ideally these should be separate tests, but since we're creating
// real AWS resources, this saves on testing time and possibly
// cost.
it ( "are all handled by this one test" , async ( ) => {
await testCreateFunction ( ) ;
2023-10-02 12:43:28 -04:00
await waitForFunctionActive ( { FunctionName : funcName } ) ;
2023-08-24 09:56:09 -04:00
await testHello ( ) ;
2022-09-19 12:00:09 -04:00
await updateFunctionConfiguration ( funcName ) ;
2023-10-02 12:43:28 -04:00
await retry ( { intervalInMs : 2000 , maxRetries : 15 } , testInvokeFunction ) ;
2022-09-19 12:00:09 -04:00
await testListFunctions ( ) ;
await testUpdateFunction ( ) ;
await testDeleteFunction ( ) ;
} ) ;
} ) ;