Skip to content

Commit

Permalink
Reduce execution time for integration test inventory_aws_ec2
Browse files Browse the repository at this point in the history
  • Loading branch information
abikouo committed Nov 9, 2022
1 parent 5a0b603 commit 80f56b4
Show file tree
Hide file tree
Showing 21 changed files with 147 additions and 367 deletions.
1 change: 0 additions & 1 deletion plugins/inventory/aws_ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,6 @@ def _get_instances_by_region(self, regions, filters, strict_permissions):
:return A list of instance dictionaries
'''
all_instances = []

# By default find non-terminated/terminating instances
if not any(f['Name'] == 'instance-state-name' for f in filters):
filters.append({'Name': 'instance-state-name', 'Values': ['running', 'pending', 'stopping', 'stopped']})
Expand Down
5 changes: 4 additions & 1 deletion plugins/plugin_utils/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,10 @@ def _boto3_conn(self, regions, resource):
credentials = self._get_credentials()
iam_role_arn = self.iam_role_arn

regions = self._boto3_regions(credentials, iam_role_arn, resource)
if not regions:
# list regions as none was provided
regions = self._boto3_regions(credentials, iam_role_arn, resource)

# I give up, now you MUST give me regions
if not regions:
self.fail_aws('Unable to get regions list from available methods, you must specify the "regions" option to continue.')
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/targets/inventory_aws_ec2/aliases
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
time=45m
time=10m

cloud/aws
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
- hosts: 127.0.0.1
connection: local
gather_facts: no

collections:
- amazon.aws
- community.aws

vars_files:
- vars/main.yml

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 }}'

tasks:
- include_tasks: "{{ task }}.yml"
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,7 @@
region: '{{ aws_region }}'
block:

# Create VPC, subnet, security group, and find image_id to create instance

- include_tasks: setup.yml
# - pause:
# seconds: 240

- name: assert group was populated with inventory but is empty
assert:
that:
- "'aws_ec2' in groups"
- "not groups.aws_ec2"

# Create new host, add it to inventory and then terminate it without updating the cache

- name: create a new host
ec2_instance:
image_id: '{{ image_id }}'
name: '{{ resource_prefix }}'
instance_type: t2.micro
wait: no
security_groups: '{{ sg_id }}'
vpc_subnet_id: '{{ subnet_id }}'
register: setup_instance

- meta: refresh_inventory

always:

- name: remove setup ec2 instance
ec2_instance:
instance_type: t2.micro
instance_ids: '{{ setup_instance.instance_ids }}'
state: absent
name: '{{ resource_prefix }}'
security_groups: '{{ sg_id }}'
vpc_subnet_id: '{{ subnet_id }}'
ignore_errors: yes
when: setup_instance is defined
- debug:
var: groups

- include_tasks: tear_down.yml
11 changes: 10 additions & 1 deletion tests/integration/targets/inventory_aws_ec2/playbooks/setup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
- name: create a subnet to use for creating an ec2 instance
ec2_vpc_subnet:
az: '{{ aws_region }}a'
tags: '{{ resource_prefix }}_setup'
vpc_id: '{{ setup_vpc.vpc.id }}'
cidr: '{{ subnet_cidr }}'
state: present
Expand All @@ -50,3 +49,13 @@

- set_fact:
sg_id: '{{ setup_sg.group_id }}'

- name: Create ec2 instance
ec2_instance:
image_id: '{{ image_id }}'
name: '{{ resource_prefix }}'
instance_type: t2.micro
security_groups: '{{ sg_id }}'
vpc_subnet_id: '{{ subnet_id }}'
wait: no
register: setup_instance
78 changes: 51 additions & 27 deletions tests/integration/targets/inventory_aws_ec2/playbooks/tear_down.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,54 @@
vpc_cidr: '10.{{ 256 | random(seed=resource_prefix) }}.0.0/16'
subnet_cidr: '10.{{ 256 | random(seed=resource_prefix) }}.0.0/24'

- name: remove setup security group
ec2_security_group:
name: '{{ resource_prefix }}_setup'
description: 'created by Ansible integration tests'
state: absent
vpc_id: '{{ vpc_id }}'
ignore_errors: yes

- name: remove setup subnet
ec2_vpc_subnet:
az: '{{ aws_region }}a'
tags: '{{ resource_prefix }}_setup'
vpc_id: '{{ vpc_id }}'
cidr: '{{ subnet_cidr }}'
state: absent
resource_tags:
Name: '{{ resource_prefix }}_setup'
ignore_errors: yes

- name: remove setup VPC
ec2_vpc_net:
cidr_block: '{{ vpc_cidr }}'
state: absent
name: '{{ resource_prefix }}_setup'
resource_tags:
Name: '{{ resource_prefix }}_setup'
ignore_errors: yes
- name: describe vpc
ec2_vpc_net_info:
filters:
tag:Name: '{{ resource_prefix }}_setup'
register: vpc_info

- block:
- set_fact:
vpc_id: "{{ vpc_info.vpcs.0.vpc_id }}"

- name: list existing instances
ec2_instance_info:
filters:
vpc-id: "{{ vpc_id }}"
register: existing

- name: remove ec2 instances
ec2_instance:
instance_ids: "{{ existing.instances | map(attribute='instance_id') | list }}"
wait: yes
state: absent

- name: remove setup security group
ec2_security_group:
name: '{{ resource_prefix }}_setup'
description: 'created by Ansible integration tests'
state: absent
vpc_id: '{{ vpc_id }}'
ignore_errors: yes

- name: remove setup subnet
ec2_vpc_subnet:
az: '{{ aws_region }}a'
tags: '{{ resource_prefix }}_setup'
vpc_id: '{{ vpc_id }}'
cidr: '{{ subnet_cidr }}'
state: absent
resource_tags:
Name: '{{ resource_prefix }}_setup'
ignore_errors: yes

- name: remove setup VPC
ec2_vpc_net:
cidr_block: '{{ vpc_cidr }}'
state: absent
name: '{{ resource_prefix }}_setup'
resource_tags:
Name: '{{ resource_prefix }}_setup'
ignore_errors: yes

when: vpc_info.vpcs | length > 0
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
assert:
that:
- "'aws_ec2' in groups"
- "groups.aws_ec2 | length == 1"
- "groups.aws_ec2 | length > 0"

- meta: refresh_inventory

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,75 +4,9 @@
gather_facts: no
environment: "{{ ansible_test.environment }}"
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 }}'

block:

# Create VPC, subnet, security group, and find image_id to create instance

- include_tasks: setup.yml

- name: assert group was populated with inventory but is empty
assert:
that:
- "'aws_ec2' in groups"
- "not groups.aws_ec2"

# Create new host, refresh inventory, remove host, refresh inventory

- name: create a new host
ec2_instance:
image_id: '{{ image_id }}'
name: '{{ resource_prefix }}'
instance_type: t2.micro
security_groups: '{{ sg_id }}'
vpc_subnet_id: '{{ subnet_id }}'
wait: no
register: setup_instance

- meta: refresh_inventory

- name: assert group was populated with inventory and is no longer empty
assert:
that:
- "'aws_ec2' in groups"
- "groups.aws_ec2 | length == 1"
- "groups.aws_ec2.0 == '{{ resource_prefix }}'"

- name: remove setup ec2 instance
ec2_instance:
instance_type: t2.micro
instance_ids: '{{ setup_instance.instance_ids }}'
state: absent
name: '{{ resource_prefix }}'
security_groups: '{{ sg_id }}'
vpc_subnet_id: '{{ subnet_id }}'

- meta: refresh_inventory

- name: assert group was populated with inventory but is empty
assert:
that:
- "'aws_ec2' in groups"
- "not groups.aws_ec2"

always:

- name: remove setup ec2 instance
ec2_instance:
instance_type: t2.micro
instance_ids: '{{ setup_instance.instance_ids }}'
state: absent
name: '{{ resource_prefix }}'
security_groups: '{{ sg_id }}'
vpc_subnet_id: '{{ subnet_id }}'
ignore_errors: yes
when: setup_instance is defined

- include_tasks: tear_down.yml
- name: assert group was populated with inventory and is no longer empty
assert:
that:
- "'aws_ec2' in groups"
- "groups.aws_ec2 | length == 1"
- "groups.aws_ec2.0 == '{{ resource_prefix }}'"
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
name: '{{ resource_prefix }}'
tags:
OtherTag: value
purge_tags: true
instance_type: t2.micro
security_groups: '{{ sg_id }}'
vpc_subnet_id: '{{ subnet_id }}'
wait: no
wait: false
register: setup_instance

- meta: refresh_inventory
Expand All @@ -39,18 +40,3 @@
assert:
that:
- expected_hostname in hostvars

always:

- name: remove setup ec2 instance
ec2_instance:
instance_type: t2.micro
instance_ids: '{{ setup_instance.instance_ids }}'
state: absent
name: '{{ resource_prefix }}'
security_groups: "{{ sg_id }}"
vpc_subnet_id: "{{ subnet_id }}"
ignore_errors: yes
when: setup_instance is defined

- include_tasks: tear_down.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@
tags:
tag1: value1
tag2: value2
purge_tags: true
instance_type: t2.micro
security_groups: '{{ sg_id }}'
vpc_subnet_id: '{{ subnet_id }}'
wait: no
wait: false
register: setup_instance

- meta: refresh_inventory
Expand All @@ -52,18 +53,3 @@
- "groups.arch_x86_64 | length == 1"
- "groups.tag_with_name_key | length == 1"
- vars.hostvars[groups.aws_ec2.0]['test_compose_var_sum'] == 'value1value2'

always:

- name: remove setup ec2 instance
ec2_instance:
instance_type: t2.micro
instance_ids: '{{ setup_instance.instance_ids }}'
state: absent
name: '{{ resource_prefix }}'
security_groups: "{{ sg_id }}"
vpc_subnet_id: "{{ subnet_id }}"
ignore_errors: yes
when: setup_instance is defined

- include_tasks: tear_down.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
tags:
Tag1: Test1
Tag2: Test2
purge_tags: true
instance_type: t2.micro
security_groups: '{{ sg_id }}'
vpc_subnet_id: '{{ subnet_id }}'
Expand All @@ -45,18 +46,3 @@
- "'Tag2_Test2' not in groups['aws_ec2']"
- "'Tag1_Test1' in hostvars"
- "'Tag2_Test2' not in hostvars"

always:

- name: remove ec2 instance
ec2_instance:
instance_type: t2.micro
instance_ids: '{{ setup_instance.instance_ids }}'
state: absent
name: '{{ resource_prefix }}'
security_groups: "{{ sg_id }}"
vpc_subnet_id: "{{ subnet_id }}"
ignore_errors: true
when: setup_instance is defined

- include_tasks: tear_down.yml
Loading

0 comments on commit 80f56b4

Please sign in to comment.