Commit d8bcd0a8 authored by Iustin Pop's avatar Iustin Pop
Browse files

Add function for nodes to (nodgroup, nodes) split



Unittests included. The function will be needed for consistency checks
in the algorithms.
Signed-off-by: default avatarIustin Pop <iustin@google.com>
Reviewed-by: default avatarBalazs Lecz <leczb@google.com>
parent c4d98278
......@@ -66,11 +66,13 @@ module Ganeti.HTools.Node
, AssocList
, AllocElement
, noSecondary
, computeGroups
) where
import Data.List hiding (group)
import qualified Data.Map as Map
import qualified Data.Foldable as Foldable
import Data.Ord (comparing)
import Text.Printf (printf)
import qualified Ganeti.HTools.Container as Container
......@@ -538,3 +540,11 @@ defaultFields =
, "rmem", "tdsk", "fdsk", "tcpu", "ucpu", "pcnt", "scnt"
, "pfmem", "pfdsk", "rcpu"
, "cload", "mload", "dload", "nload" ]
-- | Split a list of nodes into a list of (node group UUID, list of
-- associated nodes)
computeGroups :: [Node] -> [(T.GroupID, [Node])]
computeGroups nodes =
let nodes' = sortBy (comparing group) nodes
nodes'' = groupBy (\a b -> group a == group b) nodes'
in map (\nl -> (group (head nl), nl)) nodes''
......@@ -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
]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment