Merge "Adds parser tests and cleanup"
[apex.git] / apex / tests / test_apex_inventory.py
1 ##############################################################################
2 # Copyright (c) 2016 Dan Radez (Red Hat)
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 import os
11
12 from nose.tools import (
13     assert_equal,
14     assert_is_instance,
15     assert_raises)
16
17 from apex import Inventory
18 from apex.inventory.inventory import InventoryException
19 from apex.tests.constants import TEST_CONFIG_DIR
20
21 inventory_files = ('intel_pod2_settings.yaml',
22                    'nokia_pod1_settings.yaml',
23                    'pod_example_settings.yaml')
24
25 files_dir = os.path.join(TEST_CONFIG_DIR, 'inventory')
26
27
28 class TestInventory:
29     @classmethod
30     def setup_class(cls):
31         """This method is run once for each class before any tests are run"""
32
33     @classmethod
34     def teardown_class(cls):
35         """This method is run once for each class _after_ all tests are run"""
36
37     def setup(self):
38         """This method is run once before _each_ test method is executed"""
39
40     def teardown(self):
41         """This method is run once after _each_ test method is executed"""
42
43     def test_init(self):
44         for f in inventory_files:
45             i = Inventory(os.path.join(files_dir, f))
46             assert_equal(i.dump_instackenv_json(), None)
47
48         # test virtual
49         i = Inventory(i, virtual=True)
50         assert_equal(i.dump_instackenv_json(), None)
51
52         # Remove nodes to violate HA node count
53         while len(i['nodes']) >= 5:
54             i['nodes'].pop()
55         assert_raises(InventoryException,
56                       Inventory, i)
57
58         # Remove nodes to violate non-HA node count
59         while len(i['nodes']) >= 2:
60             i['nodes'].pop()
61         assert_raises(InventoryException,
62                       Inventory, i, ha=False)
63
64     def test_exception(self):
65         e = InventoryException("test")
66         print(e)
67         assert_is_instance(e, InventoryException)