+ return instances
+
+
+def _SelectQueryFields(rnd, fields):
+ """Generates a list of fields for query tests.
+
+ """
+ # Create copy for shuffling
+ fields = list(fields)
+ rnd.shuffle(fields)
+
+ # Check all fields
+ yield fields
+ yield sorted(fields)
+
+ # Duplicate fields
+ yield fields + fields
+
+ # Check small groups of fields
+ while fields:
+ yield [fields.pop() for _ in range(rnd.randint(2, 10)) if fields]
+
+
+def _List(listcmd, fields, names):
+ """Runs a list command.
+
+ """
+ master = qa_config.GetMasterNode()
+
+ cmd = [listcmd, "list", "--separator=|", "--no-header",
+ "--output", ",".join(fields)]
+
+ if names:
+ cmd.extend(names)
+
+ return GetCommandOutput(master["primary"],
+ utils.ShellQuoteArgs(cmd)).splitlines()
+
+
+def GenericQueryTest(cmd, fields):
+ """Runs a number of tests on query commands.
+
+ @param cmd: Command name
+ @param fields: List of field names
+
+ """
+ rnd = random.Random(hash(cmd))
+
+ fields = list(fields)
+ rnd.shuffle(fields)
+
+ # Test a number of field combinations
+ for testfields in _SelectQueryFields(rnd, fields):
+ AssertCommand([cmd, "list", "--output", ",".join(testfields)])
+
+ namelist_fn = compat.partial(_List, cmd, ["name"])
+
+ # When no names were requested, the list must be sorted
+ names = namelist_fn(None)
+ AssertEqual(names, utils.NiceSort(names))
+
+ # When requesting specific names, the order must be kept
+ revnames = list(reversed(names))
+ AssertEqual(namelist_fn(revnames), revnames)
+
+ randnames = list(names)
+ rnd.shuffle(randnames)
+ AssertEqual(namelist_fn(randnames), randnames)
+
+ # Listing unknown items must fail
+ AssertCommand([cmd, "list", "this.name.certainly.does.not.exist"], fail=True)
+
+ # Check exit code for listing unknown field
+ AssertEqual(AssertCommand([cmd, "list", "--output=field/does/not/exist"],
+ fail=True),
+ constants.EXIT_UNKNOWN_FIELD)
+
+
+def GenericQueryFieldsTest(cmd, fields):
+ master = qa_config.GetMasterNode()
+
+ # Listing fields
+ AssertCommand([cmd, "list-fields"])
+ AssertCommand([cmd, "list-fields"] + fields)
+
+ # Check listed fields (all, must be sorted)
+ realcmd = [cmd, "list-fields", "--separator=|", "--no-headers"]
+ output = GetCommandOutput(master["primary"],
+ utils.ShellQuoteArgs(realcmd)).splitlines()
+ AssertEqual([line.split("|", 1)[0] for line in output],
+ utils.NiceSort(fields))
+
+ # Check exit code for listing unknown field
+ AssertEqual(AssertCommand([cmd, "list-fields", "field/does/not/exist"],
+ fail=True),
+ constants.EXIT_UNKNOWN_FIELD)
+
+
+def _FormatWithColor(text, seq):
+ if not seq:
+ return text
+ return "%s%s%s" % (seq, text, _RESET_SEQ)
+
+
+FormatWarning = lambda text: _FormatWithColor(text, _WARNING_SEQ)
+FormatError = lambda text: _FormatWithColor(text, _ERROR_SEQ)
+FormatInfo = lambda text: _FormatWithColor(text, _INFO_SEQ)
+
+
+def AddToEtcHosts(hostnames):
+ """Adds hostnames to /etc/hosts.
+
+ @param hostnames: List of hostnames first used A records, all other CNAMEs
+
+ """
+ master = qa_config.GetMasterNode()
+ tmp_hosts = UploadData(master["primary"], "", mode=0644)
+
+ quoted_tmp_hosts = utils.ShellQuote(tmp_hosts)
+ data = []
+ for localhost in ("::1", "127.0.0.1"):
+ data.append("%s %s" % (localhost, " ".join(hostnames)))
+
+ try:
+ AssertCommand(("cat /etc/hosts > %s && echo -e '%s' >> %s && mv %s"
+ " /etc/hosts") % (quoted_tmp_hosts, "\\n".join(data),
+ quoted_tmp_hosts, quoted_tmp_hosts))
+ except qa_error.Error:
+ AssertCommand(["rm", tmp_hosts])
+
+
+def RemoveFromEtcHosts(hostnames):
+ """Remove hostnames from /etc/hosts.
+
+ @param hostnames: List of hostnames first used A records, all other CNAMEs
+
+ """
+ master = qa_config.GetMasterNode()
+ tmp_hosts = UploadData(master["primary"], "", mode=0644)
+ quoted_tmp_hosts = utils.ShellQuote(tmp_hosts)
+
+ sed_data = " ".join(hostnames)
+ try:
+ AssertCommand(("sed -e '/^\(::1\|127\.0\.0\.1\)\s\+%s/d' /etc/hosts > %s"
+ " && mv %s /etc/hosts") % (sed_data, quoted_tmp_hosts,
+ quoted_tmp_hosts))
+ except qa_error.Error:
+ AssertCommand(["rm", tmp_hosts])