Updates to Inventory object 97/44397/7
authorDan Radez <dradez@redhat.com>
Thu, 5 Oct 2017 19:17:53 +0000 (15:17 -0400)
committerDan Radez <dradez@redhat.com>
Wed, 11 Oct 2017 18:06:52 +0000 (14:06 -0400)
- moved get_node_counts over from overcloud/deploy.py
- added more test coverage

Change-Id: I7774bebb99169aa1da0f07cb2cfc0c1d197af5ca
Signed-off-by: Dan Radez <dradez@redhat.com>
apex/inventory/inventory.py
apex/overcloud/overcloud_deploy.py
apex/tests/test_apex_inventory.py

index 3483e57..b5ffd2f 100644 (file)
@@ -41,34 +41,35 @@ class Inventory(dict):
         # move ipmi_* to pm_*
         # make mac a list
         def munge_node(node):
-            node['pm_addr'] = node['ipmi_ip']
-            node['pm_password'] = node['ipmi_pass']
-            node['pm_user'] = node['ipmi_user']
-            node['mac'] = [node['mac_address']]
-            if 'cpus' in node:
-                node['cpu'] = node['cpus']
+            pairs = (('pm_addr', 'ipmi_ip'), ('pm_password', 'ipmi_pass'),
+                     ('pm_user', 'ipmi_user'), ('mac', 'mac_address'),
+                     ('cpu', 'cpus'), (None, 'disk_device'))
+
+            for x, y in pairs:
+                if y in node:
+                    if y == 'disk_device':
+                        self.root_device = node[y]
+                    elif x == 'mac':
+                        node[x] = [node[y]]
+                    elif x is not None and y in node:
+                        node[x] = node[y]
+                    del node[y]
 
             # aarch64 is always uefi
             if 'arch' in node and node['arch'] == 'aarch64':
                 node['capabilities'] += ',boot_mode:uefi'
 
-            for i in ('ipmi_ip', 'ipmi_pass', 'ipmi_user', 'mac_address',
-                      'disk_device'):
-                if i in node.keys():
-                    if i == 'disk_device':
-                        self.root_device = node[i]
-                    del node[i]
-
             return node
+
         super().__init__({'nodes': list(map(munge_node, init_dict['nodes']))})
 
         # verify number of nodes
         if ha and len(self['nodes']) < 5:
-            raise InventoryException('You must provide at least 5 '
-                                     'nodes for HA deployment')
+            raise ApexInventoryException('You must provide at least 5 '
+                                         'nodes for HA deployment')
         elif len(self['nodes']) < 2:
-            raise InventoryException('You must provide at least 2 nodes '
-                                     'for non-HA deployment')
+            raise ApexInventoryException('You must provide at least 2 nodes '
+                                         'for non-HA deployment')
 
         if virtual:
             self['host-ip'] = '192.168.122.1'
@@ -81,10 +82,25 @@ class Inventory(dict):
     def dump_instackenv_json(self):
         print(json.dumps(dict(self), sort_keys=True, indent=4))
 
-
-class InventoryException(Exception):
-    def __init__(self, value):
-        self.value = value
-
-    def __str__(self):
-            return self.value
+    def get_node_counts(self):
+        """
+        Return numbers of controller and compute nodes in inventory
+        :param inventory: node inventory data structure
+        :return: number of controller and compute nodes in inventory
+        """
+        nodes = self['nodes']
+        num_control = 0
+        num_compute = 0
+        for node in nodes:
+            if 'profile:control' in node['capabilities']:
+                num_control += 1
+            elif 'profile:compute' in node['capabilities']:
+                num_compute += 1
+            else:
+                raise ApexInventoryException("Node missing capabilities "
+                                             "key: {}".format(node))
+        return num_control, num_compute
+
+
+class ApexInventoryException(Exception):
+    pass
index e324853..ef916a4 100644 (file)
@@ -93,34 +93,6 @@ def build_sdn_env_list(ds, sdn_map, env_list=None):
     return env_list
 
 
-def _get_node_counts(inventory):
-    """
-    Return numbers of controller and compute nodes in inventory
-
-    :param inventory: node inventory data structure
-    :return: number of controller and compute nodes in inventory
-    """
-    if not inventory:
-        raise ApexDeployException("Empty inventory")
-
-    nodes = inventory['nodes']
-    num_control = 0
-    num_compute = 0
-    for node in nodes:
-        if node['capabilities'] == 'profile:control':
-            num_control += 1
-        elif node['capabilities'] == 'profile:compute':
-            num_compute += 1
-        else:
-            # TODO(trozet) do we want to allow capabilities to not exist?
-            logging.error("Every node must include a 'capabilities' key "
-                          "tagged with either 'profile:control' or "
-                          "'profile:compute'")
-            raise ApexDeployException("Node missing capabilities "
-                                      "key: {}".format(node))
-    return num_control, num_compute
-
-
 def create_deploy_cmd(ds, ns, inv, tmp_dir,
                       virtual, env_file='opnfv-environment.yaml'):
 
@@ -146,7 +118,7 @@ def create_deploy_cmd(ds, ns, inv, tmp_dir,
     else:
         deploy_options.append('baremetal-environment.yaml')
 
-    num_control, num_compute = _get_node_counts(inv)
+    num_control, num_compute = inv.get_node_counts()
     if num_control == 0 or num_compute == 0:
         logging.error("Detected 0 control or compute nodes.  Control nodes: "
                       "{}, compute nodes{}".format(num_control, num_compute))
@@ -410,7 +382,7 @@ def prep_env(ds, ns, inv, opnfv_env, net_env, tmp_dir):
             if 'OS::TripleO::Services::NeutronDhcpAgent' in line:
                 output_line = ''
             elif 'NeutronDhcpAgentsPerNetwork' in line:
-                num_control, num_compute = _get_node_counts(inv)
+                num_control, num_compute = inv.get_node_counts()
                 output_line = ("  NeutronDhcpAgentsPerNetwork: {}"
                                .format(num_compute))
             elif 'ComputeServices' in line:
index 87e7d50..7197946 100644 (file)
@@ -15,7 +15,7 @@ from nose.tools import (
     assert_raises)
 
 from apex import Inventory
-from apex.inventory.inventory import InventoryException
+from apex.inventory.inventory import ApexInventoryException
 from apex.tests.constants import (
     TEST_CONFIG_DIR,
     TEST_DUMMY_CONFIG
@@ -47,14 +47,17 @@ class TestInventory:
         for f in inventory_files:
             i = Inventory(os.path.join(files_dir, f))
             assert_equal(i.dump_instackenv_json(), None)
+            i['nodes'][0]['arch'] = 'aarch64'
+            i = Inventory(i)
+            assert_equal(i['nodes'][0]['arch'], 'aarch64')
 
     def test_inventory_invalid_ha_count(self):
-        assert_raises(InventoryException, Inventory,
+        assert_raises(ApexInventoryException, Inventory,
                       os.path.join(TEST_DUMMY_CONFIG, 'inventory-virt.yaml'),
                       virtual=True, ha=True)
 
     def test_inventory_invalid_noha_count(self):
-        assert_raises(InventoryException, Inventory,
+        assert_raises(ApexInventoryException, Inventory,
                       os.path.join(TEST_DUMMY_CONFIG,
                                    'inventory-virt-1-node.yaml'),
                       virtual=True, ha=False)
@@ -64,7 +67,7 @@ class TestInventory:
                       virtual=True, ha=False)
         assert_equal(i.dump_instackenv_json(), None)
 
-    def test_exception(self):
-        e = InventoryException("test")
-        print(e)
-        assert_is_instance(e, InventoryException)
+    def test_get_node_counts(self):
+        i = Inventory(os.path.join(TEST_DUMMY_CONFIG, 'inventory-virt.yaml'),
+                      virtual=True, ha=False)
+        assert_equal(i.get_node_counts(), (1, 1))