From 288d6440828eb65f23e3e95dfd8915d7392ba8a2 Mon Sep 17 00:00:00 2001 From: Michael Hanselmann <hansmi@google.com> Date: Mon, 6 Dec 2010 19:54:16 +0100 Subject: [PATCH] QA: Improve tests for instance/node list - 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 | 10 +++++++ qa/qa-sample.json | 1 + qa/qa_instance.py | 3 ++- qa/qa_node.py | 6 +++++ qa/qa_utils.py | 69 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 88 insertions(+), 1 deletion(-) diff --git a/qa/ganeti-qa.py b/qa/ganeti-qa.py index 027c06590..41dc3f9b6 100755 --- a/qa/ganeti-qa.py +++ b/qa/ganeti-qa.py @@ -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. diff --git a/qa/qa-sample.json b/qa/qa-sample.json index fa8cc8f30..8c42c3081 100644 --- a/qa/qa-sample.json +++ b/qa/qa-sample.json @@ -65,6 +65,7 @@ "group-list": true, "group-rwops": true, + "node-list": true, "node-info": true, "node-volumes": true, "node-readd": true, diff --git a/qa/qa_instance.py b/qa/qa_instance.py index 4a3d6bd05..0b27e115f 100644 --- a/qa/qa_instance.py +++ b/qa/qa_instance.py @@ -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): diff --git a/qa/qa_node.py b/qa/qa_node.py index 1c6a0cc9a..ed7c95082 100644 --- a/qa/qa_node.py +++ b/qa/qa_node.py @@ -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()) diff --git a/qa/qa_utils.py b/qa/qa_utils.py index 0926f74e5..112e2cec8 100644 --- a/qa/qa_utils.py +++ b/qa/qa_utils.py @@ -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 -- GitLab