Fix vmware_inventory unit tests so they run.
This commit is contained in:
parent
e7ddff1928
commit
0785656344
2 changed files with 70 additions and 97 deletions
|
@ -10,6 +10,7 @@ pytest
|
||||||
pytest-mock
|
pytest-mock
|
||||||
pytest-xdist
|
pytest-xdist
|
||||||
python-memcached
|
python-memcached
|
||||||
|
pyvmomi
|
||||||
pyyaml
|
pyyaml
|
||||||
redis
|
redis
|
||||||
requests
|
requests
|
||||||
|
|
|
@ -1,31 +1,9 @@
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import pickle
|
import pytest
|
||||||
import unittest
|
|
||||||
import sys
|
import sys
|
||||||
from nose.plugins.skip import SkipTest
|
|
||||||
|
|
||||||
|
BASIC_INVENTORY = {
|
||||||
try:
|
|
||||||
from pyVmomi import vim, vmodl
|
|
||||||
except ImportError:
|
|
||||||
raise SkipTest("test_vmware_inventory.py requires the python module 'pyVmomi'")
|
|
||||||
|
|
||||||
try:
|
|
||||||
from vmware_inventory import VMWareInventory
|
|
||||||
except ImportError:
|
|
||||||
raise SkipTest("test_vmware_inventory.py requires the python module 'vmware_inventory'")
|
|
||||||
|
|
||||||
# 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))
|
|
||||||
|
|
||||||
# cleanup so that nose's path is not polluted with other inv scripts
|
|
||||||
sys.path.remove(os.path.abspath(inventory_dir))
|
|
||||||
|
|
||||||
BASICINVENTORY = {
|
|
||||||
'all': {
|
'all': {
|
||||||
'hosts': ['foo', 'bar']
|
'hosts': ['foo', 'bar']
|
||||||
},
|
},
|
||||||
|
@ -46,85 +24,79 @@ class FakeArgs(object):
|
||||||
list = True
|
list = True
|
||||||
|
|
||||||
|
|
||||||
class TestVMWareInventory(unittest.TestCase):
|
@pytest.fixture
|
||||||
|
def vmware_inventory():
|
||||||
|
inventory_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..', '..', 'contrib', 'inventory'))
|
||||||
|
|
||||||
def test_host_info_returns_single_host(self):
|
sys.path.append(inventory_dir)
|
||||||
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):
|
try:
|
||||||
fakeargs = FakeArgs()
|
import vmware_inventory
|
||||||
vmw = VMWareInventory(load=False)
|
except BaseException as ex:
|
||||||
vmw.args = fakeargs
|
pytest.skip(ex)
|
||||||
vmw.inventory = BASICINVENTORY
|
finally:
|
||||||
showdata = vmw.show()
|
sys.path.remove(inventory_dir)
|
||||||
serializable = False
|
|
||||||
|
|
||||||
try:
|
return vmware_inventory
|
||||||
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:
|
def test_host_info_returns_single_host(vmware_inventory):
|
||||||
json.loads(showdata)
|
vmw = vmware_inventory.VMWareInventory(load=False)
|
||||||
serializable = True
|
vmw.inventory = BASIC_INVENTORY
|
||||||
except:
|
foo = vmw.get_host_info('foo')
|
||||||
pass
|
bar = vmw.get_host_info('bar')
|
||||||
assert serializable
|
assert foo == {'hostname': 'foo'}
|
||||||
# import epdb; epdb.st()
|
assert bar == {'hostname': 'bar'}
|
||||||
|
|
||||||
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):
|
def test_show_returns_serializable_data(vmware_inventory):
|
||||||
fakeargs = FakeArgs()
|
fakeargs = FakeArgs()
|
||||||
vmw = VMWareInventory(load=False)
|
vmw = vmware_inventory.VMWareInventory(load=False)
|
||||||
vmw.args = fakeargs
|
vmw.args = fakeargs
|
||||||
vmw.args.host = 'foo'
|
vmw.inventory = BASIC_INVENTORY
|
||||||
vmw.inventory = BASICINVENTORY
|
showdata = vmw.show()
|
||||||
showdata = vmw.show()
|
json.loads(showdata)
|
||||||
serializable = False
|
|
||||||
|
|
||||||
try:
|
|
||||||
json.loads(showdata)
|
|
||||||
serializable = True
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
assert serializable
|
|
||||||
# import epdb; epdb.st()
|
|
||||||
|
|
||||||
def test_show_host_returns_just_host(self):
|
def test_show_list_returns_serializable_data(vmware_inventory):
|
||||||
fakeargs = FakeArgs()
|
fakeargs = FakeArgs()
|
||||||
vmw = VMWareInventory(load=False)
|
vmw = vmware_inventory.VMWareInventory(load=False)
|
||||||
vmw.args = fakeargs
|
vmw.args = fakeargs
|
||||||
vmw.args.list = False
|
vmw.args.list = True
|
||||||
vmw.args.host = 'foo'
|
vmw.inventory = BASIC_INVENTORY
|
||||||
vmw.inventory = BASICINVENTORY
|
showdata = vmw.show()
|
||||||
showdata = vmw.show()
|
json.loads(showdata)
|
||||||
expected = BASICINVENTORY['_meta']['hostvars']['foo']
|
|
||||||
expected = json.dumps(expected, indent=2)
|
|
||||||
# import epdb; epdb.st()
|
def test_show_list_returns_all_data(vmware_inventory):
|
||||||
assert showdata == expected
|
fakeargs = FakeArgs()
|
||||||
|
vmw = vmware_inventory.VMWareInventory(load=False)
|
||||||
|
vmw.args = fakeargs
|
||||||
|
vmw.args.list = True
|
||||||
|
vmw.inventory = BASIC_INVENTORY
|
||||||
|
showdata = vmw.show()
|
||||||
|
expected = json.dumps(BASIC_INVENTORY, indent=2)
|
||||||
|
assert showdata == expected
|
||||||
|
|
||||||
|
|
||||||
|
def test_show_host_returns_serializable_data(vmware_inventory):
|
||||||
|
fakeargs = FakeArgs()
|
||||||
|
vmw = vmware_inventory.VMWareInventory(load=False)
|
||||||
|
vmw.args = fakeargs
|
||||||
|
vmw.args.host = 'foo'
|
||||||
|
vmw.inventory = BASIC_INVENTORY
|
||||||
|
showdata = vmw.show()
|
||||||
|
json.loads(showdata)
|
||||||
|
|
||||||
|
|
||||||
|
def test_show_host_returns_just_host(vmware_inventory):
|
||||||
|
fakeargs = FakeArgs()
|
||||||
|
vmw = vmware_inventory.VMWareInventory(load=False)
|
||||||
|
vmw.args = fakeargs
|
||||||
|
vmw.args.list = False
|
||||||
|
vmw.args.host = 'foo'
|
||||||
|
vmw.inventory = BASIC_INVENTORY
|
||||||
|
showdata = vmw.show()
|
||||||
|
expected = BASIC_INVENTORY['_meta']['hostvars']['foo']
|
||||||
|
expected = json.dumps(expected, indent=2)
|
||||||
|
assert showdata == expected
|
||||||
|
|
Loading…
Reference in a new issue