4 # Copyright (C) 2007, 2011 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 """Utilities for QA tests.
35 except ImportError, err:
36 raise ImportError("Python 2.5 or higher is required: %s" % err)
38 from ganeti import utils
39 from ganeti import compat
40 from ganeti import constants
54 #: Unique ID per QA run
55 _RUN_UUID = utils.NewUUID()
59 INST_UP) = range(500, 502)
62 RETURN_VALUE) = range(1000, 1002)
66 """Initializes the colour constants.
69 # pylint: disable=W0603
71 global _INFO_SEQ, _WARNING_SEQ, _ERROR_SEQ, _RESET_SEQ
73 # Don't use colours if stdout isn't a terminal
74 if not sys.stdout.isatty():
80 # Don't use colours if curses module can't be imported
85 _RESET_SEQ = curses.tigetstr("op")
87 setaf = curses.tigetstr("setaf")
88 _INFO_SEQ = curses.tparm(setaf, curses.COLOR_GREEN)
89 _WARNING_SEQ = curses.tparm(setaf, curses.COLOR_YELLOW)
90 _ERROR_SEQ = curses.tparm(setaf, curses.COLOR_RED)
96 def AssertIn(item, sequence):
97 """Raises an error when item is not in sequence.
100 if item not in sequence:
101 raise qa_error.Error("%r not in %r" % (item, sequence))
104 def AssertNotIn(item, sequence):
105 """Raises an error when item is in sequence.
109 raise qa_error.Error("%r in %r" % (item, sequence))
112 def AssertEqual(first, second):
113 """Raises an error when values aren't equal.
116 if not first == second:
117 raise qa_error.Error("%r == %r" % (first, second))
120 def AssertNotEqual(first, second):
121 """Raises an error when values are equal.
124 if not first != second:
125 raise qa_error.Error("%r != %r" % (first, second))
128 def AssertMatch(string, pattern):
129 """Raises an error when string doesn't match regexp pattern.
132 if not re.match(pattern, string):
133 raise qa_error.Error("%r doesn't match /%r/" % (string, pattern))
136 def _GetName(entity, key):
137 """Tries to get name of an entity.
139 @type entity: string or dict
141 @param key: Dictionary key containing name
144 if isinstance(entity, basestring):
146 elif isinstance(entity, dict):
149 raise qa_error.Error("Expected string or dictionary, got %s: %s" %
150 (type(entity), entity))
152 if not ht.TNonEmptyString(result):
153 raise Exception("Invalid name '%s'" % result)
158 def AssertCommand(cmd, fail=False, node=None):
159 """Checks that a remote command succeeds.
161 @param cmd: either a string (the command to execute) or a list (to
162 be converted using L{utils.ShellQuoteArgs} into a string)
164 @param fail: if the command is expected to fail instead of succeeding
165 @param node: if passed, it should be the node on which the command
166 should be executed, instead of the master node (can be either a
171 node = qa_config.GetMasterNode()
173 nodename = _GetName(node, "primary")
175 if isinstance(cmd, basestring):
178 cmdstr = utils.ShellQuoteArgs(cmd)
180 rcode = StartSSH(nodename, cmdstr).wait()
184 raise qa_error.Error("Command '%s' on node %s was expected to fail but"
185 " didn't" % (cmdstr, nodename))
188 raise qa_error.Error("Command '%s' on node %s failed, exit code %s" %
189 (cmdstr, nodename, rcode))
194 def GetSSHCommand(node, cmd, strict=True, opts=None, tty=True):
195 """Builds SSH command to be executed.
198 @param node: node the command should run on
200 @param cmd: command to be executed in the node; if None or empty
201 string, no command will be executed
202 @type strict: boolean
203 @param strict: whether to enable strict host key checking
205 @param opts: list of additional options
207 @param tty: If we should use tty
210 args = ["ssh", "-oEscapeChar=none", "-oBatchMode=yes", "-l", "root"]
219 args.append("-oStrictHostKeyChecking=%s" % tmp)
220 args.append("-oClearAllForwardings=yes")
221 args.append("-oForwardAgent=yes")
224 if node in _MULTIPLEXERS:
225 spath = _MULTIPLEXERS[node][0]
226 args.append("-oControlPath=%s" % spath)
227 args.append("-oControlMaster=no")
235 def StartLocalCommand(cmd, **kwargs):
236 """Starts a local command.
239 print "Command: %s" % utils.ShellQuoteArgs(cmd)
240 return subprocess.Popen(cmd, shell=False, **kwargs)
243 def StartSSH(node, cmd, strict=True):
247 return StartLocalCommand(GetSSHCommand(node, cmd, strict=strict))
250 def StartMultiplexer(node):
251 """Starts a multiplexer command.
253 @param node: the node for which to open the multiplexer
256 if node in _MULTIPLEXERS:
259 # Note: yes, we only need mktemp, since we'll remove the file anyway
260 sname = tempfile.mktemp(prefix="ganeti-qa-multiplexer.")
261 utils.RemoveFile(sname)
262 opts = ["-N", "-oControlPath=%s" % sname, "-oControlMaster=yes"]
263 print "Created socket at %s" % sname
264 child = StartLocalCommand(GetSSHCommand(node, None, opts=opts))
265 _MULTIPLEXERS[node] = (sname, child)
268 def CloseMultiplexers():
269 """Closes all current multiplexers and cleans up.
272 for node in _MULTIPLEXERS.keys():
273 (sname, child) = _MULTIPLEXERS.pop(node)
274 utils.KillProcess(child.pid, timeout=10, waitpid=True)
275 utils.RemoveFile(sname)
278 def GetCommandOutput(node, cmd, tty=True):
279 """Returns the output of a command executed on the given node.
282 p = StartLocalCommand(GetSSHCommand(node, cmd, tty=tty),
283 stdout=subprocess.PIPE)
284 AssertEqual(p.wait(), 0)
285 return p.stdout.read()
288 def UploadFile(node, src):
289 """Uploads a file to a node and returns the filename.
291 Caller needs to remove the returned file on the node when it's not needed
295 # Make sure nobody else has access to it while preserving local permissions
296 mode = os.stat(src).st_mode & 0700
298 cmd = ('tmp=$(tempfile --mode %o --prefix gnt) && '
299 '[[ -f "${tmp}" ]] && '
301 'echo "${tmp}"') % mode
305 p = subprocess.Popen(GetSSHCommand(node, cmd), shell=False, stdin=f,
306 stdout=subprocess.PIPE)
307 AssertEqual(p.wait(), 0)
309 # Return temporary filename
310 return p.stdout.read().strip()
315 def UploadData(node, data, mode=0600, filename=None):
316 """Uploads data to a node and returns the filename.
318 Caller needs to remove the returned file on the node when it's not needed
323 tmp = "tmp=%s" % utils.ShellQuote(filename)
325 tmp = "tmp=$(tempfile --mode %o --prefix gnt)" % mode
327 "[[ -f \"${tmp}\" ]] && "
328 "cat > \"${tmp}\" && "
329 "echo \"${tmp}\"") % tmp
331 p = subprocess.Popen(GetSSHCommand(node, cmd), shell=False,
332 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
335 AssertEqual(p.wait(), 0)
337 # Return temporary filename
338 return p.stdout.read().strip()
341 def BackupFile(node, path):
342 """Creates a backup of a file on the node and returns the filename.
344 Caller needs to remove the returned file on the node when it's not needed
348 cmd = ("tmp=$(tempfile --prefix .gnt --directory=$(dirname %s)) && "
349 "[[ -f \"$tmp\" ]] && "
351 "echo $tmp") % (utils.ShellQuote(path), utils.ShellQuote(path))
353 # Return temporary filename
354 return GetCommandOutput(node, cmd).strip()
357 def _ResolveName(cmd, key):
361 master = qa_config.GetMasterNode()
363 output = GetCommandOutput(master["primary"], utils.ShellQuoteArgs(cmd))
364 for line in output.splitlines():
365 (lkey, lvalue) = line.split(":", 1)
367 return lvalue.lstrip()
368 raise KeyError("Key not found")
371 def ResolveInstanceName(instance):
372 """Gets the full name of an instance.
374 @type instance: string
375 @param instance: Instance name
378 return _ResolveName(["gnt-instance", "info", instance],
382 def ResolveNodeName(node):
383 """Gets the full name of a node.
386 return _ResolveName(["gnt-node", "info", node["primary"]],
390 def GetNodeInstances(node, secondaries=False):
391 """Gets a list of instances on a node.
394 master = qa_config.GetMasterNode()
395 node_name = ResolveNodeName(node)
397 # Get list of all instances
398 cmd = ["gnt-instance", "list", "--separator=:", "--no-headers",
399 "--output=name,pnode,snodes"]
400 output = GetCommandOutput(master["primary"], utils.ShellQuoteArgs(cmd))
403 for line in output.splitlines():
404 (name, pnode, snodes) = line.split(":", 2)
405 if ((not secondaries and pnode == node_name) or
406 (secondaries and node_name in snodes.split(","))):
407 instances.append(name)
412 def _SelectQueryFields(rnd, fields):
413 """Generates a list of fields for query tests.
416 # Create copy for shuffling
417 fields = list(fields)
425 yield fields + fields
427 # Check small groups of fields
429 yield [fields.pop() for _ in range(rnd.randint(2, 10)) if fields]
432 def _List(listcmd, fields, names):
433 """Runs a list command.
436 master = qa_config.GetMasterNode()
438 cmd = [listcmd, "list", "--separator=|", "--no-headers",
439 "--output", ",".join(fields)]
444 return GetCommandOutput(master["primary"],
445 utils.ShellQuoteArgs(cmd)).splitlines()
448 def GenericQueryTest(cmd, fields, namefield="name", test_unknown=True):
449 """Runs a number of tests on query commands.
451 @param cmd: Command name
452 @param fields: List of field names
455 rnd = random.Random(hash(cmd))
457 fields = list(fields)
460 # Test a number of field combinations
461 for testfields in _SelectQueryFields(rnd, fields):
462 AssertCommand([cmd, "list", "--output", ",".join(testfields)])
464 if namefield is not None:
465 namelist_fn = compat.partial(_List, cmd, [namefield])
467 # When no names were requested, the list must be sorted
468 names = namelist_fn(None)
469 AssertEqual(names, utils.NiceSort(names))
471 # When requesting specific names, the order must be kept
472 revnames = list(reversed(names))
473 AssertEqual(namelist_fn(revnames), revnames)
475 randnames = list(names)
476 rnd.shuffle(randnames)
477 AssertEqual(namelist_fn(randnames), randnames)
480 # Listing unknown items must fail
481 AssertCommand([cmd, "list", "this.name.certainly.does.not.exist"],
484 # Check exit code for listing unknown field
485 AssertEqual(AssertCommand([cmd, "list", "--output=field/does/not/exist"],
487 constants.EXIT_UNKNOWN_FIELD)
490 def GenericQueryFieldsTest(cmd, fields):
491 master = qa_config.GetMasterNode()
494 AssertCommand([cmd, "list-fields"])
495 AssertCommand([cmd, "list-fields"] + fields)
497 # Check listed fields (all, must be sorted)
498 realcmd = [cmd, "list-fields", "--separator=|", "--no-headers"]
499 output = GetCommandOutput(master["primary"],
500 utils.ShellQuoteArgs(realcmd)).splitlines()
501 AssertEqual([line.split("|", 1)[0] for line in output],
502 utils.NiceSort(fields))
504 # Check exit code for listing unknown field
505 AssertEqual(AssertCommand([cmd, "list-fields", "field/does/not/exist"],
507 constants.EXIT_UNKNOWN_FIELD)
510 def _FormatWithColor(text, seq):
513 return "%s%s%s" % (seq, text, _RESET_SEQ)
516 FormatWarning = lambda text: _FormatWithColor(text, _WARNING_SEQ)
517 FormatError = lambda text: _FormatWithColor(text, _ERROR_SEQ)
518 FormatInfo = lambda text: _FormatWithColor(text, _INFO_SEQ)
521 def AddToEtcHosts(hostnames):
522 """Adds hostnames to /etc/hosts.
524 @param hostnames: List of hostnames first used A records, all other CNAMEs
527 master = qa_config.GetMasterNode()
528 tmp_hosts = UploadData(master["primary"], "", mode=0644)
530 quoted_tmp_hosts = utils.ShellQuote(tmp_hosts)
532 for localhost in ("::1", "127.0.0.1"):
533 data.append("%s %s" % (localhost, " ".join(hostnames)))
536 AssertCommand(("cat /etc/hosts > %s && echo -e '%s' >> %s && mv %s"
537 " /etc/hosts") % (quoted_tmp_hosts, "\\n".join(data),
538 quoted_tmp_hosts, quoted_tmp_hosts))
539 except qa_error.Error:
540 AssertCommand(["rm", tmp_hosts])
543 def RemoveFromEtcHosts(hostnames):
544 """Remove hostnames from /etc/hosts.
546 @param hostnames: List of hostnames first used A records, all other CNAMEs
549 master = qa_config.GetMasterNode()
550 tmp_hosts = UploadData(master["primary"], "", mode=0644)
551 quoted_tmp_hosts = utils.ShellQuote(tmp_hosts)
553 sed_data = " ".join(hostnames)
555 AssertCommand(("sed -e '/^\(::1\|127\.0\.0\.1\)\s\+%s/d' /etc/hosts > %s"
556 " && mv %s /etc/hosts") % (sed_data, quoted_tmp_hosts,
558 except qa_error.Error:
559 AssertCommand(["rm", tmp_hosts])
562 def RunInstanceCheck(instance, running):
563 """Check if instance is running or not.
566 instance_name = _GetName(instance, "name")
568 script = qa_config.GetInstanceCheckScript()
572 master_node = qa_config.GetMasterNode()
574 # Build command to connect to master node
575 master_ssh = GetSSHCommand(master_node["primary"], "--")
578 running_shellval = "1"
581 running_shellval = ""
582 running_text = "not "
584 print FormatInfo("Checking if instance '%s' is %srunning" %
585 (instance_name, running_text))
587 args = [script, instance_name]
589 "PATH": constants.HOOKS_PATH,
590 "RUN_UUID": _RUN_UUID,
591 "MASTER_SSH": utils.ShellQuoteArgs(master_ssh),
592 "INSTANCE_NAME": instance_name,
593 "INSTANCE_RUNNING": running_shellval,
596 result = os.spawnve(os.P_WAIT, script, args, env)
598 raise qa_error.Error("Instance check failed with result %s" % result)
601 def _InstanceCheckInner(expected, instarg, args, result):
602 """Helper function used by L{InstanceCheck}.
605 if instarg == FIRST_ARG:
607 elif instarg == RETURN_VALUE:
610 raise Exception("Invalid value '%s' for instance argument" % instarg)
612 if expected in (INST_DOWN, INST_UP):
613 RunInstanceCheck(instance, (expected == INST_UP))
614 elif expected is not None:
615 raise Exception("Invalid value '%s'" % expected)
618 def InstanceCheck(before, after, instarg):
619 """Decorator to check instance status before and after test.
621 @param before: L{INST_DOWN} if instance must be stopped before test,
622 L{INST_UP} if instance must be running before test, L{None} to not check.
623 @param after: L{INST_DOWN} if instance must be stopped after test,
624 L{INST_UP} if instance must be running after test, L{None} to not check.
625 @param instarg: L{FIRST_ARG} to use first argument to test as instance (a
626 dictionary), L{RETURN_VALUE} to use return value (disallows pre-checks)
631 def wrapper(*args, **kwargs):
632 _InstanceCheckInner(before, instarg, args, NotImplemented)
634 result = fn(*args, **kwargs)
636 _InstanceCheckInner(after, instarg, args, result)