Debian Codegraph @

test/hs/Test/Ganeti/HTools/Backend/Simu.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.Backend.Simu (testHTools_Backend_Simu) where
39
40 import Test.QuickCheck hiding (Result)
41
42 import Control.Monad
43 import qualified Data.IntMap as IntMap
44 import Text.Printf (printf)
45
46 import Test.Ganeti.TestHelper
47 import Test.Ganeti.TestCommon
48
49 import Ganeti.BasicTypes
50 import qualified Ganeti.Constants as C
51 import qualified Ganeti.HTools.Backend.Simu as Simu
52 import qualified Ganeti.HTools.Container as Container
53 import qualified Ganeti.HTools.Group as Group
54 import qualified Ganeti.HTools.Loader as Loader
55 import qualified Ganeti.HTools.Node as Node
56 import qualified Ganeti.HTools.Types as Types
57
58 -- | Generates a tuple of specs for simulation.
59 genSimuSpec :: Gen (String, Int, Int, Int, Int)
60 genSimuSpec = do
61 pol <- elements [C.allocPolicyPreferred,
62 C.allocPolicyLastResort, C.allocPolicyUnallocable,
63 "p", "a", "u"]
64 -- should be reasonable (nodes/group), bigger values only complicate
65 -- the display of failed tests, and we don't care (in this particular
66 -- test) about big node groups
67 nodes <- choose (0, 20)
68 dsk <- choose (0, maxDsk)
69 mem <- choose (0, maxMem)
70 cpu <- choose (0, maxCpu)
71 return (pol, nodes, dsk, mem, cpu)
72
73 -- | Checks that given a set of corrects specs, we can load them
74 -- successfully, and that at high-level the values look right.
75 prop_Load :: Property
76 prop_Load =
77 forAll (choose (0, 10)) $ \ngroups ->
78 forAll (replicateM ngroups genSimuSpec) $ \specs ->
79 let strspecs = map (\(p, n, d, m, c) -> printf "%s,%d,%d,%d,%d"
80 p n d m c::String) specs
81 totnodes = sum $ map (\(_, n, _, _, _) -> n) specs
82 mdc_in = concatMap (\(_, n, d, m, c) ->
83 replicate n (fromIntegral m, fromIntegral d,
84 fromIntegral c,
85 fromIntegral m, fromIntegral d))
86 specs :: [(Double, Double, Double, Int, Int)]
87 in case Simu.parseData strspecs of
88 Bad msg -> failTest $ "Failed to load specs: " ++ msg
89 Ok (Loader.ClusterData gl nl il tags ipol) ->
90 let nodes = map snd $ IntMap.toAscList nl
91 nidx = map Node.idx nodes
92 mdc_out = map (\n -> (Node.tMem n, Node.tDsk n, Node.tCpu n,
93 Node.fMem n, Node.fDsk n)) nodes
94 in conjoin [ Container.size gl ==? ngroups
95 , Container.size nl ==? totnodes
96 , Container.size il ==? 0
97 , length tags ==? 0
98 , ipol ==? Types.defIPolicy
99 , nidx ==? [1..totnodes]
100 , mdc_in ==? mdc_out
101 , map Group.iPolicy (Container.elems gl) ==?
102 replicate ngroups Types.defIPolicy
103 ]
104
105 testSuite "HTools/Backend/Simu"
106 [ 'prop_Load
107 ]