summaryrefslogtreecommitdiff
path: root/test/units/contrib/inventory/test_vmware_inventory.py
blob: 52e750b844f7abd024c60eda33828c2a45a4456d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import json
import os
import pickle
import unittest
import sys

try:
    from vmware_inventory import VMWareInventory
except ImportError:
    from nose.plugins.skip import SkipTest
    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': {
        '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