commit da4a52a324e7f2347de8831325c306862860ee63
Author: Thomas Thrainer <thomasth@google.com>
Date:   Tue Jun 11 17:05:41 2013 +0200

    Index instances by their UUID
    
    No longer index instances by their name but by their UUID in the cluster
    config. This change changes large parts of the code, as the following
    adjustments were necessary:
     * Change the index key to UUID in the configuration and the
       ConfigWriter, including all methods.
     * External interfaces (command line interface, IAllocator interface,
       hook scripts, etc.) are kept stable.
     * Instance UUID's are resolved in ExpandNames and then stored in the
       OpCode. This allows to check for instance renames if the OpCode is
       reloaded after a cluster restart. This check is currently only done
       for single instance parameters.
     * Instance locking unfortunately can't use instances UUID as
       identifiers. The reasons is that new instances (which have no UUID
       yet) have to be locked as well, so the instance name is used.
     * Variable names are renamed to follow the following pattern:
       - Suffix is 'inst' or 'insts': Variable holds Instance objects
       - Suffix is 'name' or 'names': Variable holds Instance names
       - Suffix is 'uuid' or 'uuids': Variable holds Instance UUID's
     * Tests are adapted.
    
    Signed-off-by: Thomas Thrainer <thomasth@google.com>
    Reviewed-by: Klaus Aehlig <aehlig@google.com>

diff --git a/test/py/ganeti.query_unittest.py b/test/py/ganeti.query_unittest.py
index 0c7d117..0b81c41 100755
--- a/test/py/ganeti.query_unittest.py
+++ b/test/py/ganeti.query_unittest.py
@@ -346,7 +346,7 @@ class TestNodeQuery(unittest.TestCase):
                    ndparams={constants.ND_SPINDLE_COUNT: 4}),
       ]
     for live_data in [None, dict.fromkeys([node.name for node in nodes], {})]:
-      nqd = query.NodeQueryData(nodes, live_data, None, None, None,
+      nqd = query.NodeQueryData(nodes, live_data, None, None, None, None,
                                 groups, None, cluster)
 
       q = self._Create(["name", "drained"])
@@ -448,11 +448,20 @@ class TestNodeQuery(unittest.TestCase):
       dict((query._NODE_LIVE_FIELDS[name][2], value)
            for name, value in fake_live_data.items())
 
-    node_to_primary = dict((node.uuid, set()) for node in nodes)
-    node_to_primary[master_node.uuid].update(["inst1", "inst2"])
+    node_to_primary_uuid = dict((node.uuid, set()) for node in nodes)
+    node_to_primary_uuid[master_node.uuid].update(["inst1", "inst2"])
 
-    node_to_secondary = dict((node.uuid, set()) for node in nodes)
-    node_to_secondary[live_data_node.uuid].update(["instX", "instY", "instZ"])
+    node_to_secondary_uuid = dict((node.uuid, set()) for node in nodes)
+    node_to_secondary_uuid[live_data_node.uuid].update(["instX", "instY",
+                                                        "instZ"])
+
+    inst_uuid_to_inst_name = {
+      "inst1": "inst1-name",
+      "inst2": "inst2-name",
+      "instX": "instX-name",
+      "instY": "instY-name",
+      "instZ": "instZ-name"
+    }
 
     ng_uuid = "492b4b74-8670-478a-b98d-4c53a76238e6"
     groups = {
@@ -468,8 +477,9 @@ class TestNodeQuery(unittest.TestCase):
     master_node.group = ng_uuid
 
     nqd = query.NodeQueryData(nodes, live_data, master_node.uuid,
-                              node_to_primary, node_to_secondary, groups,
-                              oob_support, cluster)
+                              node_to_primary_uuid, node_to_secondary_uuid,
+                              inst_uuid_to_inst_name, groups, oob_support,
+                              cluster)
     result = q.Query(nqd)
     self.assert_(compat.all(len(row) == len(selected) for row in result))
     self.assertEqual([row[field_index["name"]] for row in result],
@@ -523,11 +533,13 @@ class TestNodeQuery(unittest.TestCase):
                      (constants.RS_NORMAL, 3))
     self.assertEqual(master_row[field_index["pinst_list"]],
                      (constants.RS_NORMAL,
-                      list(node_to_primary[master_node.uuid])))
+                      [inst_uuid_to_inst_name[uuid] for uuid in
+                       node_to_primary_uuid[master_node.uuid]]))
     self.assertEqual(live_data_row[field_index["sinst_list"]],
                      (constants.RS_NORMAL,
-                      utils.NiceSort(list(
-                        node_to_secondary[live_data_node.uuid]))))
+                      utils.NiceSort(
+                        [inst_uuid_to_inst_name[uuid] for uuid in
+                        node_to_secondary_uuid[live_data_node.uuid]])))
 
   def testGetLiveNodeField(self):
     nodes = [
@@ -545,7 +557,8 @@ class TestNodeQuery(unittest.TestCase):
     live_data = dict.fromkeys([node.name for node in nodes], {})
 
     # No data
-    nqd = query.NodeQueryData(None, None, None, None, None, None, None, None)
+    nqd = query.NodeQueryData(None, None, None, None, None, None, None, None,
+                              None)
     self.assertEqual(query._GetLiveNodeField("hello", constants.QFT_NUMBER,
                                              nqd, nodes[0]),
                      query._FS_NODATA)
@@ -674,7 +687,7 @@ class TestInstanceQuery(unittest.TestCase):
 
     instances = [
       objects.Instance(name="inst1", hvparams={}, beparams={}, nics=[],
-        uuid="f90eccb3-e227-4e3c-bf2a-94a21ca8f9cd",
+        uuid="inst1-uuid",
         ctime=1291244000, mtime=1291244400, serial_no=30,
         admin_state=constants.ADMINST_UP, hypervisor=constants.HT_XEN_PVM,
         os="linux1",
@@ -684,7 +697,7 @@ class TestInstanceQuery(unittest.TestCase):
         disks_active=True,
         osparams={}),
       objects.Instance(name="inst2", hvparams={}, nics=[],
-        uuid="73a0f8a7-068c-4630-ada2-c3440015ab1a",
+        uuid="inst2-uuid",
         ctime=1291211000, mtime=1291211077, serial_no=1,
         admin_state=constants.ADMINST_UP, hypervisor=constants.HT_XEN_HVM,
         os="deb99",
@@ -698,7 +711,7 @@ class TestInstanceQuery(unittest.TestCase):
         },
         osparams={}),
       objects.Instance(name="inst3", hvparams={}, beparams={},
-        uuid="11ec8dff-fb61-4850-bfe0-baa1803ff280",
+        uuid="inst3-uuid",
         ctime=1291011000, mtime=1291013000, serial_no=1923,
         admin_state=constants.ADMINST_DOWN, hypervisor=constants.HT_KVM,
         os="busybox",
@@ -715,7 +728,7 @@ class TestInstanceQuery(unittest.TestCase):
           ],
         osparams={}),
       objects.Instance(name="inst4", hvparams={}, beparams={},
-        uuid="68dab168-3ef5-4c9d-b4d3-801e0672068c",
+        uuid="inst4-uuid",
         ctime=1291244390, mtime=1291244395, serial_no=25,
         admin_state=constants.ADMINST_DOWN, hypervisor=constants.HT_XEN_PVM,
         os="linux1",
@@ -741,7 +754,7 @@ class TestInstanceQuery(unittest.TestCase):
           ],
         osparams={}),
       objects.Instance(name="inst5", hvparams={}, nics=[],
-        uuid="0e3dca12-5b42-4e24-98a2-415267545bd0",
+        uuid="inst5-uuid",
         ctime=1231211000, mtime=1261200000, serial_no=3,
         admin_state=constants.ADMINST_UP, hypervisor=constants.HT_XEN_HVM,
         os="deb99",
@@ -755,7 +768,7 @@ class TestInstanceQuery(unittest.TestCase):
         },
         osparams={}),
       objects.Instance(name="inst6", hvparams={}, nics=[],
-        uuid="72de6580-c8d5-4661-b902-38b5785bb8b3",
+        uuid="inst6-uuid",
         ctime=7513, mtime=11501, serial_no=13390,
         admin_state=constants.ADMINST_DOWN, hypervisor=constants.HT_XEN_HVM,
         os="deb99",
@@ -771,7 +784,7 @@ class TestInstanceQuery(unittest.TestCase):
           "clean_install": "no",
           }),
       objects.Instance(name="inst7", hvparams={}, nics=[],
-        uuid="ceec5dc4-b729-4f42-ae28-69b3cd24920e",
+        uuid="inst7-uuid",
         ctime=None, mtime=None, serial_no=1947,
         admin_state=constants.ADMINST_DOWN, hypervisor=constants.HT_XEN_HVM,
         os="deb99",
@@ -782,7 +795,7 @@ class TestInstanceQuery(unittest.TestCase):
         beparams={},
         osparams={}),
       objects.Instance(name="inst8", hvparams={}, nics=[],
-        uuid="ceec5dc4-b729-4f42-ae28-69b3cd24920f",
+        uuid="inst8-uuid",
         ctime=None, mtime=None, serial_no=19478,
         admin_state=constants.ADMINST_OFFLINE, hypervisor=constants.HT_XEN_HVM,
         os="deb99",
@@ -794,40 +807,41 @@ class TestInstanceQuery(unittest.TestCase):
         osparams={}),
       ]
 
+    assert not utils.FindDuplicates(inst.uuid for inst in instances)
     assert not utils.FindDuplicates(inst.name for inst in instances)
 
     instbyname = dict((inst.name, inst) for inst in instances)
 
-    disk_usage = dict((inst.name,
+    disk_usage = dict((inst.uuid,
                        gmi.ComputeDiskSize(inst.disk_template,
                                            [{"size": disk.size}
                                            for disk in inst.disks]))
                       for inst in instances)
 
     inst_bridges = {
-      "inst3": [constants.DEFAULT_BRIDGE, constants.DEFAULT_BRIDGE],
-      "inst4": [constants.DEFAULT_BRIDGE, constants.DEFAULT_BRIDGE,
-                None, "eth123"],
+      "inst3-uuid": [constants.DEFAULT_BRIDGE, constants.DEFAULT_BRIDGE],
+      "inst4-uuid": [constants.DEFAULT_BRIDGE, constants.DEFAULT_BRIDGE,
+                     None, "eth123"],
       }
 
     live_data = {
-      "inst2": {
+      "inst2-uuid": {
         "vcpus": 3,
         },
-      "inst4": {
+      "inst4-uuid": {
         "memory": 123,
         },
-      "inst6": {
+      "inst6-uuid": {
         "memory": 768,
         },
-      "inst7": {
+      "inst7-uuid": {
         "vcpus": 3,
         },
       }
-    wrongnode_inst = set(["inst7"])
+    wrongnode_inst = set(["inst7-uuid"])
 
-    consinfo = dict((inst.name, None) for inst in instances)
-    consinfo["inst7"] = \
+    consinfo = dict((inst.uuid, None) for inst in instances)
+    consinfo["inst7-uuid"] = \
       objects.InstanceConsole(instance="inst7", kind=constants.CONS_SSH,
                               host=instbyname["inst7"].primary_node,
                               user="root",
@@ -862,8 +876,8 @@ class TestInstanceQuery(unittest.TestCase):
         exp_status = constants.INSTST_NODEOFFLINE
       elif inst.primary_node in bad_nodes:
         exp_status = constants.INSTST_NODEDOWN
-      elif inst.name in live_data:
-        if inst.name in wrongnode_inst:
+      elif inst.uuid in live_data:
+        if inst.uuid in wrongnode_inst:
           exp_status = constants.INSTST_WRONGNODE
         elif inst.admin_state == constants.ADMINST_UP:
           exp_status = constants.INSTST_RUNNING
@@ -886,8 +900,8 @@ class TestInstanceQuery(unittest.TestCase):
       for (field, livefield) in [("oper_vcpus", "vcpus")]:
         if inst.primary_node in bad_nodes:
           exp = (constants.RS_NODATA, None)
-        elif inst.name in live_data:
-          value = live_data[inst.name].get(livefield, None)
+        elif inst.uuid in live_data:
+          value = live_data[inst.uuid].get(livefield, None)
           if value is None:
             exp = (constants.RS_UNAVAIL, None)
           else:
@@ -897,7 +911,7 @@ class TestInstanceQuery(unittest.TestCase):
 
         self.assertEqual(row[fieldidx[field]], exp)
 
-      bridges = inst_bridges.get(inst.name, [])
+      bridges = inst_bridges.get(inst.uuid, [])
       self.assertEqual(row[fieldidx["nic.bridges"]],
                        (constants.RS_NORMAL, bridges))
       if bridges:
@@ -917,12 +931,12 @@ class TestInstanceQuery(unittest.TestCase):
       if inst.primary_node in bad_nodes:
         exp = (constants.RS_NODATA, None)
       else:
-        exp = (constants.RS_NORMAL, inst.name in live_data)
+        exp = (constants.RS_NORMAL, inst.uuid in live_data)
       self.assertEqual(row[fieldidx["oper_state"]], exp)
 
       cust_exp = (constants.RS_NORMAL, {})
       if inst.os == "deb99":
-        if inst.name == "inst6":
+        if inst.uuid == "inst6-uuid":
           exp = (constants.RS_NORMAL, {"clean_install": "no"})
           cust_exp = exp
         else:
@@ -932,7 +946,7 @@ class TestInstanceQuery(unittest.TestCase):
       self.assertEqual(row[fieldidx["osparams"]], exp)
       self.assertEqual(row[fieldidx["custom_osparams"]], cust_exp)
 
-      usage = disk_usage[inst.name]
+      usage = disk_usage[inst.uuid]
       if usage is None:
         usage = 0
       self.assertEqual(row[fieldidx["disk_usage"]],
