mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
511 lines
19 KiB
Python
511 lines
19 KiB
Python
13 years ago
|
import os
|
||
|
import unittest
|
||
11 years ago
|
from nose.tools import raises
|
||
13 years ago
|
|
||
11 years ago
|
from ansible import errors
|
||
13 years ago
|
from ansible.inventory import Inventory
|
||
|
|
||
|
class TestInventory(unittest.TestCase):
|
||
|
|
||
|
def setUp(self):
|
||
11 years ago
|
|
||
13 years ago
|
self.cwd = os.getcwd()
|
||
11 years ago
|
self.test_dir = os.path.join(self.cwd, 'inventory_test_data')
|
||
13 years ago
|
|
||
12 years ago
|
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')
|
||
12 years ago
|
self.inventory_dir = os.path.join(self.test_dir, 'inventory_dir')
|
||
13 years ago
|
|
||
|
os.chmod(self.inventory_script, 0755)
|
||
|
|
||
|
def tearDown(self):
|
||
|
os.chmod(self.inventory_script, 0644)
|
||
|
|
||
12 years ago
|
def compare(self, left, right, sort=True):
|
||
|
if sort:
|
||
|
left = sorted(left)
|
||
|
right = sorted(right)
|
||
13 years ago
|
print left
|
||
|
print right
|
||
|
assert left == right
|
||
|
|
||
11 years ago
|
def empty_inventory(self):
|
||
|
return Inventory(None)
|
||
|
|
||
13 years ago
|
def simple_inventory(self):
|
||
13 years ago
|
return Inventory(self.inventory_file)
|
||
13 years ago
|
|
||
12 years ago
|
def large_range_inventory(self):
|
||
|
return Inventory(self.large_range_inventory_file)
|
||
|
|
||
13 years ago
|
def script_inventory(self):
|
||
13 years ago
|
return Inventory(self.inventory_script)
|
||
13 years ago
|
|
||
13 years ago
|
def complex_inventory(self):
|
||
|
return Inventory(self.complex_inventory_file)
|
||
|
|
||
12 years ago
|
def dir_inventory(self):
|
||
|
return Inventory(self.inventory_dir)
|
||
|
|
||
11 years ago
|
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',
|
||
10 years ago
|
'BastC', 'BastD', 'neptun', 'goldorak', ]
|
||
12 years ago
|
|
||
11 years ago
|
#####################################
|
||
|
### Empty inventory format tests
|
||
|
|
||
|
def test_empty(self):
|
||
|
inventory = self.empty_inventory()
|
||
|
hosts = inventory.list_hosts()
|
||
|
self.assertEqual(hosts, [])
|
||
|
|
||
13 years ago
|
#####################################
|
||
|
### Simple inventory format tests
|
||
13 years ago
|
|
||
13 years ago
|
def test_simple(self):
|
||
|
inventory = self.simple_inventory()
|
||
|
hosts = inventory.list_hosts()
|
||
12 years ago
|
self.assertEqual(sorted(hosts), sorted(self.all_simple_hosts))
|
||
13 years ago
|
|
||
|
def test_simple_all(self):
|
||
|
inventory = self.simple_inventory()
|
||
|
hosts = inventory.list_hosts('all')
|
||
12 years ago
|
self.assertEqual(sorted(hosts), sorted(self.all_simple_hosts))
|
||
13 years ago
|
|
||
11 years ago
|
def test_get_hosts(self):
|
||
|
inventory = Inventory('127.0.0.1,192.168.1.1')
|
||
|
hosts = inventory.get_hosts('!10.0.0.1')
|
||
|
hosts_all = inventory.get_hosts('all')
|
||
|
self.assertEqual(sorted(hosts), sorted(hosts_all))
|
||
|
|
||
|
def test_no_src(self):
|
||
|
inventory = Inventory('127.0.0.1,')
|
||
|
self.assertEqual(inventory.src(), None)
|
||
|
|
||
13 years ago
|
def test_simple_norse(self):
|
||
|
inventory = self.simple_inventory()
|
||
|
hosts = inventory.list_hosts("norse")
|
||
|
|
||
11 years ago
|
expected_hosts=['thor', 'odin', 'loki']
|
||
13 years ago
|
assert sorted(hosts) == sorted(expected_hosts)
|
||
13 years ago
|
|
||
13 years ago
|
def test_simple_ungrouped(self):
|
||
|
inventory = self.simple_inventory()
|
||
|
hosts = inventory.list_hosts("ungrouped")
|
||
|
|
||
11 years ago
|
expected_hosts=['jupiter', 'saturn',
|
||
|
'thrudgelmir0', 'thrudgelmir1', 'thrudgelmir2',
|
||
|
'thrudgelmir3', 'thrudgelmir4', 'thrudgelmir5']
|
||
13 years ago
|
assert sorted(hosts) == sorted(expected_hosts)
|
||
13 years ago
|
|
||
13 years ago
|
def test_simple_combined(self):
|
||
|
inventory = self.simple_inventory()
|
||
|
hosts = inventory.list_hosts("norse:greek")
|
||
|
|
||
11 years ago
|
expected_hosts=['zeus', 'hera', 'poseidon',
|
||
|
'cerberus001','cerberus002','cerberus003',
|
||
|
'cottus99','cottus100',
|
||
|
'thor', 'odin', 'loki']
|
||
13 years ago
|
assert sorted(hosts) == sorted(expected_hosts)
|
||
13 years ago
|
|
||
|
def test_simple_restrict(self):
|
||
|
inventory = self.simple_inventory()
|
||
|
|
||
|
restricted_hosts = ['hera', 'poseidon', 'thor']
|
||
11 years ago
|
expected_hosts=['zeus', 'hera', 'poseidon',
|
||
|
'cerberus001','cerberus002','cerberus003',
|
||
|
'cottus99', 'cottus100',
|
||
|
'thor', 'odin', 'loki']
|
||
13 years ago
|
|
||
|
inventory.restrict_to(restricted_hosts)
|
||
|
hosts = inventory.list_hosts("norse:greek")
|
||
|
|
||
13 years ago
|
assert sorted(hosts) == sorted(restricted_hosts)
|
||
13 years ago
|
|
||
|
inventory.lift_restriction()
|
||
|
hosts = inventory.list_hosts("norse:greek")
|
||
|
|
||
13 years ago
|
assert sorted(hosts) == sorted(expected_hosts)
|
||
13 years ago
|
|
||
11 years ago
|
def test_simple_string_ipv4(self):
|
||
|
inventory = Inventory('127.0.0.1,192.168.1.1')
|
||
|
hosts = inventory.list_hosts()
|
||
11 years ago
|
self.assertEqual(sorted(hosts), sorted(['127.0.0.1','192.168.1.1']))
|
||
11 years ago
|
|
||
|
def test_simple_string_ipv4_port(self):
|
||
|
inventory = Inventory('127.0.0.1:2222,192.168.1.1')
|
||
|
hosts = inventory.list_hosts()
|
||
11 years ago
|
self.assertEqual(sorted(hosts), sorted(['127.0.0.1','192.168.1.1']))
|
||
11 years ago
|
|
||
|
def test_simple_string_ipv4_vars(self):
|
||
|
inventory = Inventory('127.0.0.1:2222,192.168.1.1')
|
||
|
var = inventory.get_variables('127.0.0.1')
|
||
|
self.assertEqual(var['ansible_ssh_port'], 2222)
|
||
|
|
||
|
def test_simple_string_ipv6(self):
|
||
|
inventory = Inventory('FE80:EF45::12:1,192.168.1.1')
|
||
|
hosts = inventory.list_hosts()
|
||
11 years ago
|
self.assertEqual(sorted(hosts), sorted(['FE80:EF45::12:1','192.168.1.1']))
|
||
11 years ago
|
|
||
|
def test_simple_string_ipv6_port(self):
|
||
|
inventory = Inventory('[FE80:EF45::12:1]:2222,192.168.1.1')
|
||
|
hosts = inventory.list_hosts()
|
||
11 years ago
|
self.assertEqual(sorted(hosts), sorted(['FE80:EF45::12:1','192.168.1.1']))
|
||
11 years ago
|
|
||
|
def test_simple_string_ipv6_vars(self):
|
||
|
inventory = Inventory('[FE80:EF45::12:1]:2222,192.168.1.1')
|
||
|
var = inventory.get_variables('FE80:EF45::12:1')
|
||
|
self.assertEqual(var['ansible_ssh_port'], 2222)
|
||
|
|
||
11 years ago
|
def test_simple_string_fqdn(self):
|
||
|
inventory = Inventory('foo.example.com,bar.example.com')
|
||
|
hosts = inventory.list_hosts()
|
||
11 years ago
|
self.assertEqual(sorted(hosts), sorted(['foo.example.com','bar.example.com']))
|
||
11 years ago
|
|
||
|
def test_simple_string_fqdn_port(self):
|
||
|
inventory = Inventory('foo.example.com:2222,bar.example.com')
|
||
|
hosts = inventory.list_hosts()
|
||
11 years ago
|
self.assertEqual(sorted(hosts), sorted(['foo.example.com','bar.example.com']))
|
||
11 years ago
|
|
||
|
def test_simple_string_fqdn_vars(self):
|
||
|
inventory = Inventory('foo.example.com:2222,bar.example.com')
|
||
|
var = inventory.get_variables('foo.example.com')
|
||
|
self.assertEqual(var['ansible_ssh_port'], 2222)
|
||
|
|
||
13 years ago
|
def test_simple_vars(self):
|
||
|
inventory = self.simple_inventory()
|
||
|
vars = inventory.get_variables('thor')
|
||
|
|
||
13 years ago
|
assert vars == {'group_names': ['norse'],
|
||
13 years ago
|
'inventory_hostname': 'thor',
|
||
|
'inventory_hostname_short': 'thor'}
|
||
13 years ago
|
|
||
13 years ago
|
def test_simple_port(self):
|
||
|
inventory = self.simple_inventory()
|
||
|
vars = inventory.get_variables('hera')
|
||
|
|
||
11 years ago
|
expected = { 'ansible_ssh_port': 3000,
|
||
|
'group_names': ['greek'],
|
||
|
'inventory_hostname': 'hera',
|
||
|
'inventory_hostname_short': 'hera' }
|
||
13 years ago
|
assert vars == expected
|
||
13 years ago
|
|
||
12 years ago
|
def test_large_range(self):
|
||
|
inventory = self.large_range_inventory()
|
||
|
hosts = inventory.list_hosts()
|
||
11 years ago
|
self.assertEqual(sorted(hosts), sorted('bob%03i' %i for i in range(0, 143)))
|
||
12 years ago
|
|
||
11 years ago
|
def test_subset(self):
|
||
|
inventory = self.simple_inventory()
|
||
|
inventory.subset('odin;thor,loki')
|
||
11 years ago
|
self.assertEqual(sorted(inventory.list_hosts()), sorted(['thor','odin','loki']))
|
||
11 years ago
|
|
||
11 years ago
|
def test_subset_range(self):
|
||
|
inventory = self.simple_inventory()
|
||
|
inventory.subset('greek[0-2];norse[0]')
|
||
11 years ago
|
self.assertEqual(sorted(inventory.list_hosts()), sorted(['zeus','hera','thor']))
|
||
11 years ago
|
|
||
11 years ago
|
def test_subet_range_empty_group(self):
|
||
|
inventory = self.simple_inventory()
|
||
|
inventory.subset('missing[0]')
|
||
|
self.assertEqual(sorted(inventory.list_hosts()), sorted([]))
|
||
|
|
||
11 years ago
|
def test_subset_filename(self):
|
||
|
inventory = self.simple_inventory()
|
||
|
inventory.subset('@' + os.path.join(self.test_dir, 'restrict_pattern'))
|
||
11 years ago
|
self.assertEqual(sorted(inventory.list_hosts()), sorted(['thor','odin']))
|
||
11 years ago
|
|
||
10 years ago
|
def test_vars_yaml_extension(self):
|
||
|
inventory = self.simple_inventory()
|
||
|
vars = inventory.get_variables('goldorak')
|
||
|
assert vars['YAML_FILENAME_EXTENSIONS_TEST']
|
||
|
|
||
11 years ago
|
@raises(errors.AnsibleError)
|
||
|
def testinvalid_entry(self):
|
||
11 years ago
|
Inventory('1234')
|
||
11 years ago
|
|
||
13 years ago
|
###################################################
|
||
|
### INI file advanced tests
|
||
|
|
||
|
def test_complex_vars(self):
|
||
|
inventory = self.complex_inventory()
|
||
|
|
||
|
vars = inventory.get_variables('rtp_a')
|
||
|
print vars
|
||
|
|
||
|
expected = dict(
|
||
11 years ago
|
a=1, b=2, c=3, d=10002, e=10003, f='10004 != 10005',
|
||
12 years ago
|
g=' g ', h=' h ', i="' i \"", j='" j',
|
||
11 years ago
|
k=[ 'k1', 'k2' ],
|
||
|
rga=1, rgb=2, rgc=3,
|
||
12 years ago
|
inventory_hostname='rtp_a', inventory_hostname_short='rtp_a',
|
||
11 years ago
|
group_names=[ 'eastcoast', 'nc', 'redundantgroup', 'redundantgroup2', 'redundantgroup3', 'rtp', 'us' ]
|
||
13 years ago
|
)
|
||
|
print vars
|
||
|
print expected
|
||
|
assert vars == expected
|
||
|
|
||
12 years ago
|
def test_complex_group_names(self):
|
||
|
inventory = self.complex_inventory()
|
||
|
tests = {
|
||
11 years ago
|
'host1': [ 'role1', 'role3' ],
|
||
|
'host2': [ 'role1', 'role2' ],
|
||
|
'host3': [ 'role2', 'role3' ]
|
||
12 years ago
|
}
|
||
|
for host, roles in tests.iteritems():
|
||
|
group_names = inventory.get_variables(host)['group_names']
|
||
|
assert sorted(group_names) == sorted(roles)
|
||
|
|
||
13 years ago
|
def test_complex_exclude(self):
|
||
|
inventory = self.complex_inventory()
|
||
12 years ago
|
hosts = inventory.list_hosts("nc:florida:!triangle:!orlando")
|
||
12 years ago
|
expected_hosts = ['miami', 'rtp_a', 'rtp_b', 'rtp_c']
|
||
12 years ago
|
print "HOSTS=%s" % sorted(hosts)
|
||
|
print "EXPECTED=%s" % sorted(expected_hosts)
|
||
13 years ago
|
assert sorted(hosts) == sorted(expected_hosts)
|
||
|
|
||
12 years ago
|
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)
|
||
|
|
||
11 years ago
|
def test_regex_grouping(self):
|
||
|
inventory = self.simple_inventory()
|
||
|
hosts = inventory.list_hosts("~(cer[a-z]|berc)(erus00[13])")
|
||
|
expected_hosts = ['cerberus001', 'cerberus003']
|
||
|
print "HOSTS=%s" % sorted(hosts)
|
||
|
print "EXPECTED=%s" % sorted(expected_hosts)
|
||
|
assert sorted(hosts) == sorted(expected_hosts)
|
||
|
|
||
12 years ago
|
def test_complex_enumeration(self):
|
||
|
|
||
11 years ago
|
|
||
11 years ago
|
expected1 = ['rtp_b']
|
||
|
expected2 = ['rtp_a', 'rtp_b']
|
||
|
expected3 = ['rtp_a', 'rtp_b', 'rtp_c', 'tri_a', 'tri_b', 'tri_c']
|
||
11 years ago
|
expected4 = ['rtp_b', 'orlando' ]
|
||
11 years ago
|
expected5 = ['blade-a-1']
|
||
12 years ago
|
|
||
|
inventory = self.complex_inventory()
|
||
11 years ago
|
hosts = inventory.list_hosts("nc[1]")
|
||
12 years ago
|
self.compare(hosts, expected1, sort=False)
|
||
11 years ago
|
hosts = inventory.list_hosts("nc[0-2]")
|
||
12 years ago
|
self.compare(hosts, expected2, sort=False)
|
||
11 years ago
|
hosts = inventory.list_hosts("nc[0-99999]")
|
||
12 years ago
|
self.compare(hosts, expected3, sort=False)
|
||
11 years ago
|
hosts = inventory.list_hosts("nc[1-2]:florida[0-1]")
|
||
12 years ago
|
self.compare(hosts, expected4, sort=False)
|
||
11 years ago
|
hosts = inventory.list_hosts("blade-a-1")
|
||
|
self.compare(hosts, expected5, sort=False)
|
||
12 years ago
|
|
||
12 years ago
|
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'])
|
||
|
|
||
11 years ago
|
@raises(errors.AnsibleError)
|
||
|
def test_invalid_range(self):
|
||
11 years ago
|
Inventory(os.path.join(self.test_dir, 'inventory','test_incorrect_range'))
|
||
12 years ago
|
|
||
11 years ago
|
@raises(errors.AnsibleError)
|
||
|
def test_missing_end(self):
|
||
11 years ago
|
Inventory(os.path.join(self.test_dir, 'inventory','test_missing_end'))
|
||
11 years ago
|
|
||
11 years ago
|
@raises(errors.AnsibleError)
|
||
|
def test_incorrect_format(self):
|
||
11 years ago
|
Inventory(os.path.join(self.test_dir, 'inventory','test_incorrect_format'))
|
||
11 years ago
|
|
||
11 years ago
|
@raises(errors.AnsibleError)
|
||
|
def test_alpha_end_before_beg(self):
|
||
11 years ago
|
Inventory(os.path.join(self.test_dir, 'inventory','test_alpha_end_before_beg'))
|
||
11 years ago
|
|
||
11 years ago
|
def test_combined_range(self):
|
||
11 years ago
|
i = Inventory(os.path.join(self.test_dir, 'inventory','test_combined_range'))
|
||
11 years ago
|
hosts = i.list_hosts('test')
|
||
11 years ago
|
expected_hosts=['host1A','host2A','host1B','host2B']
|
||
11 years ago
|
assert sorted(hosts) == sorted(expected_hosts)
|
||
11 years ago
|
|
||
11 years ago
|
def test_leading_range(self):
|
||
11 years ago
|
i = Inventory(os.path.join(self.test_dir, 'inventory','test_leading_range'))
|
||
11 years ago
|
hosts = i.list_hosts('test')
|
||
11 years ago
|
expected_hosts=['1.host','2.host','A.host','B.host']
|
||
11 years ago
|
assert sorted(hosts) == sorted(expected_hosts)
|
||
|
|
||
11 years ago
|
hosts2 = i.list_hosts('test2')
|
||
11 years ago
|
expected_hosts2=['1.host','2.host','3.host']
|
||
11 years ago
|
assert sorted(hosts2) == sorted(expected_hosts2)
|
||
|
|
||
13 years ago
|
###################################################
|
||
13 years ago
|
### Inventory API tests
|
||
|
|
||
|
def test_script(self):
|
||
|
inventory = self.script_inventory()
|
||
|
hosts = inventory.list_hosts()
|
||
|
|
||
11 years ago
|
expected_hosts=['jupiter', 'saturn', 'zeus', 'hera', 'poseidon', 'thor', 'odin', 'loki']
|
||
13 years ago
|
|
||
11 years ago
|
print "Expected: %s"%(expected_hosts)
|
||
|
print "Got : %s"%(hosts)
|
||
13 years ago
|
assert sorted(hosts) == sorted(expected_hosts)
|
||
|
|
||
|
def test_script_all(self):
|
||
|
inventory = self.script_inventory()
|
||
|
hosts = inventory.list_hosts('all')
|
||
|
|
||
11 years ago
|
expected_hosts=['jupiter', 'saturn', 'zeus', 'hera', 'poseidon', 'thor', 'odin', 'loki']
|
||
13 years ago
|
assert sorted(hosts) == sorted(expected_hosts)
|
||
|
|
||
|
def test_script_norse(self):
|
||
|
inventory = self.script_inventory()
|
||
|
hosts = inventory.list_hosts("norse")
|
||
|
|
||
11 years ago
|
expected_hosts=['thor', 'odin', 'loki']
|
||
13 years ago
|
assert sorted(hosts) == sorted(expected_hosts)
|
||
|
|
||
|
def test_script_combined(self):
|
||
|
inventory = self.script_inventory()
|
||
|
hosts = inventory.list_hosts("norse:greek")
|
||
|
|
||
11 years ago
|
expected_hosts=['zeus', 'hera', 'poseidon', 'thor', 'odin', 'loki']
|
||
13 years ago
|
assert sorted(hosts) == sorted(expected_hosts)
|
||
|
|
||
|
def test_script_restrict(self):
|
||
|
inventory = self.script_inventory()
|
||
|
|
||
|
restricted_hosts = ['hera', 'poseidon', 'thor']
|
||
11 years ago
|
expected_hosts=['zeus', 'hera', 'poseidon', 'thor', 'odin', 'loki']
|
||
13 years ago
|
|
||
|
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')
|
||
|
|
||
13 years ago
|
print "VARS=%s" % vars
|
||
|
|
||
11 years ago
|
assert vars == {'hammer':True,
|
||
13 years ago
|
'group_names': ['norse'],
|
||
13 years ago
|
'inventory_hostname': 'thor',
|
||
|
'inventory_hostname_short': 'thor'}
|
||
12 years ago
|
|
||
|
def test_hosts_list(self):
|
||
12 years ago
|
# Test the case when playbook 'hosts' var is a list.
|
||
12 years ago
|
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
|
||
12 years ago
|
|
||
|
def test_script_multiple_groups(self):
|
||
|
inventory = self.script_inventory()
|
||
|
vars = inventory.get_variables('zeus')
|
||
|
|
||
|
print "VARS=%s" % vars
|
||
12 years ago
|
|
||
12 years ago
|
assert vars == {'inventory_hostname': 'zeus',
|
||
|
'inventory_hostname_short': 'zeus',
|
||
|
'group_names': ['greek', 'major-god']}
|
||
12 years ago
|
|
||
11 years ago
|
def test_allows_equals_sign_in_var(self):
|
||
|
inventory = self.simple_inventory()
|
||
|
auth = inventory.get_variables('neptun')['auth']
|
||
|
assert auth == 'YWRtaW46YWRtaW4='
|
||
|
|
||
11 years ago
|
def test_dir_inventory(self):
|
||
|
inventory = self.dir_inventory()
|
||
|
|
||
|
host_vars = inventory.get_variables('zeus')
|
||
|
|
||
|
expected_vars = {'inventory_hostname': 'zeus',
|
||
|
'inventory_hostname_short': 'zeus',
|
||
11 years ago
|
'group_names': ['greek', 'major-god'],
|
||
11 years ago
|
'var_a': '3#4'}
|
||
|
|
||
|
print "HOST VARS=%s" % host_vars
|
||
|
print "EXPECTED VARS=%s" % expected_vars
|
||
|
|
||
11 years ago
|
assert host_vars == expected_vars
|
||
|
|
||
|
def test_dir_inventory_multiple_groups(self):
|
||
11 years ago
|
inventory = self.dir_inventory()
|
||
|
group_greek = inventory.get_hosts('greek')
|
||
|
actual_host_names = [host.name for host in group_greek]
|
||
|
print "greek : %s " % actual_host_names
|
||
|
assert actual_host_names == ['zeus', 'morpheus']
|
||
11 years ago
|
|
||
|
def test_dir_inventory_skip_extension(self):
|
||
|
inventory = self.dir_inventory()
|
||
|
assert 'skipme' not in [h.name for h in inventory.get_hosts()]
|
||
11 years ago
|
|
||
11 years ago
|
def test_dir_inventory_group_hosts(self):
|
||
|
inventory = self.dir_inventory()
|
||
|
expected_groups = {'all': ['morpheus', 'thor', 'zeus'],
|
||
|
'major-god': ['thor', 'zeus'],
|
||
|
'minor-god': ['morpheus'],
|
||
|
'norse': ['thor'],
|
||
|
'greek': ['morpheus', 'zeus'],
|
||
|
'ungrouped': []}
|
||
|
|
||
|
actual_groups = {}
|
||
|
for group in inventory.get_groups():
|
||
|
actual_groups[group.name] = sorted([h.name for h in group.get_hosts()])
|
||
|
print "INVENTORY groups[%s].hosts=%s" % (group.name, actual_groups[group.name])
|
||
|
print "EXPECTED groups[%s].hosts=%s" % (group.name, expected_groups[group.name])
|
||
|
|
||
|
assert actual_groups == expected_groups
|
||
|
|
||
|
def test_dir_inventory_groups_for_host(self):
|
||
|
inventory = self.dir_inventory()
|
||
|
expected_groups_for_host = {'morpheus': ['all', 'greek', 'minor-god'],
|
||
11 years ago
|
'thor': ['all', 'major-god', 'norse'],
|
||
11 years ago
|
'zeus': ['all', 'greek', 'major-god']}
|
||
|
|
||
|
actual_groups_for_host = {}
|
||
|
for (host, expected) in expected_groups_for_host.iteritems():
|
||
|
groups = inventory.groups_for_host(host)
|
||
|
names = sorted([g.name for g in groups])
|
||
|
actual_groups_for_host[host] = names
|
||
|
print "INVENTORY groups_for_host(%s)=%s" % (host, names)
|
||
|
print "EXPECTED groups_for_host(%s)=%s" % (host, expected)
|
||
|
|
||
|
assert actual_groups_for_host == expected_groups_for_host
|
||
|
|
||
|
def test_dir_inventory_groups_list(self):
|
||
|
inventory = self.dir_inventory()
|
||
|
inventory_groups = inventory.groups_list()
|
||
|
|
||
|
expected_groups = {'all': ['morpheus', 'thor', 'zeus'],
|
||
|
'major-god': ['thor', 'zeus'],
|
||
|
'minor-god': ['morpheus'],
|
||
|
'norse': ['thor'],
|
||
|
'greek': ['morpheus', 'zeus'],
|
||
|
'ungrouped': []}
|
||
|
|
||
|
for (name, expected_hosts) in expected_groups.iteritems():
|
||
|
inventory_groups[name] = sorted(inventory_groups.get(name, []))
|
||
|
print "INVENTORY groups_list['%s']=%s" % (name, inventory_groups[name])
|
||
|
print "EXPECTED groups_list['%s']=%s" % (name, expected_hosts)
|
||
|
|
||
|
assert inventory_groups == expected_groups
|
||
|
|