TestInventory.py 10.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
import os
import unittest

from ansible.inventory import Inventory

class TestInventory(unittest.TestCase):

    def setUp(self):
        self.cwd = os.getcwd()
        self.test_dir = os.path.join(self.cwd, 'test')

12 13 14 15
        self.inventory_file             = os.path.join(self.test_dir, 'simple_hosts')
        self.large_range_inventory_file = os.path.join(self.test_dir, 'large_range')
        self.complex_inventory_file     = os.path.join(self.test_dir, 'complex_hosts')
        self.inventory_script           = os.path.join(self.test_dir, 'inventory_api.py')
16
        self.inventory_dir              = os.path.join(self.test_dir, 'inventory_dir')
17 18 19 20 21 22

        os.chmod(self.inventory_script, 0755)

    def tearDown(self):
        os.chmod(self.inventory_script, 0644)

23 24 25 26
    def compare(self, left, right, sort=True):
        if sort:
            left = sorted(left)
            right = sorted(right)
27 28 29 30
        print left
        print right
        assert left == right

31
    def simple_inventory(self):
32
        return Inventory(self.inventory_file)
33

34 35 36
    def large_range_inventory(self):
        return Inventory(self.large_range_inventory_file)

37
    def script_inventory(self):
38
        return Inventory(self.inventory_script)
39

40 41 42
    def complex_inventory(self):
        return Inventory(self.complex_inventory_file)

43 44 45
    def dir_inventory(self):
        return Inventory(self.inventory_dir)

46 47 48 49 50 51 52 53 54
    all_simple_hosts=['jupiter', 'saturn', 'zeus', 'hera',
            'cerberus001','cerberus002','cerberus003',
            'cottus99', 'cottus100',
            'poseidon', 'thor', 'odin', 'loki',
            'thrudgelmir0', 'thrudgelmir1', 'thrudgelmir2',
            'thrudgelmir3', 'thrudgelmir4', 'thrudgelmir5',
            'Hotep-a', 'Hotep-b', 'Hotep-c',
            'BastC', 'BastD', ]

55 56
    #####################################
    ### Simple inventory format tests
57

58 59 60
    def test_simple(self):
        inventory = self.simple_inventory()
        hosts = inventory.list_hosts()
61
        self.assertEqual(sorted(hosts), sorted(self.all_simple_hosts))
62 63 64 65

    def test_simple_all(self):
        inventory = self.simple_inventory()
        hosts = inventory.list_hosts('all')
66
        self.assertEqual(sorted(hosts), sorted(self.all_simple_hosts))
67 68 69 70 71 72

    def test_simple_norse(self):
        inventory = self.simple_inventory()
        hosts = inventory.list_hosts("norse")

        expected_hosts=['thor', 'odin', 'loki']
73
        assert sorted(hosts) == sorted(expected_hosts)
74

75 76 77 78
    def test_simple_ungrouped(self):
        inventory = self.simple_inventory()
        hosts = inventory.list_hosts("ungrouped")

Michael DeHaan committed
79 80
        expected_hosts=['jupiter', 'saturn',
                        'thrudgelmir0', 'thrudgelmir1', 'thrudgelmir2',
81
                        'thrudgelmir3', 'thrudgelmir4', 'thrudgelmir5']
82
        assert sorted(hosts) == sorted(expected_hosts)
83

84 85 86 87
    def test_simple_combined(self):
        inventory = self.simple_inventory()
        hosts = inventory.list_hosts("norse:greek")

Michael DeHaan committed
88
        expected_hosts=['zeus', 'hera', 'poseidon',
89 90 91
                        'cerberus001','cerberus002','cerberus003',
                        'cottus99','cottus100',
                        'thor', 'odin', 'loki']
92
        assert sorted(hosts) == sorted(expected_hosts)
93 94 95 96 97

    def test_simple_restrict(self):
        inventory = self.simple_inventory()

        restricted_hosts = ['hera', 'poseidon', 'thor']
Michael DeHaan committed
98
        expected_hosts=['zeus', 'hera', 'poseidon',
99 100 101
                        'cerberus001','cerberus002','cerberus003',
                        'cottus99', 'cottus100',
                        'thor', 'odin', 'loki']
102 103 104 105

        inventory.restrict_to(restricted_hosts)
        hosts = inventory.list_hosts("norse:greek")

106 107 108
        print "Hosts=%s" % hosts
        print "Restricted=%s" % restricted_hosts
        assert sorted(hosts) == sorted(restricted_hosts)
109 110 111 112

        inventory.lift_restriction()
        hosts = inventory.list_hosts("norse:greek")

113 114 115
        print hosts
        print expected_hosts
        assert sorted(hosts) == sorted(expected_hosts)
116 117 118 119 120

    def test_simple_vars(self):
        inventory = self.simple_inventory()
        vars = inventory.get_variables('thor')

121
        print vars
122
        assert vars == {'group_names': ['norse'],
123 124
                        'inventory_hostname': 'thor',
                        'inventory_hostname_short': 'thor'}
125

126 127 128 129
    def test_simple_port(self):
        inventory = self.simple_inventory()
        vars = inventory.get_variables('hera')

130
        print vars
131 132 133 134
        expected = { 'ansible_ssh_port': 3000,
                     'group_names': ['greek'],
                     'inventory_hostname': 'hera',
                     'inventory_hostname_short': 'hera' }
135 136
        print expected
        assert vars == expected
137

138 139 140 141 142
    def test_large_range(self):
        inventory = self.large_range_inventory()
        hosts = inventory.list_hosts()
        self.assertEqual(sorted(hosts),  sorted('bob%03i' %i  for i in range(0, 143)))

143 144 145 146 147 148 149 150 151 152
    ###################################################
    ### INI file advanced tests

    def test_complex_vars(self):
        inventory = self.complex_inventory()

        vars = inventory.get_variables('rtp_a')
        print vars

        expected = dict(
153 154 155
            a='1', b='2', c='3', d='10002', e='10003', f='10004 != 10005',
            g='  g  ', h='  h  ', i="'  i  \"", j='"  j',
            rga='1', rgb='2', rgc='3',
Michael DeHaan committed
156
            inventory_hostname='rtp_a', inventory_hostname_short='rtp_a',
157
            group_names=[ 'eastcoast', 'nc', 'redundantgroup', 'redundantgroup2', 'redundantgroup3', 'rtp', 'us' ]
158 159 160 161 162
        )
        print vars
        print expected
        assert vars == expected

163 164 165 166 167 168 169 170 171 172 173
    def test_complex_group_names(self):
        inventory = self.complex_inventory()
        tests = {
            'host1': [ 'role1' ],
            'host2': [ 'role1', 'role2' ],
            'host3': [ 'role2' ]
        }
        for host, roles in tests.iteritems():
            group_names = inventory.get_variables(host)['group_names']
            assert sorted(group_names) == sorted(roles)

174 175
    def test_complex_exclude(self):
        inventory = self.complex_inventory()
176
        hosts = inventory.list_hosts("nc:florida:!triangle:!orlando")
177
        expected_hosts = ['miami', 'rtp_a', 'rtp_b', 'rtp_c']
178 179
        print "HOSTS=%s" % sorted(hosts)
        print "EXPECTED=%s" % sorted(expected_hosts)
180 181
        assert sorted(hosts) == sorted(expected_hosts)

182 183 184 185 186 187 188 189
    def test_regex_exclude(self):
        inventory = self.complex_inventory()
        hosts = inventory.list_hosts("~rtp_[ac]")
        expected_hosts = ['rtp_a', 'rtp_c']
        print "HOSTS=%s" % sorted(hosts)
        print "EXPECTED=%s" % sorted(expected_hosts)
        assert sorted(hosts) == sorted(expected_hosts)

190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
    def test_complex_enumeration(self):


        expected1 = ['rtp_a', 'rtp_b']
        expected2 = ['rtp_c', 'tri_a']
        expected3 = ['rtp_b', 'rtp_c', 'tri_a', 'tri_b', 'tri_c']
        expected4 = ['orlando', 'rtp_c', 'tri_a']

        inventory = self.complex_inventory()
        print "ALL NC=%s" % inventory.list_hosts("nc")
        hosts = inventory.list_hosts("nc[0-1]")
        self.compare(hosts, expected1, sort=False)
        hosts = inventory.list_hosts("nc[2-3]")
        self.compare(hosts, expected2, sort=False)
        hosts = inventory.list_hosts("nc[1-99999]")
        self.compare(hosts, expected3, sort=False)
        hosts = inventory.list_hosts("nc[2-3]:florida[1-2]")
        self.compare(hosts, expected4, sort=False)

209 210 211 212 213 214 215
    def test_complex_intersect(self):
        inventory = self.complex_inventory()
        hosts = inventory.list_hosts("nc:&redundantgroup:!rtp_c")
        self.compare(hosts, ['rtp_a'])
        hosts = inventory.list_hosts("nc:&triangle:!tri_c")
        self.compare(hosts, ['tri_a', 'tri_b'])

216

217
    ###################################################
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
    ### Inventory API tests

    def test_script(self):
        inventory = self.script_inventory()
        hosts = inventory.list_hosts()

        expected_hosts=['jupiter', 'saturn', 'zeus', 'hera', 'poseidon', 'thor', 'odin', 'loki']

        print "Expected: %s"%(expected_hosts)
        print "Got     : %s"%(hosts)
        assert sorted(hosts) == sorted(expected_hosts)

    def test_script_all(self):
        inventory = self.script_inventory()
        hosts = inventory.list_hosts('all')

        expected_hosts=['jupiter', 'saturn', 'zeus', 'hera', 'poseidon', 'thor', 'odin', 'loki']
        assert sorted(hosts) == sorted(expected_hosts)

    def test_script_norse(self):
        inventory = self.script_inventory()
        hosts = inventory.list_hosts("norse")

        expected_hosts=['thor', 'odin', 'loki']
        assert sorted(hosts) == sorted(expected_hosts)

    def test_script_combined(self):
        inventory = self.script_inventory()
        hosts = inventory.list_hosts("norse:greek")

        expected_hosts=['zeus', 'hera', 'poseidon', 'thor', 'odin', 'loki']
        assert sorted(hosts) == sorted(expected_hosts)

    def test_script_restrict(self):
        inventory = self.script_inventory()

        restricted_hosts = ['hera', 'poseidon', 'thor']
        expected_hosts=['zeus', 'hera', 'poseidon', 'thor', 'odin', 'loki']

        inventory.restrict_to(restricted_hosts)
        hosts = inventory.list_hosts("norse:greek")

        assert sorted(hosts) == sorted(restricted_hosts)

        inventory.lift_restriction()
        hosts = inventory.list_hosts("norse:greek")

        assert sorted(hosts) == sorted(expected_hosts)

    def test_script_vars(self):
        inventory = self.script_inventory()
        vars = inventory.get_variables('thor')

271 272
        print "VARS=%s" % vars

273 274
        assert vars == {'hammer':True,
                        'group_names': ['norse'],
275 276
                        'inventory_hostname': 'thor',
                        'inventory_hostname_short': 'thor'}
277 278

    def test_hosts_list(self):
Michael DeHaan committed
279
        # Test the case when playbook 'hosts' var is a list.
280 281 282 283 284
        inventory = self.script_inventory()
        host_names = sorted(['thor', 'loki', 'odin'])       # Not sure if sorting is in the contract or not
        actual_hosts = inventory.get_hosts(host_names)
        actual_host_names = [host.name for host in actual_hosts]
        assert host_names == actual_host_names
285 286 287 288 289 290

    def test_script_multiple_groups(self):
        inventory = self.script_inventory()
        vars = inventory.get_variables('zeus')

        print "VARS=%s" % vars
291

292 293 294
        assert vars == {'inventory_hostname': 'zeus',
                        'inventory_hostname_short': 'zeus',
                        'group_names': ['greek', 'major-god']}
295 296 297 298 299 300 301 302 303 304 305

    def test_dir_inventory(self):
        inventory = self.dir_inventory()
        vars = inventory.get_variables('zeus')

        print "VARS=%s" % vars

        assert vars == {'inventory_hostname': 'zeus',
                        'inventory_hostname_short': 'zeus',
                        'group_names': ['greek', 'major-god', 'ungrouped'],
                        'var_a': '1'}