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