2012-04-13 20:50:30 +02:00
|
|
|
import os
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
from ansible.inventory import Inventory
|
2012-04-14 09:29:14 +02:00
|
|
|
from ansible.runner import Runner
|
2012-05-06 23:03:17 +02:00
|
|
|
# from nose.plugins.skip import SkipTest
|
2012-04-13 20:50:30 +02:00
|
|
|
|
|
|
|
class TestInventory(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.cwd = os.getcwd()
|
|
|
|
self.test_dir = os.path.join(self.cwd, 'test')
|
|
|
|
|
2012-05-07 00:01:11 +02:00
|
|
|
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')
|
2012-04-13 20:50:30 +02:00
|
|
|
|
|
|
|
os.chmod(self.inventory_script, 0755)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
os.chmod(self.inventory_script, 0644)
|
|
|
|
|
2012-05-06 23:03:17 +02:00
|
|
|
def compare(self, left, right):
|
|
|
|
left = sorted(left)
|
|
|
|
right = sorted(right)
|
|
|
|
print left
|
|
|
|
print right
|
|
|
|
assert left == right
|
|
|
|
|
|
|
|
|
2012-04-13 20:50:30 +02:00
|
|
|
def simple_inventory(self):
|
2012-05-07 00:01:11 +02:00
|
|
|
return Inventory(self.inventory_file)
|
2012-04-13 20:50:30 +02:00
|
|
|
|
|
|
|
def script_inventory(self):
|
2012-05-07 00:01:11 +02:00
|
|
|
return Inventory(self.inventory_script)
|
2012-04-13 20:50:30 +02:00
|
|
|
|
2012-04-14 15:45:24 +02:00
|
|
|
def yaml_inventory(self):
|
2012-05-07 00:01:11 +02:00
|
|
|
return Inventory(self.inventory_yaml)
|
|
|
|
|
|
|
|
def complex_inventory(self):
|
|
|
|
return Inventory(self.complex_inventory_file)
|
|
|
|
|
|
|
|
#####################################
|
|
|
|
### Simple inventory format tests
|
2012-04-14 15:45:24 +02:00
|
|
|
|
2012-04-13 20:50:30 +02:00
|
|
|
def test_simple(self):
|
|
|
|
inventory = self.simple_inventory()
|
|
|
|
hosts = inventory.list_hosts()
|
|
|
|
|
|
|
|
expected_hosts=['jupiter', 'saturn', 'zeus', 'hera', 'poseidon', 'thor', 'odin', 'loki']
|
2012-05-05 22:31:03 +02:00
|
|
|
assert sorted(hosts) == sorted(expected_hosts)
|
2012-04-13 20:50:30 +02:00
|
|
|
|
|
|
|
def test_simple_all(self):
|
|
|
|
inventory = self.simple_inventory()
|
|
|
|
hosts = inventory.list_hosts('all')
|
|
|
|
|
|
|
|
expected_hosts=['jupiter', 'saturn', 'zeus', 'hera', 'poseidon', 'thor', 'odin', 'loki']
|
2012-05-05 22:31:03 +02:00
|
|
|
assert sorted(hosts) == sorted(expected_hosts)
|
2012-04-13 20:50:30 +02:00
|
|
|
|
|
|
|
def test_simple_norse(self):
|
|
|
|
inventory = self.simple_inventory()
|
|
|
|
hosts = inventory.list_hosts("norse")
|
|
|
|
|
|
|
|
expected_hosts=['thor', 'odin', 'loki']
|
2012-05-05 22:31:03 +02:00
|
|
|
assert sorted(hosts) == sorted(expected_hosts)
|
2012-04-13 20:50:30 +02:00
|
|
|
|
2012-04-16 10:55:08 +02:00
|
|
|
def test_simple_ungrouped(self):
|
|
|
|
inventory = self.simple_inventory()
|
|
|
|
hosts = inventory.list_hosts("ungrouped")
|
|
|
|
|
|
|
|
expected_hosts=['jupiter', 'saturn']
|
2012-05-05 22:31:03 +02:00
|
|
|
assert sorted(hosts) == sorted(expected_hosts)
|
2012-04-16 10:55:08 +02:00
|
|
|
|
2012-04-13 20:50:30 +02:00
|
|
|
def test_simple_combined(self):
|
|
|
|
inventory = self.simple_inventory()
|
|
|
|
hosts = inventory.list_hosts("norse:greek")
|
|
|
|
|
|
|
|
expected_hosts=['zeus', 'hera', 'poseidon', 'thor', 'odin', 'loki']
|
2012-05-05 22:31:03 +02:00
|
|
|
assert sorted(hosts) == sorted(expected_hosts)
|
2012-04-13 20:50:30 +02:00
|
|
|
|
|
|
|
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")
|
|
|
|
|
2012-05-05 22:31:03 +02:00
|
|
|
print "Hosts=%s" % hosts
|
|
|
|
print "Restricted=%s" % restricted_hosts
|
|
|
|
assert sorted(hosts) == sorted(restricted_hosts)
|
2012-04-13 20:50:30 +02:00
|
|
|
|
|
|
|
inventory.lift_restriction()
|
|
|
|
hosts = inventory.list_hosts("norse:greek")
|
|
|
|
|
2012-05-05 22:31:03 +02:00
|
|
|
print hosts
|
|
|
|
print expected_hosts
|
|
|
|
assert sorted(hosts) == sorted(expected_hosts)
|
2012-04-13 20:50:30 +02:00
|
|
|
|
2012-06-15 18:18:27 +02:00
|
|
|
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)
|
|
|
|
|
2012-04-13 20:50:30 +02:00
|
|
|
def test_simple_vars(self):
|
|
|
|
inventory = self.simple_inventory()
|
|
|
|
vars = inventory.get_variables('thor')
|
|
|
|
|
2012-05-05 22:31:03 +02:00
|
|
|
print vars
|
2012-05-02 09:56:58 +02:00
|
|
|
assert vars == {'group_names': ['norse'],
|
|
|
|
'inventory_hostname': 'thor'}
|
2012-04-13 20:50:30 +02:00
|
|
|
|
2012-04-14 13:12:32 +02:00
|
|
|
def test_simple_port(self):
|
|
|
|
inventory = self.simple_inventory()
|
|
|
|
vars = inventory.get_variables('hera')
|
|
|
|
|
2012-05-05 22:31:03 +02:00
|
|
|
print vars
|
|
|
|
expected = {'ansible_ssh_port': 3000,
|
2012-05-02 09:56:58 +02:00
|
|
|
'group_names': ['greek'],
|
|
|
|
'inventory_hostname': 'hera'}
|
2012-05-05 22:31:03 +02:00
|
|
|
print expected
|
|
|
|
assert vars == expected
|
2012-04-14 13:12:32 +02:00
|
|
|
|
2012-05-07 00:01:11 +02:00
|
|
|
###################################################
|
|
|
|
### INI file advanced tests
|
|
|
|
|
|
|
|
def test_complex_vars(self):
|
|
|
|
inventory = self.complex_inventory()
|
|
|
|
|
|
|
|
vars = inventory.get_variables('rtp_a')
|
|
|
|
print vars
|
|
|
|
|
|
|
|
expected = dict(
|
2012-05-19 23:24:54 +02:00
|
|
|
a='1', b='2', c='3', d='100002', rga='1', rgb='2', rgc='3',
|
2012-05-07 00:01:11 +02:00
|
|
|
inventory_hostname='rtp_a',
|
2012-05-19 23:24:54 +02:00
|
|
|
group_names=[ 'eastcoast', 'nc', 'redundantgroup', 'redundantgroup2', 'redundantgroup3', 'rtp', 'us' ]
|
2012-05-07 00:01:11 +02:00
|
|
|
)
|
|
|
|
print vars
|
|
|
|
print expected
|
|
|
|
assert vars == expected
|
|
|
|
|
2012-06-15 18:18:27 +02:00
|
|
|
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)
|
|
|
|
|
2012-05-07 00:01:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
###################################################
|
2012-04-13 20:50:30 +02:00
|
|
|
### 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')
|
|
|
|
|
2012-05-06 20:47:05 +02:00
|
|
|
print "VARS=%s" % vars
|
|
|
|
|
2012-05-02 09:56:58 +02:00
|
|
|
assert vars == {'hammer':True,
|
|
|
|
'group_names': ['norse'],
|
|
|
|
'inventory_hostname': 'thor'}
|
2012-04-13 20:50:30 +02:00
|
|
|
|
2012-04-14 15:45:24 +02:00
|
|
|
### Tests for yaml inventory file
|
|
|
|
|
|
|
|
def test_yaml(self):
|
|
|
|
inventory = self.yaml_inventory()
|
|
|
|
hosts = inventory.list_hosts()
|
2012-07-12 07:29:51 +02:00
|
|
|
expected_hosts=['garfield', 'goofy', 'hera', 'jerry', 'jupiter', 'loki', 'mars', 'mickey', 'odie', 'odin', 'poseidon', 'saturn', 'thor', 'tom', 'zeus']
|
2012-05-06 23:03:17 +02:00
|
|
|
self.compare(hosts, expected_hosts)
|
2012-04-14 15:45:24 +02:00
|
|
|
|
|
|
|
def test_yaml_all(self):
|
|
|
|
inventory = self.yaml_inventory()
|
|
|
|
hosts = inventory.list_hosts('all')
|
|
|
|
|
2012-07-12 07:29:51 +02:00
|
|
|
expected_hosts=['garfield', 'goofy', 'hera', 'jerry', 'jupiter', 'loki', 'mars', 'mickey', 'odie', 'odin', 'poseidon', 'saturn', 'thor', 'tom', 'zeus']
|
2012-05-06 23:03:17 +02:00
|
|
|
self.compare(hosts, expected_hosts)
|
2012-04-14 15:45:24 +02:00
|
|
|
|
|
|
|
def test_yaml_norse(self):
|
|
|
|
inventory = self.yaml_inventory()
|
|
|
|
hosts = inventory.list_hosts("norse")
|
|
|
|
|
|
|
|
expected_hosts=['thor', 'odin', 'loki']
|
2012-05-06 23:03:17 +02:00
|
|
|
self.compare(hosts, expected_hosts)
|
2012-04-14 15:45:24 +02:00
|
|
|
|
2012-05-08 09:44:59 +02:00
|
|
|
def test_yaml_ungrouped(self):
|
2012-04-16 10:55:08 +02:00
|
|
|
inventory = self.yaml_inventory()
|
|
|
|
hosts = inventory.list_hosts("ungrouped")
|
|
|
|
|
2012-06-26 11:38:10 +02:00
|
|
|
expected_hosts=['jupiter', 'mars']
|
2012-05-06 23:03:17 +02:00
|
|
|
self.compare(hosts, expected_hosts)
|
2012-04-16 10:55:08 +02:00
|
|
|
|
2012-04-14 15:45:24 +02:00
|
|
|
def test_yaml_combined(self):
|
|
|
|
inventory = self.yaml_inventory()
|
|
|
|
hosts = inventory.list_hosts("norse:greek")
|
|
|
|
|
|
|
|
expected_hosts=['zeus', 'hera', 'poseidon', 'thor', 'odin', 'loki']
|
2012-05-06 23:03:17 +02:00
|
|
|
self.compare(hosts, expected_hosts)
|
2012-04-14 15:45:24 +02:00
|
|
|
|
|
|
|
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")
|
|
|
|
|
2012-05-06 23:03:17 +02:00
|
|
|
self.compare(hosts, restricted_hosts)
|
2012-04-14 15:45:24 +02:00
|
|
|
|
|
|
|
inventory.lift_restriction()
|
|
|
|
hosts = inventory.list_hosts("norse:greek")
|
|
|
|
|
2012-05-06 23:03:17 +02:00
|
|
|
self.compare(hosts, expected_hosts)
|
2012-04-14 15:45:24 +02:00
|
|
|
|
|
|
|
def test_yaml_vars(self):
|
|
|
|
inventory = self.yaml_inventory()
|
|
|
|
vars = inventory.get_variables('thor')
|
2012-05-02 09:56:58 +02:00
|
|
|
assert vars == {'group_names': ['norse'],
|
|
|
|
'hammer':True,
|
|
|
|
'inventory_hostname': 'thor'}
|
2012-04-14 15:45:24 +02:00
|
|
|
|
2012-05-08 09:19:55 +02:00
|
|
|
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'}
|
|
|
|
|
2012-04-16 10:59:34 +02:00
|
|
|
def test_yaml_change_vars(self):
|
|
|
|
inventory = self.yaml_inventory()
|
|
|
|
vars = inventory.get_variables('thor')
|
|
|
|
|
|
|
|
vars["hammer"] = False
|
|
|
|
|
|
|
|
vars = inventory.get_variables('thor')
|
2012-05-06 23:03:17 +02:00
|
|
|
print vars
|
2012-05-02 09:56:58 +02:00
|
|
|
assert vars == {'hammer':True,
|
|
|
|
'inventory_hostname': 'thor',
|
|
|
|
'group_names': ['norse']}
|
2012-04-16 10:59:34 +02:00
|
|
|
|
2012-04-14 15:45:24 +02:00
|
|
|
def test_yaml_host_vars(self):
|
|
|
|
inventory = self.yaml_inventory()
|
|
|
|
vars = inventory.get_variables('saturn')
|
|
|
|
|
2012-05-06 23:03:17 +02:00
|
|
|
print vars
|
2012-05-02 09:56:58 +02:00
|
|
|
assert vars == {'inventory_hostname': 'saturn',
|
|
|
|
'moon': 'titan',
|
|
|
|
'moon2': 'enceladus',
|
2012-04-28 18:23:44 +02:00
|
|
|
'group_names': ['multiple']}
|
2012-04-14 15:45:24 +02:00
|
|
|
|
|
|
|
def test_yaml_port(self):
|
|
|
|
inventory = self.yaml_inventory()
|
|
|
|
vars = inventory.get_variables('hera')
|
|
|
|
|
2012-05-06 23:03:17 +02:00
|
|
|
print vars
|
2012-04-28 18:23:44 +02:00
|
|
|
assert vars == {'ansible_ssh_port': 3000,
|
2012-05-02 09:56:58 +02:00
|
|
|
'inventory_hostname': 'hera',
|
2012-04-28 18:23:44 +02:00
|
|
|
'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' ]
|
2012-04-14 15:45:24 +02:00
|
|
|
|
2012-07-12 07:29:51 +02:00
|
|
|
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'}
|
|
|
|
|