1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Merge pull request #6260 from j2sol/6258

Avoid range selection on empty groups
This commit is contained in:
Michael DeHaan 2014-03-03 17:05:37 -05:00
commit eb80c8fb19
2 changed files with 11 additions and 0 deletions

View file

@ -227,6 +227,12 @@ class Inventory(object):
given a pattern like foo[0:5], where foo matches hosts, return the first 6 hosts given a pattern like foo[0:5], where foo matches hosts, return the first 6 hosts
""" """
# If there are no hosts to select from, just return the
# empty set. This prevents trying to do selections on an empty set.
# issue#6258
if not hosts:
return hosts
(loose_pattern, limits) = self._enumeration_info(pat) (loose_pattern, limits) = self._enumeration_info(pat)
if not limits: if not limits:
return hosts return hosts

View file

@ -212,6 +212,11 @@ class TestInventory(unittest.TestCase):
inventory.subset('greek[0-2];norse[0]') inventory.subset('greek[0-2];norse[0]')
self.assertEqual(sorted(inventory.list_hosts()), sorted(['zeus','hera','thor'])) self.assertEqual(sorted(inventory.list_hosts()), sorted(['zeus','hera','thor']))
def test_subet_range_empty_group(self):
inventory = self.simple_inventory()
inventory.subset('missing[0]')
self.assertEqual(sorted(inventory.list_hosts()), sorted([]))
def test_subset_filename(self): def test_subset_filename(self):
inventory = self.simple_inventory() inventory = self.simple_inventory()
inventory.subset('@' + os.path.join(self.test_dir, 'restrict_pattern')) inventory.subset('@' + os.path.join(self.test_dir, 'restrict_pattern'))