|
22 | 22 | import string |
23 | 23 |
|
24 | 24 | from ansible.compat.tests import unittest |
25 | | -from ansible.compat.tests.mock import patch, MagicMock |
| 25 | +from ansible.compat.tests.mock import patch |
26 | 26 |
|
27 | | -from ansible.errors import AnsibleError, AnsibleParserError |
28 | 27 | from ansible.inventory import Inventory |
29 | 28 | from ansible.inventory.expand_hosts import expand_hostname_range |
30 | 29 | from ansible.vars import VariableManager |
@@ -115,3 +114,60 @@ def test_expand_hostname_range(self): |
115 | 114 | for e in self.ranges_to_expand: |
116 | 115 | r = self.ranges_to_expand[e] |
117 | 116 | self.assertEqual(r, expand_hostname_range(e)) |
| 117 | + |
| 118 | + |
| 119 | +class InventoryDefaultGroup(unittest.TestCase): |
| 120 | + |
| 121 | + def test_empty_inventory(self): |
| 122 | + inventory = self._get_inventory('') |
| 123 | + |
| 124 | + self.assertIn('all', inventory.groups) |
| 125 | + self.assertIn('ungrouped', inventory.groups) |
| 126 | + self.assertFalse(inventory.groups['all'].get_hosts()) |
| 127 | + self.assertFalse(inventory.groups['ungrouped'].get_hosts()) |
| 128 | + |
| 129 | + def test_ini(self): |
| 130 | + self._test_default_groups(""" |
| 131 | + host1 |
| 132 | + host2 |
| 133 | + host3 |
| 134 | + [servers] |
| 135 | + host3 |
| 136 | + host4 |
| 137 | + host5 |
| 138 | + """) |
| 139 | + |
| 140 | + def test_ini_explicit_ungrouped(self): |
| 141 | + self._test_default_groups(""" |
| 142 | + [ungrouped] |
| 143 | + host1 |
| 144 | + host2 |
| 145 | + host3 |
| 146 | + [servers] |
| 147 | + host3 |
| 148 | + host4 |
| 149 | + host5 |
| 150 | + """) |
| 151 | + |
| 152 | + def _get_inventory(self, inventory_content): |
| 153 | + v = VariableManager() |
| 154 | + |
| 155 | + fake_loader = DictDataLoader({ |
| 156 | + 'hosts': inventory_content |
| 157 | + }) |
| 158 | + |
| 159 | + with patch.object(Inventory, 'basedir') as mock_basedir: |
| 160 | + mock_basedir.return_value = './' |
| 161 | + return Inventory(loader=fake_loader, variable_manager=v, host_list='hosts') |
| 162 | + |
| 163 | + def _test_default_groups(self, inventory_content): |
| 164 | + inventory = self._get_inventory(inventory_content) |
| 165 | + |
| 166 | + self.assertIn('all', inventory.groups) |
| 167 | + self.assertIn('ungrouped', inventory.groups) |
| 168 | + all_hosts = set(host.name for host in inventory.groups['all'].get_hosts()) |
| 169 | + self.assertEqual(set(['host1', 'host2', 'host3', 'host4', 'host5']), all_hosts) |
| 170 | + ungrouped_hosts = set(host.name for host in inventory.groups['ungrouped'].get_hosts()) |
| 171 | + self.assertEqual(set(['host1', 'host2', 'host3']), ungrouped_hosts) |
| 172 | + servers_hosts = set(host.name for host in inventory.groups['servers'].get_hosts()) |
| 173 | + self.assertEqual(set(['host3', 'host4', 'host5']), servers_hosts) |
0 commit comments