Debian Codegraph @

test/hs/Test/Ganeti/TestHTools.hs

raw
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 All rights reserved.
11
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are
14 met:
15
16 1. Redistributions of source code must retain the above copyright notice,
17 this list of conditions and the following disclaimer.
18
19 2. Redistributions in binary form must reproduce the above copyright
20 notice, this list of conditions and the following disclaimer in the
21 documentation and/or other materials provided with the distribution.
22
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
27 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
35 -}
36
37 module Test.Ganeti.TestHTools
38 ( nullIPolicy
39 , nullISpec
40 , defGroup
41 , defGroupList
42 , defGroupAssoc
43 , createInstance
44 , makeSmallCluster
45 , setInstanceSmallerThanNode
46 ) where
47
48 import qualified Data.Map as Map
49
50 import Test.Ganeti.TestCommon
51
52 import qualified Ganeti.Constants as C
53 import qualified Ganeti.HTools.Container as Container
54 import qualified Ganeti.HTools.Group as Group
55 import qualified Ganeti.HTools.Instance as Instance
56 import qualified Ganeti.HTools.Loader as Loader
57 import qualified Ganeti.HTools.Node as Node
58 import qualified Ganeti.HTools.Types as Types
59
60 -- * Helpers
61
62 -- | An ISpec with 0 resources.
63 nullISpec :: Types.ISpec
64 nullISpec = Types.ISpec { Types.iSpecMemorySize = 0
65 , Types.iSpecCpuCount = 0
66 , Types.iSpecDiskSize = 0
67 , Types.iSpecDiskCount = 0
68 , Types.iSpecNicCount = 0
69 , Types.iSpecSpindleUse = 0
70 }
71
72 -- | Null iPolicy, and by null we mean very liberal.
73 nullIPolicy :: Types.IPolicy
74 nullIPolicy = Types.IPolicy
75 { Types.iPolicyMinMaxISpecs = [Types.MinMaxISpecs
76 { Types.minMaxISpecsMinSpec = nullISpec
77 , Types.minMaxISpecsMaxSpec = Types.ISpec
78 { Types.iSpecMemorySize = maxBound
79 , Types.iSpecCpuCount = maxBound
80 , Types.iSpecDiskSize = maxBound
81 , Types.iSpecDiskCount = C.maxDisks
82 , Types.iSpecNicCount = C.maxNics
83 , Types.iSpecSpindleUse = maxBound
84 }
85 }]
86 , Types.iPolicyStdSpec = Types.ISpec { Types.iSpecMemorySize = Types.unitMem
87 , Types.iSpecCpuCount = Types.unitCpu
88 , Types.iSpecDiskSize = Types.unitDsk
89 , Types.iSpecDiskCount = 1
90 , Types.iSpecNicCount = 1
91 , Types.iSpecSpindleUse = 1
92 }
93 , Types.iPolicyDiskTemplates = [minBound..maxBound]
94 , Types.iPolicyVcpuRatio = maxVcpuRatio -- somewhat random value, high
95 -- enough to not impact us
96 , Types.iPolicySpindleRatio = maxSpindleRatio
97 }
98
99 -- | Default group definition.
100 defGroup :: Group.Group
101 defGroup = flip Group.setIdx 0 $
102 Group.create "default" Types.defaultGroupID Types.AllocPreferred
103 [] nullIPolicy []
104
105 -- | Default group, as a (singleton) 'Group.List'.
106 defGroupList :: Group.List
107 defGroupList = Container.fromList [(Group.idx defGroup, defGroup)]
108
109 -- | Default group, as a string map.
110 defGroupAssoc :: Map.Map String Types.Gdx
111 defGroupAssoc = Map.singleton (Group.uuid defGroup) (Group.idx defGroup)
112
113 -- | Create an instance given its spec.
114 createInstance :: Int -> Int -> Int -> Instance.Instance
115 createInstance mem dsk vcpus =
116 Instance.create "inst-unnamed" mem dsk [Instance.Disk dsk Nothing] vcpus
117 Types.Running [] True (-1) (-1) Types.DTDrbd8 1 [] False
118
119 -- | Create a small cluster by repeating a node spec.
120 makeSmallCluster :: Node.Node -> Int -> Node.List
121 makeSmallCluster node count =
122 let origname = Node.name node
123 origalias = Node.alias node
124 nodes = map (\idx -> node { Node.name = origname ++ "-" ++ show idx
125 , Node.alias = origalias ++ "-" ++ show idx })
126 [1..count]
127 fn = flip Node.buildPeers Container.empty
128 namelst = map (\n -> (Node.name n, fn n)) nodes
129 (_, nlst) = Loader.assignIndices namelst
130 in nlst
131
132 -- | Update an instance to be smaller than a node.
133 setInstanceSmallerThanNode :: Node.Node
134 -> Instance.Instance -> Instance.Instance
135 setInstanceSmallerThanNode node inst =
136 let new_dsk = Node.availDisk node `div` 2
137 in inst { Instance.mem = Node.availMem node `div` 2
138 , Instance.dsk = new_dsk
139 , Instance.vcpus = Node.availCpu node `div` 2
140 , Instance.disks = [Instance.Disk new_dsk
141 (if Node.exclStorage node
142 then Just $ Node.fSpindlesForth node `div` 2
143 else Nothing)]
144 }