Skip to content

Commit

Permalink
Add ec2_lc* integration tests (ansible-collections#824)
Browse files Browse the repository at this point in the history
Add ec2_lc* integration tests

SUMMARY

Added integration tests which exercise ec2_lc, ec2_lc_find, and ec2_lc_info.

ISSUE TYPE


Feature Pull Request

ADDITIONAL INFORMATION


ec2_lc (launch configurations) work with ASGs (auto-scaling groups) to define launch config for instances in the ASG.
We have tests for ec2_asg that make use of ec2_lc but as it is slow already, it makes sense to have a dedicated test suite for ec2_lc.

Reviewed-by: Alina Buzachis <None>
Reviewed-by: Mandar Kulkarni <[email protected]>
Reviewed-by: Jill R <None>
Reviewed-by: Markus Bergholz <[email protected]>
  • Loading branch information
mandar242 authored Jan 25, 2022
1 parent c0b3e1b commit af6a28f
Show file tree
Hide file tree
Showing 8 changed files with 370 additions and 1 deletion.
2 changes: 1 addition & 1 deletion plugins/modules/ec2_lc.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ def create_block_device_meta(module, volume):
if 'no_device' in volume:
return_object['NoDevice'] = volume.get('no_device')

if any(key in volume for key in ['snapshot', 'volume_size', 'volume_type', 'delete_on_termination', 'ips', 'encrypted']):
if any(key in volume for key in ['snapshot', 'volume_size', 'volume_type', 'delete_on_termination', 'iops', 'encrypted']):
return_object['Ebs'] = {}

if 'snapshot' in volume:
Expand Down
4 changes: 4 additions & 0 deletions tests/integration/targets/ec2_lc/aliases
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cloud/aws

ec2_lc_info
ec2_lc_find
7 changes: 7 additions & 0 deletions tests/integration/targets/ec2_lc/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
# defaults file for ec2_instance
ec2_instance_name: '{{ resource_prefix }}-node'
ec2_instance_owner: 'integration-run-{{ resource_prefix }}'
ec2_instance_type: t2.micro
ec2_ami_name: "amzn-ami-hvm*"
alarm_prefix: "ansible-test"
7 changes: 7 additions & 0 deletions tests/integration/targets/ec2_lc/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
dependencies:
- prepare_tests
- setup_ec2
- setup_ec2_facts
- role: setup_botocore_pip
vars:
boto3_version: "1.17.86"
94 changes: 94 additions & 0 deletions tests/integration/targets/ec2_lc/tasks/env_cleanup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
- name: remove any instances in the test VPC
ec2_instance:
filters:
vpc_id: "{{ testing_vpc.vpc.id }}"
state: absent
register: removed
until: removed is not failed
ignore_errors: yes
retries: 10

- name: Get ENIs
ec2_eni_info:
filters:
vpc-id: "{{ testing_vpc.vpc.id }}"
register: enis

- name: delete all ENIs
ec2_eni:
eni_id: "{{ item.id }}"
state: absent
until: removed is not failed
with_items: "{{ enis.network_interfaces }}"
ignore_errors: yes
retries: 10

- name: remove the security group
ec2_group:
name: "{{ resource_prefix }}-sg"
description: a security group for ansible tests
vpc_id: "{{ testing_vpc.vpc.id }}"
state: absent
register: removed
until: removed is not failed
ignore_errors: yes
retries: 10

- name: remove routing rules
ec2_vpc_route_table:
state: absent
vpc_id: "{{ testing_vpc.vpc.id }}"
tags:
created: "{{ resource_prefix }}-route"
routes:
- dest: 0.0.0.0/0
gateway_id: "{{ igw.gateway_id }}"
subnets:
- "{{ testing_subnet_a.subnet.id }}"
- "{{ testing_subnet_b.subnet.id }}"
register: removed
until: removed is not failed
ignore_errors: yes
retries: 10

- name: remove internet gateway
ec2_vpc_igw:
vpc_id: "{{ testing_vpc.vpc.id }}"
state: absent
register: removed
until: removed is not failed
ignore_errors: yes
retries: 10

- name: remove subnet A
ec2_vpc_subnet:
state: absent
vpc_id: "{{ testing_vpc.vpc.id }}"
cidr: 10.22.32.0/24
register: removed
until: removed is not failed
ignore_errors: yes
retries: 10

- name: remove subnet B
ec2_vpc_subnet:
state: absent
vpc_id: "{{ testing_vpc.vpc.id }}"
cidr: 10.22.33.0/24
register: removed
until: removed is not failed
ignore_errors: yes
retries: 10

- name: remove the VPC
ec2_vpc_net:
name: "{{ resource_prefix }}-vpc"
cidr_block: 10.22.32.0/23
state: absent
tags:
Name: Ansible Testing VPC
tenancy: default
register: removed
until: removed is not failed
ignore_errors: yes
retries: 10
64 changes: 64 additions & 0 deletions tests/integration/targets/ec2_lc/tasks/env_setup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
- name: Create VPC for use in testing
ec2_vpc_net:
name: "{{ resource_prefix }}-vpc"
cidr_block: 10.22.32.0/23
tags:
Name: Ansible ec2_lc Testing VPC
tenancy: default
register: testing_vpc

- name: Create internet gateway for use in testing
ec2_vpc_igw:
vpc_id: "{{ testing_vpc.vpc.id }}"
state: present
tags:
Name: Ansible ec2_lc Testing gateway
register: igw

- name: Create default subnet in zone A
ec2_vpc_subnet:
state: present
vpc_id: "{{ testing_vpc.vpc.id }}"
cidr: 10.22.32.0/24
az: "{{ aws_region }}a"
resource_tags:
Name: "{{ resource_prefix }}-subnet-a"
register: testing_subnet_a

- name: Create secondary subnet in zone B
ec2_vpc_subnet:
state: present
vpc_id: "{{ testing_vpc.vpc.id }}"
cidr: 10.22.33.0/24
az: "{{ aws_region }}b"
resource_tags:
Name: "{{ resource_prefix }}-subnet-b"
register: testing_subnet_b

- name: create routing rules
ec2_vpc_route_table:
vpc_id: "{{ testing_vpc.vpc.id }}"
tags:
created: "{{ resource_prefix }}-route"
routes:
- dest: 0.0.0.0/0
gateway_id: "{{ igw.gateway_id }}"
subnets:
- "{{ testing_subnet_a.subnet.id }}"
- "{{ testing_subnet_b.subnet.id }}"

- name: create a security group with the vpc
ec2_group:
name: "{{ resource_prefix }}-sg"
description: a security group for ansible tests
vpc_id: "{{ testing_vpc.vpc.id }}"
rules:
- proto: tcp
from_port: 22
to_port: 22
cidr_ip: 0.0.0.0/0
- proto: tcp
from_port: 80
to_port: 80
cidr_ip: 0.0.0.0/0
register: sg
192 changes: 192 additions & 0 deletions tests/integration/targets/ec2_lc/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
- name: run ec2_lc tests
module_defaults:
group/aws:
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
security_token: "{{ security_token | default(omit) }}"
region: "{{ aws_region }}"
collections:
- amazon.aws

block:

- name: set up environment for testing.
include_tasks: env_setup.yml

- name: Create launch configuration 1
community.aws.ec2_lc:
name: '{{ resource_prefix }}-lc1'
image_id: '{{ ec2_ami_id }}'
assign_public_ip: yes
instance_type: '{{ ec2_instance_type }}'
security_groups: '{{ sg.group_id }}'
volumes:
- device_name: /dev/xvda
volume_size: 10
volume_type: gp2
delete_on_termination: true
register: lc_1_create

- name: Gather information about launch configuration 1
vars:
ansible_python_interpreter: "{{ botocore_virtualenv_interpreter }}"
community.aws.ec2_lc_info:
name: '{{ resource_prefix }}-lc1'
register: lc_1_info_result

- assert:
that:
- lc_1_create is changed
- '"autoscaling:CreateLaunchConfiguration" in lc_1_create.resource_actions'
- '"throughput" not in lc_1_info_result.launch_configurations[0].block_device_mappings[0].ebs'
- lc_1_info_result.launch_configurations[0].block_device_mappings[0].ebs.volume_size == 10
- lc_1_info_result.launch_configurations[0].block_device_mappings[0].ebs.volume_type == 'gp2'
- lc_1_info_result.launch_configurations[0].instance_type == 't2.micro'

- name: Create launch configuration 1 - Idempotency
community.aws.ec2_lc:
name: '{{ resource_prefix }}-lc1'
image_id: '{{ ec2_ami_id }}'
assign_public_ip: yes
instance_type: '{{ ec2_instance_type }}'
security_groups: '{{ sg.group_id }}'
volumes:
- device_name: /dev/xvda
volume_size: 10
volume_type: gp2
delete_on_termination: true
register: lc_1_create_idem

- assert:
that:
- lc_1_create_idem is not changed
- '"autoscaling:CreateLaunchConfiguration" not in lc_1_create_idem.resource_actions'

- name: Create launch configuration 2
community.aws.ec2_lc:
name: '{{ resource_prefix }}-lc2'
image_id: '{{ ec2_ami_id }}'
assign_public_ip: yes
instance_type: 't3.small'
security_groups: '{{ sg.group_id }}'
volumes:
- device_name: /dev/xvda
volume_size: 10
volume_type: gp2
delete_on_termination: true
register: lc_2_create

- name: Gather information about launch configuration 2
vars:
ansible_python_interpreter: "{{ botocore_virtualenv_interpreter }}"
community.aws.ec2_lc_info:
name: '{{ resource_prefix }}-lc2'
register: lc_2_info_result

- assert:
that:
- lc_2_create is changed
- '"autoscaling:CreateLaunchConfiguration" in lc_2_create.resource_actions'
- '"throughput" not in lc_2_info_result.launch_configurations[0].block_device_mappings[0].ebs'
- lc_2_info_result.launch_configurations[0].block_device_mappings[0].ebs.volume_size == 10
- lc_2_info_result.launch_configurations[0].block_device_mappings[0].ebs.volume_type == 'gp2'
- lc_2_info_result.launch_configurations[0].instance_type == 't3.small'
- '"autoscaling:CreateLaunchConfiguration" in lc_2_create.resource_actions'

- name: Create launch configuration 2 - Idempotency
community.aws.ec2_lc:
name: '{{ resource_prefix }}-lc2'
image_id: '{{ ec2_ami_id }}'
assign_public_ip: yes
instance_type: '{{ ec2_instance_type }}'
security_groups: '{{ sg.group_id }}'
volumes:
- device_name: /dev/xvda
volume_size: 10
volume_type: gp2
delete_on_termination: true
register: lc_2_create_idem

- assert:
that:
- lc_2_create_idem is not changed
- '"autoscaling:CreateLaunchConfiguration" not in lc_2_create_idem.resource_actions'

- name: Search for the Launch Configurations that start with test resource_prefix
community.aws.ec2_lc_find:
name_regex: '{{ resource_prefix }}*'
sort_order: descending
register: lc_find_result

- assert:
that:
- lc_find_result.results | length == 2
- '"autoscaling:DescribeLaunchConfigurations" in lc_find_result.resource_actions'

- name: Delete launch configuration 1
community.aws.ec2_lc:
name: '{{ resource_prefix }}-lc1'
state: absent
register: lc_1_delete

- assert:
that:
- lc_1_delete is changed
- '"autoscaling:DeleteLaunchConfiguration" in lc_1_delete.resource_actions'

- name: Delete launch configuration 1 - Idempotency
community.aws.ec2_lc:
name: '{{ resource_prefix }}-lc1'
state: absent
register: lc_1_delete_idem

- assert:
that:
- lc_1_delete_idem is not changed
- '"autoscaling:DeleteLaunchConfiguration" not in lc_1_delete_idem.resource_actions'

- name: Gather information about launch configuration 1
community.aws.ec2_lc_info:
name: '{{ resource_prefix }}-lc1'
register: lc_1_info_result

- assert:
that:
- lc_1_info_result is not changed
- lc_1_info_result.launch_configurations | length == 0

- name: Delete launch configuration 2
community.aws.ec2_lc:
name: '{{ resource_prefix }}-lc2'
state: absent
register: lc_2_delete

- assert:
that:
- lc_2_delete is changed
- '"autoscaling:DeleteLaunchConfiguration" in lc_2_delete.resource_actions'

- name: Delete launch configuration 2 - Idempotency
community.aws.ec2_lc:
name: '{{ resource_prefix }}-lc2'
state: absent
register: lc_2_delete_idem

- assert:
that:
- lc_2_delete_idem is not changed
- '"autoscaling:DeleteLaunchConfiguration" not in lc_2_delete_idem.resource_actions'

- name: Gather information about launch configuration 2
community.aws.ec2_lc_info:
name: '{{ resource_prefix }}-lc2'
register: lc_2_info_result

- assert:
that:
- lc_2_info_result is not changed
- lc_2_info_result.launch_configurations | length == 0

always:

- include_tasks: env_cleanup.yml
1 change: 1 addition & 0 deletions tests/integration/targets/ec2_lc/vars/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---

0 comments on commit af6a28f

Please sign in to comment.