+def _GetInstanceInfo(instance):
+ """Return information about the actual state of an instance.
+
+ @type instance: string
+ @param instance: the instance name
+ @return: a dictionary with the following keys:
+ - "nodes": instance nodes, a list of strings
+ - "volumes": instance volume IDs, a list of strings
+ - "drbd-minors": DRBD minors used by the instance, a dictionary where
+ keys are nodes, and values are lists of integers (or an empty
+ dictionary for non-DRBD instances)
+
+ """
+ master = qa_config.GetMasterNode()
+ 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+\([^)]+\))?"
+ # re_nodelist matches a list of nodes returned by gnt-instance info, e.g.:
+ # node1.fqdn
+ # node2.fqdn,node3.fqdn
+ # node4.fqdn (group mygroup, group UUID 01234567-abcd-0123-4567-0123456789ab)
+ # FIXME This works with no more than 2 secondaries
+ re_nodelist = re.compile(node_elem + "(?:," + node_elem + ")?$")
+ re_vol = re.compile(r"^\s+logical_id:\s+(\S+)$")
+ re_drbdnode = re.compile(r"^\s+node[AB]:\s+([^\s,]+),\s+minor=([0-9]+)$")
+ nodes = []
+ vols = []
+ drbd_min = {}
+ for line in info_out.splitlines():
+ m = re_node.match(line)
+ if m:
+ nodestr = m.group(1)
+ m2 = re_nodelist.match(nodestr)
+ if m2:
+ nodes.extend(filter(None, m2.groups()))
+ else:
+ nodes.append(nodestr)
+ m = re_vol.match(line)
+ if m:
+ vols.append(m.group(1))
+ m = re_drbdnode.match(line)
+ if m:
+ node = m.group(1)
+ minor = int(m.group(2))
+ if drbd_min.get(node) is not None:
+ drbd_min[node].append(minor)
+ else:
+ drbd_min[node] = [minor]
+ assert vols
+ assert nodes
+ return {"nodes": nodes, "volumes": vols, "drbd-minors": drbd_min}
+
+
+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 _GetInstanceField(instance, field):
+ """Get the value of a field of an instance.
+
+ @type instance: string
+ @param instance: Instance name
+ @type field: string
+ @param field: Name of the field
+ @rtype: string
+
+ """
+ master = qa_config.GetMasterNode()
+ infocmd = utils.ShellQuoteArgs(["gnt-instance", "list", "--no-headers",
+ "--units", "m", "-o", field, instance])
+ return qa_utils.GetCommandOutput(master["primary"], infocmd).strip()
+
+
+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
+ @rtype: bool
+
+ """
+ info_out = _GetInstanceField(instance, field)
+ 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))
+
+
+def _GetNumInstanceField(instance, field):
+ """Get a numeric value of a field of an instance.
+
+ @type instance: string
+ @param instance: Instance name
+ @type field: string
+ @param field: Name of the field
+ @rtype: int or float
+
+ """
+ info_out = _GetInstanceField(instance, field)
+ try:
+ ret = int(info_out)
+ except ValueError:
+ try:
+ ret = float(info_out)
+ except ValueError:
+ raise qa_error.Error("Field %s of instance %s has a non-numeric value:"
+ " %s" % (field, instance, info_out))
+ return ret
+
+
+def GetInstanceSpec(instance, spec):
+ """Return the current spec for the given parameter.
+
+ @type instance: string
+ @param instance: Instance name
+ @type spec: string
+ @param spec: one of the supported parameters: "mem-size", "cpu-count",
+ "disk-count", "disk-size", "nic-count"
+ @rtype: tuple
+ @return: (minspec, maxspec); minspec and maxspec can be different only for
+ memory and disk size
+
+ """
+ specmap = {
+ "mem-size": ["be/minmem", "be/maxmem"],
+ "cpu-count": ["vcpus"],
+ "disk-count": ["disk.count"],
+ "disk-size": ["disk.size/ "],
+ "nic-count": ["nic.count"],
+ }
+ # For disks, first we need the number of disks
+ if spec == "disk-size":
+ (numdisk, _) = GetInstanceSpec(instance, "disk-count")
+ fields = ["disk.size/%s" % k for k in range(0, numdisk)]
+ else:
+ assert spec in specmap, "%s not in %s" % (spec, specmap)
+ fields = specmap[spec]
+ values = [_GetNumInstanceField(instance, f) for f in fields]
+ return (min(values), max(values))
+
+
+def IsFailoverSupported(instance):
+ templ = qa_config.GetInstanceTemplate(instance)
+ return templ in constants.DTS_MIRRORED
+
+
+def IsMigrationSupported(instance):
+ templ = qa_config.GetInstanceTemplate(instance)
+ return templ in constants.DTS_MIRRORED
+
+
+def IsDiskReplacingSupported(instance):
+ templ = qa_config.GetInstanceTemplate(instance)
+ return templ == constants.DT_DRBD8
+
+
+def IsDiskSupported(instance):
+ templ = qa_config.GetInstanceTemplate(instance)
+ return templ != constants.DT_DISKLESS
+
+
+def TestInstanceAddWithPlainDisk(nodes, fail=False):