Debian Codegraph @

test/hs/Test/Ganeti/HTools/Loader.hs

raw
1 {-# LANGUAGE TemplateHaskell #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3
4 {-| Unittests for ganeti-htools.
5
6 -}
7
8 {-
9
10 Copyright (C) 2009, 2010, 2011, 2012 Google Inc.
11 All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are
15 met:
16
17 1. Redistributions of source code must retain the above copyright notice,
18 this list of conditions and the following disclaimer.
19
20 2. Redistributions in binary form must reproduce the above copyright
21 notice, this list of conditions and the following disclaimer in the
22 documentation and/or other materials provided with the distribution.
23
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
28 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
36 -}
37
38 module Test.Ganeti.HTools.Loader (testHTools_Loader) where
39
40 import Test.QuickCheck
41
42 import qualified Data.IntMap as IntMap
43 import qualified Data.Map as Map
44 import Data.List
45 import System.Time (ClockTime(..))
46
47 import Test.Ganeti.TestHelper
48 import Test.Ganeti.TestCommon
49 import Test.Ganeti.HTools.Node ()
50
51 import qualified Ganeti.BasicTypes as BasicTypes
52 import qualified Ganeti.HTools.Container as Container
53 import qualified Ganeti.HTools.Loader as Loader
54 import qualified Ganeti.HTools.Node as Node
55
56 prop_lookupNode :: [(String, Int)] -> String -> String -> Property
57 prop_lookupNode ktn inst node =
58 Loader.lookupNode nl inst node ==? Map.lookup node nl
59 where nl = Map.fromList ktn
60
61 prop_lookupInstance :: [(String, Int)] -> String -> Property
62 prop_lookupInstance kti inst =
63 Loader.lookupInstance il inst ==? Map.lookup inst il
64 where il = Map.fromList kti
65
66 prop_assignIndices :: Property
67 prop_assignIndices =
68 -- generate nodes with unique names
69 forAll (arbitrary `suchThat`
70 (\nodes ->
71 let names = map Node.name nodes
72 in length names == length (nub names))) $ \nodes ->
73 let (nassoc, kt) =
74 Loader.assignIndices (map (\n -> (Node.name n, n)) nodes)
75 in Map.size nassoc == length nodes &&
76 Container.size kt == length nodes &&
77 (null nodes || maximum (IntMap.keys kt) == length nodes - 1)
78
79 -- | Checks that the number of primary instances recorded on the nodes
80 -- is zero.
81 prop_mergeData :: [Node.Node] -> Bool
82 prop_mergeData ns =
83 let na = Container.fromList $ map (\n -> (Node.idx n, n)) ns
84 in case Loader.mergeData [] [] [] [] (TOD 0 0)
85 (Loader.emptyCluster {Loader.cdNodes = na}) of
86 BasicTypes.Bad _ -> False
87 BasicTypes.Ok (Loader.ClusterData _ nl il _ _) ->
88 let nodes = Container.elems nl
89 instances = Container.elems il
90 in (sum . map (length . Node.pList)) nodes == 0 &&
91 null instances
92
93 -- | Check that compareNameComponent on equal strings works.
94 prop_compareNameComponent_equal :: String -> Bool
95 prop_compareNameComponent_equal s =
96 BasicTypes.compareNameComponent s s ==
97 BasicTypes.LookupResult BasicTypes.ExactMatch s
98
99 -- | Check that compareNameComponent on prefix strings works.
100 prop_compareNameComponent_prefix :: NonEmptyList Char -> String -> Bool
101 prop_compareNameComponent_prefix (NonEmpty s1) s2 =
102 BasicTypes.compareNameComponent (s1 ++ "." ++ s2) s1 ==
103 BasicTypes.LookupResult BasicTypes.PartialMatch s1
104
105 testSuite "HTools/Loader"
106 [ 'prop_lookupNode
107 , 'prop_lookupInstance
108 , 'prop_assignIndices
109 , 'prop_mergeData
110 , 'prop_compareNameComponent_equal
111 , 'prop_compareNameComponent_prefix
112 ]