Add tests for new advanced inventory features (groups of groups, group variables) in the default INI format file.

This commit is contained in:
Michael DeHaan 2012-05-06 18:01:11 -04:00
parent 1a00e2635e
commit 157d21b1c3
4 changed files with 94 additions and 10 deletions

View file

@ -57,6 +57,6 @@ class Host(object):
results.update(self.vars)
results['inventory_hostname'] = self.name
groups = self.get_groups()
results['group_names'] = [ g.name for g in groups if g.name != 'all']
results['group_names'] = sorted([ g.name for g in groups if g.name != 'all'])
return results

View file

@ -119,7 +119,7 @@ class Inventory(object):
results = utils.parse_json(out)
results['inventory_hostname'] = hostname
groups = [ g.name for g in host.get_groups() if g.name != 'all' ]
results['group_names'] = groups
results['group_names'] = sorted(groups)
return results
host = self.get_host(hostname)

View file

@ -11,9 +11,10 @@ class TestInventory(unittest.TestCase):
self.cwd = os.getcwd()
self.test_dir = os.path.join(self.cwd, 'test')
self.inventory_file = os.path.join(self.test_dir, 'simple_hosts')
self.inventory_script = os.path.join(self.test_dir, 'inventory_api.py')
self.inventory_yaml = os.path.join(self.test_dir, 'yaml_hosts')
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')
os.chmod(self.inventory_script, 0755)
@ -28,16 +29,20 @@ class TestInventory(unittest.TestCase):
assert left == right
### Simple inventory format tests
def simple_inventory(self):
return Inventory( self.inventory_file )
return Inventory(self.inventory_file)
def script_inventory(self):
return Inventory( self.inventory_script )
return Inventory(self.inventory_script)
def yaml_inventory(self):
return Inventory( self.inventory_yaml )
return Inventory(self.inventory_yaml)
def complex_inventory(self):
return Inventory(self.complex_inventory_file)
#####################################
### Simple inventory format tests
def test_simple(self):
inventory = self.simple_inventory()
@ -113,6 +118,27 @@ class TestInventory(unittest.TestCase):
print expected
assert vars == expected
###################################################
### INI file advanced tests
def test_complex_vars(self):
inventory = self.complex_inventory()
vars = inventory.get_variables('rtp_a')
print vars
expected = dict(
a='1', b='2', c='3', d='100002',
inventory_hostname='rtp_a',
group_names=[ 'eastcoast', 'nc', 'rtp', 'us' ]
)
print vars
print expected
assert vars == expected
###################################################
### Inventory API tests
def test_script(self):

58
test/complex_hosts Normal file
View file

@ -0,0 +1,58 @@
# order of groups, children, and vars is not signficant
# so this example mixes them up for maximum testing
[nc:children]
rtp
triangle
[eastcoast:children]
nc
florida
[us:children]
eastcoast
[nc:vars]
b=10000
c=10001
d=10002
[rtp]
rtp_a
rtp_b
rtb_c
[rtp:vars]
a=1
b=2
c=3
[triangle]
tri_a
tri_b
tri_c
[triangle:vars]
a=11
b=12
c=13
[florida]
orlando
miami
[florida:vars]
a=100
b=101
c=102
[eastcoast:vars]
b=100000
c=100001
d=100002
[us:vars]
c=1000000