diff --git a/test/integration/cloud-config-aws.ini.template b/test/integration/cloud-config-aws.ini.template index aa5bd1ad4d5..88b9fea63e5 100644 --- a/test/integration/cloud-config-aws.ini.template +++ b/test/integration/cloud-config-aws.ini.template @@ -16,6 +16,10 @@ aws_access_key: @ACCESS_KEY aws_secret_key: @SECRET_KEY security_token: @SECURITY_TOKEN aws_region: @REGION +# aws_cleanup controls whether the environment is cleaned up after tests have completed +# This only applies to tests that have a cleanup stage +# Defaults to true when using this template +# aws_cleanup: true # aliases for backwards compatibility with older integration test playbooks ec2_access_key: {{ aws_access_key }} ec2_secret_key: {{ aws_secret_key }} diff --git a/test/integration/targets/ec2_instance/aliases b/test/integration/targets/ec2_instance/aliases index 56927195182..6e3860bee23 100644 --- a/test/integration/targets/ec2_instance/aliases +++ b/test/integration/targets/ec2_instance/aliases @@ -1,2 +1,2 @@ cloud/aws -unsupported +shippable/aws/group2 diff --git a/test/integration/targets/ec2_instance/defaults/main.yml b/test/integration/targets/ec2_instance/defaults/main.yml new file mode 100644 index 00000000000..61383708144 --- /dev/null +++ b/test/integration/targets/ec2_instance/defaults/main.yml @@ -0,0 +1,23 @@ +--- +# defaults file for ec2_instance +ec2_instance_name: '{{ resource_prefix }}-node' +ec2_instance_owner: 'integration-run-{{ resource_prefix }}' +ec2_ami_image: + # Amazon Linux - collected 2019-05-10 + ap-northeast-1: ami-086063d8c95bfa211 + ap-northeast-2: ami-04a1d511fd8937540 + ap-south-1: ami-0d1ca7565b637e9a5 + ap-southeast-1: ami-07dd66a9764fbb937 + ap-southeast-2: ami-0ca7144d77a93c7a8 + ca-central-1: ami-0e5e1c6b373fed93b + eu-central-1: ami-05ba799ee5fc165db + eu-west-1: ami-055ff4f3e62e8e65e + eu-west-2: ami-072edd63fd2b95ca7 + sa-east-1: ami-0c0698b4027cd5931 + us-east-1: ami-0771c28c5580e5716 + us-east-2: ami-063225b63017efd17 + us-west-1: ami-04abfbe0050a1ef1d + us-west-2: ami-027c5e2ccf2970def + +# We need to use ENA enabled AMIs to get EBS optimized instances. +ec2_ebs_optimized_ami_image: "{{ ec2_ami_image }}" diff --git a/test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/files/assume-role-policy.json b/test/integration/targets/ec2_instance/files/assume-role-policy.json similarity index 100% rename from test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/files/assume-role-policy.json rename to test/integration/targets/ec2_instance/files/assume-role-policy.json diff --git a/test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/meta/main.yml b/test/integration/targets/ec2_instance/meta/main.yml similarity index 100% rename from test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/meta/main.yml rename to test/integration/targets/ec2_instance/meta/main.yml diff --git a/test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/defaults/main.yml b/test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/defaults/main.yml deleted file mode 100644 index 4b6773a1d8f..00000000000 --- a/test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/defaults/main.yml +++ /dev/null @@ -1,36 +0,0 @@ ---- -# defaults file for ec2_instance -ec2_instance_name: '{{resource_prefix}}-node' -ec2_instance_owner: 'integration-run-{{resource_prefix}}' -ec2_ami_image: - # https://wiki.centos.org/Cloud/AWS collected 2018-01-10 - ap-northeast-1: ami-571e3c30 - ap-northeast-2: ami-97cb19f9 - ap-south-1: ami-11f0837e - ap-southeast-1: ami-30318f53 - ap-southeast-2: ami-24959b47 - ca-central-1: ami-daeb57be - eu-central-1: ami-7cbc6e13 - eu-west-1: ami-0d063c6b - eu-west-2: ami-c22236a6 - sa-east-1: ami-864f2dea - us-east-1: ami-ae7bfdb8 - us-east-2: ami-9cbf9bf9 - us-west-1: ami-7c280d1c - us-west-2: ami-0c2aba6c -# We need to use ENA enabled AMIs to get EBS optimized instances. -ec2_ebs_optimized_ami_image: - ap-northeast-1: ami-00f9d04b3b3092052 - ap-northeast-2: ami-0c764df09c35858b8 - ap-south-1: ami-00796998f258969fd - ap-southeast-1: ami-085fd1bd447be68e8 - ap-southeast-2: ami-0b8dea0e70b969adc - ca-central-1: ami-05cac140c6a1fb960 - eu-central-1: ami-02ea8f348fa28c108 - eu-west-1: ami-0a5e707736615003c - eu-west-2: ami-017b0e29fac27906b - sa-east-1: ami-0160a8b6087883cb6 - us-east-1: ami-013be31976ca2c322 - us-east-2: ami-0350c5670171b5391 - us-west-1: ami-01beb64058d271bc4 - us-west-2: ami-061e7ebbc234015fe diff --git a/test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/main.yml b/test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/main.yml deleted file mode 100644 index 316813e1321..00000000000 --- a/test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/main.yml +++ /dev/null @@ -1,208 +0,0 @@ ---- -# A Note about ec2 environment variable name preference: -# - EC2_URL -> AWS_URL -# - EC2_ACCESS_KEY -> AWS_ACCESS_KEY_ID -> AWS_ACCESS_KEY -# - EC2_SECRET_KEY -> AWS_SECRET_ACCESS_KEY -> AWX_SECRET_KEY -# - EC2_REGION -> AWS_REGION -# - -# - include: ../../../../../setup_ec2/tasks/common.yml module_name: ec2_instance - -- block: - - # ============================================================ - - name: set connection information for all tasks - set_fact: - aws_connection_info: &aws_connection_info - aws_access_key: "{{ aws_access_key }}" - aws_secret_key: "{{ aws_secret_key }}" - security_token: "{{ security_token }}" - region: "{{ aws_region }}" - no_log: true - - - 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_instance Testing VPC - tenancy: default - <<: *aws_connection_info - register: testing_vpc - - - name: Create internet gateway for use in testing - ec2_vpc_igw: - vpc_id: "{{ testing_vpc.vpc.id }}" - state: present - <<: *aws_connection_info - 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" - <<: *aws_connection_info - 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" - <<: *aws_connection_info - 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 }}" - <<: *aws_connection_info - - - 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 - <<: *aws_connection_info - register: sg - - - include_tasks: cpu_options.yml - - include_tasks: termination_protection.yml - - include_tasks: tags_and_vpc_settings.yml - - include_tasks: external_resource_attach.yml - - include_tasks: block_devices.yml - - include_tasks: default_vpc_tests.yml - - include_tasks: iam_instance_role.yml - - include_tasks: checkmode_tests.yml - - include_tasks: ebs_optimized.yml - - include_tasks: instance_no_wait.yml - - # ============================================================ - - always: - - name: remove any instances in the test VPC - ec2_instance: - filters: - vpc_id: "{{ testing_vpc.vpc.id }}" - state: absent - <<: *aws_connection_info - register: removed - until: removed is not failed - ignore_errors: yes - retries: 10 - - - name: remove ENIs - ec2_eni_info: - filters: - vpc-id: "{{ testing_vpc.vpc.id }}" - <<: *aws_connection_info - register: enis - - - name: delete all ENIs - ec2_eni: - eni_id: "{{ item.id }}" - state: absent - <<: *aws_connection_info - 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 - <<: *aws_connection_info - 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 }}" - <<: *aws_connection_info - 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 - <<: *aws_connection_info - 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 - <<: *aws_connection_info - 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 - <<: *aws_connection_info - 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 - <<: *aws_connection_info - register: removed - until: removed is not failed - ignore_errors: yes - retries: 10 diff --git a/test/integration/targets/ec2_instance/playbooks/version_fail.yml b/test/integration/targets/ec2_instance/playbooks/version_fail.yml index d084ae0c993..223d635ff36 100644 --- a/test/integration/targets/ec2_instance/playbooks/version_fail.yml +++ b/test/integration/targets/ec2_instance/playbooks/version_fail.yml @@ -15,16 +15,16 @@ region: "{{ aws_region }}" no_log: True - - name: Include vars file in roles/ec2_instance/defaults/main.yml + - name: Include vars file in {{ playbook_dir }}../defaults/main.yml include_vars: - file: 'roles/ec2_instance/defaults/main.yml' + file: '{{ playbook_dir }}/../defaults/main.yml' - - name: create c4.large with cpu options (fails gracefully) + - name: create t3.nano with cpu options (fails gracefully) ec2_instance: state: present name: "ansible-test-{{ resource_prefix | regex_search('([0-9]+)$') }}-ec2" image_id: "{{ ec2_ami_image[aws_region] }}" - instance_type: c4.large + instance_type: t3.nano cpu_options: core_count: 1 threads_per_core: 1 diff --git a/test/integration/targets/ec2_instance/runme.sh b/test/integration/targets/ec2_instance/runme.sh index d41775b9cd9..2d2dac49518 100755 --- a/test/integration/targets/ec2_instance/runme.sh +++ b/test/integration/targets/ec2_instance/runme.sh @@ -2,14 +2,14 @@ set -eux -export ANSIBLE_ROLES_PATH=../ +export ANSIBLE_ROLES_PATH=../../targets/ # Test graceful failure for older versions of botocore source virtualenv.sh pip install 'botocore<1.10.16' 'boto3<1.7.16' -ansible-playbook -i ../../inventory -e @../../integration_config.yml -v playbooks/version_fail.yml "$@" +ansible-playbook -v playbooks/version_fail.yml "$@" # Run full test suite source virtualenv.sh pip install 'botocore>=1.10.16' boto3 -ansible-playbook -i ../../inventory -e @../../integration_config.yml -v playbooks/full_test.yml "$@" +ansible-playbook -v playbooks/full_test.yml "$@" diff --git a/test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/block_devices.yml b/test/integration/targets/ec2_instance/tasks/block_devices.yml similarity index 82% rename from test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/block_devices.yml rename to test/integration/targets/ec2_instance/tasks/block_devices.yml index f50afa23c6b..ddb2c19e2c6 100644 --- a/test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/block_devices.yml +++ b/test/integration/targets/ec2_instance/tasks/block_devices.yml @@ -12,9 +12,6 @@ image_id: "{{ ec2_ami_image[aws_region] }}" vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}" volumes: - - device_name: /dev/sda1 - ebs: - delete_on_termination: true - device_name: /dev/sdb ebs: volume_size: 20 @@ -23,15 +20,24 @@ tags: TestId: "{{ resource_prefix }}" instance_type: t2.micro + wait: false <<: *aws_connection_info register: in_test_vpc + +- name: Gather instance facts + ec2_instance_facts: + filters: + "tag:Name": "{{ resource_prefix }}-test-ebs-vols" + <<: *aws_connection_info + register: in_test_vpc_instance + - assert: that: - in_test_vpc is not failed - in_test_vpc is changed - - in_test_vpc.instances[0].block_device_mappings[0] - - in_test_vpc.instances[0].block_device_mappings[1] - - in_test_vpc.instances[0].block_device_mappings[1].device_name == '/dev/sdb' + - in_test_vpc_instance.instances[0].block_device_mappings[0] + - in_test_vpc_instance.instances[0].block_device_mappings[1] + - in_test_vpc_instance.instances[0].block_device_mappings[1].device_name == '/dev/sdb' - name: New instance with an extra block device(check mode) ec2_instance: @@ -39,9 +45,6 @@ image_id: "{{ ec2_ami_image[aws_region] }}" vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}" volumes: - - device_name: /dev/sda1 - ebs: - delete_on_termination: true - device_name: /dev/sdb ebs: volume_size: 20 @@ -57,7 +60,6 @@ ec2_instance_info: filters: "tag:Name": "{{ resource_prefix }}-test-ebs-vols" - "instance-state-name": "running" <<: *aws_connection_info register: presented_instance_fact @@ -65,7 +67,6 @@ ec2_instance_info: filters: "tag:Name": "{{ resource_prefix }}-test-ebs-vols-checkmode" - "instance-state-name": "running" <<: *aws_connection_info register: checkmode_instance_fact diff --git a/test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/checkmode_tests.yml b/test/integration/targets/ec2_instance/tasks/checkmode_tests.yml similarity index 92% rename from test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/checkmode_tests.yml rename to test/integration/targets/ec2_instance/tasks/checkmode_tests.yml index 0c54df72496..05eed56380d 100644 --- a/test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/checkmode_tests.yml +++ b/test/integration/targets/ec2_instance/tasks/checkmode_tests.yml @@ -15,10 +15,7 @@ security_groups: "{{ sg.group_id }}" instance_type: t2.micro vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}" - volumes: - - device_name: /dev/sda1 - ebs: - delete_on_termination: true + wait: false <<: *aws_connection_info register: basic_instance @@ -29,10 +26,6 @@ security_groups: "{{ sg.group_id }}" instance_type: t2.micro vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}" - volumes: - - device_name: /dev/sda1 - ebs: - delete_on_termination: true <<: *aws_connection_info check_mode: yes @@ -40,7 +33,6 @@ ec2_instance_info: filters: "tag:Name": "{{ resource_prefix }}-checkmode-comparison" - "instance-state-name": "running" <<: *aws_connection_info register: presented_instance_fact @@ -48,7 +40,6 @@ ec2_instance_info: filters: "tag:Name": "{{ resource_prefix }}-checkmode-comparison-checkmode" - "instance-state-name": "running" <<: *aws_connection_info register: checkmode_instance_fact @@ -78,12 +69,16 @@ that: - '"{{ confirm_checkmode_stopinstance_fact.instances[0].state.name }}" != "stopped"' - - name: Stop instance in normaly. + - name: Stop instance. ec2_instance: name: "{{ resource_prefix }}-checkmode-comparison" state: stopped vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}" <<: *aws_connection_info + register: instance_stop + until: not instance_stop.failed + retries: 10 + - name: fact stopped ec2 instance ec2_instance_info: @@ -95,7 +90,7 @@ - name: Verify that it was stopped. assert: that: - - '"{{ confirm_stopinstance_fact.instances[0].state.name }}" == "stopped"' + - '"{{ confirm_stopinstance_fact.instances[0].state.name }}" in ["stopped", "stopping"]' - name: Running instance in check mode. ec2_instance: @@ -117,7 +112,7 @@ that: - '"{{ confirm_checkmode_runninginstance_fact.instances[0].state.name }}" != "running"' - - name: Running instance in normaly. + - name: Running instance. ec2_instance: name: "{{ resource_prefix }}-checkmode-comparison" state: running @@ -156,7 +151,7 @@ that: - '"{{ confirm_checkmode_terminatedinstance_fact.instances[0].state.name }}" != "terminated"' - - name: Terminate instance in check mode. + - name: Terminate instance. ec2_instance: name: "{{ resource_prefix }}-checkmode-comparison" state: absent diff --git a/test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/cpu_options.yml b/test/integration/targets/ec2_instance/tasks/cpu_options.yml similarity index 66% rename from test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/cpu_options.yml rename to test/integration/targets/ec2_instance/tasks/cpu_options.yml index e5182ca5218..c5b8b3da2a5 100644 --- a/test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/cpu_options.yml +++ b/test/integration/targets/ec2_instance/tasks/cpu_options.yml @@ -7,21 +7,18 @@ region: "{{ aws_region }}" no_log: true -- name: create c4.large instance with cpu_options +- name: create t3.nano instance with cpu_options ec2_instance: - name: "{{ resource_prefix }}-test-c4large-1-threads-per-core" + name: "{{ resource_prefix }}-test-t3nano-1-threads-per-core" image_id: "{{ ec2_ami_image[aws_region] }}" tags: TestId: "{{ resource_prefix }}" vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}" - instance_type: c4.large + instance_type: t3.nano cpu_options: core_count: 1 threads_per_core: 1 - volumes: - - device_name: /dev/sda1 - ebs: - delete_on_termination: true + wait: false <<: *aws_connection_info register: instance_creation @@ -30,25 +27,20 @@ that: - instance_creation is success - instance_creation is changed - - "instance_creation.instances[0].cpu_options.core_count == 1" - - "instance_creation.instances[0].cpu_options.threads_per_core == 1" - name: modify cpu_options on existing instance (warning displayed) ec2_instance: state: present - name: "{{ resource_prefix }}-test-c4large-1-threads-per-core" + name: "{{ resource_prefix }}-test-t3nano-1-threads-per-core" image_id: "{{ ec2_ami_image[aws_region] }}" tags: TestId: "{{ resource_prefix }}" vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}" - instance_type: c4.large - volumes: - - device_name: /dev/sda1 - ebs: - delete_on_termination: true + instance_type: t3.nano cpu_options: core_count: 1 threads_per_core: 2 + wait: false <<: *aws_connection_info register: cpu_options_update ignore_errors: yes @@ -59,37 +51,31 @@ - cpu_options_update is success - cpu_options_update is not changed -- name: create c4.large instance with cpu_options(check mode) +- name: create t3.nano instance with cpu_options(check mode) ec2_instance: - name: "{{ resource_prefix }}-test-c4large-1-threads-per-core-checkmode" + name: "{{ resource_prefix }}-test-t3nano-1-threads-per-core-checkmode" image_id: "{{ ec2_ami_image[aws_region] }}" tags: TestId: "{{ resource_prefix }}" vpc_subnet_id: "{{ testing_subnet_a.subnet.id }}" - instance_type: c4.large + instance_type: t3.nano cpu_options: core_count: 1 threads_per_core: 1 - volumes: - - device_name: /dev/sda1 - ebs: - delete_on_termination: true <<: *aws_connection_info check_mode: yes - name: "fact presented ec2 instance" ec2_instance_info: filters: - "tag:Name": "{{ resource_prefix }}-test-c4large-1-threads-per-core" - "instance-state-name": "running" + "tag:Name": "{{ resource_prefix }}-test-t3nano-1-threads-per-core" <<: *aws_connection_info register: presented_instance_fact - name: "fact checkmode ec2 instance" ec2_instance_info: filters: - "tag:Name": "{{ resource_prefix }}-test-c4large-1-threads-per-core-checkmode" - "instance-state-name": "running" + "tag:Name": "{{ resource_prefix }}-test-t3nano-1-threads-per-core-checkmode" <<: *aws_connection_info register: checkmode_instance_fact @@ -97,4 +83,7 @@ assert: that: - "{{ presented_instance_fact.instances | length }} > 0" + - "'{{ presented_instance_fact.instances.0.state.name }}' in ['running','pending']" - "{{ checkmode_instance_fact.instances | length }} == 0" + - "{{ presented_instance_fact.instances.0.cpu_options.core_count }} == 1" + - "{{ presented_instance_fact.instances.0.cpu_options.threads_per_core }} == 1" diff --git a/test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/default_vpc_tests.yml b/test/integration/targets/ec2_instance/tasks/default_vpc_tests.yml similarity index 87% rename from test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/default_vpc_tests.yml rename to test/integration/targets/ec2_instance/tasks/default_vpc_tests.yml index 72927862013..e7802a8f104 100644 --- a/test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/default_vpc_tests.yml +++ b/test/integration/targets/ec2_instance/tasks/default_vpc_tests.yml @@ -14,10 +14,7 @@ TestId: "{{ resource_prefix }}" security_groups: "{{ sg.group_id }}" instance_type: t2.micro - volumes: - - device_name: /dev/sda1 - ebs: - delete_on_termination: true + wait: false <<: *aws_connection_info register: in_default_vpc @@ -29,10 +26,6 @@ TestId: "{{ resource_prefix }}" security_groups: "{{ sg.group_id }}" instance_type: t2.micro - volumes: - - device_name: /dev/sda1 - ebs: - delete_on_termination: true <<: *aws_connection_info check_mode: yes @@ -40,7 +33,6 @@ ec2_instance_info: filters: "tag:Name": "{{ resource_prefix }}-test-default-vpc" - "instance-state-name": "running" <<: *aws_connection_info register: presented_instance_fact @@ -48,7 +40,6 @@ ec2_instance_info: filters: "tag:Name": "{{ resource_prefix }}-test-default-vpc-checkmode" - "instance-state-name": "running" <<: *aws_connection_info register: checkmode_instance_fact diff --git a/test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/ebs_optimized.yml b/test/integration/targets/ec2_instance/tasks/ebs_optimized.yml similarity index 85% rename from test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/ebs_optimized.yml rename to test/integration/targets/ec2_instance/tasks/ebs_optimized.yml index f84a12989b7..fcd75f72f45 100644 --- a/test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/ebs_optimized.yml +++ b/test/integration/targets/ec2_instance/tasks/ebs_optimized.yml @@ -17,18 +17,18 @@ vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}" ebs_optimized: true instance_type: t3.nano + wait: false <<: *aws_connection_info register: ebs_opt_in_vpc -- name: Get ec2 instance facts +- name: Get ec2 instance info ec2_instance_info: filters: "tag:Name": "{{ resource_prefix }}-test-ebs-optimized-instance-in-vpc" - "instance-state-name": "running" <<: *aws_connection_info - register: ebs_opt_instance_fact + register: ebs_opt_instance_info - name: Assert instance is ebs_optimized assert: that: - - "{{ ebs_opt_instance_fact.instances.0.ebs_optimized }}" + - "{{ ebs_opt_instance_info.instances.0.ebs_optimized }}" diff --git a/test/integration/targets/ec2_instance/tasks/env_cleanup.yml b/test/integration/targets/ec2_instance/tasks/env_cleanup.yml new file mode 100644 index 00000000000..7ce507e9cb6 --- /dev/null +++ b/test/integration/targets/ec2_instance/tasks/env_cleanup.yml @@ -0,0 +1,112 @@ +- name: set connection information for all tasks + set_fact: + aws_connection_info: &aws_connection_info + aws_access_key: "{{ aws_access_key }}" + aws_secret_key: "{{ aws_secret_key }}" + security_token: "{{ security_token }}" + region: "{{ aws_region }}" + no_log: true + +- name: remove any instances in the test VPC + ec2_instance: + filters: + vpc_id: "{{ testing_vpc.vpc.id }}" + state: absent + <<: *aws_connection_info + register: removed + until: removed is not failed + ignore_errors: yes + retries: 10 + +- name: remove ENIs + ec2_eni_info: + filters: + vpc-id: "{{ testing_vpc.vpc.id }}" + <<: *aws_connection_info + register: enis + +- name: delete all ENIs + ec2_eni: + eni_id: "{{ item.id }}" + state: absent + <<: *aws_connection_info + 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 + <<: *aws_connection_info + 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 }}" + <<: *aws_connection_info + 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 + <<: *aws_connection_info + 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 + <<: *aws_connection_info + 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 + <<: *aws_connection_info + 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 + <<: *aws_connection_info + register: removed + until: removed is not failed + ignore_errors: yes + retries: 10 diff --git a/test/integration/targets/ec2_instance/tasks/env_setup.yml b/test/integration/targets/ec2_instance/tasks/env_setup.yml new file mode 100644 index 00000000000..a17d98795c6 --- /dev/null +++ b/test/integration/targets/ec2_instance/tasks/env_setup.yml @@ -0,0 +1,77 @@ +- name: set connection information for all tasks + set_fact: + aws_connection_info: &aws_connection_info + aws_access_key: "{{ aws_access_key }}" + aws_secret_key: "{{ aws_secret_key }}" + security_token: "{{ security_token }}" + region: "{{ aws_region }}" + no_log: true + +- 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_instance Testing VPC + tenancy: default + <<: *aws_connection_info + register: testing_vpc + +- name: Create internet gateway for use in testing + ec2_vpc_igw: + vpc_id: "{{ testing_vpc.vpc.id }}" + state: present + <<: *aws_connection_info + 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" + <<: *aws_connection_info + 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" + <<: *aws_connection_info + 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 }}" + <<: *aws_connection_info + +- 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 + <<: *aws_connection_info + register: sg diff --git a/test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/external_resource_attach.yml b/test/integration/targets/ec2_instance/tasks/external_resource_attach.yml similarity index 85% rename from test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/external_resource_attach.yml rename to test/integration/targets/ec2_instance/tasks/external_resource_attach.yml index 7791fa51e54..3f89dade6d6 100644 --- a/test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/external_resource_attach.yml +++ b/test/integration/targets/ec2_instance/tasks/external_resource_attach.yml @@ -44,18 +44,22 @@ availability_zone: '{{ aws_region }}b' tags: TestId: "{{ resource_prefix }}" - volumes: - - device_name: /dev/sda1 - ebs: - delete_on_termination: true instance_type: t2.micro + wait: false <<: *aws_connection_info register: in_test_vpc +- name: "Gather {{ resource_prefix }}-test-eni-vpc facts" + ec2_instance_facts: + filters: + "tag:Name": '{{ resource_prefix }}-test-eni-vpc' + <<: *aws_connection_info + register: in_test_vpc_instance + - assert: that: - - 'in_test_vpc.instances[0].key_name == "{{ resource_prefix }}_test_key"' - - '(in_test_vpc.instances[0].network_interfaces | length) == 1' + - 'in_test_vpc_instance.instances.0.key_name == "{{ resource_prefix }}_test_key"' + - '(in_test_vpc_instance.instances.0.network_interfaces | length) == 1' - name: Add a second interface ec2_instance: @@ -68,7 +72,12 @@ tags: TestId: "{{ resource_prefix }}" instance_type: t2.micro + wait: false <<: *aws_connection_info + register: add_interface + until: add_interface is not failed + ignore_errors: yes + retries: 10 - name: Make instance in the testing subnet created in the test VPC(check mode) ec2_instance: @@ -81,10 +90,6 @@ availability_zone: '{{ aws_region }}b' tags: TestId: "{{ resource_prefix }}" - volumes: - - device_name: /dev/sda1 - ebs: - delete_on_termination: true instance_type: t2.micro <<: *aws_connection_info check_mode: yes @@ -93,7 +98,6 @@ ec2_instance_info: filters: "tag:Name": "{{ resource_prefix }}-test-eni-vpc" - "instance-state-name": "running" <<: *aws_connection_info register: presented_instance_fact @@ -101,7 +105,6 @@ ec2_instance_info: filters: "tag:Name": "{{ resource_prefix }}-test-eni-vpc-checkmode" - "instance-state-name": "running" <<: *aws_connection_info register: checkmode_instance_fact @@ -116,6 +119,7 @@ filters: tag:TestId: "{{ resource_prefix }}" state: absent + wait: false <<: *aws_connection_info register: result - assert: @@ -125,6 +129,7 @@ ec2_instance: instance_ids: "{{ in_test_vpc.instance_ids }}" state: absent + wait: false <<: *aws_connection_info register: result - assert: @@ -133,13 +138,4 @@ - ec2_key: name: "{{ resource_prefix }}_test_key" state: absent - <<: *aws_connection_info - -- ec2_eni: - eni_id: "{{ item }}" - state: absent - <<: *aws_connection_info - with_items: - - "{{ eni_a.interface.id }}" - - "{{ eni_b.interface.id }}" - - "{{ eni_c.interface.id }}" + <<: *aws_connection_info \ No newline at end of file diff --git a/test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/iam_instance_role.yml b/test/integration/targets/ec2_instance/tasks/iam_instance_role.yml similarity index 84% rename from test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/iam_instance_role.yml rename to test/integration/targets/ec2_instance/tasks/iam_instance_role.yml index fd19800d348..29864d2da92 100644 --- a/test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/iam_instance_role.yml +++ b/test/integration/targets/ec2_instance/tasks/iam_instance_role.yml @@ -10,7 +10,7 @@ - block: - name: Create IAM role for test iam_role: - name: "{{ resource_prefix }}-test-policy" + name: "ansible-test-sts-{{ resource_prefix }}-test-policy" assume_role_policy_document: "{{ lookup('file','assume-role-policy.json') }}" state: present create_instance_profile: yes @@ -21,7 +21,7 @@ - name: Create second IAM role for test iam_role: - name: "{{ resource_prefix }}-test-policy-2" + name: "ansible-test-sts-{{ resource_prefix }}-test-policy-2" assume_role_policy_document: "{{ lookup('file','assume-role-policy.json') }}" state: present create_instance_profile: yes @@ -36,11 +36,7 @@ image_id: "{{ ec2_ami_image[aws_region] }}" security_groups: "{{ sg.group_id }}" instance_type: t2.micro - instance_role: "{{ resource_prefix }}-test-policy" - volumes: - - device_name: /dev/sda1 - ebs: - delete_on_termination: true + instance_role: "ansible-test-sts-{{ resource_prefix }}-test-policy" <<: *aws_connection_info register: instance_with_role @@ -54,11 +50,7 @@ image_id: "{{ ec2_ami_image[aws_region] }}" security_groups: "{{ sg.group_id }}" instance_type: t2.micro - instance_role: "{{ resource_prefix }}-test-policy" - volumes: - - device_name: /dev/sda1 - ebs: - delete_on_termination: true + instance_role: "ansible-test-sts-{{ resource_prefix }}-test-policy" <<: *aws_connection_info check_mode: yes @@ -66,7 +58,6 @@ ec2_instance_info: filters: "tag:Name": "{{ resource_prefix }}-test-instance-role" - "instance-state-name": "running" <<: *aws_connection_info register: presented_instance_fact @@ -74,7 +65,6 @@ ec2_instance_info: filters: "tag:Name": "{{ resource_prefix }}-test-instance-role-checkmode" - "instance-state-name": "running" <<: *aws_connection_info register: checkmode_instance_fact @@ -90,9 +80,11 @@ image_id: "{{ ec2_ami_image[aws_region] }}" security_groups: "{{ sg.group_id }}" instance_type: t2.micro - instance_role: "{{ resource_prefix }}-test-policy-2" + instance_role: "ansible-test-sts-{{ resource_prefix }}-test-policy-2" <<: *aws_connection_info register: instance_with_updated_role + until: instance_with_updated_role is not failed + retries: 10 - assert: that: @@ -120,8 +112,8 @@ - AmazonEC2ContainerServiceRole <<: *aws_connection_info loop: - - "{{ resource_prefix }}-test-policy" - - "{{ resource_prefix }}-test-policy-2" + - "ansible-test-sts-{{ resource_prefix }}-test-policy" + - "ansible-test-sts-{{ resource_prefix }}-test-policy-2" register: removed until: removed is not failed ignore_errors: yes diff --git a/test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/instance_no_wait.yml b/test/integration/targets/ec2_instance/tasks/instance_no_wait.yml similarity index 96% rename from test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/instance_no_wait.yml rename to test/integration/targets/ec2_instance/tasks/instance_no_wait.yml index 019b51ea67f..298a4bce0a0 100644 --- a/test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/instance_no_wait.yml +++ b/test/integration/targets/ec2_instance/tasks/instance_no_wait.yml @@ -43,7 +43,6 @@ ec2_instance_info: filters: "tag:Name": "{{ resource_prefix }}-test-no-wait" - "instance-state-name": "running" <<: *aws_connection_info register: real_instance_fact until: real_instance_fact.instances | length > 0 @@ -53,7 +52,6 @@ ec2_instance_info: filters: "tag:Name": "{{ resource_prefix }}-test-no-wait-checkmode" - "instance-state-name": "running" <<: *aws_connection_info register: checkmode_instance_fact diff --git a/test/integration/targets/ec2_instance/tasks/main.yml b/test/integration/targets/ec2_instance/tasks/main.yml new file mode 100644 index 00000000000..6e3bece24cd --- /dev/null +++ b/test/integration/targets/ec2_instance/tasks/main.yml @@ -0,0 +1,27 @@ +--- +# A Note about ec2 environment variable name preference: +# - EC2_URL -> AWS_URL +# - EC2_ACCESS_KEY -> AWS_ACCESS_KEY_ID -> AWS_ACCESS_KEY +# - EC2_SECRET_KEY -> AWS_SECRET_ACCESS_KEY -> AWX_SECRET_KEY +# - EC2_REGION -> AWS_REGION +# + +# - include: ../../../../../setup_ec2/tasks/common.yml module_name: ec2_instance + +- block: + + - include_tasks: env_setup.yml + - include_tasks: cpu_options.yml + - include_tasks: termination_protection.yml + - include_tasks: tags_and_vpc_settings.yml + - include_tasks: external_resource_attach.yml + - include_tasks: block_devices.yml + - include_tasks: default_vpc_tests.yml + - include_tasks: iam_instance_role.yml + - include_tasks: checkmode_tests.yml + - include_tasks: ebs_optimized.yml + - include_tasks: instance_no_wait.yml + + always: + - include_tasks: env_cleanup.yml + when: aws_cleanup \ No newline at end of file diff --git a/test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/tags_and_vpc_settings.yml b/test/integration/targets/ec2_instance/tasks/tags_and_vpc_settings.yml similarity index 88% rename from test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/tags_and_vpc_settings.yml rename to test/integration/targets/ec2_instance/tasks/tags_and_vpc_settings.yml index 263ec0f3a41..4f5f52e1344 100644 --- a/test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/tags_and_vpc_settings.yml +++ b/test/integration/targets/ec2_instance/tasks/tags_and_vpc_settings.yml @@ -22,10 +22,7 @@ source_dest_check: false vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}" instance_type: t2.micro - volumes: - - device_name: /dev/sda1 - ebs: - delete_on_termination: true + wait: false <<: *aws_connection_info register: in_test_vpc @@ -45,10 +42,6 @@ source_dest_check: false vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}" instance_type: t2.micro - volumes: - - device_name: /dev/sda1 - ebs: - delete_on_termination: true <<: *aws_connection_info check_mode: yes @@ -82,7 +75,6 @@ ec2_instance_info: filters: "tag:Name": "{{ resource_prefix }}-test-basic-vpc-create" - "instance-state-name": "running" <<: *aws_connection_info register: presented_instance_fact @@ -90,7 +82,6 @@ ec2_instance_info: filters: "tag:Name": "{{ resource_prefix }}-test-basic-vpc-create-checkmode" - "instance-state-name": "running" <<: *aws_connection_info register: checkmode_instance_fact @@ -135,10 +126,12 @@ vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}" instance_type: t2.micro <<: *aws_connection_info + - ec2_instance_info: instance_ids: "{{ add_another_tag.instance_ids }}" <<: *aws_connection_info register: check_tags + - name: "Remaking the same instance resulted in no changes" assert: that: @@ -149,6 +142,7 @@ filters: tag:TestId: "{{ resource_prefix }}" state: absent + wait: false <<: *aws_connection_info register: result - assert: @@ -156,8 +150,9 @@ - name: Terminate instance ec2_instance: - instance_ids: "{{ in_test_vpc.instance_ids }}" + instance_ids: "{{ add_another_tag.instance_ids }}" state: absent + wait: false <<: *aws_connection_info register: result - assert: @@ -166,11 +161,11 @@ - name: check that subnet-default public IP rule was followed assert: that: - - in_test_vpc.instances[0].public_dns_name == "" - - in_test_vpc.instances[0].private_ip_address.startswith("10.22.33") - - in_test_vpc.instances[0].subnet_id == testing_subnet_b.subnet.id + - check_tags.instances[0].public_dns_name == "" + - check_tags.instances[0].private_ip_address.startswith("10.22.33") + - check_tags.instances[0].subnet_id == testing_subnet_b.subnet.id - name: check that tags were applied assert: that: - - in_test_vpc.instances[0].tags.Name.startswith(resource_prefix) - - in_test_vpc.instances[0].state.name == 'running' + - check_tags.instances[0].tags.Name.startswith(resource_prefix) + - "'{{ check_tags.instances[0].state.name }}' in ['pending', 'running']" diff --git a/test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/termination_protection.yml b/test/integration/targets/ec2_instance/tasks/termination_protection.yml similarity index 92% rename from test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/termination_protection.yml rename to test/integration/targets/ec2_instance/tasks/termination_protection.yml index c23d5238296..f425b71166d 100644 --- a/test/integration/targets/ec2_instance/playbooks/roles/ec2_instance/tasks/termination_protection.yml +++ b/test/integration/targets/ec2_instance/tasks/termination_protection.yml @@ -16,10 +16,7 @@ vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}" termination_protection: true instance_type: t2.micro - volumes: - - device_name: /dev/sda1 - ebs: - delete_on_termination: true + state: running <<: *aws_connection_info register: in_test_vpc @@ -33,10 +30,6 @@ vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}" termination_protection: true instance_type: t2.micro - volumes: - - device_name: /dev/sda1 - ebs: - delete_on_termination: true <<: *aws_connection_info check_mode: yes @@ -44,7 +37,6 @@ ec2_instance_info: filters: "tag:Name": "{{ resource_prefix }}-test-protected-instance-in-vpc" - "instance-state-name": "running" <<: *aws_connection_info register: presented_instance_fact @@ -52,7 +44,6 @@ ec2_instance_info: filters: "tag:Name": "{{ resource_prefix }}-test-protected-instance-in-vpc-checkmode" - "instance-state-name": "running" <<: *aws_connection_info register: checkmode_instance_fact @@ -60,6 +51,7 @@ assert: that: - "{{ presented_instance_fact.instances | length }} > 0" + - "'{{ presented_instance_fact.instances.0.state.name }}' in ['running', 'pending']" - "{{ checkmode_instance_fact.instances | length }} == 0" - name: Try to terminate the instance @@ -95,6 +87,7 @@ vpc_subnet_id: "{{ testing_subnet_b.subnet.id }}" instance_type: t2.micro state: absent + wait: false <<: *aws_connection_info register: terminate_results - assert: diff --git a/test/runner/lib/cloud/aws.py b/test/runner/lib/cloud/aws.py index 9442beb902b..ff7fb9b6881 100644 --- a/test/runner/lib/cloud/aws.py +++ b/test/runner/lib/cloud/aws.py @@ -99,6 +99,9 @@ class AwsCloudEnvironment(CloudEnvironment): ansible_vars.update(dict(parser.items('default'))) + if 'aws_cleanup' not in ansible_vars: + ansible_vars['aws_cleanup'] = not self.managed + env_vars = {'ANSIBLE_DEBUG_BOTOCORE_LOGS': 'True'} return CloudEnvironmentConfig(