@ -22,9 +22,8 @@ __metaclass__ = type
import string
from ansible . compat . tests import unittest
from ansible . compat . tests . mock import patch , MagicMock
from ansible . compat . tests . mock import patch
from ansible . errors import AnsibleError , AnsibleParserError
from ansible . inventory import Inventory
from ansible . inventory . expand_hosts import expand_hostname_range
from ansible . vars import VariableManager
@ -115,3 +114,60 @@ class TestInventory(unittest.TestCase):
for e in self . ranges_to_expand :
r = self . ranges_to_expand [ e ]
self . assertEqual ( r , expand_hostname_range ( e ) )
class InventoryDefaultGroup ( unittest . TestCase ) :
def test_empty_inventory ( self ) :
inventory = self . _get_inventory ( ' ' )
self . assertIn ( ' all ' , inventory . groups )
self . assertIn ( ' ungrouped ' , inventory . groups )
self . assertFalse ( inventory . groups [ ' all ' ] . get_hosts ( ) )
self . assertFalse ( inventory . groups [ ' ungrouped ' ] . get_hosts ( ) )
def test_ini ( self ) :
self . _test_default_groups ( """
host1
host2
host3
[ servers ]
host3
host4
host5
""" )
def test_ini_explicit_ungrouped ( self ) :
self . _test_default_groups ( """
[ ungrouped ]
host1
host2
host3
[ servers ]
host3
host4
host5
""" )
def _get_inventory ( self , inventory_content ) :
v = VariableManager ( )
fake_loader = DictDataLoader ( {
' hosts ' : inventory_content
} )
with patch . object ( Inventory , ' basedir ' ) as mock_basedir :
mock_basedir . return_value = ' ./ '
return Inventory ( loader = fake_loader , variable_manager = v , host_list = ' hosts ' )
def _test_default_groups ( self , inventory_content ) :
inventory = self . _get_inventory ( inventory_content )
self . assertIn ( ' all ' , inventory . groups )
self . assertIn ( ' ungrouped ' , inventory . groups )
all_hosts = set ( host . name for host in inventory . groups [ ' all ' ] . get_hosts ( ) )
self . assertEqual ( set ( [ ' host1 ' , ' host2 ' , ' host3 ' , ' host4 ' , ' host5 ' ] ) , all_hosts )
ungrouped_hosts = set ( host . name for host in inventory . groups [ ' ungrouped ' ] . get_hosts ( ) )
self . assertEqual ( set ( [ ' host1 ' , ' host2 ' , ' host3 ' ] ) , ungrouped_hosts )
servers_hosts = set ( host . name for host in inventory . groups [ ' servers ' ] . get_hosts ( ) )
self . assertEqual ( set ( [ ' host3 ' , ' host4 ' , ' host5 ' ] ) , servers_hosts )