Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ec2_asg: Restructure integration tests to reduce runtime #1036

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions tests/integration/targets/ec2_asg/inventory
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[tests]
create_update_delete
tag_operations
instance_detach

[all:vars]
ansible_connection=local
ansible_python_interpreter="{{ ansible_playbook_python }}"
40 changes: 40 additions & 0 deletions tests/integration/targets/ec2_asg/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
# Beware: most of our tests here are run in parallel.
# To add new tests you'll need to add a new host to the inventory and a matching
# '{{ inventory_hostname }}'.yml file in roles/ec2_asg/tasks/


# Prepare the VPC and figure out which AMI to use
- hosts: all
gather_facts: no
tasks:
- 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 }}"
vars:
# We can't just use "run_once" because the facts don't propagate when
# running an 'include' that was run_once
setup_run_once: yes
block:
- include_role:
name: 'ec2_asg'
tasks_from: env_setup.yml
rescue:
- include_role:
name: 'ec2_asg'
tasks_from: env_cleanup.yml
run_once: yes
- fail:
msg: 'Environment preparation failed'
run_once: yes

# VPC should get cleaned up once all hosts have run
- hosts: all
gather_facts: no
strategy: free
serial: 6
roles:
- ec2_asg
Original file line number Diff line number Diff line change
Expand Up @@ -9,98 +9,6 @@

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

- name: test without specifying required module options
ec2_asg:
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
security_token: "{{ security_token | default(omit) }}"
ignore_errors: yes
register: result

- name: assert name is a required module option
assert:
that:
- "result.msg == 'missing required arguments: name'"
mandar242 marked this conversation as resolved.
Show resolved Hide resolved

- name: Run ec2_asg integration 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: Find AMI to use
ec2_ami_info:
owners: 'amazon'
filters:
name: '{{ ec2_ami_name }}'
register: ec2_amis
- set_fact:
ec2_ami_image: '{{ ec2_amis.images[0].image_id }}'

# Set up the testing dependencies: VPC, subnet, security group, and two launch configurations
- name: Create VPC for use in testing
ec2_vpc_net:
name: "{{ resource_prefix }}-vpc"
cidr_block: 10.55.77.0/24
tenancy: default
register: testing_vpc

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

- name: Create subnet for use in testing
ec2_vpc_subnet:
state: present
vpc_id: "{{ testing_vpc.vpc.id }}"
cidr: 10.55.77.0/24
az: "{{ aws_region }}a"
resource_tags:
Name: "{{ resource_prefix }}-subnet"
register: testing_subnet

- 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.subnet.id }}"

- name: create a security group with the vpc created in the ec2_setup
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

- include_tasks: tag_operations.yml

- include_tasks: instance_detach.yml

- name: ensure launch configs exist
ec2_lc:
name: "{{ item }}"
Expand Down Expand Up @@ -670,132 +578,3 @@
- "output.target_group_arns | length == 1"
- "output.target_group_arns[0] == out_tg1.target_group_arn"
- "output.changed == false"

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

always:

- name: kill asg
ec2_asg:
name: "{{ resource_prefix }}-asg"
state: absent
register: removed
until: removed is not failed
ignore_errors: yes
retries: 10

# Remove the testing dependencies
- name: remove target group
elb_target_group:
name: "{{ item }}"
state: absent
register: removed
until: removed is not failed
ignore_errors: yes
retries: 10
loop:
- "{{ tg1_name }}"
- "{{ tg2_name }}"

- name: remove the load balancer
ec2_elb_lb:
name: "{{ load_balancer_name }}"
state: absent
security_group_ids:
- "{{ sg.group_id }}"
subnets: "{{ testing_subnet.subnet.id }}"
wait: yes
connection_draining_timeout: 60
listeners:
- protocol: http
load_balancer_port: 80
instance_port: 80
health_check:
ping_protocol: tcp
ping_port: 80
ping_path: "/"
response_timeout: 5
interval: 10
unhealthy_threshold: 4
healthy_threshold: 2
register: removed
until: removed is not failed
ignore_errors: yes
retries: 10

- name: remove launch configs
ec2_lc:
name: "{{ item }}"
state: absent
register: removed
until: removed is not failed
ignore_errors: yes
retries: 10
loop:
- "{{ resource_prefix }}-lc"
- "{{ resource_prefix }}-lc-2"

- name: delete launch template
ec2_launch_template:
name: "{{ resource_prefix }}-lt"
state: absent
register: del_lt
retries: 10
until: del_lt is not failed
ignore_errors: true

- 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.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 the subnet
ec2_vpc_subnet:
state: absent
vpc_id: "{{ testing_vpc.vpc.id }}"
cidr: 10.55.77.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.55.77.0/24
state: absent
register: removed
until: removed is not failed
ignore_errors: yes
retries: 10
Loading