Add a few Loader tests
authorIustin Pop <iustin@google.com>
Tue, 1 Jun 2010 17:54:22 +0000 (19:54 +0200)
committerIustin Pop <iustin@google.com>
Tue, 1 Jun 2010 17:54:22 +0000 (19:54 +0200)
These are not comprehensive, but at least we have a start.

Ganeti/HTools/QC.hs
test.hs

index 15bb141..d57ae48 100644 (file)
@@ -31,10 +31,12 @@ module Ganeti.HTools.QC
     , testText
     , testOpCodes
     , testCluster
+    , testLoader
     ) where
 
 import Test.QuickCheck
 import Test.QuickCheck.Batch
+import Data.List (findIndex)
 import Data.Maybe
 import Control.Monad
 import qualified Text.JSON as J
@@ -662,3 +664,49 @@ prop_OpCodes_serialization op =
 testOpCodes =
   [ run prop_OpCodes_serialization
   ]
+
+-- | Loader tests
+
+prop_Loader_lookupNode ktn inst node =
+  isJust (Loader.lookupNode ktn inst node) == (node `elem` names)
+    where names = map fst ktn
+
+prop_Loader_lookupInstance kti inst =
+  isJust (Loader.lookupInstance kti inst) == (inst `elem` names)
+    where names = map fst kti
+
+prop_Loader_lookupInstanceIdx kti inst =
+  case (Loader.lookupInstance kti inst,
+        findIndex (\p -> fst p == inst) kti) of
+    (Nothing, Nothing) -> True
+    (Just idx, Just ex) -> idx == snd (kti !! ex)
+
+prop_Loader_assignIndices enames =
+  length nassoc == length enames &&
+  length kt == length enames &&
+  (if not (null enames)
+   then maximum (map fst kt) == (length enames) - 1
+   else True)
+  where (nassoc, kt) = Loader.assignIndices enames
+        _types = (enames::[(String, Node.Node)])
+
+
+-- | Checks that the number of primary instances recorded on the nodes
+-- is zero
+prop_Loader_mergeData ns =
+  let na = map (\n -> (Node.idx n, n)) ns
+  in case Loader.mergeData [] [] [] (na, [], []) of
+    Types.Bad _ -> False
+    Types.Ok (nl, il, _) ->
+      let nodes = Container.elems nl
+          instances = Container.elems il
+      in (sum . map (length . Node.pList)) nodes == 0 &&
+         length instances == 0
+
+testLoader =
+  [ run prop_Loader_lookupNode
+  , run prop_Loader_lookupInstance
+  , run prop_Loader_lookupInstanceIdx
+  , run prop_Loader_assignIndices
+  , run prop_Loader_mergeData
+  ]
diff --git a/test.hs b/test.hs
index 4a5aeb9..710b1f8 100644 (file)
--- a/test.hs
+++ b/test.hs
@@ -70,6 +70,7 @@ allTests =
   , ("Node", fast, testNode)
   , ("Text", fast, testText)
   , ("OpCodes", fast, testOpCodes)
+  , ("Loader", fast, testLoader)
   , ("Cluster", slow, testCluster)
   ]