From af6a28f177a3b782dade6cb0e88ffa6717f4addd Mon Sep 17 00:00:00 2001 From: Mandar Kulkarni Date: Mon, 24 Jan 2022 16:31:40 -0800 Subject: [PATCH] Add ec2_lc* integration tests (#824) Add ec2_lc* integration tests SUMMARY Added integration tests which exercise ec2_lc, ec2_lc_find, and ec2_lc_info. ISSUE TYPE Feature Pull Request ADDITIONAL INFORMATION ec2_lc (launch configurations) work with ASGs (auto-scaling groups) to define launch config for instances in the ASG. We have tests for ec2_asg that make use of ec2_lc but as it is slow already, it makes sense to have a dedicated test suite for ec2_lc. Reviewed-by: Alina Buzachis Reviewed-by: Mandar Kulkarni Reviewed-by: Jill R Reviewed-by: Markus Bergholz --- plugins/modules/ec2_lc.py | 2 +- tests/integration/targets/ec2_lc/aliases | 4 + .../targets/ec2_lc/defaults/main.yml | 7 + .../integration/targets/ec2_lc/meta/main.yml | 7 + .../targets/ec2_lc/tasks/env_cleanup.yml | 94 +++++++++ .../targets/ec2_lc/tasks/env_setup.yml | 64 ++++++ .../integration/targets/ec2_lc/tasks/main.yml | 192 ++++++++++++++++++ .../integration/targets/ec2_lc/vars/main.yml | 1 + 8 files changed, 370 insertions(+), 1 deletion(-) create mode 100644 tests/integration/targets/ec2_lc/aliases create mode 100644 tests/integration/targets/ec2_lc/defaults/main.yml create mode 100644 tests/integration/targets/ec2_lc/meta/main.yml create mode 100644 tests/integration/targets/ec2_lc/tasks/env_cleanup.yml create mode 100644 tests/integration/targets/ec2_lc/tasks/env_setup.yml create mode 100644 tests/integration/targets/ec2_lc/tasks/main.yml create mode 100644 tests/integration/targets/ec2_lc/vars/main.yml diff --git a/plugins/modules/ec2_lc.py b/plugins/modules/ec2_lc.py index 2cdf0463863..19f8dfe2972 100644 --- a/plugins/modules/ec2_lc.py +++ b/plugins/modules/ec2_lc.py @@ -478,7 +478,7 @@ def create_block_device_meta(module, volume): if 'no_device' in volume: return_object['NoDevice'] = volume.get('no_device') - if any(key in volume for key in ['snapshot', 'volume_size', 'volume_type', 'delete_on_termination', 'ips', 'encrypted']): + if any(key in volume for key in ['snapshot', 'volume_size', 'volume_type', 'delete_on_termination', 'iops', 'encrypted']): return_object['Ebs'] = {} if 'snapshot' in volume: diff --git a/tests/integration/targets/ec2_lc/aliases b/tests/integration/targets/ec2_lc/aliases new file mode 100644 index 00000000000..df93245119a --- /dev/null +++ b/tests/integration/targets/ec2_lc/aliases @@ -0,0 +1,4 @@ +cloud/aws + +ec2_lc_info +ec2_lc_find diff --git a/tests/integration/targets/ec2_lc/defaults/main.yml b/tests/integration/targets/ec2_lc/defaults/main.yml new file mode 100644 index 00000000000..fbbeb54fdac --- /dev/null +++ b/tests/integration/targets/ec2_lc/defaults/main.yml @@ -0,0 +1,7 @@ +--- +# defaults file for ec2_instance +ec2_instance_name: '{{ resource_prefix }}-node' +ec2_instance_owner: 'integration-run-{{ resource_prefix }}' +ec2_instance_type: t2.micro +ec2_ami_name: "amzn-ami-hvm*" +alarm_prefix: "ansible-test" diff --git a/tests/integration/targets/ec2_lc/meta/main.yml b/tests/integration/targets/ec2_lc/meta/main.yml new file mode 100644 index 00000000000..e3fef1b994a --- /dev/null +++ b/tests/integration/targets/ec2_lc/meta/main.yml @@ -0,0 +1,7 @@ +dependencies: + - prepare_tests + - setup_ec2 + - setup_ec2_facts + - role: setup_botocore_pip + vars: + boto3_version: "1.17.86" diff --git a/tests/integration/targets/ec2_lc/tasks/env_cleanup.yml b/tests/integration/targets/ec2_lc/tasks/env_cleanup.yml new file mode 100644 index 00000000000..9e5ae6a9380 --- /dev/null +++ b/tests/integration/targets/ec2_lc/tasks/env_cleanup.yml @@ -0,0 +1,94 @@ +- name: remove any instances in the test VPC + ec2_instance: + filters: + vpc_id: "{{ testing_vpc.vpc.id }}" + state: absent + register: removed + until: removed is not failed + ignore_errors: yes + retries: 10 + +- name: Get ENIs + ec2_eni_info: + filters: + vpc-id: "{{ testing_vpc.vpc.id }}" + register: enis + +- name: delete all ENIs + ec2_eni: + eni_id: "{{ item.id }}" + state: absent + until: removed is not failed + with_items: "{{ enis.network_interfaces }}" + ignore_errors: yes + retries: 10 + +- 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_a.subnet.id }}" + - "{{ testing_subnet_b.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 subnet A + ec2_vpc_subnet: + state: absent + vpc_id: "{{ testing_vpc.vpc.id }}" + cidr: 10.22.32.0/24 + register: removed + until: removed is not failed + ignore_errors: yes + retries: 10 + +- name: remove subnet B + ec2_vpc_subnet: + state: absent + vpc_id: "{{ testing_vpc.vpc.id }}" + cidr: 10.22.33.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.22.32.0/23 + state: absent + tags: + Name: Ansible Testing VPC + tenancy: default + register: removed + until: removed is not failed + ignore_errors: yes + retries: 10 diff --git a/tests/integration/targets/ec2_lc/tasks/env_setup.yml b/tests/integration/targets/ec2_lc/tasks/env_setup.yml new file mode 100644 index 00000000000..88f5bb6fe22 --- /dev/null +++ b/tests/integration/targets/ec2_lc/tasks/env_setup.yml @@ -0,0 +1,64 @@ +- name: Create VPC for use in testing + ec2_vpc_net: + name: "{{ resource_prefix }}-vpc" + cidr_block: 10.22.32.0/23 + tags: + Name: Ansible ec2_lc Testing VPC + tenancy: default + register: testing_vpc + +- name: Create internet gateway for use in testing + ec2_vpc_igw: + vpc_id: "{{ testing_vpc.vpc.id }}" + state: present + tags: + Name: Ansible ec2_lc Testing gateway + register: igw + +- name: Create default subnet in zone A + ec2_vpc_subnet: + state: present + vpc_id: "{{ testing_vpc.vpc.id }}" + cidr: 10.22.32.0/24 + az: "{{ aws_region }}a" + resource_tags: + Name: "{{ resource_prefix }}-subnet-a" + register: testing_subnet_a + +- name: Create secondary subnet in zone B + ec2_vpc_subnet: + state: present + vpc_id: "{{ testing_vpc.vpc.id }}" + cidr: 10.22.33.0/24 + az: "{{ aws_region }}b" + resource_tags: + Name: "{{ resource_prefix }}-subnet-b" + register: testing_subnet_b + +- 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_a.subnet.id }}" + - "{{ testing_subnet_b.subnet.id }}" + +- name: create a security group with the vpc + 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 diff --git a/tests/integration/targets/ec2_lc/tasks/main.yml b/tests/integration/targets/ec2_lc/tasks/main.yml new file mode 100644 index 00000000000..b8c255f4481 --- /dev/null +++ b/tests/integration/targets/ec2_lc/tasks/main.yml @@ -0,0 +1,192 @@ +- name: run ec2_lc 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: set up environment for testing. + include_tasks: env_setup.yml + + - name: Create launch configuration 1 + community.aws.ec2_lc: + name: '{{ resource_prefix }}-lc1' + image_id: '{{ ec2_ami_id }}' + assign_public_ip: yes + instance_type: '{{ ec2_instance_type }}' + security_groups: '{{ sg.group_id }}' + volumes: + - device_name: /dev/xvda + volume_size: 10 + volume_type: gp2 + delete_on_termination: true + register: lc_1_create + + - name: Gather information about launch configuration 1 + vars: + ansible_python_interpreter: "{{ botocore_virtualenv_interpreter }}" + community.aws.ec2_lc_info: + name: '{{ resource_prefix }}-lc1' + register: lc_1_info_result + + - assert: + that: + - lc_1_create is changed + - '"autoscaling:CreateLaunchConfiguration" in lc_1_create.resource_actions' + - '"throughput" not in lc_1_info_result.launch_configurations[0].block_device_mappings[0].ebs' + - lc_1_info_result.launch_configurations[0].block_device_mappings[0].ebs.volume_size == 10 + - lc_1_info_result.launch_configurations[0].block_device_mappings[0].ebs.volume_type == 'gp2' + - lc_1_info_result.launch_configurations[0].instance_type == 't2.micro' + + - name: Create launch configuration 1 - Idempotency + community.aws.ec2_lc: + name: '{{ resource_prefix }}-lc1' + image_id: '{{ ec2_ami_id }}' + assign_public_ip: yes + instance_type: '{{ ec2_instance_type }}' + security_groups: '{{ sg.group_id }}' + volumes: + - device_name: /dev/xvda + volume_size: 10 + volume_type: gp2 + delete_on_termination: true + register: lc_1_create_idem + + - assert: + that: + - lc_1_create_idem is not changed + - '"autoscaling:CreateLaunchConfiguration" not in lc_1_create_idem.resource_actions' + + - name: Create launch configuration 2 + community.aws.ec2_lc: + name: '{{ resource_prefix }}-lc2' + image_id: '{{ ec2_ami_id }}' + assign_public_ip: yes + instance_type: 't3.small' + security_groups: '{{ sg.group_id }}' + volumes: + - device_name: /dev/xvda + volume_size: 10 + volume_type: gp2 + delete_on_termination: true + register: lc_2_create + + - name: Gather information about launch configuration 2 + vars: + ansible_python_interpreter: "{{ botocore_virtualenv_interpreter }}" + community.aws.ec2_lc_info: + name: '{{ resource_prefix }}-lc2' + register: lc_2_info_result + + - assert: + that: + - lc_2_create is changed + - '"autoscaling:CreateLaunchConfiguration" in lc_2_create.resource_actions' + - '"throughput" not in lc_2_info_result.launch_configurations[0].block_device_mappings[0].ebs' + - lc_2_info_result.launch_configurations[0].block_device_mappings[0].ebs.volume_size == 10 + - lc_2_info_result.launch_configurations[0].block_device_mappings[0].ebs.volume_type == 'gp2' + - lc_2_info_result.launch_configurations[0].instance_type == 't3.small' + - '"autoscaling:CreateLaunchConfiguration" in lc_2_create.resource_actions' + + - name: Create launch configuration 2 - Idempotency + community.aws.ec2_lc: + name: '{{ resource_prefix }}-lc2' + image_id: '{{ ec2_ami_id }}' + assign_public_ip: yes + instance_type: '{{ ec2_instance_type }}' + security_groups: '{{ sg.group_id }}' + volumes: + - device_name: /dev/xvda + volume_size: 10 + volume_type: gp2 + delete_on_termination: true + register: lc_2_create_idem + + - assert: + that: + - lc_2_create_idem is not changed + - '"autoscaling:CreateLaunchConfiguration" not in lc_2_create_idem.resource_actions' + + - name: Search for the Launch Configurations that start with test resource_prefix + community.aws.ec2_lc_find: + name_regex: '{{ resource_prefix }}*' + sort_order: descending + register: lc_find_result + + - assert: + that: + - lc_find_result.results | length == 2 + - '"autoscaling:DescribeLaunchConfigurations" in lc_find_result.resource_actions' + + - name: Delete launch configuration 1 + community.aws.ec2_lc: + name: '{{ resource_prefix }}-lc1' + state: absent + register: lc_1_delete + + - assert: + that: + - lc_1_delete is changed + - '"autoscaling:DeleteLaunchConfiguration" in lc_1_delete.resource_actions' + + - name: Delete launch configuration 1 - Idempotency + community.aws.ec2_lc: + name: '{{ resource_prefix }}-lc1' + state: absent + register: lc_1_delete_idem + + - assert: + that: + - lc_1_delete_idem is not changed + - '"autoscaling:DeleteLaunchConfiguration" not in lc_1_delete_idem.resource_actions' + + - name: Gather information about launch configuration 1 + community.aws.ec2_lc_info: + name: '{{ resource_prefix }}-lc1' + register: lc_1_info_result + + - assert: + that: + - lc_1_info_result is not changed + - lc_1_info_result.launch_configurations | length == 0 + + - name: Delete launch configuration 2 + community.aws.ec2_lc: + name: '{{ resource_prefix }}-lc2' + state: absent + register: lc_2_delete + + - assert: + that: + - lc_2_delete is changed + - '"autoscaling:DeleteLaunchConfiguration" in lc_2_delete.resource_actions' + + - name: Delete launch configuration 2 - Idempotency + community.aws.ec2_lc: + name: '{{ resource_prefix }}-lc2' + state: absent + register: lc_2_delete_idem + + - assert: + that: + - lc_2_delete_idem is not changed + - '"autoscaling:DeleteLaunchConfiguration" not in lc_2_delete_idem.resource_actions' + + - name: Gather information about launch configuration 2 + community.aws.ec2_lc_info: + name: '{{ resource_prefix }}-lc2' + register: lc_2_info_result + + - assert: + that: + - lc_2_info_result is not changed + - lc_2_info_result.launch_configurations | length == 0 + + always: + + - include_tasks: env_cleanup.yml diff --git a/tests/integration/targets/ec2_lc/vars/main.yml b/tests/integration/targets/ec2_lc/vars/main.yml new file mode 100644 index 00000000000..ed97d539c09 --- /dev/null +++ b/tests/integration/targets/ec2_lc/vars/main.yml @@ -0,0 +1 @@ +---