QA: Improve tests for instance/node list
authorMichael Hanselmann <hansmi@google.com>
Mon, 6 Dec 2010 18:54:16 +0000 (19:54 +0100)
committerMichael Hanselmann <hansmi@google.com>
Fri, 10 Dec 2010 16:26:45 +0000 (17:26 +0100)
- Query all known fields
- Random combinations (using a PRNG with a fixed seed) of fields
- Order of result names

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>

qa/ganeti-qa.py
qa/qa-sample.json
qa/qa_instance.py
qa/qa_node.py
qa/qa_utils.py

index 027c065..41dc3f9 100755 (executable)
@@ -121,6 +121,11 @@ def SetupCluster(rapi_user, rapi_secret):
   """
   RunTestIf("create-cluster", qa_cluster.TestClusterInit,
             rapi_user, rapi_secret)
+
+  # Test on empty cluster
+  RunTestIf("node-list", qa_node.TestNodeList)
+  RunTestIf("instance-list", qa_instance.TestInstanceList)
+
   RunTestIf("create-cluster", qa_node.TestNodeAddAll)
   if not qa_config.TestEnabled("create-cluster"):
     # consider the nodes are already there
@@ -131,6 +136,8 @@ def SetupCluster(rapi_user, rapi_secret):
   # enable the watcher (unconditionally)
   RunTest(qa_daemon.TestResumeWatcher)
 
+  RunTestIf("node-list", qa_node.TestNodeList)
+
   RunTestIf("node-info", qa_node.TestNodeInfo)
 
 
@@ -217,6 +224,9 @@ def RunCommonInstanceTests(instance):
 
   RunTestIf("rapi", qa_rapi.TestInstance, instance)
 
+  # Lists instances, too
+  RunTestIf("node-list", qa_node.TestNodeList)
+
 
 def RunCommonNodeTests():
   """Run a few common node tests.
index fa8cc8f..8c42c30 100644 (file)
@@ -65,6 +65,7 @@
     "group-list": true,
     "group-rwops": true,
 
+    "node-list": true,
     "node-info": true,
     "node-volumes": true,
     "node-readd": true,
index 4a3d6bd..0b27e11 100644 (file)
@@ -28,6 +28,7 @@ import time
 
 from ganeti import utils
 from ganeti import constants
+from ganeti import query
 
 import qa_config
 import qa_utils
@@ -209,7 +210,7 @@ def TestInstanceConvertDisk(instance, snode):
 
 def TestInstanceList():
   """gnt-instance list"""
-  AssertCommand(["gnt-instance", "list"])
+  qa_utils.GenericQueryTest("gnt-instance", query.INSTANCE_FIELDS.keys())
 
 
 def TestInstanceConsole(instance):
index 1c6a0cc..ed7c950 100644 (file)
@@ -21,6 +21,7 @@
 
 from ganeti import utils
 from ganeti import constants
+from ganeti import query
 
 import qa_config
 import qa_error
@@ -189,3 +190,8 @@ def TestNodeModify(node):
 
   AssertCommand(["gnt-node", "modify", "--master-candidate=yes",
                  "--auto-promote", node["primary"]])
+
+
+def TestNodeList():
+  """gnt-node list"""
+  qa_utils.GenericQueryTest("gnt-node", query.NODE_FIELDS.keys())
index 0926f74..112e2ce 100644 (file)
@@ -27,8 +27,10 @@ import os
 import re
 import sys
 import subprocess
+import random
 
 from ganeti import utils
+from ganeti import compat
 
 import qa_config
 import qa_error
@@ -284,6 +286,73 @@ def GetNodeInstances(node, secondaries=False):
   return instances
 
 
+def _SelectQueryFields(rnd, fields):
+  """Generates a list of fields for query tests.
+
+  """
+  # Create copy for shuffling
+  fields = list(fields)
+  rnd.shuffle(fields)
+
+  # Check all fields
+  yield fields
+  yield sorted(fields)
+
+  # Duplicate fields
+  yield fields + fields
+
+  # Check small groups of fields
+  while fields:
+    yield [fields.pop() for _ in range(rnd.randint(2, 10)) if fields]
+
+
+def _List(listcmd, fields, names):
+  """Runs a list command.
+
+  """
+  master = qa_config.GetMasterNode()
+
+  cmd = [listcmd, "list", "--separator=|", "--no-header",
+         "--output", ",".join(fields)]
+
+  if names:
+    cmd.extend(names)
+
+  return GetCommandOutput(master["primary"],
+                          utils.ShellQuoteArgs(cmd)).splitlines()
+
+
+def GenericQueryTest(cmd, fields):
+  """Runs a number of tests on query commands.
+
+  @param cmd: Command name
+  @param fields: List of field names
+
+  """
+  rnd = random.Random(hash(cmd))
+
+  randfields = list(fields)
+  rnd.shuffle(fields)
+
+  # Test a number of field combinations
+  for testfields in _SelectQueryFields(rnd, fields):
+    AssertCommand([cmd, "list", "--output", ",".join(testfields)])
+
+  namelist_fn = compat.partial(_List, cmd, ["name"])
+
+  # When no names were requested, the list must be sorted
+  names = namelist_fn(None)
+  AssertEqual(names, utils.NiceSort(names))
+
+  # When requesting specific names, the order must be kept
+  revnames = list(reversed(names))
+  AssertEqual(namelist_fn(revnames), revnames)
+
+  randnames = list(names)
+  rnd.shuffle(randnames)
+  AssertEqual(namelist_fn(randnames), randnames)
+
+
 def _FormatWithColor(text, seq):
   if not seq:
     return text