From 4cb19c31881117e8e5f52d4e304ebe383d77158f Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Fri, 24 Sep 2021 13:59:10 -0700 Subject: [PATCH] Integration tests --- plugins/modules/ec2_snapshot.py | 3 +- .../targets/ec2_snapshot/tasks/main.yml | 125 +++++++++--------- 2 files changed, 65 insertions(+), 63 deletions(-) diff --git a/plugins/modules/ec2_snapshot.py b/plugins/modules/ec2_snapshot.py index 778f17b5478..4e26fea3bbd 100644 --- a/plugins/modules/ec2_snapshot.py +++ b/plugins/modules/ec2_snapshot.py @@ -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") diff --git a/tests/integration/targets/ec2_snapshot/tasks/main.yml b/tests/integration/targets/ec2_snapshot/tasks/main.yml index 6866789df78..037ece3146c 100644 --- a/tests/integration/targets/ec2_snapshot/tasks/main.yml +++ b/tests/integration/targets/ec2_snapshot/tasks/main.yml @@ -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: @@ -23,7 +20,6 @@ collections: - community.aws - block: - name: Gather availability zones aws_az_facts: @@ -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 @@ -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 @@ -105,7 +105,7 @@ filters: "tag:Name": '{{ resource_prefix }}' register: info_check - check_mode: yes + check_mode: true - assert: that: @@ -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: @@ -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 @@ -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: @@ -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 }}' @@ -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: