Skip to content

Commit

Permalink
Return order of block devices not guaranteed.
Browse files Browse the repository at this point in the history
  • Loading branch information
tremble committed Nov 15, 2023
1 parent e539479 commit bbd10b0
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 200 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@
that:
- block_device_instances is not failed
- block_device_instances is changed
- block_device_instances_info.instances[0].block_device_mappings[0]
- block_device_instances_info.instances[0].block_device_mappings[1]
- block_device_instances_info.instances[0].block_device_mappings[1].device_name == '/dev/sdb'
- instance_info.block_device_mappings | length == 2
- '"/dev/sdb" in instance_info.block_device_mappings | map(attribute="device_name")'
vars:
instance_info: "{{ block_device_instances_info.instances[0] }}"

- name: New instance with an extra block device (check mode)
amazon.aws.ec2_instance:
Expand Down Expand Up @@ -108,7 +109,15 @@
that:
- block_device_instances_gp3 is not failed
- block_device_instances_gp3 is changed
- block_device_instances_gp3.spec.BlockDeviceMappings[0].DeviceName == '/dev/sdb'
- block_device_instances_gp3.spec.BlockDeviceMappings[0].Ebs.VolumeType == 'gp3'
- block_device_instances_gp3.spec.BlockDeviceMappings[0].Ebs.VolumeSize == 20
- block_device_instances_gp3.spec.BlockDeviceMappings[0].Ebs.Throughput == 500
- instance_info.block_device_mappings | length == 2
- '"/dev/sdb" in instance_info.block_device_mappings | map(attribute="device_name")'
- block_device_spec | length == 1
- '"DeviceName" in block_device_spec[0]'
- '"Ebs" in block_device_spec[0]'
- block_device_spec[0].DeviceName == "/dev/sdb"
- block_device_spec[0].Ebs.VolumeType == "gp3"
- block_device_spec[0].Ebs.VolumeSize == 20
- block_device_spec[0].Ebs.Throughput == 500
vars:
instance_info: "{{ block_device_instances_gp3.instances[0] }}"
block_device_spec: "{{ block_device_instances_gp3.spec.BlockDeviceMappings }}"
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---
# defaults file for ec2_instance_state_config_updates
ec2_instance_type: 't3.micro'
ec2_instance_tag_TestId: '{{ resource_prefix }}-state-config-updates'
ec2_instance_type: t3.micro
ec2_instance_tag_TestId: "{{ resource_prefix }}-state-config-updates"
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
# this just makes sure they're in the right place
dependencies:
- role: setup_ec2_facts
- role: setup_ec2_instance_env
vars:
ec2_instance_test_name: state_config_updates
- role: setup_ec2_facts
- role: setup_ec2_instance_env
vars:
ec2_instance_test_name: state_config_updates
Original file line number Diff line number Diff line change
@@ -1,133 +1,140 @@
---
# Test that configuration changes, like security groups and instance attributes,
# are updated correctly when the instance has different states, and also when
# changing the state of an instance.
# https://github.com/ansible-collections/community.aws/issues/16
- module_defaults:
- name: Wrap tests in block to set module defaults
module_defaults:
group/aws:
access_key: "{{ aws_access_key }}"
secret_key: "{{ aws_secret_key }}"
session_token: "{{ security_token | default(omit) }}"
region: "{{ aws_region }}"
block:
- name: "Make instance with sg and termination protection enabled"
ec2_instance:
state: running
name: "{{ resource_prefix }}-test-state-param-changes"
image_id: "{{ ec2_ami_id }}"
tags:
TestId: "{{ ec2_instance_tag_TestId }}"
security_groups: "{{ sg.group_id }}"
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
termination_protection: False
instance_type: "{{ ec2_instance_type }}"
wait: True
register: create_result
- name: Make instance with sg and termination protection enabled
amazon.aws.ec2_instance:
state: running
name: "{{ resource_prefix }}-test-state-param-changes"
image_id: "{{ ec2_ami_id }}"
tags:
TestId: "{{ ec2_instance_tag_TestId }}"
security_groups: "{{ sg.group_id }}"
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
termination_protection: false
instance_type: "{{ ec2_instance_type }}"
wait: true
register: create_result

- assert:
that:
- create_result is not failed
- create_result.changed
- '"instances" in create_result'
- '"instance_ids" in create_result'
- '"spec" in create_result'
- create_result.instances[0].security_groups[0].group_id == "{{ sg.group_id }}"
- create_result.spec.DisableApiTermination == False
- name: Verify creation
ansible.builtin.assert:
that:
- create_result is not failed
- create_result.changed
- '"instances" in create_result'
- '"instance_ids" in create_result'
- '"spec" in create_result'
- create_result.instances[0].security_groups[0].group_id == "{{ sg.group_id }}"
- create_result.spec.DisableApiTermination == False

- name: "Change sg and termination protection while instance is in state running"
ec2_instance:
state: running
name: "{{ resource_prefix }}-test-state-param-changes"
image_id: "{{ ec2_ami_id }}"
tags:
TestId: "{{ ec2_instance_tag_TestId }}"
security_groups: "{{ sg2.group_id }}"
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
termination_protection: True
instance_type: "{{ ec2_instance_type }}"
register: change_params_result
- name: Change sg and termination protection while instance is in state running
amazon.aws.ec2_instance:
state: running
name: "{{ resource_prefix }}-test-state-param-changes"
image_id: "{{ ec2_ami_id }}"
tags:
TestId: "{{ ec2_instance_tag_TestId }}"
security_groups: "{{ sg2.group_id }}"
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
termination_protection: true
instance_type: "{{ ec2_instance_type }}"
register: change_params_result

- assert:
that:
- change_params_result is not failed
- change_params_result.changed
- '"instances" in change_params_result'
- '"instance_ids" in change_params_result'
- '"changes" in change_params_result'
- change_params_result.instances[0].security_groups[0].group_id == "{{ sg2.group_id }}"
- change_params_result.changes[0].DisableApiTermination.Value == True
- change_params_result.changes[1].Groups[0] == "{{ sg2.group_id }}" # TODO fix this to be less fragile
- name: Verify changes
ansible.builtin.assert:
that:
- change_params_result is not failed
- change_params_result.changed
- '"instances" in change_params_result'
- '"instance_ids" in change_params_result'
- '"changes" in change_params_result'
- change_params_result.instances[0].security_groups[0].group_id == "{{ sg2.group_id }}"
- change_params_result.changes[0].DisableApiTermination.Value == True
- change_params_result.changes[1].Groups[0] == "{{ sg2.group_id }}"


- name: "Change instance state from running to stopped, and change sg and termination protection"
ec2_instance:
state: stopped
name: "{{ resource_prefix }}-test-state-param-changes"
image_id: "{{ ec2_ami_id }}"
tags:
TestId: "{{ ec2_instance_tag_TestId }}"
security_groups: "{{ sg.group_id }}"
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
termination_protection: False
instance_type: "{{ ec2_instance_type }}"
register: change_state_params_result
- name: Change instance state from running to stopped, and change sg and termination protection
amazon.aws.ec2_instance:
state: stopped
name: "{{ resource_prefix }}-test-state-param-changes"
image_id: "{{ ec2_ami_id }}"
tags:
TestId: "{{ ec2_instance_tag_TestId }}"
security_groups: "{{ sg.group_id }}"
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
termination_protection: false
instance_type: "{{ ec2_instance_type }}"
register: change_state_params_result

- assert:
that:
- change_state_params_result is not failed
- change_state_params_result.changed
- '"instances" in change_state_params_result'
- '"instance_ids" in change_state_params_result'
- '"changes" in change_state_params_result'
- '"stop_success" in change_state_params_result'
- '"stop_failed" in change_state_params_result'
- change_state_params_result.instances[0].security_groups[0].group_id == "{{ sg.group_id }}"
- change_state_params_result.changes[0].DisableApiTermination.Value == False
- name: Verify changes
ansible.builtin.assert:
that:
- change_state_params_result is not failed
- change_state_params_result.changed
- '"instances" in change_state_params_result'
- '"instance_ids" in change_state_params_result'
- '"changes" in change_state_params_result'
- '"stop_success" in change_state_params_result'
- '"stop_failed" in change_state_params_result'
- change_state_params_result.instances[0].security_groups[0].group_id == "{{ sg.group_id }}"
- change_state_params_result.changes[0].DisableApiTermination.Value == False

- name: "Change sg and termination protection while instance is in state stopped"
ec2_instance:
state: stopped
name: "{{ resource_prefix }}-test-state-param-changes"
image_id: "{{ ec2_ami_id }}"
tags:
TestId: "{{ ec2_instance_tag_TestId }}"
security_groups: "{{ sg2.group_id }}"
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
termination_protection: True
instance_type: "{{ ec2_instance_type }}"
register: change_params_stopped_result
- name: Change sg and termination protection while instance is in state stopped
amazon.aws.ec2_instance:
state: stopped
name: "{{ resource_prefix }}-test-state-param-changes"
image_id: "{{ ec2_ami_id }}"
tags:
TestId: "{{ ec2_instance_tag_TestId }}"
security_groups: "{{ sg2.group_id }}"
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
termination_protection: true
instance_type: "{{ ec2_instance_type }}"
register: change_params_stopped_result

- assert:
that:
- change_params_stopped_result is not failed
- change_params_stopped_result.changed
- '"instances" in change_params_stopped_result'
- '"instance_ids" in change_params_stopped_result'
- '"changes" in change_params_stopped_result'
- change_params_stopped_result.instances[0].security_groups[0].group_id == "{{ sg2.group_id }}"
- change_params_stopped_result.changes[0].DisableApiTermination.Value == True
- name: Verify changes
ansible.builtin.assert:
that:
- change_params_stopped_result is not failed
- change_params_stopped_result.changed
- '"instances" in change_params_stopped_result'
- '"instance_ids" in change_params_stopped_result'
- '"changes" in change_params_stopped_result'
- change_params_stopped_result.instances[0].security_groups[0].group_id == "{{ sg2.group_id }}"
- change_params_stopped_result.changes[0].DisableApiTermination.Value == True

- name: "Change instance state from stopped to running, and change sg and termination protection"
ec2_instance:
state: running
name: "{{ resource_prefix }}-test-state-param-changes"
image_id: "{{ ec2_ami_id }}"
tags:
TestId: "{{ ec2_instance_tag_TestId }}"
security_groups: "{{ sg.group_id }}"
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
termination_protection: False
instance_type: "{{ ec2_instance_type }}"
wait: True
register: change_params_start_result
- name: Change instance state from stopped to running, and change sg and termination protection
amazon.aws.ec2_instance:
state: running
name: "{{ resource_prefix }}-test-state-param-changes"
image_id: "{{ ec2_ami_id }}"
tags:
TestId: "{{ ec2_instance_tag_TestId }}"
security_groups: "{{ sg.group_id }}"
vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}"
termination_protection: false
instance_type: "{{ ec2_instance_type }}"
wait: true
register: change_params_start_result

- assert:
that:
- change_params_start_result is not failed
- change_params_start_result.changed
- '"instances" in change_params_start_result'
- '"instance_ids" in change_params_start_result'
- '"changes" in change_params_start_result'
- '"start_success" in change_params_start_result'
- '"start_failed" in change_params_start_result'
- change_params_start_result.instances[0].security_groups[0].group_id == "{{ sg.group_id }}"
- change_params_start_result.changes[0].DisableApiTermination.Value == False
- name: Verify changes
ansible.builtin.assert:
that:
- change_params_start_result is not failed
- change_params_start_result.changed
- '"instances" in change_params_start_result'
- '"instance_ids" in change_params_start_result'
- '"changes" in change_params_start_result'
- '"start_success" in change_params_start_result'
- '"start_failed" in change_params_start_result'
- change_params_start_result.instances[0].security_groups[0].group_id == "{{ sg.group_id }}"
- change_params_start_result.changes[0].DisableApiTermination.Value == False
Loading

0 comments on commit bbd10b0

Please sign in to comment.