1 {-# OPTIONS_GHC -fno-warn-orphans #-}
3 {-| Common functionality for htools-related unittests.
9 Copyright (C) 2009, 2010, 2011, 2012 Google Inc.
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.
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.
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
28 module Test.Ganeti.TestHTools where
30 import qualified Data.Map as Map
32 import Test.Ganeti.TestCommon
34 import qualified Ganeti.Constants as C
35 import qualified Ganeti.HTools.Container as Container
36 import qualified Ganeti.HTools.Group as Group
37 import qualified Ganeti.HTools.Instance as Instance
38 import qualified Ganeti.HTools.Loader as Loader
39 import qualified Ganeti.HTools.Node as Node
40 import qualified Ganeti.HTools.Types as Types
44 -- | Null iPolicy, and by null we mean very liberal.
45 nullIPolicy :: Types.IPolicy
46 nullIPolicy = Types.IPolicy
47 { Types.iPolicyMinSpec = Types.ISpec { Types.iSpecMemorySize = 0
48 , Types.iSpecCpuCount = 0
49 , Types.iSpecDiskSize = 0
50 , Types.iSpecDiskCount = 0
51 , Types.iSpecNicCount = 0
52 , Types.iSpecSpindleUse = 0
54 , Types.iPolicyMaxSpec = Types.ISpec { Types.iSpecMemorySize = maxBound
55 , Types.iSpecCpuCount = maxBound
56 , Types.iSpecDiskSize = maxBound
57 , Types.iSpecDiskCount = C.maxDisks
58 , Types.iSpecNicCount = C.maxNics
59 , Types.iSpecSpindleUse = maxBound
61 , Types.iPolicyStdSpec = Types.ISpec { Types.iSpecMemorySize = Types.unitMem
62 , Types.iSpecCpuCount = Types.unitCpu
63 , Types.iSpecDiskSize = Types.unitDsk
64 , Types.iSpecDiskCount = 1
65 , Types.iSpecNicCount = 1
66 , Types.iSpecSpindleUse = 1
68 , Types.iPolicyDiskTemplates = [minBound..maxBound]
69 , Types.iPolicyVcpuRatio = maxVcpuRatio -- somewhat random value, high
70 -- enough to not impact us
71 , Types.iPolicySpindleRatio = maxSpindleRatio
74 defGroup :: Group.Group
75 defGroup = flip Group.setIdx 0 $
76 Group.create "default" Types.defaultGroupID Types.AllocPreferred
79 defGroupList :: Group.List
80 defGroupList = Container.fromList [(Group.idx defGroup, defGroup)]
82 defGroupAssoc :: Map.Map String Types.Gdx
83 defGroupAssoc = Map.singleton (Group.uuid defGroup) (Group.idx defGroup)
85 -- | Create an instance given its spec.
86 createInstance :: Int -> Int -> Int -> Instance.Instance
87 createInstance mem dsk vcpus =
88 Instance.create "inst-unnamed" mem dsk vcpus Types.Running [] True (-1) (-1)
91 -- | Create a small cluster by repeating a node spec.
92 makeSmallCluster :: Node.Node -> Int -> Node.List
93 makeSmallCluster node count =
94 let origname = Node.name node
95 origalias = Node.alias node
96 nodes = map (\idx -> node { Node.name = origname ++ "-" ++ show idx
97 , Node.alias = origalias ++ "-" ++ show idx })
99 fn = flip Node.buildPeers Container.empty
100 namelst = map (\n -> (Node.name n, fn n)) nodes
101 (_, nlst) = Loader.assignIndices namelst
104 -- | Update an instance to be smaller than a node.
105 setInstanceSmallerThanNode :: Node.Node
106 -> Instance.Instance -> Instance.Instance
107 setInstanceSmallerThanNode node inst =
108 inst { Instance.mem = Node.availMem node `div` 2
109 , Instance.dsk = Node.availDisk node `div` 2
110 , Instance.vcpus = Node.availCpu node `div` 2