mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-30 14:21:26 -07:00
Add a new vmware inventory script backed by pyvmomi (#15967)
Add a new dynamic vmware inventory script backed by pyvmomi
This commit is contained in:
parent
d81b9ca29e
commit
018d3c3118
5 changed files with 761 additions and 0 deletions
122
test/units/contrib/inventory/test_vmware_inventory.py
Normal file
122
test/units/contrib/inventory/test_vmware_inventory.py
Normal file
|
@ -0,0 +1,122 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import json
|
||||
import os
|
||||
import pickle
|
||||
import unittest
|
||||
import sys
|
||||
|
||||
|
||||
# contrib's dirstruct doesn't contain __init__.py files
|
||||
checkout_path = os.path.dirname(__file__)
|
||||
checkout_path = checkout_path.replace('/test/units/contrib/inventory', '')
|
||||
inventory_dir = os.path.join(checkout_path, 'contrib', 'inventory')
|
||||
sys.path.append(os.path.abspath(inventory_dir))
|
||||
from vmware_inventory import VMWareInventory
|
||||
# cleanup so that nose's path is not polluted with other inv scripts
|
||||
sys.path.remove(os.path.abspath(inventory_dir))
|
||||
|
||||
|
||||
|
||||
|
||||
BASICINVENTORY = {'all': {'hosts': ['foo', 'bar']},
|
||||
'_meta': { 'hostvars': { 'foo': {'hostname': 'foo'},
|
||||
'bar': {'hostname': 'bar'}}
|
||||
}
|
||||
}
|
||||
|
||||
class FakeArgs(object):
|
||||
debug = False
|
||||
write_dumpfile = None
|
||||
load_dumpfile = None
|
||||
host = False
|
||||
list = True
|
||||
|
||||
class TestVMWareInventory(unittest.TestCase):
|
||||
|
||||
def test_host_info_returns_single_host(self):
|
||||
vmw = VMWareInventory(load=False)
|
||||
vmw.inventory = BASICINVENTORY
|
||||
foo = vmw.get_host_info('foo')
|
||||
bar = vmw.get_host_info('bar')
|
||||
assert foo == {'hostname': 'foo'}
|
||||
assert bar == {'hostname': 'bar'}
|
||||
|
||||
def test_show_returns_serializable_data(self):
|
||||
fakeargs = FakeArgs()
|
||||
vmw = VMWareInventory(load=False)
|
||||
vmw.args = fakeargs
|
||||
vmw.inventory = BASICINVENTORY
|
||||
showdata = vmw.show()
|
||||
serializable = False
|
||||
|
||||
try:
|
||||
json.loads(showdata)
|
||||
serializable = True
|
||||
except:
|
||||
pass
|
||||
assert serializable
|
||||
#import epdb; epdb.st()
|
||||
|
||||
def test_show_list_returns_serializable_data(self):
|
||||
fakeargs = FakeArgs()
|
||||
vmw = VMWareInventory(load=False)
|
||||
vmw.args = fakeargs
|
||||
vmw.args.list = True
|
||||
vmw.inventory = BASICINVENTORY
|
||||
showdata = vmw.show()
|
||||
serializable = False
|
||||
|
||||
try:
|
||||
json.loads(showdata)
|
||||
serializable = True
|
||||
except:
|
||||
pass
|
||||
assert serializable
|
||||
#import epdb; epdb.st()
|
||||
|
||||
def test_show_list_returns_all_data(self):
|
||||
fakeargs = FakeArgs()
|
||||
vmw = VMWareInventory(load=False)
|
||||
vmw.args = fakeargs
|
||||
vmw.args.list = True
|
||||
vmw.inventory = BASICINVENTORY
|
||||
showdata = vmw.show()
|
||||
expected = json.dumps(BASICINVENTORY, indent=2)
|
||||
assert showdata == expected
|
||||
|
||||
def test_show_host_returns_serializable_data(self):
|
||||
fakeargs = FakeArgs()
|
||||
vmw = VMWareInventory(load=False)
|
||||
vmw.args = fakeargs
|
||||
vmw.args.host = 'foo'
|
||||
vmw.inventory = BASICINVENTORY
|
||||
showdata = vmw.show()
|
||||
serializable = False
|
||||
|
||||
try:
|
||||
json.loads(showdata)
|
||||
serializable = True
|
||||
except:
|
||||
pass
|
||||
assert serializable
|
||||
#import epdb; epdb.st()
|
||||
|
||||
def test_show_host_returns_just_host(self):
|
||||
fakeargs = FakeArgs()
|
||||
vmw = VMWareInventory(load=False)
|
||||
vmw.args = fakeargs
|
||||
vmw.args.list = False
|
||||
vmw.args.host = 'foo'
|
||||
vmw.inventory = BASICINVENTORY
|
||||
showdata = vmw.show()
|
||||
expected = BASICINVENTORY['_meta']['hostvars']['foo']
|
||||
expected = json.dumps(expected, indent=2)
|
||||
#import epdb; epdb.st()
|
||||
assert showdata == expected
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Loading…
Add table
Add a link
Reference in a new issue