Fix networks in _PrepareNicModifications()
[ganeti-local] / qa / qa_rapi.py
index fbcf980..4ffa916 100644 (file)
@@ -1,6 +1,7 @@
 #
+#
 
-# Copyright (C) 2007, 2008, 2009, 2010, 2011 Google Inc.
+# Copyright (C) 2007, 2008, 2009, 2010, 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
@@ -24,6 +25,8 @@
 
 import tempfile
 import random
+import re
+import itertools
 
 from ganeti import utils
 from ganeti import constants
@@ -34,15 +37,20 @@ from ganeti import objects
 from ganeti import query
 from ganeti import compat
 from ganeti import qlang
+from ganeti import pathutils
 
-import ganeti.rapi.client        # pylint: disable-msg=W0611
+import ganeti.rapi.client        # pylint: disable=W0611
 import ganeti.rapi.client_utils
 
 import qa_config
 import qa_utils
 import qa_error
 
+from qa_instance import IsFailoverSupported
+from qa_instance import IsMigrationSupported
+from qa_instance import IsDiskReplacingSupported
 from qa_utils import (AssertEqual, AssertIn, AssertMatch, StartLocalCommand)
+from qa_utils import InstanceCheck, INST_DOWN, INST_UP, FIRST_ARG
 
 
 _rapi_ca = None
@@ -55,7 +63,7 @@ def Setup(username, password):
   """Configures the RAPI client.
 
   """
-  # pylint: disable-msg=W0603
+  # pylint: disable=W0603
   # due to global usage
   global _rapi_ca
   global _rapi_client
@@ -68,7 +76,7 @@ def Setup(username, password):
   master = qa_config.GetMasterNode()
 
   # Load RAPI certificate from master node
-  cmd = ["cat", constants.RAPI_CERT_FILE]
+  cmd = ["cat", pathutils.RAPI_CERT_FILE]
 
   # Write to temporary file
   _rapi_ca = tempfile.NamedTemporaryFile()
@@ -99,13 +107,13 @@ NODE_FIELDS = ("name", "dtotal", "dfree",
                "mtotal", "mnode", "mfree",
                "pinst_cnt", "sinst_cnt", "tags")
 
-GROUP_FIELDS = frozenset([
+GROUP_FIELDS = compat.UniqueFrozenset([
   "name", "uuid",
   "alloc_policy",
   "node_cnt", "node_list",
   ])
 
-JOB_FIELDS = frozenset([
+JOB_FIELDS = compat.UniqueFrozenset([
   "id", "ops", "status", "summary",
   "opstatus", "opresult", "oplog",
   "received_ts", "start_ts", "end_ts",
@@ -118,11 +126,11 @@ def Enabled():
   """Return whether remote API tests should be run.
 
   """
-  return qa_config.TestEnabled('rapi')
+  return qa_config.TestEnabled("rapi")
 
 
 def _DoTests(uris):
-  # pylint: disable-msg=W0212
+  # pylint: disable=W0212
   # due to _SendRequest usage
   results = []
 
@@ -144,7 +152,8 @@ def _DoTests(uris):
 
 
 def _VerifyReturnsJob(data):
-  AssertMatch(data, r'^\d+$')
+  if not isinstance(data, int):
+    AssertMatch(data, r"^\d+$")
 
 
 def TestVersion():
@@ -152,7 +161,7 @@ def TestVersion():
 
   """
   _DoTests([
-    ("/version", constants.RAPI_VERSION, 'GET', None),
+    ("/version", constants.RAPI_VERSION, "GET", None),
     ])
 
 
@@ -193,16 +202,16 @@ def TestEmptyCluster():
         AssertIn(field, group)
 
   _DoTests([
-    ("/", None, 'GET', None),
-    ("/2/info", _VerifyInfo, 'GET', None),
-    ("/2/tags", None, 'GET', None),
-    ("/2/nodes", _VerifyNodes, 'GET', None),
-    ("/2/nodes?bulk=1", _VerifyNodesBulk, 'GET', None),
-    ("/2/groups", _VerifyGroups, 'GET', None),
-    ("/2/groups?bulk=1", _VerifyGroupsBulk, 'GET', None),
-    ("/2/instances", [], 'GET', None),
-    ("/2/instances?bulk=1", [], 'GET', None),
-    ("/2/os", None, 'GET', None),
+    ("/", None, "GET", None),
+    ("/2/info", _VerifyInfo, "GET", None),
+    ("/2/tags", None, "GET", None),
+    ("/2/nodes", _VerifyNodes, "GET", None),
+    ("/2/nodes?bulk=1", _VerifyNodesBulk, "GET", None),
+    ("/2/groups", _VerifyGroups, "GET", None),
+    ("/2/groups?bulk=1", _VerifyGroupsBulk, "GET", None),
+    ("/2/instances", [], "GET", None),
+    ("/2/instances?bulk=1", [], "GET", None),
+    ("/2/os", None, "GET", None),
     ])
 
   # Test HTTP Not Found
@@ -232,6 +241,13 @@ def TestRapiQuery():
   rnd = random.Random(7818)
 
   for what in constants.QR_VIA_RAPI:
+    if what == constants.QR_JOB:
+      namefield = "id"
+    elif what == constants.QR_EXPORT:
+      namefield = "export"
+    else:
+      namefield = "name"
+
     all_fields = query.ALL_FIELDS[what].keys()
     rnd.shuffle(all_fields)
 
@@ -241,7 +257,7 @@ def TestRapiQuery():
     AssertEqual(len(qresult.fields), len(all_fields))
 
     # One field
-    result = _rapi_client.QueryFields(what, fields=["name"])
+    result = _rapi_client.QueryFields(what, fields=[namefield])
     qresult = objects.QueryFieldsResponse.FromDict(result)
     AssertEqual(len(qresult.fields), 1)
 
@@ -286,24 +302,25 @@ def TestRapiQuery():
       ("/2/query/%s?fields=%s" % (what, ",".join(all_fields)),
        compat.partial(_Check, all_fields), "GET", None),
 
-      ("/2/query/%s?fields=name" % what,
-       compat.partial(_Check, ["name"]), "GET", None),
+      ("/2/query/%s?fields=%s" % (what, namefield),
+       compat.partial(_Check, [namefield]), "GET", None),
 
       # Note the spaces
-      ("/2/query/%s?fields=name,%%20name%%09,name%%20" % what,
-       compat.partial(_Check, ["name"] * 3), "GET", None),
+      ("/2/query/%s?fields=%s,%%20%s%%09,%s%%20" %
+       (what, namefield, namefield, namefield),
+       compat.partial(_Check, [namefield] * 3), "GET", None),
 
       # PUT with fields in query
-      ("/2/query/%s?fields=name" % what,
-       compat.partial(_Check, ["name"]), "PUT", {}),
+      ("/2/query/%s?fields=%s" % (what, namefield),
+       compat.partial(_Check, [namefield]), "PUT", {}),
 
       # Fields in body
       ("/2/query/%s" % what, compat.partial(_Check, all_fields), "PUT", {
          "fields": all_fields,
          }),
 
-      ("/2/query/%s" % what, compat.partial(_Check, ["name"] * 4), "PUT", {
-         "fields": ["name"] * 4,
+      ("/2/query/%s" % what, compat.partial(_Check, [namefield] * 4), "PUT", {
+         "fields": [namefield] * 4,
          }),
       ])
 
@@ -312,7 +329,7 @@ def TestRapiQuery():
         # With filter
         ("/2/query/%s" % what, compat.partial(_Check, all_fields), "PUT", {
            "fields": all_fields,
-           "filter": [qlang.OP_TRUE, "name"],
+           "filter": [qlang.OP_TRUE, namefield],
            }),
         ])
 
@@ -329,17 +346,19 @@ def TestRapiQuery():
 
     if what == constants.QR_NODE:
       # Test with filter
-      (nodes, ) = _DoTests([("/2/query/%s" % what,
-        compat.partial(_Check, ["name", "master"]), "PUT", {
-        "fields": ["name", "master"],
-        "filter": [qlang.OP_TRUE, "master"],
-        })])
+      (nodes, ) = _DoTests(
+        [("/2/query/%s" % what,
+          compat.partial(_Check, ["name", "master"]), "PUT",
+          {"fields": ["name", "master"],
+           "filter": [qlang.OP_TRUE, "master"],
+           })])
       qresult = objects.QueryResponse.FromDict(nodes)
       AssertEqual(qresult.data, [
         [[constants.RS_NORMAL, master_name], [constants.RS_NORMAL, True]],
         ])
 
 
+@InstanceCheck(INST_UP, INST_UP, FIRST_ARG)
 def TestInstance(instance):
   """Testing getting instance(s) info via remote API.
 
@@ -358,13 +377,13 @@ def TestInstance(instance):
       _VerifyInstance(instance_data)
 
   _DoTests([
-    ("/2/instances/%s" % instance["name"], _VerifyInstance, 'GET', None),
-    ("/2/instances", _VerifyInstancesList, 'GET', None),
-    ("/2/instances?bulk=1", _VerifyInstancesBulk, 'GET', None),
+    ("/2/instances/%s" % instance["name"], _VerifyInstance, "GET", None),
+    ("/2/instances", _VerifyInstancesList, "GET", None),
+    ("/2/instances?bulk=1", _VerifyInstancesBulk, "GET", None),
     ("/2/instances/%s/activate-disks" % instance["name"],
-     _VerifyReturnsJob, 'PUT', None),
+     _VerifyReturnsJob, "PUT", None),
     ("/2/instances/%s/deactivate-disks" % instance["name"],
-     _VerifyReturnsJob, 'PUT', None),
+     _VerifyReturnsJob, "PUT", None),
     ])
 
   # Test OpBackupPrepare
@@ -399,12 +418,24 @@ def TestNode(node):
       _VerifyNode(node_data)
 
   _DoTests([
-    ("/2/nodes/%s" % node["primary"], _VerifyNode, 'GET', None),
-    ("/2/nodes", _VerifyNodesList, 'GET', None),
-    ("/2/nodes?bulk=1", _VerifyNodesBulk, 'GET', None),
+    ("/2/nodes/%s" % node["primary"], _VerifyNode, "GET", None),
+    ("/2/nodes", _VerifyNodesList, "GET", None),
+    ("/2/nodes?bulk=1", _VerifyNodesBulk, "GET", None),
     ])
 
 
+def _FilterTags(seq):
+  """Removes unwanted tags from a sequence.
+
+  """
+  ignore_re = qa_config.get("ignore-tags-re", None)
+
+  if ignore_re:
+    return itertools.ifilterfalse(re.compile(ignore_re).match, seq)
+  else:
+    return seq
+
+
 def TestTags(kind, name, tags):
   """Tests .../tags resources.
 
@@ -415,11 +446,13 @@ def TestTags(kind, name, tags):
     uri = "/2/nodes/%s/tags" % name
   elif kind == constants.TAG_INSTANCE:
     uri = "/2/instances/%s/tags" % name
+  elif kind == constants.TAG_NODEGROUP:
+    uri = "/2/groups/%s/tags" % name
   else:
     raise errors.ProgrammerError("Unknown tag kind")
 
   def _VerifyTags(data):
-    AssertEqual(sorted(tags), sorted(data))
+    AssertEqual(sorted(tags), sorted(_FilterTags(data)))
 
   queryargs = "&".join("tag=%s" % i for i in tags)
 
@@ -431,7 +464,7 @@ def TestTags(kind, name, tags):
 
   # Retrieve tags
   _DoTests([
-    (uri, _VerifyTags, 'GET', None),
+    (uri, _VerifyTags, "GET", None),
     ])
 
   # Remove tags
@@ -462,9 +495,7 @@ def TestRapiNodeGroups():
   """Test several node group operations using RAPI.
 
   """
-  groups = qa_config.get("groups", {})
-  group1, group2, group3 = groups.get("inexistent-groups",
-                                      ["group1", "group2", "group3"])[:3]
+  (group1, group2, group3) = qa_utils.GetNonexistentGroups(3)
 
   # Create a group with no attributes
   body = {
@@ -523,18 +554,22 @@ def TestRapiNodeGroups():
 def TestRapiInstanceAdd(node, use_client):
   """Test adding a new instance via RAPI"""
   instance = qa_config.AcquireInstance()
+  qa_config.SetInstanceTemplate(instance, constants.DT_PLAIN)
   try:
-    memory = utils.ParseUnit(qa_config.get("mem"))
     disk_sizes = [utils.ParseUnit(size) for size in qa_config.get("disk")]
+    disks = [{"size": size} for size in disk_sizes]
+    nic0_mac = qa_config.GetInstanceNicMac(instance,
+                                           default=constants.VALUE_GENERATE)
+    nics = [{
+      constants.INIC_MAC: nic0_mac,
+      }]
+
+    beparams = {
+      constants.BE_MAXMEM: utils.ParseUnit(qa_config.get(constants.BE_MAXMEM)),
+      constants.BE_MINMEM: utils.ParseUnit(qa_config.get(constants.BE_MINMEM)),
+      }
 
     if use_client:
-      disks = [{"size": size} for size in disk_sizes]
-      nics = [{}]
-
-      beparams = {
-        constants.BE_MEMORY: memory,
-        }
-
       job_id = _rapi_client.CreateInstance(constants.INSTANCE_CREATE,
                                            instance["name"],
                                            constants.DT_PLAIN,
@@ -544,12 +579,15 @@ def TestRapiInstanceAdd(node, use_client):
                                            beparams=beparams)
     else:
       body = {
+        "__version__": 1,
+        "mode": constants.INSTANCE_CREATE,
         "name": instance["name"],
-        "os": qa_config.get("os"),
+        "os_type": qa_config.get("os"),
         "disk_template": constants.DT_PLAIN,
         "pnode": node["primary"],
-        "memory": memory,
-        "disks": disk_sizes,
+        "beparams": beparams,
+        "disks": disks,
+        "nics": nics,
         }
 
       (job_id, ) = _DoTests([
@@ -564,6 +602,7 @@ def TestRapiInstanceAdd(node, use_client):
     raise
 
 
+@InstanceCheck(None, INST_DOWN, FIRST_ARG)
 def TestRapiInstanceRemove(instance, use_client):
   """Test removing instance via RAPI"""
   if use_client:
@@ -578,33 +617,95 @@ def TestRapiInstanceRemove(instance, use_client):
   qa_config.ReleaseInstance(instance)
 
 
+@InstanceCheck(INST_UP, INST_UP, FIRST_ARG)
 def TestRapiInstanceMigrate(instance):
   """Test migrating instance via RAPI"""
+  if not IsMigrationSupported(instance):
+    print qa_utils.FormatInfo("Instance doesn't support migration, skipping"
+                              " test")
+    return
   # Move to secondary node
   _WaitForRapiJob(_rapi_client.MigrateInstance(instance["name"]))
+  qa_utils.RunInstanceCheck(instance, True)
   # And back to previous primary
   _WaitForRapiJob(_rapi_client.MigrateInstance(instance["name"]))
 
 
-def TestRapiInstanceRename(rename_source, rename_target):
-  """Test renaming instance via RAPI"""
+@InstanceCheck(INST_UP, INST_UP, FIRST_ARG)
+def TestRapiInstanceFailover(instance):
+  """Test failing over instance via RAPI"""
+  if not IsFailoverSupported(instance):
+    print qa_utils.FormatInfo("Instance doesn't support failover, skipping"
+                              " test")
+    return
+  # Move to secondary node
+  _WaitForRapiJob(_rapi_client.FailoverInstance(instance["name"]))
+  qa_utils.RunInstanceCheck(instance, True)
+  # And back to previous primary
+  _WaitForRapiJob(_rapi_client.FailoverInstance(instance["name"]))
+
+
+@InstanceCheck(INST_UP, INST_DOWN, FIRST_ARG)
+def TestRapiInstanceShutdown(instance):
+  """Test stopping an instance via RAPI"""
+  _WaitForRapiJob(_rapi_client.ShutdownInstance(instance["name"]))
+
+
+@InstanceCheck(INST_DOWN, INST_UP, FIRST_ARG)
+def TestRapiInstanceStartup(instance):
+  """Test starting an instance via RAPI"""
+  _WaitForRapiJob(_rapi_client.StartupInstance(instance["name"]))
+
+
+@InstanceCheck(INST_DOWN, INST_DOWN, FIRST_ARG)
+def TestRapiInstanceRenameAndBack(rename_source, rename_target):
+  """Test renaming instance via RAPI
+
+  This must leave the instance with the original name (in the
+  non-failure case).
+
+  """
   _WaitForRapiJob(_rapi_client.RenameInstance(rename_source, rename_target))
+  qa_utils.RunInstanceCheck(rename_source, False)
+  qa_utils.RunInstanceCheck(rename_target, False)
+  _WaitForRapiJob(_rapi_client.RenameInstance(rename_target, rename_source))
+  qa_utils.RunInstanceCheck(rename_target, False)
 
 
+@InstanceCheck(INST_DOWN, INST_DOWN, FIRST_ARG)
 def TestRapiInstanceReinstall(instance):
   """Test reinstalling an instance via RAPI"""
   _WaitForRapiJob(_rapi_client.ReinstallInstance(instance["name"]))
+  # By default, the instance is started again
+  qa_utils.RunInstanceCheck(instance, True)
+
+  # Reinstall again without starting
+  _WaitForRapiJob(_rapi_client.ReinstallInstance(instance["name"],
+                                                 no_startup=True))
+
+
+@InstanceCheck(INST_UP, INST_UP, FIRST_ARG)
+def TestRapiInstanceReplaceDisks(instance):
+  """Test replacing instance disks via RAPI"""
+  if not IsDiskReplacingSupported(instance):
+    print qa_utils.FormatInfo("Instance doesn't support disk replacing,"
+                              " skipping test")
+    return
+  fn = _rapi_client.ReplaceInstanceDisks
+  _WaitForRapiJob(fn(instance["name"],
+                     mode=constants.REPLACE_DISK_AUTO, disks=[]))
+  _WaitForRapiJob(fn(instance["name"],
+                     mode=constants.REPLACE_DISK_SEC, disks="0"))
 
 
+@InstanceCheck(INST_UP, INST_UP, FIRST_ARG)
 def TestRapiInstanceModify(instance):
   """Test modifying instance via RAPI"""
+  default_hv = qa_config.GetDefaultHypervisor()
+
   def _ModifyInstance(**kwargs):
     _WaitForRapiJob(_rapi_client.ModifyInstance(instance["name"], **kwargs))
 
-  _ModifyInstance(hvparams={
-    constants.HV_KERNEL_ARGS: "single",
-    })
-
   _ModifyInstance(beparams={
     constants.BE_VCPUS: 3,
     })
@@ -613,11 +714,23 @@ def TestRapiInstanceModify(instance):
     constants.BE_VCPUS: constants.VALUE_DEFAULT,
     })
 
-  _ModifyInstance(hvparams={
-    constants.HV_KERNEL_ARGS: constants.VALUE_DEFAULT,
-    })
-
-
+  if default_hv == constants.HT_XEN_PVM:
+    _ModifyInstance(hvparams={
+      constants.HV_KERNEL_ARGS: "single",
+      })
+    _ModifyInstance(hvparams={
+      constants.HV_KERNEL_ARGS: constants.VALUE_DEFAULT,
+      })
+  elif default_hv == constants.HT_XEN_HVM:
+    _ModifyInstance(hvparams={
+      constants.HV_BOOT_ORDER: "acn",
+      })
+    _ModifyInstance(hvparams={
+      constants.HV_BOOT_ORDER: constants.VALUE_DEFAULT,
+      })
+
+
+@InstanceCheck(INST_UP, INST_UP, FIRST_ARG)
 def TestRapiInstanceConsole(instance):
   """Test getting instance console information via RAPI"""
   result = _rapi_client.GetInstanceConsole(instance["name"])
@@ -626,6 +739,7 @@ def TestRapiInstanceConsole(instance):
   AssertEqual(console.instance, qa_utils.ResolveInstanceName(instance["name"]))
 
 
+@InstanceCheck(INST_DOWN, INST_DOWN, FIRST_ARG)
 def TestRapiStoppedInstanceConsole(instance):
   """Test getting stopped instance's console information via RAPI"""
   try:
@@ -645,7 +759,7 @@ def GetOperatingSystems():
 
 
 def TestInterClusterInstanceMove(src_instance, dest_instance,
-                                 pnode, snode, tnode):
+                                 inodes, tnode):
   """Test tools/move-instance"""
   master = qa_config.GetMasterNode()
 
@@ -653,20 +767,27 @@ def TestInterClusterInstanceMove(src_instance, dest_instance,
   rapi_pw_file.write(_rapi_password)
   rapi_pw_file.flush()
 
+  qa_config.SetInstanceTemplate(dest_instance,
+                                qa_config.GetInstanceTemplate(src_instance))
+
   # TODO: Run some instance tests before moving back
 
-  if snode is None:
+  if len(inodes) > 1:
+    # No disk template currently requires more than 1 secondary node. If this
+    # changes, either this test must be skipped or the script must be updated.
+    assert len(inodes) == 2
+    snode = inodes[1]
+  else:
     # instance is not redundant, but we still need to pass a node
     # (which will be ignored)
-    fsec = tnode
-  else:
-    fsec = snode
+    snode = tnode
+  pnode = inodes[0]
   # note: pnode:snode are the *current* nodes, so we move it first to
   # tnode:pnode, then back to pnode:snode
   for si, di, pn, sn in [(src_instance["name"], dest_instance["name"],
                           tnode["primary"], pnode["primary"]),
                          (dest_instance["name"], src_instance["name"],
-                          pnode["primary"], fsec["primary"])]:
+                          pnode["primary"], snode["primary"])]:
     cmd = [
       "../tools/move-instance",
       "--verbose",
@@ -682,4 +803,7 @@ def TestInterClusterInstanceMove(src_instance, dest_instance,
       si,
       ]
 
+    qa_utils.RunInstanceCheck(di, False)
     AssertEqual(StartLocalCommand(cmd).wait(), 0)
+    qa_utils.RunInstanceCheck(si, False)
+    qa_utils.RunInstanceCheck(di, True)