#
#
-# Copyright (C) 2007, 2011 Google Inc.
+# Copyright (C) 2007, 2011, 2012 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
from ganeti import compat
from ganeti import constants
from ganeti import ht
+from ganeti import pathutils
import qa_config
import qa_error
raise qa_error.Error("%r == %r" % (first, second))
-def AssertNotEqual(first, second):
- """Raises an error when values are equal.
-
- """
- if not first != second:
- raise qa_error.Error("%r != %r" % (first, second))
-
-
def AssertMatch(string, pattern):
"""Raises an error when string doesn't match regexp pattern.
@param node: if passed, it should be the node on which the command
should be executed, instead of the master node (can be either a
dict or a string)
+ @return: the return code of the command
+ @raise qa_error.Error: if the command fails when it shouldn't or vice versa
"""
if node is None:
@param tty: if we should use tty; if None, will be auto-detected
"""
- args = ["ssh", "-oEscapeChar=none", "-oBatchMode=yes", "-l", "root"]
+ args = ["ssh", "-oEscapeChar=none", "-oBatchMode=yes", "-lroot"]
if tty is None:
tty = sys.stdout.isatty()
return args
-def StartLocalCommand(cmd, **kwargs):
+def StartLocalCommand(cmd, _nolog_opts=False, **kwargs):
"""Starts a local command.
"""
- print "Command: %s" % utils.ShellQuoteArgs(cmd)
+ if _nolog_opts:
+ pcmd = [i for i in cmd if not i.startswith("-")]
+ else:
+ pcmd = cmd
+ print "Command: %s" % utils.ShellQuoteArgs(pcmd)
return subprocess.Popen(cmd, shell=False, **kwargs)
"""Starts SSH.
"""
- return StartLocalCommand(GetSSHCommand(node, cmd, strict=strict))
+ return StartLocalCommand(GetSSHCommand(node, cmd, strict=strict),
+ _nolog_opts=True)
def StartMultiplexer(node):
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])
+ AssertCommand("{ cat %s && echo -e '%s'; } > %s && mv %s %s" %
+ (utils.ShellQuote(pathutils.ETC_HOSTS),
+ "\\n".join(data),
+ utils.ShellQuote(tmp_hosts),
+ utils.ShellQuote(tmp_hosts),
+ utils.ShellQuote(pathutils.ETC_HOSTS)))
+ except Exception:
+ AssertCommand(["rm", "-f", tmp_hosts])
+ raise
def RemoveFromEtcHosts(hostnames):
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])
+ AssertCommand(("sed -e '/^\(::1\|127\.0\.0\.1\)\s\+%s/d' %s > %s"
+ " && mv %s %s") %
+ (sed_data, utils.ShellQuote(pathutils.ETC_HOSTS),
+ quoted_tmp_hosts, quoted_tmp_hosts,
+ utils.ShellQuote(pathutils.ETC_HOSTS)))
+ except Exception:
+ AssertCommand(["rm", "-f", tmp_hosts])
+ raise
def RunInstanceCheck(instance, running):
return result
return wrapper
return decorator
+
+
+def GetNonexistentGroups(count):
+ """Gets group names which shouldn't exist on the cluster.
+
+ @param count: Number of groups to get
+ @rtype: list
+
+ """
+ groups = qa_config.get("groups", {})
+
+ default = ["group1", "group2", "group3"]
+ assert count <= len(default)
+
+ candidates = groups.get("inexistent-groups", default)[:count]
+
+ if len(candidates) < count:
+ raise Exception("At least %s non-existent groups are needed" % count)
+
+ return candidates