summaryrefslogtreecommitdiffstats
path: root/awx_collection/test
diff options
context:
space:
mode:
authorJohn Westcott IV <32551173+john-westcott-iv@users.noreply.github.com>2023-07-17 19:16:55 +0200
committerGitHub <noreply@github.com>2023-07-17 19:16:55 +0200
commit8ddc19a927049b84f3f6bcfaae6595032f4a7789 (patch)
treead9e5977903e628fc1279e3a8029a52323b68a94 /awx_collection/test
parentAllow `job_template` collection module to set verbosity to 5 (#14244) (diff)
downloadawx-8ddc19a927049b84f3f6bcfaae6595032f4a7789.tar.xz
awx-8ddc19a927049b84f3f6bcfaae6595032f4a7789.zip
Changing how associations work in awx collection (#13626)
Co-authored-by: Alan Rominger <arominge@redhat.com> Co-authored-by: Jessica Steurer <70719005+jay-steurer@users.noreply.github.com>
Diffstat (limited to 'awx_collection/test')
-rw-r--r--awx_collection/test/awx/test_job_template.py40
-rw-r--r--awx_collection/test/awx/test_organization.py63
2 files changed, 101 insertions, 2 deletions
diff --git a/awx_collection/test/awx/test_job_template.py b/awx_collection/test/awx/test_job_template.py
index e785a63a34..5ee114359b 100644
--- a/awx_collection/test/awx/test_job_template.py
+++ b/awx_collection/test/awx/test_job_template.py
@@ -2,9 +2,11 @@ from __future__ import absolute_import, division, print_function
__metaclass__ = type
+import random
+
import pytest
-from awx.main.models import ActivityStream, JobTemplate, Job, NotificationTemplate
+from awx.main.models import ActivityStream, JobTemplate, Job, NotificationTemplate, Label
@pytest.mark.django_db
@@ -244,6 +246,42 @@ def test_job_template_with_survey_encrypted_default(run_module, admin_user, proj
@pytest.mark.django_db
+def test_associate_changed_status(run_module, admin_user, organization, project):
+ # create JT and labels
+ jt = JobTemplate.objects.create(name='foo', project=project, playbook='helloworld.yml')
+ labels = [Label.objects.create(name=f'foo{i}', organization=organization) for i in range(10)]
+
+ # sanity: no-op without labels involved
+ result = run_module('job_template', dict(name=jt.name, playbook='helloworld.yml'), admin_user)
+ assert not result.get('failed', False), result.get('msg', result)
+ assert result['changed'] is False
+
+ # first time adding labels, this should make the label list equal to what was specified
+ result = run_module('job_template', dict(name=jt.name, playbook='helloworld.yml', labels=[l.name for l in labels]), admin_user)
+ assert not result.get('failed', False), result.get('msg', result)
+ assert result['changed']
+ assert set(l.id for l in jt.labels.all()) == set(l.id for l in labels)
+
+ # shuffling the labels should not result in any change
+ random.shuffle(labels)
+ result = run_module('job_template', dict(name=jt.name, playbook='helloworld.yml', labels=[l.name for l in labels]), admin_user)
+ assert not result.get('failed', False), result.get('msg', result)
+ assert result['changed'] is False
+
+ # not specifying labels should not change labels
+ result = run_module('job_template', dict(name=jt.name, playbook='helloworld.yml'), admin_user)
+ assert not result.get('failed', False), result.get('msg', result)
+ assert result['changed'] is False
+
+ # should be able to remove only some labels
+ fewer_labels = labels[:7]
+ result = run_module('job_template', dict(name=jt.name, playbook='helloworld.yml', labels=[l.name for l in fewer_labels]), admin_user)
+ assert not result.get('failed', False), result.get('msg', result)
+ assert result['changed']
+ assert set(l.id for l in jt.labels.all()) == set(l.id for l in fewer_labels)
+
+
+@pytest.mark.django_db
def test_associate_only_on_success(run_module, admin_user, organization, project):
jt = JobTemplate.objects.create(
name='foo',
diff --git a/awx_collection/test/awx/test_organization.py b/awx_collection/test/awx/test_organization.py
index 08d35ed6f7..e6b3cc5e2d 100644
--- a/awx_collection/test/awx/test_organization.py
+++ b/awx_collection/test/awx/test_organization.py
@@ -3,8 +3,9 @@ from __future__ import absolute_import, division, print_function
__metaclass__ = type
import pytest
+import random
-from awx.main.models import Organization
+from awx.main.models import Organization, Credential, CredentialType
@pytest.mark.django_db
@@ -30,3 +31,63 @@ def test_create_organization(run_module, admin_user):
assert result == {"name": "foo", "changed": True, "id": org.id, "invocation": {"module_args": module_args}}
assert org.description == 'barfoo'
+
+
+@pytest.mark.django_db
+def test_galaxy_credential_order(run_module, admin_user):
+ org = Organization.objects.create(name='foo')
+ cred_type = CredentialType.defaults['galaxy_api_token']()
+ cred_type.save()
+
+ cred_ids = []
+ for number in range(1, 10):
+ new_cred = Credential.objects.create(name=f"Galaxy Credential {number}", credential_type=cred_type, organization=org, inputs={'url': 'www.redhat.com'})
+ cred_ids.append(new_cred.id)
+
+ random.shuffle(cred_ids)
+
+ module_args = {
+ 'name': 'foo',
+ 'state': 'present',
+ 'controller_host': None,
+ 'controller_username': None,
+ 'controller_password': None,
+ 'validate_certs': None,
+ 'controller_oauthtoken': None,
+ 'controller_config_file': None,
+ 'galaxy_credentials': cred_ids,
+ }
+
+ result = run_module('organization', module_args, admin_user)
+ print(result)
+ assert result['changed'] is True
+
+ cred_order_in_org = []
+ for a_cred in org.galaxy_credentials.all():
+ cred_order_in_org.append(a_cred.id)
+
+ assert cred_order_in_org == cred_ids
+
+ # Shuffle them up and try again to make sure a new order is honored
+ random.shuffle(cred_ids)
+
+ module_args = {
+ 'name': 'foo',
+ 'state': 'present',
+ 'controller_host': None,
+ 'controller_username': None,
+ 'controller_password': None,
+ 'validate_certs': None,
+ 'controller_oauthtoken': None,
+ 'controller_config_file': None,
+ 'galaxy_credentials': cred_ids,
+ }
+
+ result = run_module('organization', module_args, admin_user)
+ assert result['changed'] is True
+
+ cred_order_in_org = []
+ for a_cred in org.galaxy_credentials.all():
+ cred_order_in_org.append(a_cred.id)
+
+ assert cred_order_in_org == cred_ids