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

Loading…
Cancel
Save