Fix vmware_inventory unit tests so they run.

pull/46048/head
Matt Clay 6 years ago
parent e7ddff1928
commit 0785656344

@ -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():
def test_host_info_returns_single_host(self): inventory_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..', '..', 'contrib', 'inventory'))
vmw = VMWareInventory(load=False)
vmw.inventory = BASICINVENTORY sys.path.append(inventory_dir)
foo = vmw.get_host_info('foo')
bar = vmw.get_host_info('bar') try:
assert foo == {'hostname': 'foo'} import vmware_inventory
assert bar == {'hostname': 'bar'} except BaseException as ex:
pytest.skip(ex)
def test_show_returns_serializable_data(self): finally:
fakeargs = FakeArgs() sys.path.remove(inventory_dir)
vmw = VMWareInventory(load=False)
vmw.args = fakeargs return vmware_inventory
vmw.inventory = BASICINVENTORY
showdata = vmw.show()
serializable = False def test_host_info_returns_single_host(vmware_inventory):
vmw = vmware_inventory.VMWareInventory(load=False)
try: vmw.inventory = BASIC_INVENTORY
json.loads(showdata) foo = vmw.get_host_info('foo')
serializable = True bar = vmw.get_host_info('bar')
except: assert foo == {'hostname': 'foo'}
pass assert bar == {'hostname': 'bar'}
assert serializable
# import epdb; epdb.st()
def test_show_returns_serializable_data(vmware_inventory):
def test_show_list_returns_serializable_data(self): fakeargs = FakeArgs()
fakeargs = FakeArgs() vmw = vmware_inventory.VMWareInventory(load=False)
vmw = VMWareInventory(load=False) vmw.args = fakeargs
vmw.args = fakeargs vmw.inventory = BASIC_INVENTORY
vmw.args.list = True showdata = vmw.show()
vmw.inventory = BASICINVENTORY json.loads(showdata)
showdata = vmw.show()
serializable = False
def test_show_list_returns_serializable_data(vmware_inventory):
try: fakeargs = FakeArgs()
json.loads(showdata) vmw = vmware_inventory.VMWareInventory(load=False)
serializable = True vmw.args = fakeargs
except: vmw.args.list = True
pass vmw.inventory = BASIC_INVENTORY
assert serializable showdata = vmw.show()
# import epdb; epdb.st() json.loads(showdata)
def test_show_list_returns_all_data(self):
fakeargs = FakeArgs() def test_show_list_returns_all_data(vmware_inventory):
vmw = VMWareInventory(load=False) fakeargs = FakeArgs()
vmw.args = fakeargs vmw = vmware_inventory.VMWareInventory(load=False)
vmw.args.list = True vmw.args = fakeargs
vmw.inventory = BASICINVENTORY vmw.args.list = True
showdata = vmw.show() vmw.inventory = BASIC_INVENTORY
expected = json.dumps(BASICINVENTORY, indent=2) showdata = vmw.show()
assert showdata == expected expected = json.dumps(BASIC_INVENTORY, indent=2)
assert showdata == expected
def test_show_host_returns_serializable_data(self):
fakeargs = FakeArgs()
vmw = VMWareInventory(load=False) def test_show_host_returns_serializable_data(vmware_inventory):
vmw.args = fakeargs fakeargs = FakeArgs()
vmw.args.host = 'foo' vmw = vmware_inventory.VMWareInventory(load=False)
vmw.inventory = BASICINVENTORY vmw.args = fakeargs
showdata = vmw.show() vmw.args.host = 'foo'
serializable = False vmw.inventory = BASIC_INVENTORY
showdata = vmw.show()
try: json.loads(showdata)
json.loads(showdata)
serializable = True
except: def test_show_host_returns_just_host(vmware_inventory):
pass fakeargs = FakeArgs()
assert serializable vmw = vmware_inventory.VMWareInventory(load=False)
# import epdb; epdb.st() vmw.args = fakeargs
vmw.args.list = False
def test_show_host_returns_just_host(self): vmw.args.host = 'foo'
fakeargs = FakeArgs() vmw.inventory = BASIC_INVENTORY
vmw = VMWareInventory(load=False) showdata = vmw.show()
vmw.args = fakeargs expected = BASIC_INVENTORY['_meta']['hostvars']['foo']
vmw.args.list = False expected = json.dumps(expected, indent=2)
vmw.args.host = 'foo' assert showdata == expected
vmw.inventory = BASICINVENTORY
showdata = vmw.show()
expected = BASICINVENTORY['_meta']['hostvars']['foo']
expected = json.dumps(expected, indent=2)
# import epdb; epdb.st()
assert showdata == expected

Loading…
Cancel
Save