root / test / hs / Test / Ganeti / TestHTools.hs @ 241cea1e
History | View | Annotate | Download (4.7 kB)
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.iPolicyMinSpec = Types.ISpec { Types.iSpecMemorySize = 0 |
56 |
, Types.iSpecCpuCount = 0 |
57 |
, Types.iSpecDiskSize = 0 |
58 |
, Types.iSpecDiskCount = 0 |
59 |
, Types.iSpecNicCount = 0 |
60 |
, Types.iSpecSpindleUse = 0 |
61 |
} |
62 |
, Types.iPolicyMaxSpec = Types.ISpec { Types.iSpecMemorySize = maxBound |
63 |
, Types.iSpecCpuCount = maxBound |
64 |
, Types.iSpecDiskSize = maxBound |
65 |
, Types.iSpecDiskCount = C.maxDisks |
66 |
, Types.iSpecNicCount = C.maxNics |
67 |
, Types.iSpecSpindleUse = maxBound |
68 |
} |
69 |
, Types.iPolicyStdSpec = Types.ISpec { Types.iSpecMemorySize = Types.unitMem |
70 |
, Types.iSpecCpuCount = Types.unitCpu |
71 |
, Types.iSpecDiskSize = Types.unitDsk |
72 |
, Types.iSpecDiskCount = 1 |
73 |
, Types.iSpecNicCount = 1 |
74 |
, Types.iSpecSpindleUse = 1 |
75 |
} |
76 |
, Types.iPolicyDiskTemplates = [minBound..maxBound] |
77 |
, Types.iPolicyVcpuRatio = maxVcpuRatio -- somewhat random value, high |
78 |
-- enough to not impact us |
79 |
, Types.iPolicySpindleRatio = maxSpindleRatio |
80 |
} |
81 |
|
82 |
-- | Default group definition. |
83 |
defGroup :: Group.Group |
84 |
defGroup = flip Group.setIdx 0 $ |
85 |
Group.create "default" Types.defaultGroupID Types.AllocPreferred |
86 |
nullIPolicy [] |
87 |
|
88 |
-- | Default group, as a (singleton) 'Group.List'. |
89 |
defGroupList :: Group.List |
90 |
defGroupList = Container.fromList [(Group.idx defGroup, defGroup)] |
91 |
|
92 |
-- | Default group, as a string map. |
93 |
defGroupAssoc :: Map.Map String Types.Gdx |
94 |
defGroupAssoc = Map.singleton (Group.uuid defGroup) (Group.idx defGroup) |
95 |
|
96 |
-- | Create an instance given its spec. |
97 |
createInstance :: Int -> Int -> Int -> Instance.Instance |
98 |
createInstance mem dsk vcpus = |
99 |
Instance.create "inst-unnamed" mem dsk [dsk] vcpus Types.Running [] True (-1) |
100 |
(-1) Types.DTDrbd8 1 |
101 |
|
102 |
-- | Create a small cluster by repeating a node spec. |
103 |
makeSmallCluster :: Node.Node -> Int -> Node.List |
104 |
makeSmallCluster node count = |
105 |
let origname = Node.name node |
106 |
origalias = Node.alias node |
107 |
nodes = map (\idx -> node { Node.name = origname ++ "-" ++ show idx |
108 |
, Node.alias = origalias ++ "-" ++ show idx }) |
109 |
[1..count] |
110 |
fn = flip Node.buildPeers Container.empty |
111 |
namelst = map (\n -> (Node.name n, fn n)) nodes |
112 |
(_, nlst) = Loader.assignIndices namelst |
113 |
in nlst |
114 |
|
115 |
-- | Update an instance to be smaller than a node. |
116 |
setInstanceSmallerThanNode :: Node.Node |
117 |
-> Instance.Instance -> Instance.Instance |
118 |
setInstanceSmallerThanNode node inst = |
119 |
inst { Instance.mem = Node.availMem node `div` 2 |
120 |
, Instance.dsk = Node.availDisk node `div` 2 |
121 |
, Instance.vcpus = Node.availCpu node `div` 2 |
122 |
} |