diff --git a/__tests__/run_config_change_detection.test.js b/__tests__/run_config_change_detection.test.js new file mode 100644 index 0000000..8e3791b --- /dev/null +++ b/__tests__/run_config_change_detection.test.js @@ -0,0 +1,90 @@ +const core = require('@actions/core'); +const { + LambdaClient, + UpdateFunctionConfigurationCommand, + UpdateFunctionCodeCommand +} = require('@aws-sdk/client-lambda'); +const mainModule = require('../index'); +const validations = require('../validations'); + +jest.mock('@actions/core'); +jest.mock('@aws-sdk/client-lambda'); +jest.mock('@aws-sdk/client-s3'); +jest.mock('@aws-sdk/client-sts'); + +// These tests go through run() with the real hasConfigurationChanged(), so +// they catch the result of the comparison being ignored (for example when +// the async function is called without await and the pending Promise is +// treated as "changed"). +describe('run() configuration change detection', () => { + const currentConfig = { + FunctionName: 'test-function', + PackageType: 'Image', + MemorySize: 512, + Timeout: 60, + Environment: { Variables: { ENV: 'production' } }, + State: 'Active', + LastUpdateStatus: 'Successful' + }; + + let mockSend; + + const setInputs = (overrides = {}) => { + validations.validateAllInputs = jest.fn().mockReturnValue({ + valid: true, + functionName: 'test-function', + packageType: 'Image', + imageUri: '123456789012.dkr.ecr.us-east-1.amazonaws.com/repo:latest', + parsedMemorySize: 512, + timeout: 60, + parsedEnvironment: { ENV: 'production' }, + dryRun: false, + ...overrides + }); + }; + + beforeEach(() => { + jest.clearAllMocks(); + process.env.AWS_REGION = 'us-east-1'; + mockSend = jest.fn().mockImplementation(async () => ({ + ...currentConfig, + FunctionArn: 'arn:aws:lambda:us-east-1:123456789012:function:test-function', + Version: '$LATEST' + })); + LambdaClient.mockImplementation(() => ({ send: mockSend })); + }); + + test('does not update the configuration when nothing changed', async () => { + setInputs(); + + await mainModule.run(); + + expect(core.setFailed).not.toHaveBeenCalled(); + expect(core.info).toHaveBeenCalledWith('No configuration changes detected'); + expect(UpdateFunctionConfigurationCommand).not.toHaveBeenCalled(); + expect(UpdateFunctionCodeCommand).toHaveBeenCalledTimes(1); + }); + + test('updates the configuration when an input differs', async () => { + setInputs({ parsedMemorySize: 1024 }); + + await mainModule.run(); + + expect(core.setFailed).not.toHaveBeenCalled(); + expect(UpdateFunctionConfigurationCommand).toHaveBeenCalledTimes(1); + expect(UpdateFunctionConfigurationCommand.mock.calls[0][0]).toHaveProperty('MemorySize', 1024); + expect(UpdateFunctionCodeCommand).toHaveBeenCalledTimes(1); + }); + + test('dry run still validates the code update when nothing changed', async () => { + setInputs({ dryRun: true }); + + await mainModule.run(); + + expect(core.setFailed).not.toHaveBeenCalled(); + expect(core.info).not.toHaveBeenCalledWith('[DRY RUN] Configuration updates are not simulated in dry run mode'); + expect(UpdateFunctionConfigurationCommand).not.toHaveBeenCalled(); + expect(UpdateFunctionCodeCommand).toHaveBeenCalledTimes(1); + expect(UpdateFunctionCodeCommand.mock.calls[0][0]).toHaveProperty('DryRun', true); + }); +}); diff --git a/dist/index.js b/dist/index.js index 61dc86e..a0c4ddb 100644 --- a/dist/index.js +++ b/dist/index.js @@ -104,7 +104,7 @@ async function run() { return; } - const configChanged = hasConfigurationChanged(currentConfig, { + const configChanged = await hasConfigurationChanged(currentConfig, { ...(role && { Role: role }), // Only include handler, runtime, and layers for Zip package type ...(packageType === 'Zip' && handler && { Handler: handler }), diff --git a/index.js b/index.js index 686c883..e3e2078 100644 --- a/index.js +++ b/index.js @@ -98,7 +98,7 @@ async function run() { return; } - const configChanged = hasConfigurationChanged(currentConfig, { + const configChanged = await hasConfigurationChanged(currentConfig, { ...(role && { Role: role }), // Only include handler, runtime, and layers for Zip package type ...(packageType === 'Zip' && handler && { Handler: handler }),