Add two functions for checking cluster consistency
[ganeti-local] / Ganeti / HTools / QC.hs
index 8b46aa5..818827d 100644 (file)
@@ -38,7 +38,7 @@ module Ganeti.HTools.QC
 
 import Test.QuickCheck
 import Test.QuickCheck.Batch
-import Data.List (findIndex, intercalate)
+import Data.List (findIndex, intercalate, nub)
 import Data.Maybe
 import Control.Monad
 import qualified Text.JSON as J
@@ -564,6 +564,15 @@ prop_Node_showField node =
   fst (Node.showHeader field) /= Types.unknownField &&
   Node.showField node field /= Types.unknownField
 
+
+prop_Node_computeGroups nodes =
+  let ng = Node.computeGroups nodes
+      onlyuuid = map fst ng
+  in length nodes == sum (map (length . snd) ng) &&
+     all (\(guuid, ns) -> all ((== guuid) . Node.group) ns) ng &&
+     length (nub onlyuuid) == length onlyuuid &&
+     if null nodes then True else not (null ng)
+
 testNode =
     [ run prop_Node_setAlias
     , run prop_Node_setOffline
@@ -577,6 +586,7 @@ testNode =
     , run prop_Node_tagMaps_idempotent
     , run prop_Node_tagMaps_reject
     , run prop_Node_showField
+    , run prop_Node_computeGroups
     ]
 
 
@@ -694,6 +704,22 @@ prop_ClusterAllocBalance node =
                        tbl = Cluster.Table ynl il' cv []
                    in canBalance tbl True False
 
+-- | Checks consistency
+prop_ClusterCheckConsistency node inst =
+  let nl = makeSmallCluster node 3
+      [node1, node2, node3] = Container.elems nl
+      node3' = node3 { Node.group = "other-uuid" }
+      nl' = Container.add (Node.idx node3') node3' nl
+      inst1 = Instance.setBoth inst (Node.idx node1) (Node.idx node2)
+      inst2 = Instance.setBoth inst (Node.idx node1) Node.noSecondary
+      inst3 = Instance.setBoth inst (Node.idx node1) (Node.idx node3)
+      ccheck = Cluster.findSplitInstances nl' . Container.fromAssocList
+  in null (ccheck [(0, inst1)]) &&
+     null (ccheck [(0, inst2)]) &&
+     (not . null $ ccheck [(0, inst3)])
+
+
+
 testCluster =
     [ run prop_Score_Zero
     , run prop_CStats_sane
@@ -701,6 +727,7 @@ testCluster =
     , run prop_ClusterCanTieredAlloc
     , run prop_ClusterAllocEvac
     , run prop_ClusterAllocBalance
+    , run prop_ClusterCheckConsistency
     ]
 
 -- | Check that opcode serialization is idempotent