From 5f4fdf93236b6be25064ab1ed7f5d773861aaf94 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Mon, 3 Dec 2012 13:28:12 +0100
Subject: [PATCH] Fix generation of clusters via 'genEmptyCluster'

The current uniqueness algorithm (generate random node names, suffix
them with node index) is actually wrong: a node named "21" at index 5
will end up with the same name as a node named "2" at position 15.

To fix this, we also add a character from a different "set" ("-"), so
that such mixups can't happen again, and also add an explicit check
for it.

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>
---
 htest/Test/Ganeti/Objects.hs | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/htest/Test/Ganeti/Objects.hs b/htest/Test/Ganeti/Objects.hs
index 1aa151450..66b704299 100644
--- a/htest/Test/Ganeti/Objects.hs
+++ b/htest/Test/Ganeti/Objects.hs
@@ -169,11 +169,17 @@ genEmptyCluster ncount = do
   nodes <- vector ncount
   version <- arbitrary
   let guuid = "00"
-      nodes' = zipWith (\n idx -> n { nodeGroup = guuid,
-                                      nodeName = nodeName n ++ show idx })
+      nodes' = zipWith (\n idx ->
+                          let newname = nodeName n ++ "-" ++ show idx
+                          in (newname, n { nodeGroup = guuid,
+                                           nodeName = newname}))
                nodes [(1::Int)..]
-      contnodes = GenericContainer . Map.fromList $
-                  map (\n -> (nodeName n, n)) nodes'
+      nodemap = Map.fromList nodes'
+      contnodes = if Map.size nodemap /= ncount
+                    then error ("Inconsistent node map, duplicates in" ++
+                                " node name list? Names: " ++
+                                show (map fst nodes'))
+                    else GenericContainer nodemap
       continsts = GenericContainer Map.empty
   grp <- arbitrary
   let contgroups = GenericContainer $ Map.singleton guuid grp
-- 
GitLab