Add label tests and flake8 fixes

This commit is contained in:
AlanCoding
2020-03-20 20:03:59 -04:00
parent 8cd4e9b488
commit abdcdbca76
2 changed files with 52 additions and 5 deletions

View File

@@ -22,6 +22,8 @@ short_description: create, update, or destroy Ansible Tower labels.
description: description:
- Create, update, or destroy Ansible Tower labels. See - Create, update, or destroy Ansible Tower labels. See
U(https://www.ansible.com/tower) for an overview. U(https://www.ansible.com/tower) for an overview.
- Note, labels can only be created via the Tower API, they can not be deleted.
Once they are fully disassociated the API will clean them up on its own.
options: options:
name: name:
description: description:
@@ -31,7 +33,6 @@ options:
new_name: new_name:
description: description:
- Setting this option will change the existing name (looked up via the name field). - Setting this option will change the existing name (looked up via the name field).
required: True
type: str type: str
organization: organization:
description: description:
@@ -51,7 +52,6 @@ options:
type: str type: str
version_added: "3.7" version_added: "3.7"
extends_documentation_fragment: awx.awx.auth extends_documentation_fragment: awx.awx.auth
note: Labels can only be created via the Tower API, they can not be deleted. Once they are fully disassociated the API will clean them up on its own.
''' '''
EXAMPLES = ''' EXAMPLES = '''
@@ -70,7 +70,7 @@ def main():
name=dict(required=True, type='str'), name=dict(required=True, type='str'),
new_name=dict(required=False, type='str'), new_name=dict(required=False, type='str'),
organization=dict(required=True, type='str'), organization=dict(required=True, type='str'),
state=dict(choices=['present', 'absent'], default='present'), state=dict(choices=['present'], default='present'),
) )
# Create a module for ourselves # Create a module for ourselves
@@ -97,7 +97,7 @@ def main():
# Create the data that gets sent for create and update # Create the data that gets sent for create and update
new_fields = {} new_fields = {}
new_fields['name'] = new_name if new_name else name new_fields['name'] = new_name if new_name else name
if organization != None: if organization:
new_fields['organization'] = organization_id new_fields['organization'] = organization_id
module.create_or_update_if_needed( module.create_or_update_if_needed(

View File

@@ -0,0 +1,47 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import pytest
from awx.main.models import Label
@pytest.mark.django_db
def test_create_label(run_module, admin_user, organization):
result = run_module('tower_label', dict(
name='test-label',
organization=organization.name
), admin_user)
assert not result.get('failed'), result.get('msg', result)
assert result.get('changed', False)
assert Label.objects.get(name='test-label').organization == organization
@pytest.mark.django_db
def test_create_label_using_org_id(run_module, admin_user, organization):
result = run_module('tower_label', dict(
name='test-label',
organization=organization.id
), admin_user)
assert not result.get('failed'), result.get('msg', result)
assert result.get('changed', False)
assert Label.objects.get(name='test-label').organization == organization
@pytest.mark.django_db
def test_modify_label(run_module, admin_user, organization):
label = Label.objects.create(name='test-label', organization=organization)
result = run_module('tower_label', dict(
name='test-label',
new_name='renamed-label',
organization=organization.name
), admin_user)
assert not result.get('failed'), result.get('msg', result)
assert result.get('changed', False)
label.refresh_from_db()
assert label.organization == organization
assert label.name == 'renamed-label'