From da1e43dc125a818843ed235ec2dcd69c42126a39 Mon Sep 17 00:00:00 2001 From: AlanCoding Date: Thu, 31 Oct 2019 07:53:43 -0400 Subject: [PATCH] Make tower_group idempotent --- awx_collection/plugins/modules/tower_group.py | 9 ++- awx_collection/test/awx/test_group.py | 56 +++++++++++++++++++ 2 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 awx_collection/test/awx/test_group.py diff --git a/awx_collection/plugins/modules/tower_group.py b/awx_collection/plugins/modules/tower_group.py index bd9feaf91f..a30500d09d 100644 --- a/awx_collection/plugins/modules/tower_group.py +++ b/awx_collection/plugins/modules/tower_group.py @@ -49,7 +49,6 @@ options: description: - The source to use for this group. choices: ["manual", "file", "ec2", "rax", "vmware", "gce", "azure", "azure_rm", "openstack", "satellite6" , "cloudforms", "custom"] - default: manual type: str source_regions: description: @@ -127,15 +126,15 @@ def main(): credential=dict(), source=dict(choices=["manual", "file", "ec2", "rax", "vmware", "gce", "azure", "azure_rm", "openstack", - "satellite6", "cloudforms", "custom"], default="manual"), + "satellite6", "cloudforms", "custom"]), source_regions=dict(), source_vars=dict(), instance_filters=dict(), group_by=dict(), source_script=dict(), - overwrite=dict(type='bool', default=False), - overwrite_vars=dict(type='bool', default=False), - update_on_launch=dict(type='bool', default=False), + overwrite=dict(type='bool'), + overwrite_vars=dict(type='bool'), + update_on_launch=dict(type='bool'), state=dict(choices=['present', 'absent'], default='present'), ) diff --git a/awx_collection/test/awx/test_group.py b/awx_collection/test/awx/test_group.py new file mode 100644 index 0000000000..c5cb12c8d7 --- /dev/null +++ b/awx_collection/test/awx/test_group.py @@ -0,0 +1,56 @@ +import pytest + +from awx.main.models import Organization, Inventory, Group + + +@pytest.mark.django_db +def test_create_group(run_module, admin_user): + org = Organization.objects.create(name='test-org') + inv = Inventory.objects.create(name='test-inv', organization=org) + + result = run_module('tower_group', dict( + name='Test Group', + inventory='test-inv', + variables='ansible_network_os: iosxr', + state='present' + ), admin_user) + assert result.get('changed'), result + + group = Group.objects.get(name='Test Group') + assert group.inventory == inv + assert group.variables == 'ansible_network_os: iosxr' + + result.pop('invocation') + assert result == { + 'id': group.id, + 'group': 'Test Group', + 'changed': True, + 'state': 'present' + } + + +@pytest.mark.django_db +def test_tower_group_idempotent(run_module, admin_user): + # https://github.com/ansible/ansible/issues/46803 + org = Organization.objects.create(name='test-org') + inv = Inventory.objects.create(name='test-inv', organization=org) + group = Group.objects.create( + name='Test Group', + inventory=inv, + variables='ansible_network_os: iosxr' + ) + + result = run_module('tower_group', dict( + name='Test Group', + inventory='test-inv', + variables='ansible_network_os: iosxr', + state='present' + ), admin_user) + + result.pop('invocation') + assert result == { + 'id': group.id, + 'group': 'Test Group', + 'changed': False, # idempotency assertion + 'state': 'present' + }