From d7f31f9777300a8b478a27ba364499522981e4f5 Mon Sep 17 00:00:00 2001 From: Chris Church Date: Mon, 31 Mar 2014 17:37:35 -0400 Subject: [PATCH] Fix edge case in inventory import that would cause a group to become its own parent. --- .../management/commands/inventory_import.py | 2 +- awx/main/tests/inventory.py | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/awx/main/management/commands/inventory_import.py b/awx/main/management/commands/inventory_import.py index c17b9e625f..cc25e675be 100644 --- a/awx/main/management/commands/inventory_import.py +++ b/awx/main/management/commands/inventory_import.py @@ -649,7 +649,7 @@ class Command(NoArgsCommand): self.logger.info('Group "%s" variables updated', k) else: self.logger.info('Group "%s" variables unmodified', k) - if self.inventory_source.group: + if self.inventory_source.group and self.inventory_source.group != group: self.inventory_source.group.children.add(group) group.inventory_sources.add(self.inventory_source) diff --git a/awx/main/tests/inventory.py b/awx/main/tests/inventory.py index db0da1664d..e33baaa4bd 100644 --- a/awx/main/tests/inventory.py +++ b/awx/main/tests/inventory.py @@ -1270,6 +1270,11 @@ class InventoryUpdatesTest(BaseTransactionTest): user=self.super_django_user, username=source_username, password=source_password) + # Set parent group name to one that might be created by the sync. + group = self.group + group.name = 'ec2' + group.save() + self.group = group inventory_source = self.update_inventory_source(self.group, source='ec2', credential=credential, source_regions=source_regions, source_vars='---') @@ -1279,7 +1284,10 @@ class InventoryUpdatesTest(BaseTransactionTest): host.enabled = False host.save() self.check_inventory_source(inventory_source, initial=False) - + # Verify that main group is in top level groups (hasn't been added as + # its own child). + self.assertTrue(self.group in self.inventory.root_groups) + def test_update_from_rax(self): source_username = getattr(settings, 'TEST_RACKSPACE_USERNAME', '') source_password = getattr(settings, 'TEST_RACKSPACE_API_KEY', '') @@ -1290,6 +1298,11 @@ class InventoryUpdatesTest(BaseTransactionTest): user=self.super_django_user, username=source_username, password=source_password) + # Set parent group name to one that might be created by the sync. + group = self.group + group.name = 'DFW' + group.save() + self.group = group inventory_source = self.update_inventory_source(self.group, source='rax', credential=credential, source_regions=source_regions) self.check_inventory_source(inventory_source) @@ -1303,3 +1316,6 @@ class InventoryUpdatesTest(BaseTransactionTest): inventory_source2 = self.update_inventory_source(self.group2, source='rax', credential=credential, source_regions='') self.check_inventory_source(inventory_source2) + # Verify that main group is in top level groups (hasn't been added as + # its own child). + self.assertTrue(self.group in self.inventory.root_groups)