Skip to content

Commit

Permalink
Fix lambda wait issue and add more integration tests (#15)
Browse files Browse the repository at this point in the history
* Add missing waits after changing Lambda state

* Add integration test for lambda module

* Add integration test for lambda_alias module
  • Loading branch information
mattclay authored Jan 26, 2023
1 parent b533ed7 commit 8f2d450
Show file tree
Hide file tree
Showing 11 changed files with 244 additions and 1 deletion.
16 changes: 15 additions & 1 deletion plugins/modules/lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,9 @@ def create_function(self):
del args['Environment']

api = self.al.create_function(**args)

self.wait_on_create()

result = self.make_result(api)
else:
result = self.make_result(args)
Expand Down Expand Up @@ -402,6 +405,13 @@ def update_function_configuration(self, args, changed):

return args

def wait_on_create(self):
status = 'Pending'

while status == 'Pending':
time.sleep(1)
status = self.get_function(None)['State']

def wait_on_function(self):
status = 'InProgress'

Expand All @@ -415,7 +425,11 @@ def update_function_code(self, changed):
args['Publish'] = self.params['publish']

if changed and not self.check_mode:
return self.al.update_function_code(**args)
result = self.al.update_function_code(**args)

self.wait_on_function()

return result

return args

Expand Down
2 changes: 2 additions & 0 deletions tests/integration/targets/lambda/aliases
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cloud/aws
gather_facts/no
4 changes: 4 additions & 0 deletions tests/integration/targets/lambda/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function_name: "{{ resource_prefix }}-event"
function_arn: "arn:aws:lambda:{{ aws_region }}:{{ aws_account_id }}:function:{{ function_name }}:test"
iam_role_name: "{{ ('' if resource_prefix.startswith('ansible-test-') else 'ansible-test-') + resource_prefix }}"
iam_role_arn: "arn:aws:iam::{{ aws_account_id }}:role/{{ iam_role_name }}"
9 changes: 9 additions & 0 deletions tests/integration/targets/lambda/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
- block:
- import_tasks: setup.yml
- import_tasks: tests.yml
module_defaults:
group/mattclay.aws.common:
access_key: "{{ aws_access_key }}"
secret_key: "{{ aws_secret_key }}"
aws_region: "{{ aws_region }}"
security_token: "{{ security_token }}"
19 changes: 19 additions & 0 deletions tests/integration/targets/lambda/tasks/setup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
- name: Create a role
community.aws.iam_role:
name: "{{ iam_role_name }}"
assume_role_policy_document:
Version: '2012-10-17'
Statement:
- Action: sts:AssumeRole
Effect: Allow
Principal:
Service: lambda.amazonaws.com
managed_policies:
- arn:aws:iam::aws:policy/service-role/AWSLambdaSQSQueueExecutionRole

- name: Wait for role to be usable
pause:
seconds: 10

- name: Gather account facts
aws_account_facts:
89 changes: 89 additions & 0 deletions tests/integration/targets/lambda/tasks/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
- name: Create a Lambda function (check)
lambda:
name: "{{ function_name }}"
code: "# no actual code"
runtime: python3.8
timeout: 60
handler: lambda_function.lambda_handler
memory_size: 128
role: "{{ iam_role_arn }}"
publish: yes
qualifier: "test"
check_mode: yes
register: create_check

- name: Create a Lambda function (changed)
lambda:
name: "{{ function_name }}"
code: "# no actual code"
runtime: python3.8
timeout: 60
handler: lambda_function.lambda_handler
memory_size: 128
role: "{{ iam_role_arn }}"
publish: yes
qualifier: "test"
register: create_changed

- name: Create a Lambda function (unchanged)
lambda:
name: "{{ function_name }}"
code: "# no actual code"
runtime: python3.8
timeout: 60
handler: lambda_function.lambda_handler
memory_size: 128
role: "{{ iam_role_arn }}"
publish: no # publishing would force an update and thus report changed
qualifier: "test"
register: create_unchanged

- name: Update a Lambda function (check)
lambda:
name: "{{ function_name }}"
code: "# updated code"
runtime: python3.8
timeout: 60
handler: lambda_function.lambda_handler
memory_size: 128
role: "{{ iam_role_arn }}"
publish: yes
qualifier: "test"
check_mode: yes
register: update_check

- name: Update a Lambda function (changed)
lambda:
name: "{{ function_name }}"
code: "# updated code"
runtime: python3.8
timeout: 60
handler: lambda_function.lambda_handler
memory_size: 128
role: "{{ iam_role_arn }}"
publish: yes
qualifier: "test"
register: update_changed

- name: Update a Lambda function (unchanged)
lambda:
name: "{{ function_name }}"
code: "# updated code"
runtime: python3.8
timeout: 60
handler: lambda_function.lambda_handler
memory_size: 128
role: "{{ iam_role_arn }}"
publish: no # publishing would force an update and thus report changed
qualifier: "test"
register: update_unchanged

- name: Check results
assert:
that:
- create_check is changed
- create_changed is changed
- create_unchanged is not changed
- update_check is changed
- update_changed is changed
- update_unchanged is not changed
2 changes: 2 additions & 0 deletions tests/integration/targets/lambda_alias/aliases
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cloud/aws
gather_facts/no
4 changes: 4 additions & 0 deletions tests/integration/targets/lambda_alias/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function_name: "{{ resource_prefix }}-event"
function_arn: "arn:aws:lambda:{{ aws_region }}:{{ aws_account_id }}:function:{{ function_name }}:test"
iam_role_name: "{{ ('' if resource_prefix.startswith('ansible-test-') else 'ansible-test-') + resource_prefix }}"
iam_role_arn: "arn:aws:iam::{{ aws_account_id }}:role/{{ iam_role_name }}"
9 changes: 9 additions & 0 deletions tests/integration/targets/lambda_alias/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
- block:
- import_tasks: setup.yml
- import_tasks: tests.yml
module_defaults:
group/mattclay.aws.common:
access_key: "{{ aws_access_key }}"
secret_key: "{{ aws_secret_key }}"
aws_region: "{{ aws_region }}"
security_token: "{{ security_token }}"
32 changes: 32 additions & 0 deletions tests/integration/targets/lambda_alias/tasks/setup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
- name: Create a role
community.aws.iam_role:
name: "{{ iam_role_name }}"
assume_role_policy_document:
Version: '2012-10-17'
Statement:
- Action: sts:AssumeRole
Effect: Allow
Principal:
Service: lambda.amazonaws.com
managed_policies:
- arn:aws:iam::aws:policy/service-role/AWSLambdaSQSQueueExecutionRole

- name: Wait for role to be usable
pause:
seconds: 10

- name: Gather account facts
aws_account_facts:

- name: Create a Lambda function
lambda:
name: "{{ function_name }}"
code: "# no actual code"
runtime: python3.8
timeout: 60
handler: lambda_function.lambda_handler
memory_size: 128
role: "{{ iam_role_arn }}"
publish: yes
qualifier: "test"
register: lambda_function
59 changes: 59 additions & 0 deletions tests/integration/targets/lambda_alias/tasks/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
- name: Create the Lambda function alias (check)
lambda_alias:
name: "test"
description: "test alias"
function_name: "{{ lambda_function.meta.function_name }}"
version: "{{ lambda_function.meta.version }}"
check_mode: yes
register: create_check

- name: Create the Lambda function alias (changed)
lambda_alias:
name: "test"
description: "test alias"
function_name: "{{ lambda_function.meta.function_name }}"
version: "{{ lambda_function.meta.version }}"
register: create_changed

- name: Create the Lambda function alias (unchanged)
lambda_alias:
name: "test"
description: "test alias"
function_name: "{{ lambda_function.meta.function_name }}"
version: "{{ lambda_function.meta.version }}"
register: create_unchanged

- name: Update the Lambda function alias (check)
lambda_alias:
name: "test"
description: "updated alias"
function_name: "{{ lambda_function.meta.function_name }}"
version: "{{ lambda_function.meta.version }}"
check_mode: yes
register: update_check

- name: Update the Lambda function alias (changed)
lambda_alias:
name: "test"
description: "updated alias"
function_name: "{{ lambda_function.meta.function_name }}"
version: "{{ lambda_function.meta.version }}"
register: update_changed

- name: Update the Lambda function alias (unchanged)
lambda_alias:
name: "test"
description: "updated alias"
function_name: "{{ lambda_function.meta.function_name }}"
version: "{{ lambda_function.meta.version }}"
register: update_unchanged

- name: Check results
assert:
that:
- create_check is changed
- create_changed is changed
- create_unchanged is not changed
- update_check is changed
- update_changed is changed
- update_unchanged is not changed

0 comments on commit 8f2d450

Please sign in to comment.