From 0785656344c3c42b5f1ff408d2fad3e29f98f278 Mon Sep 17 00:00:00 2001 From: Matt Clay Date: Tue, 9 Oct 2018 22:14:26 -0700 Subject: [PATCH] Fix vmware_inventory unit tests so they run. --- test/runner/requirements/units.txt | 1 + .../inventory/test_vmware_inventory.py | 166 ++++++++---------- 2 files changed, 70 insertions(+), 97 deletions(-) diff --git a/test/runner/requirements/units.txt b/test/runner/requirements/units.txt index a1fecb6025..83d5cad5b0 100644 --- a/test/runner/requirements/units.txt +++ b/test/runner/requirements/units.txt @@ -10,6 +10,7 @@ pytest pytest-mock pytest-xdist python-memcached +pyvmomi pyyaml redis requests diff --git a/test/units/contrib/inventory/test_vmware_inventory.py b/test/units/contrib/inventory/test_vmware_inventory.py index 8a7df53667..8ecc8efcb6 100644 --- a/test/units/contrib/inventory/test_vmware_inventory.py +++ b/test/units/contrib/inventory/test_vmware_inventory.py @@ -1,31 +1,9 @@ import json import os -import pickle -import unittest +import pytest import sys -from nose.plugins.skip import SkipTest - -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 = { +BASIC_INVENTORY = { 'all': { 'hosts': ['foo', 'bar'] }, @@ -46,85 +24,79 @@ class FakeArgs(object): 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): - 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'} + sys.path.append(inventory_dir) - 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: + import vmware_inventory + except BaseException as ex: + pytest.skip(ex) + finally: + sys.path.remove(inventory_dir) - try: - json.loads(showdata) - serializable = True - except: - pass - assert serializable - # import epdb; epdb.st() + return vmware_inventory - 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_host_info_returns_single_host(vmware_inventory): + vmw = vmware_inventory.VMWareInventory(load=False) + vmw.inventory = BASIC_INVENTORY + foo = vmw.get_host_info('foo') + bar = vmw.get_host_info('bar') + assert foo == {'hostname': 'foo'} + 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): - fakeargs = FakeArgs() - vmw = VMWareInventory(load=False) - vmw.args = fakeargs - vmw.args.host = 'foo' - vmw.inventory = BASICINVENTORY - showdata = vmw.show() - serializable = False +def test_show_returns_serializable_data(vmware_inventory): + fakeargs = FakeArgs() + vmw = vmware_inventory.VMWareInventory(load=False) + vmw.args = fakeargs + vmw.inventory = BASIC_INVENTORY + showdata = vmw.show() + json.loads(showdata) - 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 +def test_show_list_returns_serializable_data(vmware_inventory): + fakeargs = FakeArgs() + vmw = vmware_inventory.VMWareInventory(load=False) + vmw.args = fakeargs + vmw.args.list = True + vmw.inventory = BASIC_INVENTORY + showdata = vmw.show() + json.loads(showdata) + + +def test_show_list_returns_all_data(vmware_inventory): + 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