Migrates Apex to Python
[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 import sys
12 from io import StringIO
13
14 from nose.tools import assert_equal
15 from nose.tools import assert_is_instance
16 from nose.tools import assert_raises
17 from nose.tools import assert_regexp_matches
18
19 from apex import Inventory
20 from apex.inventory.inventory import InventoryException
21 from apex.tests.constants import TEST_CONFIG_DIR
22
23 inventory_files = ('intel_pod2_settings.yaml',
24                    'nokia_pod1_settings.yaml',
25                    'pod_example_settings.yaml')
26
27 files_dir = os.path.join(TEST_CONFIG_DIR, 'inventory')
28
29
30 class TestInventory(object):
31     @classmethod
32     def setup_class(klass):
33         """This method is run once for each class before any tests are run"""
34
35     @classmethod
36     def teardown_class(klass):
37         """This method is run once for each class _after_ all tests are run"""
38
39     def setUp(self):
40         """This method is run once before _each_ test method is executed"""
41
42     def teardown(self):
43         """This method is run once after _each_ test method is executed"""
44
45     def test_init(self):
46         for f in inventory_files:
47             i = Inventory(os.path.join(files_dir, f))
48             assert_equal(i.dump_instackenv_json(), None)
49
50         # test virtual
51         i = Inventory(i, virtual=True)
52         assert_equal(i.dump_instackenv_json(), None)
53
54         # Remove nodes to violate HA node count
55         while len(i['nodes']) >= 5:
56             i['nodes'].pop()
57         assert_raises(InventoryException,
58                       Inventory, i)
59
60         # Remove nodes to violate non-HA node count
61         while len(i['nodes']) >= 2:
62             i['nodes'].pop()
63         assert_raises(InventoryException,
64                       Inventory, i, ha=False)
65
66     def test_exception(self):
67         e = InventoryException("test")
68         print(e)
69         assert_is_instance(e, InventoryException)