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

Create initial test suite for ec2_metadata_facts #212

Merged
merged 6 commits into from
Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion tests/integration/targets/ec2_metadata_facts/aliases
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
non_local
cloud/aws
shippable/aws/group2
shippable/aws/group4

This file was deleted.

3 changes: 0 additions & 3 deletions tests/integration/targets/ec2_metadata_facts/meta/main.yml

This file was deleted.

141 changes: 141 additions & 0 deletions tests/integration/targets/ec2_metadata_facts/playbooks/setup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
---
- 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 }}"

hosts: localhost

collections:
- community.aws

vars:
vpc_name: '{{ resource_prefix }}-vpc'
vpc_seed: '{{ resource_prefix }}'
vpc_cidr: '10.{{ 256 | random(seed=vpc_seed) }}.0.0/16'
subnet_cidr: '10.{{ 256 | random(seed=vpc_seed) }}.32.0/24'
ec2_ami_name: 'amzn2-ami-hvm-2.*-x86_64-gp2'
sshkey_file: '{{ resource_prefix }}_key'

tasks:

- name: Create an ssh key
shell: echo 'y' | ssh-keygen -P '' -f ../{{ sshkey_file }}

- name: Get available AZs
aws_az_info:
filters:
region-name: "{{ aws_region }}"
register: az_info

- name: Pick an AZ
set_fact:
availability_zone: "{{ az_info['availability_zones'][0]['zone_name'] }}"

# ============================================================
- name: create a VPC
ec2_vpc_net:
name: "{{ resource_prefix }}-vpc"
state: present
cidr_block: "{{ vpc_cidr }}"
tags:
Name: "{{ resource_prefix }}-vpc"
Description: "Created by ansible-test"
register: vpc_result

- set_fact:
vpc_id: "{{ vpc_result.vpc.id }}"

- name: create an internet gateway
ec2_vpc_igw:
vpc_id: "{{ vpc_id }}"
state: present
tags:
"Name": "{{ resource_prefix }}"
register: igw_result

- name: create a subnet
ec2_vpc_subnet:
cidr: "{{ vpc_cidr }}"
az: "{{ availability_zone }}"
vpc_id: "{{ vpc_id }}"
tags:
Name: "{{ resource_prefix }}-vpc"
Description: "Created by ansible-test"
state: present
register: vpc_subnet_result

- name: create a public route table
ec2_vpc_route_table:
vpc_id: "{{ vpc_id }}"
tags:
"Name": "{{ resource_prefix }}"
subnets:
- "{{ vpc_subnet_result.subnet.id }}"
routes:
- dest: 0.0.0.0/0
gateway_id: "{{ igw_result.gateway_id }}"
register: public_route_table

- name: create a security group
ec2_group:
name: "{{ resource_prefix }}-sg"
description: "Created by {{ resource_prefix }}"
rules:
- proto: tcp
ports: 22
cidr_ip: 0.0.0.0/0
- proto: icmp
from_port: -1
to_port: -1
state: present
vpc_id: "{{ vpc_result.vpc.id }}"
register: vpc_sg_result

- name: Create a key
ec2_key:
name: '{{ resource_prefix }}'
key_material: "{{ lookup('file', '../' ~ sshkey_file ~ '.pub') }}"
state: present
register: ec2_key_result

- name: Get a list of images
ec2_ami_info:
filters:
owner-alias: amazon
name: "amzn2-ami-minimal-hvm-*"
description: "Amazon Linux 2 AMI *"
register: images_info

- name: Set facts to simplify use of extra resources
set_fact:
vpc_subnet_id: "{{ vpc_subnet_result.subnet.id }}"
vpc_sg_id: "{{ vpc_sg_result.group_id }}"
vpc_igw_id: "{{ igw_result.gateway_id }}"
vpc_route_table_id: "{{ public_route_table.route_table.id }}"
image_id: "{{ images_info.images | sort(attribute='creation_date') | reverse | first | json_query('image_id') }}"
ec2_key_name: "{{ ec2_key_result.key.name }}"

- name: Create an instance to test with
ec2_instance:
name: "{{ resource_prefix }}-ec2-metadata-facts"
image_id: "{{ image_id }}"
vpc_subnet_id: "{{ vpc_subnet_id }}"
security_group: "{{ vpc_sg_id }}"
instance_type: t2.micro
key_name: "{{ ec2_key_name }}"
network:
assign_public_ip: true
wait: true
wait_timeout: 300
register: ec2_instance

- set_fact:
ec2_instance_id: "{{ ec2_instance.instances[0].instance_id }}"

- name: Create inventory file
template:
src: ../templates/inventory.j2
dest: ../inventory
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
- 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 }}"

hosts: localhost

collections:
- community.aws

tasks:
# ============================================================

- name: terminate the instance
ec2_instance:
state: absent
instance_ids:
- "{{ ec2_instance_id }}"
wait: True
ignore_errors: true
retries: 5

- name: remove ssh key
ec2_key:
name: "{{ ec2_key_name }}"
state: absent
ignore_errors: true

- name: remove the security group
ec2_group:
group_id: "{{ vpc_sg_id }}"
state: absent
ignore_errors: true
retries: 5

- name: remove the public route table
ec2_vpc_route_table:
vpc_id: "{{ vpc_id }}"
route_table_id: "{{ vpc_route_table_id }}"
lookup: id
state: absent
ignore_errors: true
retries: 5

- name: remove the subnet
ec2_vpc_subnet:
cidr: "{{ vpc_cidr }}"
az: "{{ availability_zone }}"
vpc_id: "{{ vpc_id }}"
state: absent
ignore_errors: true
retries: 5

- name: remove the internet gateway
ec2_vpc_igw:
vpc_id: "{{ vpc_id }}"
state: absent
ignore_errors: true
retries: 5

- name: remove the VPC
ec2_vpc_net:
name: "{{ resource_prefix }}-vpc"
cidr_block: "{{ vpc_cidr }}"
state: absent
ignore_errors: true
retries: 5
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
- hosts: testhost
tasks:

- name: Wait for EC2 to be available
wait_for_connection:

- amazon.aws.ec2_metadata_facts:

- name: Assert initial metadata for the instance
assert:
that:
- ansible_ec2_ami_id == image_id
- ansible_ec2_placement_availability_zone == "{{ availability_zone }}"
- ansible_ec2_security_groups == "{{ resource_prefix }}-sg"
- ansible_ec2_user_data == "None"
22 changes: 22 additions & 0 deletions tests/integration/targets/ec2_metadata_facts/runme.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash

set -eux
export ANSIBLE_HOST_KEY_CHECKING=False
export ANSIBLE_SSH_ARGS='-o UserKnownHostsFile=/dev/null'

CMD_ARGS=("$@")

# Destroy Environment
cleanup() {
ansible-playbook playbooks/teardown.yml -i inventory -c local "${CMD_ARGS[@]}"
}
trap "cleanup" EXIT

# create test resources and inventory
ansible-playbook playbooks/setup.yml -c local "$@"

tremble marked this conversation as resolved.
Show resolved Hide resolved
# test ec2_instance_metadata
ansible-playbook playbooks/test_metadata.yml -i inventory \
-e local_tmp=/tmp/ansible-local \
-e remote_tmp=/tmp/ansible-remote \
"$@"
2 changes: 0 additions & 2 deletions tests/integration/targets/ec2_metadata_facts/tasks/main.yml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[testhost]
"{{ ec2_instance.instances[0].public_ip_address }}"

[testhost:vars]
ansible_user=ec2-user
ansible_ssh_private_key_file="{{ sshkey_file }}"
ansible_python_interpreter=/usr/bin/env python

[all:vars]
# Template vars that will need to be used in used in tests and teardown
vpc_id="{{ vpc_id }}"
vpc_subnet_id="{{ vpc_subnet_id }}"
vpc_sg_id="{{ vpc_sg_id }}"
vpc_cidr="{{ vpc_cidr }}"
vpc_igw="{{ vpc_igw_id }}"
vpc_route_table_id="{{ vpc_route_table_id }}"
ec2_key_name="{{ ec2_key_name }}"
availability_zone="{{ availability_zone }}"
image_id="{{ image_id }}"
ec2_instance_id="{{ ec2_instance_id }}"
2 changes: 0 additions & 2 deletions tests/integration/targets/ec2_metadata_facts/vars/main.yml

This file was deleted.