module Test.Ganeti.HTools.Loader (testHTools_Loader) where
import Test.QuickCheck
import qualified Data.IntMap as IntMap
import qualified Data.Map as Map
import Data.List
import System.Time (ClockTime(..))
import Test.Ganeti.TestHelper
import Test.Ganeti.TestCommon
import Test.Ganeti.HTools.Node ()
import qualified Ganeti.BasicTypes as BasicTypes
import qualified Ganeti.HTools.Container as Container
import qualified Ganeti.HTools.Loader as Loader
import qualified Ganeti.HTools.Node as Node
prop_lookupNode :: [(String, Int)] -> String -> String -> Property
prop_lookupNode ktn inst node =
Loader.lookupNode nl inst node ==? Map.lookup node nl
where nl = Map.fromList ktn
prop_lookupInstance :: [(String, Int)] -> String -> Property
prop_lookupInstance kti inst =
Loader.lookupInstance il inst ==? Map.lookup inst il
where il = Map.fromList kti
prop_assignIndices :: Property
prop_assignIndices =
forAll (arbitrary `suchThat`
(\nodes ->
let names = map Node.name nodes
in length names == length (nub names))) $ \nodes ->
let (nassoc, kt) =
Loader.assignIndices (map (\n -> (Node.name n, n)) nodes)
in Map.size nassoc == length nodes &&
Container.size kt == length nodes &&
(null nodes || maximum (IntMap.keys kt) == length nodes 1)
prop_mergeData :: [Node.Node] -> Bool
prop_mergeData ns =
let na = Container.fromList $ map (\n -> (Node.idx n, n)) ns
in case Loader.mergeData [] [] [] [] (TOD 0 0)
(Loader.emptyCluster {Loader.cdNodes = na}) of
BasicTypes.Bad _ -> False
BasicTypes.Ok (Loader.ClusterData _ nl il _ _) ->
let nodes = Container.elems nl
instances = Container.elems il
in (sum . map (length . Node.pList)) nodes == 0 &&
null instances
prop_compareNameComponent_equal :: String -> Bool
prop_compareNameComponent_equal s =
BasicTypes.compareNameComponent s s ==
BasicTypes.LookupResult BasicTypes.ExactMatch s
prop_compareNameComponent_prefix :: NonEmptyList Char -> String -> Bool
prop_compareNameComponent_prefix (NonEmpty s1) s2 =
BasicTypes.compareNameComponent (s1 ++ "." ++ s2) s1 ==
BasicTypes.LookupResult BasicTypes.PartialMatch s1
testSuite "HTools/Loader"
[ 'prop_lookupNode
, 'prop_lookupInstance
, 'prop_assignIndices
, 'prop_mergeData
, 'prop_compareNameComponent_equal
, 'prop_compareNameComponent_prefix
]