Skip to content

Commit

Permalink
elb_instance - initial integration tests (#768)
Browse files Browse the repository at this point in the history
elb_instance - initial integration tests

SUMMARY
Rewrite elb_instance (ec2_elb) for boto3
ISSUE TYPE

Feature Pull Request

COMPONENT NAME
elb_instance
ADDITIONAL INFORMATION
fixes: #384

Reviewed-by: Markus Bergholz <[email protected]>
Reviewed-by: Alina Buzachis <None>
Reviewed-by: None <None>
  • Loading branch information
tremble authored Oct 20, 2021
1 parent cb5e541 commit 4e1c5db
Show file tree
Hide file tree
Showing 16 changed files with 753 additions and 3 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/384-elb_instance-python3-fixups.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- elb_instance - Python 3 compatability fix (https://github.com/ansible-collections/community.aws/issues/384).
2 changes: 1 addition & 1 deletion plugins/modules/elb_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def _get_instance_lbs(self, ec2_elbs=None):
break

if ec2_elbs:
lbs = sorted(lb for lb in elbs if lb.name in ec2_elbs)
lbs = sorted([lb for lb in elbs if lb.name in ec2_elbs], key=lambda lb: lb.__repr__())
else:
lbs = []
for lb in elbs:
Expand Down
1 change: 1 addition & 0 deletions tests/integration/targets/elb_instance/aliases
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cloud/aws
17 changes: 17 additions & 0 deletions tests/integration/targets/elb_instance/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
# defaults file for ec2_elb_lb
elb_name_1: 'ansible-test-{{ tiny_prefix }}-1'
elb_name_2: 'ansible-test-{{ tiny_prefix }}-2'

vpc_cidr: '10.{{ 256 | random(seed=resource_prefix) }}.0.0/16'
subnet_cidr_1: '10.{{ 256 | random(seed=resource_prefix) }}.1.0/24'
subnet_cidr_2: '10.{{ 256 | random(seed=resource_prefix) }}.2.0/24'

availability_zone_a: '{{ ec2_availability_zone_names[0] }}'
availability_zone_b: '{{ ec2_availability_zone_names[1] }}'

elb_listeners:
- protocol: tcp
load_balancer_port: 80
instance_port: 80
instance_protocol: tcp
4 changes: 4 additions & 0 deletions tests/integration/targets/elb_instance/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dependencies:
- prepare_tests
- setup_ec2
- setup_ec2_facts
16 changes: 16 additions & 0 deletions tests/integration/targets/elb_instance/tasks/cleanup_elbs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
- name: remove the public load balancer
elb_classic_lb:
name: "{{ elb_name_1 }}"
state: absent
wait: true
register: result
ignore_errors: true

- name: remove the private load balancer
elb_classic_lb:
name: "{{ elb_name_2 }}"
state: absent
wait: true
register: result
ignore_errors: true
23 changes: 23 additions & 0 deletions tests/integration/targets/elb_instance/tasks/cleanup_instances.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
- name: Delete instance
ec2_instance:
instance_ids:
- '{{ instance_a }}'
- '{{ instance_b }}'
state: absent
wait: true
ignore_errors: true

- name: Delete ASG
ec2_asg:
name: "ansible-test-{{ tiny_prefix }}-elb"
state: absent
ignore_errors: true
register: ec2_asg_a

- name: Delete Launch Template
ec2_lc:
name: "ansible-test-{{ tiny_prefix }}-elb"
state: absent
ignore_errors: true
register: ec2_lc_a
26 changes: 26 additions & 0 deletions tests/integration/targets/elb_instance/tasks/cleanup_vpc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
- name: delete security groups
ec2_group:
name: '{{ item }}'
state: absent
ignore_errors: true
loop:
- '{{ resource_prefix }}-a'
- '{{ resource_prefix }}-b'

- name: delete subnets
ec2_vpc_subnet:
vpc_id: '{{ setup_vpc.vpc.id }}'
cidr: '{{ item }}'
state: absent
ignore_errors: true
loop:
- '{{ subnet_cidr_1 }}'
- '{{ subnet_cidr_2 }}'

- name: delete VPC
ec2_vpc_net:
cidr_block: '{{ vpc_cidr }}'
state: absent
name: '{{ resource_prefix }}'
ignore_errors: true
29 changes: 29 additions & 0 deletions tests/integration/targets/elb_instance/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
- module_defaults:
group/aws:
region: "{{ aws_region }}"
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
security_token: "{{ security_token | default(omit) }}"
collections:
- community.aws
- amazon.aws
block:
# ============================================================

- include_tasks: setup_vpc.yml
- include_tasks: setup_elbs.yml
- include_tasks: setup_instances.yml

# ============================================================

- include_tasks: manage_instances.yml
- include_tasks: manage_asgs.yml

always:

# ============================================================

- include_tasks: cleanup_elbs.yml
- include_tasks: cleanup_instances.yml
- include_tasks: cleanup_vpc.yml
110 changes: 110 additions & 0 deletions tests/integration/targets/elb_instance/tasks/manage_asgs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
---
- name: Get ASG info
ec2_asg_info:
name: "ansible-test-{{ tiny_prefix }}-elb$"
register: asg_info

- name: Store Instance ID from ASG
set_fact:
instance_asg: '{{ asg_info.results[0].instances[0].instance_id }}'

- name: 'Remove an instance from ELB (check_mode)'
elb_instance:
instance_id: '{{ instance_asg }}'
state: 'absent'
wait_timeout: 60
register: remove_instance
check_mode: true

- elb_classic_lb_info:
names: '{{ elb_name_1 }}'
register: elb_info_1
- elb_classic_lb_info:
names: '{{ elb_name_2 }}'
register: elb_info_2

- assert:
that:
- remove_instance is successful
# - remove_instance is changed
# It really shouldn't be returning a fact here
- '"ansible_facts" in remove_instance'
- '"ec2_elbs" in remove_instance.ansible_facts'
- elb_name_1 in remove_instance.ansible_facts.ec2_elbs
- elb_name_2 in remove_instance.ansible_facts.ec2_elbs
# Check the real state didn't change
- instance_asg in elb_info_1.elbs[0].instances_inservice
- instance_asg in elb_info_2.elbs[0].instances_inservice

- name: 'Remove an instance from ELB'
elb_instance:
instance_id: '{{ instance_asg }}'
state: 'absent'
wait_timeout: 60
register: remove_instance

- elb_classic_lb_info:
names: '{{ elb_name_1 }}'
register: elb_info_1
- elb_classic_lb_info:
names: '{{ elb_name_2 }}'
register: elb_info_2

- assert:
that:
- remove_instance is successful
- remove_instance is changed
# It really shouldn't be returning a fact here
- '"ansible_facts" in remove_instance'
- '"ec2_elbs" in remove_instance.ansible_facts'
- elb_name_1 in remove_instance.ansible_facts.ec2_elbs
- elb_name_2 in remove_instance.ansible_facts.ec2_elbs
# Check the real state
- instance_asg not in elb_info_1.elbs[0].instances_inservice
- instance_asg not in elb_info_2.elbs[0].instances_inservice

- name: 'Remove an instance from ELB - idempotency (check_mode)'
elb_instance:
instance_id: '{{ instance_asg }}'
state: 'absent'
wait_timeout: 60
register: remove_instance
check_mode: true

- elb_classic_lb_info:
names: '{{ elb_name_1 }}'
register: elb_info_1
- elb_classic_lb_info:
names: '{{ elb_name_2 }}'
register: elb_info_2

- assert:
that:
- remove_instance is successful
- remove_instance is not changed
# Check the real state
- instance_asg not in elb_info_1.elbs[0].instances_inservice
- instance_asg not in elb_info_2.elbs[0].instances_inservice

- name: 'Remove an instance from ELB - idempotency'
elb_instance:
instance_id: '{{ instance_asg }}'
state: 'absent'
wait_timeout: 60
register: remove_instance

- elb_classic_lb_info:
names: '{{ elb_name_1 }}'
register: elb_info_1
- elb_classic_lb_info:
names: '{{ elb_name_2 }}'
register: elb_info_2

- assert:
that:
- remove_instance is successful
# XXX always returns the ELBs the ASG belongs to
# - remove_instance is not changed
# Check the real state
- instance_asg not in elb_info_1.elbs[0].instances_inservice
- instance_asg not in elb_info_2.elbs[0].instances_inservice
Loading

0 comments on commit 4e1c5db

Please sign in to comment.