Add functionality for checking validity of names
[ganeti-local] / htest / Test / Ganeti / TestHTools.hs
1 {-# LANGUAGE TemplateHaskell #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3
4 {-| Common functionality for htools-related unittests.
5
6 -}
7
8 {-
9
10 Copyright (C) 2009, 2010, 2011, 2012 Google Inc.
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 02110-1301, USA.
26
27 -}
28
29 module Test.Ganeti.TestHTools where
30
31 import qualified Data.Map as Map
32
33 import Test.Ganeti.TestHelper
34 import Test.Ganeti.TestCommon
35
36 import qualified Ganeti.Constants as C
37 import qualified Ganeti.HTools.Container as Container
38 import qualified Ganeti.HTools.Group as Group
39 import qualified Ganeti.HTools.Instance as Instance
40 import qualified Ganeti.HTools.Loader as Loader
41 import qualified Ganeti.HTools.Node as Node
42 import qualified Ganeti.HTools.Types as Types
43
44 -- * Helpers
45
46 -- | Null iPolicy, and by null we mean very liberal.
47 nullIPolicy :: Types.IPolicy
48 nullIPolicy = Types.IPolicy
49   { Types.iPolicyMinSpec = Types.ISpec { Types.iSpecMemorySize = 0
50                                        , Types.iSpecCpuCount   = 0
51                                        , Types.iSpecDiskSize   = 0
52                                        , Types.iSpecDiskCount  = 0
53                                        , Types.iSpecNicCount   = 0
54                                        , Types.iSpecSpindleUse = 0
55                                        }
56   , Types.iPolicyMaxSpec = Types.ISpec { Types.iSpecMemorySize = maxBound
57                                        , Types.iSpecCpuCount   = maxBound
58                                        , Types.iSpecDiskSize   = maxBound
59                                        , Types.iSpecDiskCount  = C.maxDisks
60                                        , Types.iSpecNicCount   = C.maxNics
61                                        , Types.iSpecSpindleUse = maxBound
62                                        }
63   , Types.iPolicyStdSpec = Types.ISpec { Types.iSpecMemorySize = Types.unitMem
64                                        , Types.iSpecCpuCount   = Types.unitCpu
65                                        , Types.iSpecDiskSize   = Types.unitDsk
66                                        , Types.iSpecDiskCount  = 1
67                                        , Types.iSpecNicCount   = 1
68                                        , Types.iSpecSpindleUse = 1
69                                        }
70   , Types.iPolicyDiskTemplates = [minBound..maxBound]
71   , Types.iPolicyVcpuRatio = maxVcpuRatio -- somewhat random value, high
72                                           -- enough to not impact us
73   , Types.iPolicySpindleRatio = maxSpindleRatio
74   }
75
76 defGroup :: Group.Group
77 defGroup = flip Group.setIdx 0 $
78              Group.create "default" Types.defaultGroupID Types.AllocPreferred
79                   nullIPolicy
80
81 defGroupList :: Group.List
82 defGroupList = Container.fromList [(Group.idx defGroup, defGroup)]
83
84 defGroupAssoc :: Map.Map String Types.Gdx
85 defGroupAssoc = Map.singleton (Group.uuid defGroup) (Group.idx defGroup)
86
87 -- | Simple checker for whether OpResult is fail or pass.
88 isFailure :: Types.OpResult a -> Bool
89 isFailure (Types.OpFail _) = True
90 isFailure _ = False
91
92 -- | Create an instance given its spec.
93 createInstance :: Int -> Int -> Int -> Instance.Instance
94 createInstance mem dsk vcpus =
95   Instance.create "inst-unnamed" mem dsk vcpus Types.Running [] True (-1) (-1)
96     Types.DTDrbd8 1
97
98 -- | Create a small cluster by repeating a node spec.
99 makeSmallCluster :: Node.Node -> Int -> Node.List
100 makeSmallCluster node count =
101   let origname = Node.name node
102       origalias = Node.alias node
103       nodes = map (\idx -> node { Node.name = origname ++ "-" ++ show idx
104                                 , Node.alias = origalias ++ "-" ++ show idx })
105               [1..count]
106       fn = flip Node.buildPeers Container.empty
107       namelst = map (\n -> (Node.name n, fn n)) nodes
108       (_, nlst) = Loader.assignIndices namelst
109   in nlst
110
111 -- | Update an instance to be smaller than a node.
112 setInstanceSmallerThanNode :: Node.Node
113                            -> Instance.Instance -> Instance.Instance
114 setInstanceSmallerThanNode node inst =
115   inst { Instance.mem = Node.availMem node `div` 2
116        , Instance.dsk = Node.availDisk node `div` 2
117        , Instance.vcpus = Node.availCpu node `div` 2
118        }
119
120 -- * Arbitrary instances
121
122 $(genArbitrary ''Types.InstanceStatus)