From b4d2d2cb2cb87c03e2cd49e4a30b7b417235f48c Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Fri, 11 Jan 2013 15:37:05 +0100
Subject: [PATCH] QA: Factorize retrieving non-existent groups from config

Also add a check to make sure there are enough groups defined.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Guido Trotter <ultrotter@google.com>
---
 qa/qa_group.py | 15 +++++----------
 qa/qa_rapi.py  |  4 +---
 qa/qa_utils.py | 20 ++++++++++++++++++++
 3 files changed, 26 insertions(+), 13 deletions(-)

diff --git a/qa/qa_group.py b/qa/qa_group.py
index b72ff0297..2e872ba29 100644
--- a/qa/qa_group.py
+++ b/qa/qa_group.py
@@ -43,12 +43,9 @@ def GetDefaultGroup():
 
 def TestGroupAddRemoveRename():
   """gnt-group add/remove/rename"""
-  groups = qa_config.get("groups", {})
-
   existing_group_with_nodes = GetDefaultGroup()
 
-  group1, group2, group3 = groups.get("inexistent-groups",
-                                      ["group1", "group2", "group3"])[:3]
+  (group1, group2, group3) = qa_utils.GetNonexistentGroups(3)
 
   AssertCommand(["gnt-group", "add", group1])
   AssertCommand(["gnt-group", "add", group2])
@@ -71,8 +68,7 @@ def TestGroupAddRemoveRename():
 
 def TestGroupAddWithOptions():
   """gnt-group add with options"""
-  groups = qa_config.get("groups", {})
-  group1 = groups.get("inexistent-groups", ["group1"])[0]
+  (group1, ) = qa_utils.GetNonexistentGroups(1)
 
   AssertCommand(["gnt-group", "add", "--alloc-policy", "notvalid", group1],
                 fail=True)
@@ -85,8 +81,7 @@ def TestGroupAddWithOptions():
 
 def TestGroupModify():
   """gnt-group modify"""
-  groups = qa_config.get("groups", {})
-  group1 = groups.get("inexistent-groups", ["group1"])[0]
+  (group1, ) = qa_utils.GetNonexistentGroups(1)
 
   AssertCommand(["gnt-group", "add", group1])
 
@@ -130,8 +125,8 @@ def TestAssignNodesIncludingSplit(orig_group, node1, node2):
 
   """
   assert node1 != node2
-  groups = qa_config.get("groups", {})
-  other_group = groups.get("inexistent-groups", ["group1"])[0]
+
+  (other_group, ) = qa_utils.GetNonexistentGroups(1)
 
   master_node = qa_config.GetMasterNode()["primary"]
 
diff --git a/qa/qa_rapi.py b/qa/qa_rapi.py
index 0401107b7..cd9c00444 100644
--- a/qa/qa_rapi.py
+++ b/qa/qa_rapi.py
@@ -492,9 +492,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 = {
diff --git a/qa/qa_utils.py b/qa/qa_utils.py
index 930190ff6..45baea6b4 100644
--- a/qa/qa_utils.py
+++ b/qa/qa_utils.py
@@ -647,3 +647,23 @@ def InstanceCheck(before, after, instarg):
       return result
     return wrapper
   return decorator
+
+
+def GetNonexistentGroups(count):
+  """Gets group names which shouldn't exist on the cluster.
+
+  @param count: Number of groups to get
+  @rtype: list
+
+  """
+  groups = qa_config.get("groups", {})
+
+  default = ["group1", "group2", "group3"]
+  assert count <= len(default)
+
+  candidates = groups.get("inexistent-groups", default)[:count]
+
+  if len(candidates) < count:
+    raise Exception("At least %s non-existent groups are needed" % count)
+
+  return candidates
-- 
GitLab