Get rid of constants.RAPI_ENABLE
[ganeti-local] / qa / qa_utils.py
index 41b8b93..fbc92e8 100644 (file)
@@ -1,3 +1,6 @@
+#
+#
+
 # Copyright (C) 2007 Google Inc.
 #
 # This program is free software; you can redistribute it and/or modify
@@ -42,6 +45,10 @@ def _SetupColours():
   """
   global _INFO_SEQ, _WARNING_SEQ, _ERROR_SEQ, _RESET_SEQ
 
+  # Don't use colours if stdout isn't a terminal
+  if not sys.stdout.isatty():
+    return
+
   try:
     import curses
   except ImportError:
@@ -61,19 +68,40 @@ def _SetupColours():
 _SetupColours()
 
 
-def AssertEqual(first, second, msg=None):
+def AssertIn(item, sequence):
+  """Raises an error when item is not in sequence.
+
+  """
+  if item not in sequence:
+    raise qa_error.Error('%r not in %r' % (item, sequence))
+
+
+def AssertEqual(first, second):
   """Raises an error when values aren't equal.
 
   """
   if not first == second:
-    raise qa_error.Error(msg or '%r == %r' % (first, second))
+    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 GetSSHCommand(node, cmd, strict=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
+
   """
-  args = [ 'ssh', '-oEscapeChar=none', '-oBatchMode=yes', '-l', 'root' ]
+  args = [ 'ssh', '-oEscapeChar=none', '-oBatchMode=yes', '-l', 'root', '-t' ]
 
   if strict:
     tmp = 'yes'
@@ -83,13 +111,7 @@ def GetSSHCommand(node, cmd, strict=True):
   args.append('-oClearAllForwardings=yes')
   args.append('-oForwardAgent=yes')
   args.append(node)
-
-  if qa_config.options.dry_run:
-    prefix = 'exit 0; '
-  else:
-    prefix = ''
-
-  args.append(prefix + cmd)
+  args.append(cmd)
 
   print 'SSH:', utils.ShellQuoteArgs(args)
 
@@ -158,7 +180,7 @@ def ResolveInstanceName(instance):
   """Gets the full name of an instance.
 
   """
-  return _ResolveName(['gnt-instance', 'info', instance['info']],
+  return _ResolveName(['gnt-instance', 'info', instance['name']],
                       'Instance name')
 
 
@@ -175,7 +197,6 @@ def GetNodeInstances(node, secondaries=False):
 
   """
   master = qa_config.GetMasterNode()
-
   node_name = ResolveNodeName(node)
 
   # Get list of all instances
@@ -193,29 +214,12 @@ def GetNodeInstances(node, secondaries=False):
   return instances
 
 
-def _PrintWithColor(text, seq):
-  f = sys.stdout
-
-  if not f.isatty():
-    seq = None
-
-  if seq:
-    f.write(seq)
-
-  f.write(text)
-  f.write("\n")
-
-  if seq:
-    f.write(_RESET_SEQ)
-
-
-def PrintWarning(text):
-  return _PrintWithColor(text, _WARNING_SEQ)
-
-
-def PrintError(text):
-  return _PrintWithColor(text, _ERROR_SEQ)
+def _FormatWithColor(text, seq):
+  if not seq:
+    return text
+  return "%s%s%s" % (seq, text, _RESET_SEQ)
 
 
-def PrintInfo(text):
-  return _PrintWithColor(text, _INFO_SEQ)
+FormatWarning = lambda text: _FormatWithColor(text, _WARNING_SEQ)
+FormatError = lambda text: _FormatWithColor(text, _ERROR_SEQ)
+FormatInfo = lambda text: _FormatWithColor(text, _INFO_SEQ)