diff --git a/tests/integration/targets/ec2_vpc_nat_gateway/tasks/main.yml b/tests/integration/targets/ec2_vpc_nat_gateway/tasks/main.yml index 2b411340149..36f035c81d1 100644 --- a/tests/integration/targets/ec2_vpc_nat_gateway/tasks/main.yml +++ b/tests/integration/targets/ec2_vpc_nat_gateway/tasks/main.yml @@ -1,19 +1,14 @@ -# The tests for this module are incomplete. -# The tests below were migrated from unit tests. -# They take advantage of hard-coded results within the module to trigger both changed and unchanged responses. -# They were migrated to maintain test coverage while removing unit tests that depended on use of TaskQueueManager. - --- # ============================================================ # Known issues: # -# `check_mode` is not working correctly due to the hard-coded DRY_RUN_GATEWAY (module code). The values passed here, -# when CHECK_MODE is used, don't correspond to those used for the DRY_RUN_GATEWAY and all test +# `check_mode` is not working correctly due to the hard-coded DRY_RUN_GATEWAY (module code). The values passed here, +# when CHECK_MODE is used, don't correspond to those used for the DRY_RUN_GATEWAY and all test # (except when the NAT gateway is created for the first time) fail. # -# `Create new NAT gateway with eip address` - when the task is run for the first time, do we expect changed=true? -# As we use the same EIP, I think changed should be false (if this is correct, lines 194-218 are redundant and -# lines 177 and 190 should report `not create_ngw.changed`). +# `Create new NAT gateway with eip address` - when the task is run for the first time, do we expect changed=true? +# As we use the same EIP, I think changed should be false (if this is correct, lines 194-218 are redundant and +# lines 177 and 190 should report `not create_ngw.changed`). # ============================================================ - name: ec2_vpc_nat_gateway tests @@ -26,8 +21,8 @@ collections: - amazon.aws - block: - + block: + # ============================================================ - name: Create a VPC ec2_vpc_net: @@ -52,7 +47,7 @@ reuse_existing_ip_allowed: true tag_name: FREE register: eip_result - + - name: Assert success assert: that: @@ -62,7 +57,7 @@ set_fact: eip_address: "{{ eip_result.public_ip }}" allocation_id: "{{ eip_result.allocation_id }}" - + # ============================================================ - name: Create subnet and associate to the VPC @@ -80,7 +75,7 @@ - name: "set fact: VPC subnet ID" set_fact: subnet_id: "{{ subnet_result.subnet.id }}" - + # ============================================================ - name: Search for NAT gateways by subnet - no matches @@ -91,12 +86,12 @@ register: existing_ngws retries: 10 until: existing_ngws is not failed - + - name: Assert no NAT gateway found assert: that: - existing_ngws is successful - - (existing_ngws.result|length) == 0 + - (existing_ngws.result|length) == 0 @@ -105,7 +100,7 @@ ec2_vpc_igw: vpc_id: "{{ vpc_id }}" register: create_igw - + - name: Assert success assert: that: @@ -121,7 +116,7 @@ register: create_ngw retries: 10 until: create_ngw is not failed - + - name: Assert creation happened (expected changed=true) assert: that: @@ -131,7 +126,7 @@ set_fact: nat_gateway_id: "{{ create_ngw.nat_gateway_id }}" network_interface_id: "{{ create_ngw.nat_gateway_addresses[0].network_interface_id }}" - + # - name: Create new NAT gateway with eip allocation-id - CHECK_MODE # ec2_vpc_nat_gateway: # subnet_id: "{{ subnet_id }}" @@ -159,7 +154,7 @@ assert: that: - not create_ngw.changed - + # - name: Trying this again for idempotency - create new NAT gateway with eip allocation-id - CHECK_MODE # ec2_vpc_nat_gateway: # subnet_id: "{{ subnet_id }}" @@ -167,7 +162,7 @@ # wait: yes # register: create_ngw # check_mode: yes - + # - name: Assert recreation would do nothing (expected changed=false) - CHECK_MODE # assert: # that: @@ -175,7 +170,7 @@ # ============================================================ - #- name: Create new NAT gateway with eip address + #- name: Create new NAT gateway with eip address # ec2_vpc_nat_gateway: # subnet_id: "{{ subnet_id }}" # eip_address: "{{ eip_address }}" @@ -186,7 +181,7 @@ # assert: # that: # - create_ngw.changed - + # - name: Create new nat gateway with eip address - CHECK_MODE # ec2_vpc_nat_gateway: # subnet_id: "{{ subnet_id }}" @@ -242,7 +237,7 @@ assert: that: - not create_ngw.changed - + # - name: Create new nat gateway only if one does not exist already - CHECK_MODE # ec2_vpc_nat_gateway: # if_exist_do_not_create: yes @@ -257,7 +252,7 @@ # - not create_ngw.changed - # ============================================================ + # ============================================================ - name: Delete NAT gateway ec2_vpc_nat_gateway: nat_gateway_id: "{{ nat_gateway_id }}" @@ -271,7 +266,7 @@ assert: that: - delete_nat_gateway.changed - + # - name: Delete NAT gateway - CHECK_MODE # ec2_vpc_nat_gateway: # nat_gateway_id: "{{ nat_gateway_id }}" @@ -332,8 +327,8 @@ - name: assert tag update would do nothing (expected changed=false) assert: that: - - not update_tags_ngw.changed - + - not update_tags_ngw.changed + # - name: Update the tags (no change) - CHECK_MODE # ec2_vpc_nat_gateway: # subnet_id: "{{ subnet_id }}" @@ -366,7 +361,7 @@ assert: that: - update_tags_ngw.changed - + # - name: Update the tags - remove and add - CHECK_MODE # ec2_vpc_nat_gateway: # subnet_id: "{{ subnet_id }}" @@ -431,7 +426,7 @@ assert: that: - delete_tags_ngw.changed - + # - name: Remove all tags - CHECK_MODE # ec2_vpc_nat_gateway: # subnet_id: "{{ subnet_id }}" @@ -445,8 +440,8 @@ # that: # - delete_tags_ngw.changed - - + + # ============================================================ - name: Update with CamelCase tags ec2_vpc_nat_gateway: @@ -496,8 +491,8 @@ # - update_tags_ngw.tags["snake_case"] == 'simple_snake_case ❤️' # ============================================================ - - + + always: - name: Get NAT gateways ec2_vpc_nat_gateway_info: @@ -507,7 +502,7 @@ retries: 10 until: existing_ngws is not failed ignore_errors: true - + - name: Tidy up NAT gateway ec2_vpc_nat_gateway: subnet_id: "{{ item.subnet_id }}" @@ -517,19 +512,19 @@ wait: yes with_items: "{{ existing_ngws.result }}" ignore_errors: true - + - name: Delete IGW ec2_vpc_igw: vpc_id: "{{ vpc_id }}" state: absent - ignore_errors: true - + ignore_errors: true + - name: Remove subnet ec2_vpc_subnet: state: absent cidr: "{{ subnet_cidr }}" vpc_id: "{{ vpc_id }}" - ignore_errors: true + ignore_errors: true - name: Ensure EIP is actually released ec2_eip: diff --git a/tests/unit/plugins/modules/test_ec2_vpc_nat_gateway.py b/tests/unit/plugins/modules/test_ec2_vpc_nat_gateway.py deleted file mode 100644 index 88d5032b11d..00000000000 --- a/tests/unit/plugins/modules/test_ec2_vpc_nat_gateway.py +++ /dev/null @@ -1,207 +0,0 @@ -# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) - -from __future__ import (absolute_import, division, print_function) -__metaclass__ = type - -import pytest -import unittest - -from mock import patch - -import ansible_collections.community.aws.plugins.modules.ec2_vpc_nat_gateway as ng - - -boto3 = pytest.importorskip("boto3") -botocore = pytest.importorskip("botocore") - -aws_region = 'us-west-2' - - -class AnsibleEc2VpcNatGatewayFunctions(unittest.TestCase): - - def test_get_nat_gateways(self): - client = boto3.client('ec2', region_name=aws_region) - success, err_msg, stream = ( - ng.get_nat_gateways(client, 'subnet-123456789', check_mode=True) - ) - should_return = ng.DRY_RUN_GATEWAYS - self.assertTrue(success) - self.assertEqual(stream, should_return) - - def test_get_nat_gateways_no_gateways_found(self): - client = boto3.client('ec2', region_name=aws_region) - success, err_msg, stream = ( - ng.get_nat_gateways(client, 'subnet-1234567', check_mode=True) - ) - self.assertTrue(success) - self.assertEqual(stream, []) - - def test_wait_for_status(self): - client = boto3.client('ec2', region_name=aws_region) - success, err_msg, gws = ( - ng.wait_for_status( - client, 5, 'nat-123456789', 'available', check_mode=True - ) - ) - should_return = ng.DRY_RUN_GATEWAYS[0] - self.assertTrue(success) - self.assertEqual(gws, should_return) - - @patch('time.sleep') - def test_wait_for_status_to_timeout(self, mock_sleep): - client = boto3.client('ec2', region_name=aws_region) - success, err_msg, gws = ( - ng.wait_for_status( - client, 2, 'nat-12345678', 'available', check_mode=True - ) - ) - self.assertFalse(success) - self.assertEqual(gws, {}) - - def test_gateway_in_subnet_exists_with_allocation_id(self): - client = boto3.client('ec2', region_name=aws_region) - gws, err_msg = ( - ng.gateway_in_subnet_exists( - client, 'subnet-123456789', 'eipalloc-1234567', check_mode=True - ) - ) - should_return = ng.DRY_RUN_GATEWAYS - self.assertEqual(gws, should_return) - - def test_gateway_in_subnet_exists_with_allocation_id_does_not_exist(self): - client = boto3.client('ec2', region_name=aws_region) - gws, err_msg = ( - ng.gateway_in_subnet_exists( - client, 'subnet-123456789', 'eipalloc-123', check_mode=True - ) - ) - should_return = list() - self.assertEqual(gws, should_return) - - def test_gateway_in_subnet_exists_without_allocation_id(self): - client = boto3.client('ec2', region_name=aws_region) - gws, err_msg = ( - ng.gateway_in_subnet_exists( - client, 'subnet-123456789', check_mode=True - ) - ) - should_return = ng.DRY_RUN_GATEWAYS - self.assertEqual(gws, should_return) - - def test_get_eip_allocation_id_by_address(self): - client = boto3.client('ec2', region_name=aws_region) - allocation_id, error_msg = ( - ng.get_eip_allocation_id_by_address( - client, '55.55.55.55', check_mode=True - ) - ) - should_return = 'eipalloc-1234567' - self.assertEqual(allocation_id, should_return) - - def test_get_eip_allocation_id_by_address_does_not_exist(self): - client = boto3.client('ec2', region_name=aws_region) - allocation_id, err_msg = ( - ng.get_eip_allocation_id_by_address( - client, '52.52.52.52', check_mode=True - ) - ) - self.assertEqual(err_msg, 'EIP 52.52.52.52 does not exist') - self.assertTrue(allocation_id is None) - - def test_allocate_eip_address(self): - client = boto3.client('ec2', region_name=aws_region) - success, err_msg, eip_id = ( - ng.allocate_eip_address( - client, check_mode=True - ) - ) - self.assertTrue(success) - - def test_release_address(self): - client = boto3.client('ec2', region_name=aws_region) - success, err_msg = ( - ng.release_address( - client, 'eipalloc-1234567', check_mode=True - ) - ) - self.assertTrue(success) - - def test_create(self): - client = boto3.client('ec2', region_name=aws_region) - success, changed, err_msg, results = ( - ng.create( - client, 'subnet-123456', 'eipalloc-1234567', check_mode=True - ) - ) - self.assertTrue(success) - self.assertTrue(changed) - - def test_pre_create(self): - client = boto3.client('ec2', region_name=aws_region) - success, changed, err_msg, results = ( - ng.pre_create( - client, 'subnet-123456', check_mode=True - ) - ) - self.assertTrue(success) - self.assertTrue(changed) - - def test_pre_create_idemptotent_with_allocation_id(self): - client = boto3.client('ec2', region_name=aws_region) - success, changed, err_msg, results = ( - ng.pre_create( - client, 'subnet-123456789', allocation_id='eipalloc-1234567', check_mode=True - ) - ) - self.assertTrue(success) - self.assertFalse(changed) - - def test_pre_create_idemptotent_with_eip_address(self): - client = boto3.client('ec2', region_name=aws_region) - success, changed, err_msg, results = ( - ng.pre_create( - client, 'subnet-123456789', eip_address='55.55.55.55', check_mode=True - ) - ) - self.assertTrue(success) - self.assertFalse(changed) - - def test_pre_create_idemptotent_if_exist_do_not_create(self): - client = boto3.client('ec2', region_name=aws_region) - success, changed, err_msg, results = ( - ng.pre_create( - client, 'subnet-123456789', if_exist_do_not_create=True, check_mode=True - ) - ) - self.assertTrue(success) - self.assertFalse(changed) - - def test_delete(self): - client = boto3.client('ec2', region_name=aws_region) - success, changed, err_msg, results = ( - ng.remove( - client, 'nat-123456789', check_mode=True - ) - ) - self.assertTrue(success) - self.assertTrue(changed) - - def test_delete_and_release_ip(self): - client = boto3.client('ec2', region_name=aws_region) - success, changed, err_msg, results = ( - ng.remove( - client, 'nat-123456789', release_eip=True, check_mode=True - ) - ) - self.assertTrue(success) - self.assertTrue(changed) - - def test_delete_if_does_not_exist(self): - client = boto3.client('ec2', region_name=aws_region) - success, changed, err_msg, results = ( - ng.remove( - client, 'nat-12345', check_mode=True - ) - ) - self.assertFalse(success) - self.assertFalse(changed)