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

Add a unittest for the 'Simu' backend



This only test successful parsing, but for this case it tests
reasonably well the resulting cluster properties, I think (coverage
is >70% for expressions).
Signed-off-by: default avatarIustin Pop <iustin@google.com>
Reviewed-by: default avatarRené Nussbaumer <rn@google.com>
parent 59ed268d
......@@ -32,6 +32,7 @@ module Ganeti.HTools.QC
, testInstance
, testNode
, testText
, testSimu
, testOpCodes
, testJobs
, testCluster
......@@ -40,7 +41,9 @@ module Ganeti.HTools.QC
) where
import Test.QuickCheck
import Text.Printf (printf)
import Data.List (findIndex, intercalate, nub, isPrefixOf)
import qualified Data.Set as Set
import Data.Maybe
import Control.Monad
import qualified Text.JSON as J
......@@ -62,7 +65,7 @@ import qualified Ganeti.HTools.Node as Node
import qualified Ganeti.HTools.Group as Group
import qualified Ganeti.HTools.PeerMap as PeerMap
import qualified Ganeti.HTools.Rapi
import qualified Ganeti.HTools.Simu
import qualified Ganeti.HTools.Simu as Simu
import qualified Ganeti.HTools.Text as Text
import qualified Ganeti.HTools.Types as Types
import qualified Ganeti.HTools.Utils as Utils
......@@ -705,7 +708,9 @@ testSuite "Instance"
, 'prop_Instance_setMovable
]
-- ** Text backend tests
-- ** Backends
-- *** Text backend tests
-- Instance text loader tests
......@@ -856,6 +861,57 @@ testSuite "Text"
, 'prop_Text_CreateSerialise
]
-- *** Simu backend
-- | Generates a tuple of specs for simulation.
genSimuSpec :: Gen (String, Int, Int, Int, Int)
genSimuSpec = do
pol <- elements [C.allocPolicyPreferred,
C.allocPolicyLastResort, C.allocPolicyUnallocable,
"p", "a", "u"]
-- should be reasonable (nodes/group), bigger values only complicate
-- the display of failed tests, and we don't care (in this particular
-- test) about big node groups
nodes <- choose (0, 20)
dsk <- choose (0, maxDsk)
mem <- choose (0, maxMem)
cpu <- choose (0, maxCpu)
return (pol, nodes, dsk, mem, cpu)
-- | Checks that given a set of corrects specs, we can load them
-- successfully, and that at high-level the values look right.
prop_SimuLoad =
forAll (choose (0, 10)) $ \ngroups ->
forAll (replicateM ngroups genSimuSpec) $ \specs ->
let strspecs = map (\(p, n, d, m, c) -> printf "%s,%d,%d,%d,%d"
p n d m c::String) specs
totnodes = sum $ map (\(_, n, _, _, _) -> n) specs
mdc_in = concatMap (\(_, n, d, m, c) ->
replicate n (fromIntegral m, fromIntegral d,
fromIntegral c,
fromIntegral m, fromIntegral d)) specs
in case Simu.parseData strspecs of
Types.Bad msg -> failTest $ "Failed to load specs: " ++ msg
Types.Ok (Loader.ClusterData gl nl il tags ipol) ->
let nodes = map snd $ IntMap.toAscList nl
nidx = map Node.idx nodes
mdc_out = map (\n -> (Node.tMem n, Node.tDsk n, Node.tCpu n,
Node.fMem n, Node.fDsk n)) nodes
in
Container.size gl ==? ngroups .&&.
Container.size nl ==? totnodes .&&.
Container.size il ==? 0 .&&.
length tags ==? 0 .&&.
ipol ==? Types.defIPolicy .&&.
nidx ==? [1..totnodes] .&&.
mdc_in ==? mdc_out .&&.
map Group.iPolicy (Container.elems gl) ==?
replicate ngroups Types.defIPolicy
testSuite "Simu"
[ 'prop_SimuLoad
]
-- ** Node tests
prop_Node_setAlias node name =
......
......@@ -4,7 +4,7 @@
{-
Copyright (C) 2009, 2011 Google Inc.
Copyright (C) 2009, 2011, 2012 Google Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -115,6 +115,7 @@ allTests =
, (fast, testInstance)
, (fast, testNode)
, (fast, testText)
, (fast, testSimu)
, (fast, testOpCodes)
, (fast, testJobs)
, (fast, testLoader)
......
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