#
#
-# Copyright (C) 2007 Google Inc.
+# Copyright (C) 2007, 2011 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
import sys
import subprocess
import random
+import tempfile
from ganeti import utils
from ganeti import compat
+from ganeti import constants
import qa_config
import qa_error
_ERROR_SEQ = None
_RESET_SEQ = None
+_MULTIPLEXERS = {}
+
def _SetupColours():
"""Initializes the colour constants.
"""
+ # pylint: disable=W0603
+ # due to global usage
global _INFO_SEQ, _WARNING_SEQ, _ERROR_SEQ, _RESET_SEQ
# Don't use colours if stdout isn't a terminal
"""
if item not in sequence:
- raise qa_error.Error('%r not in %r' % (item, sequence))
+ raise qa_error.Error("%r not in %r" % (item, sequence))
+
+
+def AssertNotIn(item, sequence):
+ """Raises an error when item is in sequence.
+
+ """
+ if item in sequence:
+ raise qa_error.Error("%r in %r" % (item, sequence))
def AssertEqual(first, second):
"""
if not first == second:
- raise qa_error.Error('%r == %r' % (first, second))
+ raise qa_error.Error("%r == %r" % (first, second))
def AssertNotEqual(first, second):
"""
if not first != second:
- raise qa_error.Error('%r != %r' % (first, second))
+ raise qa_error.Error("%r != %r" % (first, second))
def AssertMatch(string, pattern):
raise qa_error.Error("Command '%s' on node %s failed, exit code %s" %
(cmdstr, nodename, rcode))
+ return rcode
+
-def GetSSHCommand(node, cmd, strict=True):
+def GetSSHCommand(node, cmd, strict=True, opts=None, tty=True):
"""Builds SSH command to be executed.
- Args:
- - node: Node the command should run on
- - cmd: Command to be executed as a list with all parameters
- - strict: Whether to enable strict host key checking
+ @type node: string
+ @param node: node the command should run on
+ @type cmd: string
+ @param cmd: command to be executed in the node; if None or empty
+ string, no command will be executed
+ @type strict: boolean
+ @param strict: whether to enable strict host key checking
+ @type opts: list
+ @param opts: list of additional options
+ @type tty: Bool
+ @param tty: If we should use tty
"""
- args = [ 'ssh', '-oEscapeChar=none', '-oBatchMode=yes', '-l', 'root', '-t' ]
+ args = ["ssh", "-oEscapeChar=none", "-oBatchMode=yes", "-l", "root"]
+
+ if tty:
+ args.append("-t")
if strict:
- tmp = 'yes'
+ tmp = "yes"
else:
- tmp = 'no'
- args.append('-oStrictHostKeyChecking=%s' % tmp)
- args.append('-oClearAllForwardings=yes')
- args.append('-oForwardAgent=yes')
+ tmp = "no"
+ args.append("-oStrictHostKeyChecking=%s" % tmp)
+ args.append("-oClearAllForwardings=yes")
+ args.append("-oForwardAgent=yes")
+ if opts:
+ args.extend(opts)
+ if node in _MULTIPLEXERS:
+ spath = _MULTIPLEXERS[node][0]
+ args.append("-oControlPath=%s" % spath)
+ args.append("-oControlMaster=no")
args.append(node)
- args.append(cmd)
+ if cmd:
+ args.append(cmd)
return args
return StartLocalCommand(GetSSHCommand(node, cmd, strict=strict))
-def GetCommandOutput(node, cmd):
+def StartMultiplexer(node):
+ """Starts a multiplexer command.
+
+ @param node: the node for which to open the multiplexer
+
+ """
+ if node in _MULTIPLEXERS:
+ return
+
+ # Note: yes, we only need mktemp, since we'll remove the file anyway
+ sname = tempfile.mktemp(prefix="ganeti-qa-multiplexer.")
+ utils.RemoveFile(sname)
+ opts = ["-N", "-oControlPath=%s" % sname, "-oControlMaster=yes"]
+ print "Created socket at %s" % sname
+ child = StartLocalCommand(GetSSHCommand(node, None, opts=opts))
+ _MULTIPLEXERS[node] = (sname, child)
+
+
+def CloseMultiplexers():
+ """Closes all current multiplexers and cleans up.
+
+ """
+ for node in _MULTIPLEXERS.keys():
+ (sname, child) = _MULTIPLEXERS.pop(node)
+ utils.KillProcess(child.pid, timeout=10, waitpid=True)
+ utils.RemoveFile(sname)
+
+
+def GetCommandOutput(node, cmd, tty=True):
"""Returns the output of a command executed on the given node.
"""
- p = StartLocalCommand(GetSSHCommand(node, cmd), stdout=subprocess.PIPE)
+ p = StartLocalCommand(GetSSHCommand(node, cmd, tty=tty),
+ stdout=subprocess.PIPE)
AssertEqual(p.wait(), 0)
return p.stdout.read()
'cat > "${tmp}" && '
'echo "${tmp}"') % mode
- f = open(src, 'r')
+ f = open(src, "r")
try:
p = subprocess.Popen(GetSSHCommand(node, cmd), shell=False, stdin=f,
stdout=subprocess.PIPE)
f.close()
+def UploadData(node, data, mode=0600, filename=None):
+ """Uploads data to a node and returns the filename.
+
+ Caller needs to remove the returned file on the node when it's not needed
+ anymore.
+
+ """
+ if filename:
+ tmp = "tmp=%s" % utils.ShellQuote(filename)
+ else:
+ tmp = "tmp=$(tempfile --mode %o --prefix gnt)" % mode
+ cmd = ("%s && "
+ "[[ -f \"${tmp}\" ]] && "
+ "cat > \"${tmp}\" && "
+ "echo \"${tmp}\"") % tmp
+
+ p = subprocess.Popen(GetSSHCommand(node, cmd), shell=False,
+ stdin=subprocess.PIPE, stdout=subprocess.PIPE)
+ p.stdin.write(data)
+ p.stdin.close()
+ AssertEqual(p.wait(), 0)
+
+ # Return temporary filename
+ return p.stdout.read().strip()
+
+
def BackupFile(node, path):
"""Creates a backup of a file on the node and returns the filename.
"""
master = qa_config.GetMasterNode()
- output = GetCommandOutput(master['primary'], utils.ShellQuoteArgs(cmd))
+ output = GetCommandOutput(master["primary"], utils.ShellQuoteArgs(cmd))
for line in output.splitlines():
- (lkey, lvalue) = line.split(':', 1)
+ (lkey, lvalue) = line.split(":", 1)
if lkey == key:
return lvalue.lstrip()
raise KeyError("Key not found")
@param instance: Instance name
"""
- return _ResolveName(['gnt-instance', 'info', instance],
- 'Instance name')
+ return _ResolveName(["gnt-instance", "info", instance],
+ "Instance name")
def ResolveNodeName(node):
"""Gets the full name of a node.
"""
- return _ResolveName(['gnt-node', 'info', node['primary']],
- 'Node name')
+ return _ResolveName(["gnt-node", "info", node["primary"]],
+ "Node name")
def GetNodeInstances(node, secondaries=False):
node_name = ResolveNodeName(node)
# Get list of all instances
- cmd = ['gnt-instance', 'list', '--separator=:', '--no-headers',
- '--output=name,pnode,snodes']
- output = GetCommandOutput(master['primary'], utils.ShellQuoteArgs(cmd))
+ cmd = ["gnt-instance", "list", "--separator=:", "--no-headers",
+ "--output=name,pnode,snodes"]
+ output = GetCommandOutput(master["primary"], utils.ShellQuoteArgs(cmd))
instances = []
for line in output.splitlines():
- (name, pnode, snodes) = line.split(':', 2)
+ (name, pnode, snodes) = line.split(":", 2)
if ((not secondaries and pnode == node_name) or
- (secondaries and node_name in snodes.split(','))):
+ (secondaries and node_name in snodes.split(","))):
instances.append(name)
return instances
"""
rnd = random.Random(hash(cmd))
- randfields = list(fields)
+ fields = list(fields)
rnd.shuffle(fields)
# Test a number of field combinations
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:
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])