Skip to content

Commit

Permalink
Integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mandar242 committed Sep 24, 2021
1 parent 7a2e5ef commit 4cb19c3
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 63 deletions.
3 changes: 2 additions & 1 deletion plugins/modules/ec2_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,8 @@ def create_snapshot(module, ec2, description=None, wait=None,
}]
try:
if module.check_mode:
module.exit_json(changed=True, msg='Would have created a snapshot if not in check mode', volume_id=volume['VolumeId'], volume_size=volume['Size'])
module.exit_json(changed=True, msg='Would have created a snapshot if not in check mode',
volume_id=volume['VolumeId'], volume_size=volume['Size'])
snapshot = _create_snapshot(ec2, **params)
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e:
module.fail_json_aws(e, msg="Failed to create snapshot")
Expand Down
125 changes: 63 additions & 62 deletions tests/integration/targets/ec2_snapshot/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
# Tests ec2_snapshot_info:
# - Listing snapshots for filter: tag
#
# Possible Bugs:
# - check_mode not supported
#
- name: Integration testing for ec2_snapshot
module_defaults:
group/aws:
Expand All @@ -23,7 +20,6 @@
collections:
- community.aws


block:
- name: Gather availability zones
aws_az_facts:
Expand All @@ -48,7 +44,7 @@
- untagged_snapshot.snapshots| length == 1
- untagged_snapshot.snapshots[0].volume_id == volume_detached.volume_id

- name: Setup an instance for testing
- name: Setup an instance for testing, make sure volumes are attached before next task
ec2_instance:
name: '{{ resource_prefix }}'
instance_type: t2.nano
Expand All @@ -60,25 +56,29 @@
volume_size: 8
delete_on_termination: true
register: instance
retries: 5
delay: 60
until: "instance.instances[0].block_device_mappings | length > 0 and instance.instances[0].block_device_mappings[-1].ebs.status =='attached'"

- set_fact:
volume_id: '{{ instance.instances[0].block_device_mappings[0].ebs.volume_id }}'
instance_id: '{{ instance.instances[0].instance_id }}'
device_name: '{{ instance.instances[0].block_device_mappings[0].device_name }}'

# JR: Check mode not supported
# - name: Take snapshot (check mode)
# ec2_snapshot:
# instance_id: '{{ instance_id }}'
# check_mode: true
# snapshot_tags:
# Test: '{{ resource_prefix }}'
# register: result
# - assert:
# that:
# - result is changed

- name: Take snapshot of volume
- name: Take snapshot (check mode)
ec2_snapshot:
instance_id: '{{ instance_id }}'
device_name: '{{ device_name }}'
snapshot_tags:
Test: '{{ resource_prefix }}'
check_mode: true
register: result

- assert:
that:
- result is changed

- name: Take snapshot of volume - first
ec2_snapshot:
volume_id: '{{ volume_id }}'
register: result
Expand All @@ -105,7 +105,7 @@
filters:
"tag:Name": '{{ resource_prefix }}'
register: info_check
check_mode: yes
check_mode: true

- assert:
that:
Expand All @@ -116,18 +116,17 @@
- info_check.snapshots[0].volume_size == result.volume_size
- info_check.snapshots[0].tags == result.tags

# JR: Check mode not supported
# - name: Take snapshot if most recent >1hr (False) (check mode)
# ec2_snapshot:
# volume_id: '{{ volume_id }}'
# snapshot_tags:
# Name: '{{ resource_prefix }}'
# last_snapshot_min_age: 60
# check_mode: true
# register: result
# - assert:
# that:
# - result is not changed
- name: Take snapshot if most recent >1hr (False) (check mode)
ec2_snapshot:
volume_id: '{{ volume_id }}'
snapshot_tags:
Name: '{{ resource_prefix }}'
last_snapshot_min_age: 60
check_mode: true
register: result
- assert:
that:
- result is not changed

- name: Take snapshot if most recent >1hr (False)
ec2_snapshot:
Expand All @@ -150,20 +149,19 @@
pause:
minutes: 1

# JR: Check mode not supported
# - name: Take snapshot if most recent >1min (True) (check mode)
# ec2_snapshot:
# volume_id: '{{ volume_id }}'
# snapshot_tags:
# Name: '{{ resource_prefix }}'
# last_snapshot_min_age: 1
# check_mode: true
# register: result
# - assert:
# that:
# - result is changed

- name: Take snapshot if most recent >1min (True)
- name: Take snapshot if most recent >1min (True) (check mode)
ec2_snapshot:
volume_id: '{{ volume_id }}'
snapshot_tags:
Name: '{{ resource_prefix }}'
last_snapshot_min_age: 1
check_mode: true
register: result
- assert:
that:
- result is changed

- name: Take snapshot if most recent >1min (True) - second
ec2_snapshot:
volume_id: '{{ volume_id }}'
last_snapshot_min_age: 1
Expand All @@ -181,18 +179,18 @@
- info_result.snapshots| length == 2
- result.snapshot_id in ( info_result.snapshots | map(attribute='snapshot_id') | list )

# JR: Check mode not supported
# - name: Take snapshot with a tag (check mode)
# ec2_snapshot:
# volume_id: '{{ volume_id }}'
# snapshot_tags:
# MyTag: '{{ resource_prefix }}'
# register: result
# - assert:
# that:
# - result is changed

- name: Take snapshot and tag it
- name: Take snapshot with a tag (check mode)
ec2_snapshot:
volume_id: '{{ volume_id }}'
snapshot_tags:
MyTag: '{{ resource_prefix }}'
check_mode: true
register: result
- assert:
that:
- result is changed

- name: Take snapshot and tag it - third
ec2_snapshot:
volume_id: '{{ volume_id }}'
snapshot_tags:
Expand Down Expand Up @@ -222,11 +220,8 @@
"tag:Name": '{{ resource_prefix }}'
register: info_result

- assert:
that:
- info_result.snapshots| length == 3

- ec2_snapshot:
- name: Generate extra snapshots - five more
ec2_snapshot:
volume_id: '{{ volume_id }}'
snapshot_tags:
ResourcePrefix: '{{ resource_prefix }}'
Expand All @@ -242,6 +237,12 @@
pause:
minutes: 2

- name: Get info about all snapshots for this test
ec2_snapshot_info:
filters:
"tag:Name": '{{ resource_prefix }}'
register: info_result

# check that snapshot_ids and max_results are mutually exclusive
- name: Check that max_results and snapshot_ids are mutually exclusive
ec2_snapshot_info:
Expand Down

0 comments on commit 4cb19c3

Please sign in to comment.