Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions __tests__/run_config_change_detection.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
2 changes: 1 addition & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }),
Expand Down