Skip to content

Commit

Permalink
In integration tests use a common AMI rather than hard coded per test (
Browse files Browse the repository at this point in the history
…#1067) (#1080)

[PR #1067/3f434f3f backport][stable-3] In integration tests use a common AMI rather than hard coded per test

This is a backport of PR #1067 as merged into main (3f434f3).
Depends-On: #1068
SUMMARY
Rather than hard coding the AMIs on a per-test basis, use a common AMI defined in setup_ec2_facts
ISSUE TYPE

Feature Pull Request

COMPONENT NAME
ec2_asg_instance_refresh
ec2_asg_lifecycle_hook
ec2_asg_scheduled_action
ec2_launch_template
ec2_lc
ec2_metric_alarm
ec2_scaling_policy
elb_network_lb
elb_target
elb_target_info
ADDITIONAL INFORMATION

Reviewed-by: Markus Bergholz <git@osuv.de>
  • Loading branch information
patchback[bot] authored Apr 20, 2022
1 parent ba9b75f commit 5202b2c
Show file tree
Hide file tree
Showing 29 changed files with 85 additions and 162 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
---
# defaults file for ec2_asg
vpc_seed: '{{ tiny_prefix }}'
ec2_ami_name: 'amzn2-ami-hvm-2.*-x86_64-gp2'
subnet_a_cidr: '10.{{ 256 | random(seed=vpc_seed) }}.32.0/24'

default_resource_name: '{{ resource_prefix }}-asg-refresh'
short_resource_name: '{{ tiny_prefix }}-asg-refresh'

vpc_name: '{{ default_resource_name }}'
subnet_name: '{{ default_resource_name }}'
route_name: '{{ default_resource_name }}'
sg_name: '{{ default_resource_name }}'
asg_name: '{{ default_resource_name }}'
lc_name_1: '{{ default_resource_name }}-1'
lc_name_2: '{{ default_resource_name }}-2'
load_balancer_name: '{{ short_resource_name }}'
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dependencies:
- setup_ec2_facts
94 changes: 39 additions & 55 deletions tests/integration/targets/ec2_asg_instance_refresh/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,10 @@
- amazon.aws

block:

#NOTE: entire ASG setup is 'borrowed' from ec2_asg
- 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 }}'

- name: load balancer name has to be less than 32 characters
set_fact:
load_balancer_name: "{{ item }}-lb"
loop: "{{ resource_prefix | regex_findall('.{8}$') }}"

# 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"
name: "{{ vpc_name }}"
cidr_block: '{{ subnet_a_cidr }}'
tenancy: default
register: testing_vpc
Expand All @@ -48,14 +32,14 @@
cidr: '{{ subnet_a_cidr }}'
az: "{{ aws_region }}a"
resource_tags:
Name: "{{ resource_prefix }}-subnet"
Name: "{{ subnet_name }}"
register: testing_subnet

- name: create routing rules
ec2_vpc_route_table:
vpc_id: "{{ testing_vpc.vpc.id }}"
tags:
created: "{{ resource_prefix }}-route"
created: "{{ route_name }}"
routes:
- dest: 0.0.0.0/0
gateway_id: "{{ igw.gateway_id }}"
Expand All @@ -64,7 +48,7 @@

- name: create a security group with the vpc created in the ec2_setup
ec2_group:
name: "{{ resource_prefix }}-sg"
name: "{{ sg_name }}"
description: a security group for ansible tests
vpc_id: "{{ testing_vpc.vpc.id }}"
rules:
Expand All @@ -82,7 +66,7 @@
ec2_lc:
name: "{{ item }}"
assign_public_ip: true
image_id: "{{ ec2_ami_image }}"
image_id: "{{ ec2_ami_id }}"
user_data: |
package_upgrade: true
package_update: true
Expand All @@ -93,13 +77,13 @@
security_groups: "{{ sg.group_id }}"
instance_type: t3.micro
loop:
- "{{ resource_prefix }}-lc"
- "{{ resource_prefix }}-lc-2"
- "{{ lc_name_1 }}"
- "{{ lc_name_2 }}"

- name: launch asg and do not wait for instances to be deemed healthy (no ELB)
ec2_asg:
name: "{{ resource_prefix }}-asg"
launch_config_name: "{{ resource_prefix }}-lc"
name: "{{ asg_name }}"
launch_config_name: "{{ lc_name_1 }}"
desired_capacity: 1
min_size: 1
max_size: 1
Expand All @@ -116,7 +100,7 @@

- name: test invalid cancelation - V1 - (pre-refresh)
ec2_asg_instance_refresh:
name: "{{ resource_prefix }}-asg"
name: "{{ asg_name }}"
state: "cancelled"
ignore_errors: yes
register: result
Expand All @@ -127,7 +111,7 @@

- name: test starting a refresh with a valid ASG name - check_mode
ec2_asg_instance_refresh:
name: "{{ resource_prefix }}-asg"
name: "{{ asg_name }}"
state: "started"
check_mode: true
register: output
Expand All @@ -140,7 +124,7 @@

- name: test starting a refresh with a valid ASG name
ec2_asg_instance_refresh:
name: "{{ resource_prefix }}-asg"
name: "{{ asg_name }}"
state: "started"
register: output

Expand All @@ -150,7 +134,7 @@

- name: test starting a refresh with a valid ASG name - Idempotent
ec2_asg_instance_refresh:
name: "{{ resource_prefix }}-asg"
name: "{{ asg_name }}"
state: "started"
ignore_errors: true
register: output
Expand All @@ -162,7 +146,7 @@

- name: test starting a refresh with a valid ASG name - Idempotent (check_mode)
ec2_asg_instance_refresh:
name: "{{ resource_prefix }}-asg"
name: "{{ asg_name }}"
state: "started"
ignore_errors: true
check_mode: true
Expand All @@ -187,7 +171,7 @@

- name: test canceling a refresh with an ASG name - check_mode
ec2_asg_instance_refresh:
name: "{{ resource_prefix }}-asg"
name: "{{ asg_name }}"
state: "cancelled"
check_mode: true
register: output
Expand All @@ -200,7 +184,7 @@

- name: test canceling a refresh with an ASG name
ec2_asg_instance_refresh:
name: "{{ resource_prefix }}-asg"
name: "{{ asg_name }}"
state: "cancelled"
register: output

Expand All @@ -210,7 +194,7 @@

- name: test canceling a refresh with a ASG name - Idempotent
ec2_asg_instance_refresh:
name: "{{ resource_prefix }}-asg"
name: "{{ asg_name }}"
state: "cancelled"
ignore_errors: yes
register: output
Expand All @@ -221,7 +205,7 @@

- name: test cancelling a refresh with a valid ASG name - Idempotent (check_mode)
ec2_asg_instance_refresh:
name: "{{ resource_prefix }}-asg"
name: "{{ asg_name }}"
state: "cancelled"
ignore_errors: true
check_mode: true
Expand All @@ -234,7 +218,7 @@

- name: test starting a refresh with an ASG name and preferences dict
ec2_asg_instance_refresh:
name: "{{ resource_prefix }}-asg"
name: "{{ asg_name }}"
state: "started"
preferences:
min_healthy_percentage: 10
Expand All @@ -249,7 +233,7 @@

- name: re-test canceling a refresh with an ASG name
ec2_asg_instance_refresh:
name: "{{ resource_prefix }}-asg"
name: "{{ asg_name }}"
state: "cancelled"
register: output

Expand All @@ -259,7 +243,7 @@

- name: test valid start - V1 - (with preferences missing instance_warmup)
ec2_asg_instance_refresh:
name: "{{ resource_prefix }}-asg"
name: "{{ asg_name }}"
state: "started"
preferences:
min_healthy_percentage: 10
Expand All @@ -274,7 +258,7 @@

- name: re-test canceling a refresh with an ASG name
ec2_asg_instance_refresh:
name: "{{ resource_prefix }}-asg"
name: "{{ asg_name }}"
state: "cancelled"
register: output

Expand All @@ -284,7 +268,7 @@

- name: test valid start - V2 - (with preferences missing min_healthy_percentage)
ec2_asg_instance_refresh:
name: "{{ resource_prefix }}-asg"
name: "{{ asg_name }}"
state: "started"
preferences:
instance_warmup: 10
Expand All @@ -299,7 +283,7 @@

- name: test invalid cancelation - V2 - (with preferences)
ec2_asg_instance_refresh:
name: "{{ resource_prefix }}-asg"
name: "{{ asg_name }}"
state: "cancelled"
preferences:
min_healthy_percentage: 10
Expand All @@ -319,7 +303,7 @@

- name: test getting info for an ASG name
ec2_asg_instance_refresh_info:
name: "{{ resource_prefix }}-asg"
name: "{{ asg_name }}"
region: "{{ aws_region }}"
ignore_errors: yes
register: output
Expand All @@ -332,7 +316,7 @@

- name: test using fake refresh ID
ec2_asg_instance_refresh_info:
name: "{{ resource_prefix }}-asg"
name: "{{ asg_name }}"
ids: ['0e367f58-blabla-bla-bla-ca870dc5dbfe']
ignore_errors: yes
register: output
Expand All @@ -343,7 +327,7 @@

- name: test using a real refresh ID
ec2_asg_instance_refresh_info:
name: "{{ resource_prefix }}-asg"
name: "{{ asg_name }}"
ids: [ '{{ refreshout.instance_refreshes.instance_refresh_id }}' ]
ignore_errors: yes
register: output
Expand All @@ -364,7 +348,7 @@

- name: assert that the correct number of records are returned
ec2_asg_instance_refresh_info:
name: "{{ resource_prefix }}-asg"
name: "{{ asg_name }}"
ignore_errors: yes
register: output

Expand All @@ -374,7 +358,7 @@

- name: assert that valid message with fake-token is returned
ec2_asg_instance_refresh_info:
name: "{{ resource_prefix }}-asg"
name: "{{ asg_name }}"
next_token: "fake-token-123"
ignore_errors: yes
register: output
Expand All @@ -385,7 +369,7 @@

- name: assert that max records=1 returns no more than one record
ec2_asg_instance_refresh_info:
name: "{{ resource_prefix }}-asg"
name: "{{ asg_name }}"
max_records: 1
ignore_errors: yes
register: output
Expand All @@ -396,7 +380,7 @@

- name: assert that valid message with real-token is returned
ec2_asg_instance_refresh_info:
name: "{{ resource_prefix }}-asg"
name: "{{ asg_name }}"
next_token: "{{ output.next_token }}"
ignore_errors: yes
register: output
Expand All @@ -407,7 +391,7 @@

- name: test using both real nextToken and max_records=1
ec2_asg_instance_refresh_info:
name: "{{ resource_prefix }}-asg"
name: "{{ asg_name }}"
max_records: 1
next_token: "{{ output.next_token }}"
ignore_errors: yes
Expand All @@ -421,7 +405,7 @@

- name: kill asg
ec2_asg:
name: "{{ resource_prefix }}-asg"
name: "{{ asg_name }}"
state: absent
register: removed
until: removed is not failed
Expand Down Expand Up @@ -457,15 +441,15 @@

- name: remove launch configs
ec2_lc:
name: "{{ resource_prefix }}-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"
- "{{ lc_name_1 }}"
- "{{ lc_name_2 }}"

- name: delete launch template
ec2_launch_template:
Expand All @@ -478,7 +462,7 @@

- name: remove the security group
ec2_group:
name: "{{ resource_prefix }}-sg"
name: "{{ sg_name }}"
description: a security group for ansible tests
vpc_id: "{{ testing_vpc.vpc.id }}"
state: absent
Expand All @@ -492,7 +476,7 @@
state: absent
vpc_id: "{{ testing_vpc.vpc.id }}"
tags:
created: "{{ resource_prefix }}-route"
created: "{{ route_name }}"
routes:
- dest: 0.0.0.0/0
gateway_id: "{{ igw.gateway_id }}"
Expand Down Expand Up @@ -524,7 +508,7 @@

- name: remove the VPC
ec2_vpc_net:
name: "{{ resource_prefix }}-vpc"
name: "{{ vpc_name }}"
cidr_block: '{{ subnet_a_cidr }}'
state: absent
register: removed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

- name: try to cancel pre-loop
ec2_asg_instance_refresh:
name: "{{ resource_prefix }}-asg"
name: "{{ asg_name }}"
state: "cancelled"
ignore_errors: yes

- name: test starting a refresh with an ASG name
ec2_asg_instance_refresh:
name: "{{ resource_prefix }}-asg"
name: "{{ asg_name }}"
state: "started"
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
Expand All @@ -21,7 +21,7 @@

- name: test cancelling a refresh with an ASG name
ec2_asg_instance_refresh:
name: "{{ resource_prefix }}-asg"
name: "{{ asg_name }}"
state: "cancelled"
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
---
# Amazon Linux 2 AMI 2.0.20211005.0 x86_64 HVM gp2
ec2_ami_name: "amzn2-ami-hvm-2.0.20211005.0-x86_64-gp2"
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
dependencies:
- role: setup_ec2_facts
- role: setup_botocore_pip
vars:
botocore_version: "1.20.24"
Loading

0 comments on commit 5202b2c

Please sign in to comment.