Merge branch 'stable-2.8' into stable-2.9
[ganeti-local] / qa / qa_node.py
index 434de38..16fdb15 100644 (file)
@@ -1,7 +1,7 @@
 #
 #
 
-# Copyright (C) 2007, 2011 Google Inc.
+# Copyright (C) 2007, 2011, 2012, 2013 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
@@ -36,26 +36,35 @@ from qa_utils import AssertCommand, AssertEqual
 
 
 def _NodeAdd(node, readd=False):
-  if not readd and node.get("_added", False):
-    raise qa_error.Error("Node %s already in cluster" % node["primary"])
-  elif readd and not node.get("_added", False):
-    raise qa_error.Error("Node %s not yet in cluster" % node["primary"])
+  if not readd and node.added:
+    raise qa_error.Error("Node %s already in cluster" % node.primary)
+  elif readd and not node.added:
+    raise qa_error.Error("Node %s not yet in cluster" % node.primary)
 
   cmd = ["gnt-node", "add", "--no-ssh-key-check"]
-  if node.get("secondary", None):
-    cmd.append("--secondary-ip=%s" % node["secondary"])
+  if node.secondary:
+    cmd.append("--secondary-ip=%s" % node.secondary)
   if readd:
     cmd.append("--readd")
-  cmd.append(node["primary"])
+  cmd.append(node.primary)
 
   AssertCommand(cmd)
 
-  node["_added"] = True
+  if readd:
+    assert node.added
+  else:
+    node.MarkAdded()
 
 
 def _NodeRemove(node):
-  AssertCommand(["gnt-node", "remove", node["primary"]])
-  node["_added"] = False
+  AssertCommand(["gnt-node", "remove", node.primary])
+  node.MarkRemoved()
+
+
+def MakeNodeOffline(node, value):
+  """gnt-node modify --offline=value"""
+  # value in ["yes", "no"]
+  AssertCommand(["gnt-node", "modify", "--offline", value, node.primary])
 
 
 def TestNodeAddAll():
@@ -75,7 +84,7 @@ def MarkNodeAddedAll():
   master = qa_config.GetMasterNode()
   for node in qa_config.get("nodes"):
     if node != master:
-      node["_added"] = True
+      node.MarkAdded()
 
 
 def TestNodeRemoveAll():
@@ -105,9 +114,18 @@ def TestNodeStorage():
   """gnt-node storage"""
   master = qa_config.GetMasterNode()
 
-  for storage_type in constants.VALID_STORAGE_TYPES:
+  # FIXME: test all storage_types in constants.STORAGE_TYPES
+  # as soon as they are implemented.
+  enabled_storage_types = qa_config.GetEnabledStorageTypes()
+  testable_storage_types = list(set(enabled_storage_types).intersection(
+      set([constants.ST_FILE, constants.ST_LVM_VG, constants.ST_LVM_PV])))
+
+  for storage_type in testable_storage_types:
+
+    cmd = ["gnt-node", "list-storage", "--storage-type", storage_type]
+
     # Test simple list
-    AssertCommand(["gnt-node", "list-storage", "--storage-type", storage_type])
+    AssertCommand(cmd)
 
     # Test all storage fields
     cmd = ["gnt-node", "list-storage", "--storage-type", storage_type,
@@ -119,7 +137,7 @@ def TestNodeStorage():
     cmd = ["gnt-node", "list-storage", "--storage-type", storage_type,
            "--output=node,name,allocatable", "--separator=|",
            "--no-headers"]
-    output = qa_utils.GetCommandOutput(master["primary"],
+    output = qa_utils.GetCommandOutput(master.primary,
                                        utils.ShellQuoteArgs(cmd))
 
     # Test with up to two devices
@@ -149,7 +167,7 @@ def TestNodeStorage():
         cmd = ["gnt-node", "list-storage", "--storage-type", storage_type,
                "--output=name,allocatable", "--separator=|",
                "--no-headers", node_name]
-        listout = qa_utils.GetCommandOutput(master["primary"],
+        listout = qa_utils.GetCommandOutput(master.primary,
                                             utils.ShellQuoteArgs(cmd))
         for line in listout.splitlines():
           (vfy_name, vfy_allocatable) = line.split("|")
@@ -173,10 +191,24 @@ def TestNodeFailover(node, node2):
                                      " it to have no primary instances.")
 
   # Fail over to secondary node
-  AssertCommand(["gnt-node", "failover", "-f", node["primary"]])
+  AssertCommand(["gnt-node", "failover", "-f", node.primary])
+
+  # ... and back again.
+  AssertCommand(["gnt-node", "failover", "-f", node2.primary])
+
+
+def TestNodeMigrate(node, node2):
+  """gnt-node migrate"""
+  if qa_utils.GetNodeInstances(node2, secondaries=False):
+    raise qa_error.UnusableNodeError("Secondary node has at least one"
+                                     " primary instance. This test requires"
+                                     " it to have no primary instances.")
+
+  # Migrate to secondary node
+  AssertCommand(["gnt-node", "migrate", "-f", node.primary])
 
   # ... and back again.
-  AssertCommand(["gnt-node", "failover", "-f", node2["primary"]])
+  AssertCommand(["gnt-node", "migrate", "-f", node2.primary])
 
 
 def TestNodeEvacuate(node, node2):
@@ -190,33 +222,43 @@ def TestNodeEvacuate(node, node2):
 
     # Evacuate all secondary instances
     AssertCommand(["gnt-node", "evacuate", "-f",
-                   "--new-secondary=%s" % node3["primary"], node2["primary"]])
+                   "--new-secondary=%s" % node3.primary, node2.primary])
 
     # ... and back again.
     AssertCommand(["gnt-node", "evacuate", "-f",
-                   "--new-secondary=%s" % node2["primary"], node3["primary"]])
+                   "--new-secondary=%s" % node2.primary, node3.primary])
   finally:
-    qa_config.ReleaseNode(node3)
+    node3.Release()
 
 
 def TestNodeModify(node):
   """gnt-node modify"""
+
+  # make sure enough master candidates will be available by disabling the
+  # master candidate role first with --auto-promote
+  AssertCommand(["gnt-node", "modify", "--master-candidate=no",
+                "--auto-promote", node.primary])
+
+  # now it's save to force-remove the master candidate role
   for flag in ["master-candidate", "drained", "offline"]:
     for value in ["yes", "no"]:
       AssertCommand(["gnt-node", "modify", "--force",
-                     "--%s=%s" % (flag, value), node["primary"]])
+                     "--%s=%s" % (flag, value), node.primary])
 
-  AssertCommand(["gnt-node", "modify", "--master-candidate=yes",
-                 "--auto-promote", node["primary"]])
+  AssertCommand(["gnt-node", "modify", "--master-candidate=yes", node.primary])
+
+  # Test setting secondary IP address
+  AssertCommand(["gnt-node", "modify", "--secondary-ip=%s" % node.secondary,
+                 node.primary])
 
 
 def _CreateOobScriptStructure():
   """Create a simple OOB handling script and its structure."""
   master = qa_config.GetMasterNode()
 
-  data_path = qa_utils.UploadData(master["primary"], "")
-  verify_path = qa_utils.UploadData(master["primary"], "")
-  exit_code_path = qa_utils.UploadData(master["primary"], "")
+  data_path = qa_utils.UploadData(master.primary, "")
+  verify_path = qa_utils.UploadData(master.primary, "")
+  exit_code_path = qa_utils.UploadData(master.primary, "")
 
   oob_script = (("#!/bin/bash\n"
                  "echo \"$@\" > %s\n"
@@ -224,7 +266,7 @@ def _CreateOobScriptStructure():
                  "exit $(< %s)\n") %
                 (utils.ShellQuote(verify_path), utils.ShellQuote(data_path),
                  utils.ShellQuote(exit_code_path)))
-  oob_path = qa_utils.UploadData(master["primary"], oob_script, mode=0700)
+  oob_path = qa_utils.UploadData(master.primary, oob_script, mode=0700)
 
   return [oob_path, verify_path, data_path, exit_code_path]
 
@@ -232,7 +274,7 @@ def _CreateOobScriptStructure():
 def _UpdateOobFile(path, data):
   """Updates the data file with data."""
   master = qa_config.GetMasterNode()
-  qa_utils.UploadData(master["primary"], data, filename=path)
+  qa_utils.UploadData(master.primary, data, filename=path)
 
 
 def _AssertOobCall(verify_path, expected_args):
@@ -240,7 +282,7 @@ def _AssertOobCall(verify_path, expected_args):
   master = qa_config.GetMasterNode()
 
   verify_output_cmd = utils.ShellQuoteArgs(["cat", verify_path])
-  output = qa_utils.GetCommandOutput(master["primary"], verify_output_cmd,
+  output = qa_utils.GetCommandOutput(master.primary, verify_output_cmd,
                                      tty=False)
 
   AssertEqual(expected_args, output.strip())
@@ -252,8 +294,8 @@ def TestOutOfBand():
 
   node = qa_config.AcquireNode(exclude=master)
 
-  master_name = master["primary"]
-  node_name = node["primary"]
+  master_name = master.primary
+  node_name = node.primary
   full_node_name = qa_utils.ResolveNodeName(node)
 
   (oob_path, verify_path,
@@ -275,7 +317,7 @@ def TestOutOfBand():
     # Power off on master without options should fail
     AssertCommand(["gnt-node", "power", "-f", "off", master_name], fail=True)
     # With force master it should still fail
-    AssertCommand(["gnt-node", "power", "-f",  "--ignore-status", "off",
+    AssertCommand(["gnt-node", "power", "-f", "--ignore-status", "off",
                    master_name],
                   fail=True)
 
@@ -302,7 +344,7 @@ def TestOutOfBand():
     AssertCommand(["gnt-node", "health"], fail=True)
 
     # Correct Data, exit 0
-    _UpdateOobFile(data_path, serializer.DumpJson({ "powered": True }))
+    _UpdateOobFile(data_path, serializer.DumpJson({"powered": True}))
 
     AssertCommand(["gnt-node", "power", "status", node_name])
     _AssertOobCall(verify_path, "power-status %s" % full_node_name)
@@ -315,7 +357,6 @@ def TestOutOfBand():
 
     AssertCommand(["gnt-node", "health"])
 
-
     # Those commands should fail as they expect no data regardless of exit 0
     AssertCommand(["gnt-node", "power", "on", node_name], fail=True)
     _AssertOobCall(verify_path, "power-on %s" % full_node_name)
@@ -379,10 +420,10 @@ def TestOutOfBand():
     AssertCommand(["gnt-node", "health"], fail=True)
 
     # Different OOB script for node
-    verify_path2 = qa_utils.UploadData(master["primary"], "")
+    verify_path2 = qa_utils.UploadData(master.primary, "")
     oob_script = ("#!/bin/sh\n"
                   "echo \"$@\" > %s\n") % verify_path2
-    oob_path2 = qa_utils.UploadData(master["primary"], oob_script, mode=0700)
+    oob_path2 = qa_utils.UploadData(master.primary, oob_script, mode=0700)
 
     try:
       AssertCommand(["gnt-node", "modify", "--node-parameters",
@@ -408,3 +449,27 @@ def TestNodeList():
 def TestNodeListFields():
   """gnt-node list-fields"""
   qa_utils.GenericQueryFieldsTest("gnt-node", query.NODE_FIELDS.keys())
+
+
+def TestNodeListDrbd(node):
+  """gnt-node list-drbd"""
+  AssertCommand(["gnt-node", "list-drbd", node.primary])
+
+
+def _BuildSetESCmd(action, value, node_name):
+  cmd = ["gnt-node"]
+  if action == "add":
+    cmd.extend(["add", "--readd"])
+  else:
+    cmd.append("modify")
+  cmd.extend(["--node-parameters", "exclusive_storage=%s" % value, node_name])
+  return cmd
+
+
+def TestExclStorSingleNode(node):
+  """gnt-node add/modify cannot change the exclusive_storage flag.
+
+  """
+  for action in ["add", "modify"]:
+    for value in (True, False, "default"):
+      AssertCommand(_BuildSetESCmd(action, value, node.primary), fail=True)