QA: Cluster-verify reports shared PVs with exclusive storage
[ganeti-local] / qa / qa_instance.py
index b2a38a3..b604d97 100644 (file)
@@ -29,6 +29,7 @@ import time
 from ganeti import utils
 from ganeti import constants
 from ganeti import query
+from ganeti import pathutils
 
 import qa_config
 import qa_utils
@@ -82,15 +83,18 @@ def _DiskTest(node, disk_template):
     raise
 
 
-def _DestroyInstanceVolumes(instance):
-  """Remove all the LVM volumes of an instance.
+def _GetInstanceInfo(instance):
+  """Return information about the actual state of an instance.
 
-  This is used to simulate HW errors (dead nodes, broken disks...); the
-  configuration of the instance is not affected.
+  @type instance: string
+  @param instance: the instance name
+  @return: a dictionary with two keys:
+      - "nodes": instance nodes, a list of strings
+      - "volumes": instance volume IDs, a list of strings
 
   """
   master = qa_config.GetMasterNode()
-  infocmd = utils.ShellQuoteArgs(["gnt-instance", "info", instance["name"]])
+  infocmd = utils.ShellQuoteArgs(["gnt-instance", "info", instance])
   info_out = qa_utils.GetCommandOutput(master["primary"], infocmd)
   re_node = re.compile(r"^\s+-\s+(?:primary|secondaries):\s+(\S.+)$")
   node_elem = r"([^,()]+)(?:\s+\([^)]+\))?"
@@ -117,10 +121,46 @@ def _DestroyInstanceVolumes(instance):
       vols.append(m.group(1))
   assert vols
   assert nodes
-  for node in nodes:
+  return {"nodes": nodes, "volumes": vols}
+
+
+def _DestroyInstanceVolumes(instance):
+  """Remove all the LVM volumes of an instance.
+
+  This is used to simulate HW errors (dead nodes, broken disks...); the
+  configuration of the instance is not affected.
+  @type instance: dictionary
+  @param instance: the instance
+
+  """
+  info = _GetInstanceInfo(instance["name"])
+  vols = info["volumes"]
+  for node in info["nodes"]:
     AssertCommand(["lvremove", "-f"] + vols, node=node)
 
 
+def _GetBoolInstanceField(instance, field):
+  """Get the Boolean value of a field of an instance.
+
+  @type instance: string
+  @param instance: Instance name
+  @type field: string
+  @param field: Name of the field
+
+  """
+  master = qa_config.GetMasterNode()
+  infocmd = utils.ShellQuoteArgs(["gnt-instance", "list", "--no-headers",
+                                  "-o", field, instance])
+  info_out = qa_utils.GetCommandOutput(master["primary"], infocmd).strip()
+  if info_out == "Y":
+    return True
+  elif info_out == "N":
+    return False
+  else:
+    raise qa_error.Error("Field %s of instance %s has a non-Boolean value:"
+                         " %s" % (field, instance, info_out))
+
+
 @InstanceCheck(None, INST_UP, RETURN_VALUE)
 def TestInstanceAddWithPlainDisk(node):
   """gnt-instance add -t plain"""
@@ -179,6 +219,12 @@ def TestInstanceReinstall(instance):
   """gnt-instance reinstall"""
   AssertCommand(["gnt-instance", "reinstall", "-f", instance["name"]])
 
+  # Test with non-existant OS definition
+  AssertCommand(["gnt-instance", "reinstall", "-f",
+                 "--os-type=NonExistantOsForQa",
+                 instance["name"]],
+                fail=True)
+
 
 def _ReadSsconfInstanceList():
   """Reads ssconf_instance_list from the master node.
@@ -186,7 +232,7 @@ def _ReadSsconfInstanceList():
   """
   master = qa_config.GetMasterNode()
 
-  cmd = ["cat", utils.PathJoin(constants.DATA_DIR,
+  cmd = ["cat", utils.PathJoin(pathutils.DATA_DIR,
                                "ssconf_%s" % constants.SS_INSTANCE_LIST)]
 
   return qa_utils.GetCommandOutput(master["primary"],
@@ -223,17 +269,35 @@ def TestInstanceRenameAndBack(rename_source, rename_target):
   finally:
     qa_utils.RemoveFromEtcHosts(["meeeeh-not-exists", rename_target])
 
+  # Check instance volume tags correctly updated
+  # FIXME: this is LVM specific!
+  info = _GetInstanceInfo(rename_source)
+  tags_cmd = ("lvs -o tags --noheadings %s | grep " %
+              (" ".join(info["volumes"]), ))
+
   # and now rename instance to rename_target...
   AssertCommand(["gnt-instance", "rename", rename_source, rename_target])
   _CheckSsconfInstanceList(rename_target)
   qa_utils.RunInstanceCheck(rename_source, False)
   qa_utils.RunInstanceCheck(rename_target, False)
 
+  # NOTE: tags might not be the exactly as the instance name, due to
+  # charset restrictions; hence the test might be flaky
+  if rename_source != rename_target:
+    for node in info["nodes"]:
+      AssertCommand(tags_cmd + rename_source, node=node, fail=True)
+      AssertCommand(tags_cmd + rename_target, node=node, fail=False)
+
   # and back
   AssertCommand(["gnt-instance", "rename", rename_target, rename_source])
   _CheckSsconfInstanceList(rename_source)
   qa_utils.RunInstanceCheck(rename_target, False)
 
+  if rename_source != rename_target:
+    for node in info["nodes"]:
+      AssertCommand(tags_cmd + rename_source, node=node, fail=False)
+      AssertCommand(tags_cmd + rename_target, node=node, fail=True)
+
 
 @InstanceCheck(INST_UP, INST_UP, FIRST_ARG)
 def TestInstanceFailover(instance):
@@ -249,16 +313,29 @@ def TestInstanceFailover(instance):
 
 
 @InstanceCheck(INST_UP, INST_UP, FIRST_ARG)
-def TestInstanceMigrate(instance):
+def TestInstanceMigrate(instance, toggle_always_failover=True):
   """gnt-instance migrate"""
   cmd = ["gnt-instance", "migrate", "--force", instance["name"]]
+  af_par = constants.BE_ALWAYS_FAILOVER
+  af_field = "be/" + constants.BE_ALWAYS_FAILOVER
+  af_init_val = _GetBoolInstanceField(instance["name"], af_field)
 
   # migrate ...
   AssertCommand(cmd)
+  # TODO: Verify the choice between failover and migration
   qa_utils.RunInstanceCheck(instance, True)
 
-  # ... and back
+  # ... and back (possibly with always_failover toggled)
+  if toggle_always_failover:
+    AssertCommand(["gnt-instance", "modify", "-B",
+                   ("%s=%s" % (af_par, not af_init_val)),
+                   instance["name"]])
   AssertCommand(cmd)
+  # TODO: Verify the choice between failover and migration
+  qa_utils.RunInstanceCheck(instance, True)
+  if toggle_always_failover:
+    AssertCommand(["gnt-instance", "modify", "-B",
+                   ("%s=%s" % (af_par, af_init_val)), instance["name"]])
 
   # TODO: Split into multiple tests
   AssertCommand(["gnt-instance", "shutdown", instance["name"]])
@@ -268,6 +345,7 @@ def TestInstanceMigrate(instance):
                  instance["name"]])
   AssertCommand(["gnt-instance", "start", instance["name"]])
   AssertCommand(cmd)
+  # @InstanceCheck enforces the check that the instance is running
   qa_utils.RunInstanceCheck(instance, True)
 
   AssertCommand(["gnt-instance", "modify", "-B",
@@ -275,10 +353,9 @@ def TestInstanceMigrate(instance):
                   (constants.BE_ALWAYS_FAILOVER, constants.VALUE_TRUE)),
                  instance["name"]])
 
-  AssertCommand(cmd, fail=True)
+  AssertCommand(cmd)
   qa_utils.RunInstanceCheck(instance, True)
-  AssertCommand(["gnt-instance", "migrate", "--force", "--allow-failover",
-                 instance["name"]])
+  # TODO: Verify that a failover has been done instead of a migration
 
   # TODO: Verify whether the default value is restored here (not hardcoded)
   AssertCommand(["gnt-instance", "modify", "-B",
@@ -298,6 +375,8 @@ def TestInstanceInfo(instance):
 @InstanceCheck(INST_UP, INST_UP, FIRST_ARG)
 def TestInstanceModify(instance):
   """gnt-instance modify"""
+  default_hv = qa_config.GetDefaultHypervisor()
+
   # Assume /sbin/init exists on all systems
   test_kernel = "/sbin/init"
   test_initrd = test_kernel
@@ -305,6 +384,7 @@ def TestInstanceModify(instance):
   orig_maxmem = qa_config.get(constants.BE_MAXMEM)
   orig_minmem = qa_config.get(constants.BE_MINMEM)
   #orig_bridge = qa_config.get("bridge", "xen-br0")
+
   args = [
     ["-B", "%s=128" % constants.BE_MINMEM],
     ["-B", "%s=128" % constants.BE_MAXMEM],
@@ -318,27 +398,36 @@ def TestInstanceModify(instance):
 
     ["-H", "%s=%s" % (constants.HV_KERNEL_PATH, test_kernel)],
     ["-H", "%s=%s" % (constants.HV_KERNEL_PATH, constants.VALUE_DEFAULT)],
-    ["-H", "%s=%s" % (constants.HV_INITRD_PATH, test_initrd)],
-    ["-H", "no_%s" % (constants.HV_INITRD_PATH, )],
-    ["-H", "%s=%s" % (constants.HV_INITRD_PATH, constants.VALUE_DEFAULT)],
 
     # TODO: bridge tests
     #["--bridge", "xen-br1"],
     #["--bridge", orig_bridge],
-
-    # TODO: Do these tests only with xen-hvm
-    #["-H", "%s=acn" % constants.HV_BOOT_ORDER],
-    #["-H", "%s=%s" % (constants.HV_BOOT_ORDER, constants.VALUE_DEFAULT)],
     ]
+
+  if default_hv == constants.HT_XEN_PVM:
+    args.extend([
+      ["-H", "%s=%s" % (constants.HV_INITRD_PATH, test_initrd)],
+      ["-H", "no_%s" % (constants.HV_INITRD_PATH, )],
+      ["-H", "%s=%s" % (constants.HV_INITRD_PATH, constants.VALUE_DEFAULT)],
+      ])
+  elif default_hv == constants.HT_XEN_HVM:
+    args.extend([
+      ["-H", "%s=acn" % constants.HV_BOOT_ORDER],
+      ["-H", "%s=%s" % (constants.HV_BOOT_ORDER, constants.VALUE_DEFAULT)],
+      ])
+
   for alist in args:
     AssertCommand(["gnt-instance", "modify"] + alist + [instance["name"]])
 
   # check no-modify
   AssertCommand(["gnt-instance", "modify", instance["name"]], fail=True)
 
-  # Marking offline/online while instance is running must fail
-  for arg in ["--online", "--offline"]:
-    AssertCommand(["gnt-instance", "modify", arg, instance["name"]], fail=True)
+  # Marking offline while instance is running must fail...
+  AssertCommand(["gnt-instance", "modify", "--offline", instance["name"]],
+                 fail=True)
+
+  # ...while making it online is ok, and should work
+  AssertCommand(["gnt-instance", "modify", "--online", instance["name"]])
 
 
 @InstanceCheck(INST_DOWN, INST_DOWN, FIRST_ARG)
@@ -352,6 +441,16 @@ def TestInstanceStoppedModify(instance):
   # Mark instance as offline
   AssertCommand(["gnt-instance", "modify", "--offline", name])
 
+  # When the instance is offline shutdown should only work with --force,
+  # while start should never work
+  AssertCommand(["gnt-instance", "shutdown", name], fail=True)
+  AssertCommand(["gnt-instance", "shutdown", "--force", name])
+  AssertCommand(["gnt-instance", "start", name], fail=True)
+  AssertCommand(["gnt-instance", "start", "--force", name], fail=True)
+
+  # Also do offline to offline
+  AssertCommand(["gnt-instance", "modify", "--offline", name])
+
   # And online again
   AssertCommand(["gnt-instance", "modify", "--online", name])
 
@@ -416,19 +515,29 @@ def TestReplaceDisks(instance, pnode, snode, othernode):
     cmd.append(instance["name"])
     return cmd
 
+  options = qa_config.get("options", {})
+  use_ialloc = options.get("use-iallocators", True)
   for data in [
     ["-p"],
     ["-s"],
-    ["--new-secondary=%s" % othernode["primary"]],
-    # and restore
+    # A placeholder; the actual command choice depends on use_ialloc
+    None,
+    # Restore the original secondary
     ["--new-secondary=%s" % snode["primary"]],
     ]:
+    if data is None:
+      if use_ialloc:
+        data = ["-I", constants.DEFAULT_IALLOCATOR_SHORTCUT]
+      else:
+        data = ["--new-secondary=%s" % othernode["primary"]]
     AssertCommand(buildcmd(data))
 
   AssertCommand(buildcmd(["-a"]))
   AssertCommand(["gnt-instance", "stop", instance["name"]])
   AssertCommand(buildcmd(["-a"]), fail=True)
   AssertCommand(["gnt-instance", "activate-disks", instance["name"]])
+  AssertCommand(["gnt-instance", "activate-disks", "--wait-for-sync",
+                 instance["name"]])
   AssertCommand(buildcmd(["-a"]))
   AssertCommand(["gnt-instance", "start", instance["name"]])
 
@@ -451,8 +560,11 @@ def _AssertRecreateDisks(cmdargs, instance, fail=False, check=True,
   if not fail and check:
     # Quick check that the disks are there
     AssertCommand(["gnt-instance", "activate-disks", instance["name"]])
+    AssertCommand(["gnt-instance", "activate-disks", "--wait-for-sync",
+                   instance["name"]])
     AssertCommand(["gnt-instance", "deactivate-disks", instance["name"]])
 
+
 @InstanceCheck(INST_UP, INST_UP, FIRST_ARG)
 def TestRecreateDisks(instance, pnode, snode, othernodes):
   """gnt-instance recreate-disks
@@ -463,19 +575,31 @@ def TestRecreateDisks(instance, pnode, snode, othernodes):
   @param othernodes: list/tuple of nodes where to temporarily recreate disks
 
   """
+  options = qa_config.get("options", {})
+  use_ialloc = options.get("use-iallocators", True)
   other_seq = ":".join([n["primary"] for n in othernodes])
   orig_seq = pnode["primary"]
   if snode:
     orig_seq = orig_seq + ":" + snode["primary"]
-  # This fails beacuse the instance is running
+  # These fail because the instance is running
   _AssertRecreateDisks(["-n", other_seq], instance, fail=True, destroy=False)
+  if use_ialloc:
+    _AssertRecreateDisks(["-I", "hail"], instance, fail=True, destroy=False)
+  else:
+    _AssertRecreateDisks(["-n", other_seq], instance, fail=True, destroy=False)
   AssertCommand(["gnt-instance", "stop", instance["name"]])
   # Disks exist: this should fail
   _AssertRecreateDisks([], instance, fail=True, destroy=False)
   # Recreate disks in place
   _AssertRecreateDisks([], instance)
   # Move disks away
-  _AssertRecreateDisks(["-n", other_seq], instance)
+  if use_ialloc:
+    _AssertRecreateDisks(["-I", "hail"], instance)
+    # Move disks somewhere else
+    _AssertRecreateDisks(["-I", constants.DEFAULT_IALLOCATOR_SHORTCUT],
+                         instance)
+  else:
+    _AssertRecreateDisks(["-n", other_seq], instance)
   # Move disks back
   _AssertRecreateDisks(["-n", orig_seq], instance, check=False)
   # This and InstanceCheck decoration check that the disks are working
@@ -511,7 +635,7 @@ def TestInstanceImport(newinst, node, expnode, name):
           "--disk-template=plain",
           "--no-ip-check",
           "--src-node=%s" % expnode["primary"],
-          "--src-dir=%s/%s" % (constants.EXPORT_DIR, name),
+          "--src-dir=%s/%s" % (pathutils.EXPORT_DIR, name),
           "--node=%s" % node["primary"]] +
          _GetGenericAddParameters(newinst, force_mac=constants.VALUE_GENERATE))
   cmd.append(newinst["name"])