From 3c89aeaba4005575386565aaab16a387621f012e Mon Sep 17 00:00:00 2001 From: Chris Meyers Date: Thu, 3 Aug 2017 16:27:21 -0400 Subject: [PATCH 1/2] fix single update_on_project_update per-inventory --- awx/main/models/inventory.py | 6 ++++-- awx/main/tests/functional/models/test_inventory.py | 3 +++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/awx/main/models/inventory.py b/awx/main/models/inventory.py index 660893ebf1..6e637cb536 100644 --- a/awx/main/models/inventory.py +++ b/awx/main/models/inventory.py @@ -16,6 +16,7 @@ from django.utils.translation import ugettext_lazy as _ from django.db import transaction from django.core.exceptions import ValidationError from django.utils.timezone import now +from django.db.models import Q # AWX from awx.api.versioning import reverse @@ -1394,8 +1395,9 @@ class InventorySource(UnifiedJobTemplate, InventorySourceOptions): if self.update_on_project_update is True and \ self.source == 'scm' and \ InventorySource.objects.filter( - inventory=self.inventory, - update_on_project_update=True, source='scm').exists(): + Q(inventory=self.inventory, + update_on_project_update=True, source='scm') & + ~Q(id=self.id)).exists(): raise ValidationError(_("Cannot update SCM-based inventory source on launch if set to update on project update. " "Instead, configure the corresponding source project to update on launch.")) return self.update_on_project_update diff --git a/awx/main/tests/functional/models/test_inventory.py b/awx/main/tests/functional/models/test_inventory.py index b342056739..fd31e50315 100644 --- a/awx/main/tests/functional/models/test_inventory.py +++ b/awx/main/tests/functional/models/test_inventory.py @@ -58,6 +58,9 @@ class TestSCMClean: inv_src1.clean_update_on_project_update() inv_src1.save() + inv_src1.source_vars = '---\nhello: world' + inv_src1.clean_update_on_project_update() + inv_src2 = InventorySource(inventory=inventory, update_on_project_update=True, source='scm') From 95c12ac1ab92cc564edd2554c62daf3e85c1e20c Mon Sep 17 00:00:00 2001 From: Chris Meyers Date: Mon, 7 Aug 2017 10:36:57 -0400 Subject: [PATCH 2/2] enforce muutally exclusivity on update_on_project_update and update_on_launch for scm inventory --- awx/main/models/inventory.py | 11 +++++++++-- awx/main/tests/functional/api/test_inventory.py | 5 ++--- awx/main/tests/unit/models/test_inventory.py | 8 ++++++++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/awx/main/models/inventory.py b/awx/main/models/inventory.py index 6e637cb536..11fc7cad45 100644 --- a/awx/main/models/inventory.py +++ b/awx/main/models/inventory.py @@ -1398,10 +1398,17 @@ class InventorySource(UnifiedJobTemplate, InventorySourceOptions): Q(inventory=self.inventory, update_on_project_update=True, source='scm') & ~Q(id=self.id)).exists(): - raise ValidationError(_("Cannot update SCM-based inventory source on launch if set to update on project update. " - "Instead, configure the corresponding source project to update on launch.")) + raise ValidationError(_("More than one SCM-based inventory source with update on project update on per-inventory not allowed.")) return self.update_on_project_update + def clean_update_on_launch(self): + if self.update_on_project_update is True and \ + self.source == 'scm' and \ + self.update_on_launch is True: + raise ValidationError(_("Cannot update SCM-based inventory source on launch if set to update on project update. " + "Instead, configure the corresponding source project to update on launch.")) + return self.update_on_launch + def clean_overwrite_vars(self): if self.source == 'scm' and not self.overwrite_vars: raise ValidationError(_("SCM type sources must set `overwrite_vars` to `true`.")) diff --git a/awx/main/tests/functional/api/test_inventory.py b/awx/main/tests/functional/api/test_inventory.py index de2a3f186e..db29682b7b 100644 --- a/awx/main/tests/functional/api/test_inventory.py +++ b/awx/main/tests/functional/api/test_inventory.py @@ -403,9 +403,8 @@ class TestControlledBySCM: {'update_on_project_update': True,}, admin_user, expect=400) content = json.loads(res.content) - assert content['update_on_project_update'] == ["Cannot update SCM-based inventory source on launch if set to update on " - "project update. Instead, configure the corresponding source project to " - "update on launch."] + assert content['update_on_project_update'] == ["More than one SCM-based inventory source with update on project update " + "on per-inventory not allowed."] def test_adding_inv_src_without_proj_access_prohibited(self, post, project, inventory, rando): inventory.admin_role.members.add(rando) diff --git a/awx/main/tests/unit/models/test_inventory.py b/awx/main/tests/unit/models/test_inventory.py index ad3018bd11..41fa27644c 100644 --- a/awx/main/tests/unit/models/test_inventory.py +++ b/awx/main/tests/unit/models/test_inventory.py @@ -108,3 +108,11 @@ class TestControlledBySCM(): with pytest.raises(ValidationError): inv_src.clean_source_path() + def test_clean_update_on_launch_update_on_project_update(self): + inv_src = InventorySource(update_on_project_update=True, + update_on_launch=True, + source='scm') + + with pytest.raises(ValidationError): + inv_src.clean_update_on_launch() +