TestInventory.py 11.6 KB
Newer Older
1 2 3 4
import os
import unittest

from ansible.inventory import Inventory
5
from ansible.runner import Runner
6
# from nose.plugins.skip import SkipTest
7 8 9 10 11 12 13

class TestInventory(unittest.TestCase):

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

14 15 16 17
        self.inventory_file         = os.path.join(self.test_dir, 'simple_hosts')
        self.complex_inventory_file = os.path.join(self.test_dir, 'complex_hosts')
        self.inventory_script       = os.path.join(self.test_dir, 'inventory_api.py')
        self.inventory_yaml         = os.path.join(self.test_dir, 'yaml_hosts')
18 19 20 21 22 23

        os.chmod(self.inventory_script, 0755)

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

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


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

    def script_inventory(self):
36
        return Inventory(self.inventory_script)
37

38
    def yaml_inventory(self):
39 40 41 42 43 44 45
        return Inventory(self.inventory_yaml)

    def complex_inventory(self):
        return Inventory(self.complex_inventory_file)

    #####################################
    ### Simple inventory format tests
46

47 48 49 50 51
    def test_simple(self):
        inventory = self.simple_inventory()
        hosts = inventory.list_hosts()

        expected_hosts=['jupiter', 'saturn', 'zeus', 'hera', 'poseidon', 'thor', 'odin', 'loki']
52
        assert sorted(hosts) == sorted(expected_hosts)
53 54 55 56 57 58

    def test_simple_all(self):
        inventory = self.simple_inventory()
        hosts = inventory.list_hosts('all')

        expected_hosts=['jupiter', 'saturn', 'zeus', 'hera', 'poseidon', 'thor', 'odin', 'loki']
59
        assert sorted(hosts) == sorted(expected_hosts)
60 61 62 63 64 65

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

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

68 69 70 71 72
    def test_simple_ungrouped(self):
        inventory = self.simple_inventory()
        hosts = inventory.list_hosts("ungrouped")

        expected_hosts=['jupiter', 'saturn']
73
        assert sorted(hosts) == sorted(expected_hosts)
74

75 76 77 78 79
    def test_simple_combined(self):
        inventory = self.simple_inventory()
        hosts = inventory.list_hosts("norse:greek")

        expected_hosts=['zeus', 'hera', 'poseidon', 'thor', 'odin', 'loki']
80
        assert sorted(hosts) == sorted(expected_hosts)
81 82 83 84 85 86 87 88 89 90

    def test_simple_restrict(self):
        inventory = self.simple_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")

91 92 93
        print "Hosts=%s" % hosts
        print "Restricted=%s" % restricted_hosts
        assert sorted(hosts) == sorted(restricted_hosts)
94 95 96 97

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

98 99 100
        print hosts
        print expected_hosts
        assert sorted(hosts) == sorted(expected_hosts)
101

102 103 104 105 106 107 108 109 110 111 112
    def test_simple_exclude(self):
        inventory = self.simple_inventory()

        hosts = inventory.list_hosts("all:!greek")
        expected_hosts=['jupiter', 'saturn', 'thor', 'odin', 'loki']
        assert sorted(hosts) == sorted(expected_hosts)

        hosts = inventory.list_hosts("all:!norse:!greek")
        expected_hosts=['jupiter', 'saturn']
        assert sorted(hosts) == sorted(expected_hosts)

113 114 115 116
    def test_simple_vars(self):
        inventory = self.simple_inventory()
        vars = inventory.get_variables('thor')

117
        print vars
118 119
        assert vars == {'group_names': ['norse'],
                        'inventory_hostname': 'thor'}
120

121 122 123 124
    def test_simple_port(self):
        inventory = self.simple_inventory()
        vars = inventory.get_variables('hera')

125 126
        print vars
        expected = {'ansible_ssh_port': 3000,
127 128
                        'group_names': ['greek'],
                        'inventory_hostname': 'hera'}
129 130
        print expected
        assert vars == expected
131

132 133 134 135 136 137 138 139 140 141
    ###################################################
    ### INI file advanced tests

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

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

        expected = dict(
142
            a='1', b='2', c='3', d='100002', rga='1', rgb='2', rgc='3', 
143
            inventory_hostname='rtp_a', 
144
            group_names=[ 'eastcoast', 'nc', 'redundantgroup', 'redundantgroup2', 'redundantgroup3', 'rtp', 'us' ]
145 146 147 148 149
        )
        print vars
        print expected
        assert vars == expected

150 151 152 153 154 155 156
    def test_complex_exclude(self):
        inventory = self.complex_inventory()

        hosts = inventory.list_hosts("nc:!triangle:florida:!orlando")
        expected_hosts=['rtp_a', 'rtp_b', 'rtb_c', 'miami']
        assert sorted(hosts) == sorted(expected_hosts)

157 158 159


    ###################################################
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
    ### 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')

213 214
        print "VARS=%s" % vars

215 216 217
        assert vars == {'hammer':True,
                        'group_names': ['norse'],
                        'inventory_hostname': 'thor'}
218

219 220 221 222 223
    ### Tests for yaml inventory file

    def test_yaml(self):
        inventory = self.yaml_inventory()
        hosts = inventory.list_hosts()
224
        expected_hosts=['garfield', 'goofy', 'hera', 'jerry', 'jupiter', 'loki', 'mars', 'mickey', 'odie', 'odin', 'poseidon', 'saturn', 'thor', 'tom', 'zeus']
225
        self.compare(hosts, expected_hosts)
226 227 228 229 230

    def test_yaml_all(self):
        inventory = self.yaml_inventory()
        hosts = inventory.list_hosts('all')

231
        expected_hosts=['garfield', 'goofy', 'hera', 'jerry', 'jupiter', 'loki', 'mars', 'mickey', 'odie', 'odin', 'poseidon', 'saturn', 'thor', 'tom', 'zeus']
232
        self.compare(hosts, expected_hosts)
233 234 235 236 237 238

    def test_yaml_norse(self):
        inventory = self.yaml_inventory()
        hosts = inventory.list_hosts("norse")

        expected_hosts=['thor', 'odin', 'loki']
239
        self.compare(hosts, expected_hosts)
240

241
    def test_yaml_ungrouped(self):
242 243 244
        inventory = self.yaml_inventory()
        hosts = inventory.list_hosts("ungrouped")

245
        expected_hosts=['jupiter', 'mars']
246
        self.compare(hosts, expected_hosts)
247

248 249 250 251 252
    def test_yaml_combined(self):
        inventory = self.yaml_inventory()
        hosts = inventory.list_hosts("norse:greek")

        expected_hosts=['zeus', 'hera', 'poseidon', 'thor', 'odin', 'loki']
253
        self.compare(hosts, expected_hosts)
254 255 256 257 258 259 260 261 262 263

    def test_yaml_restrict(self):
        inventory = self.yaml_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")

264
        self.compare(hosts, restricted_hosts)
265 266 267 268

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

269
        self.compare(hosts, expected_hosts)
270 271 272 273

    def test_yaml_vars(self):
        inventory = self.yaml_inventory()
        vars = inventory.get_variables('thor')
274 275 276
        assert vars == {'group_names': ['norse'],
                        'hammer':True,
                        'inventory_hostname': 'thor'}
277

278 279 280 281 282 283 284 285
    def test_yaml_list_vars(self):
        inventory = self.yaml_inventory()
        vars = inventory.get_variables('zeus')
        assert vars == {'ansible_ssh_port': 3001,
                        'group_names': ['greek', 'ruler'],
                        'inventory_hostname': 'zeus',
                        'ntp_server': 'olympus.example.com'}

286 287 288 289 290 291 292
    def test_yaml_change_vars(self):
        inventory = self.yaml_inventory()
        vars = inventory.get_variables('thor')

        vars["hammer"] = False

        vars = inventory.get_variables('thor')
293
        print vars
294 295 296
        assert vars == {'hammer':True,
                        'inventory_hostname': 'thor',
                        'group_names': ['norse']}
297

298 299 300 301
    def test_yaml_host_vars(self):
        inventory = self.yaml_inventory()
        vars = inventory.get_variables('saturn')

302
        print vars
303 304 305
        assert vars == {'inventory_hostname': 'saturn',
                        'moon': 'titan',
                        'moon2': 'enceladus',
306
                        'group_names': ['multiple']}
307 308 309 310 311

    def test_yaml_port(self):
        inventory = self.yaml_inventory()
        vars = inventory.get_variables('hera')

312
        print vars
313
        assert vars == {'ansible_ssh_port': 3000,
314
                        'inventory_hostname': 'hera',
315 316 317 318 319 320 321 322 323
                        'ntp_server': 'olympus.example.com',
                        'group_names': ['greek']}

    def test_yaml_multiple_groups(self):
        inventory = self.yaml_inventory()
        vars = inventory.get_variables('odin')

        assert 'group_names' in vars
        assert sorted(vars['group_names']) == [ 'norse', 'ruler' ]
324

325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
    def test_yaml_some_animals(self):
        inventory = self.yaml_inventory()
        hosts = inventory.list_hosts("cat:mouse")
        expected_hosts=['garfield', 'jerry', 'mickey', 'tom']
        self.compare(hosts, expected_hosts)

    def test_yaml_comic(self):
        inventory = self.yaml_inventory()
        hosts = inventory.list_hosts("comic")
        expected_hosts=['garfield', 'odie']
        self.compare(hosts, expected_hosts)

    def test_yaml_orange(self):
        inventory = self.yaml_inventory()
        hosts = inventory.list_hosts("orange")
        expected_hosts=['garfield', 'goofy']
        self.compare(hosts, expected_hosts)

    def test_yaml_garfield_vars(self):
        inventory = self.yaml_inventory()
        vars = inventory.get_variables('garfield')
        assert vars == {'ears': 'pointy',
                        'inventory_hostname': 'garfield',
                        'group_names': ['cat', 'comic', 'orange'],
                        'nose': 'pink'}