Debian Codegraph @

test/hs/Test/Ganeti/HTools/Backend/Text.hs

raw
1 {-# LANGUAGE TemplateHaskell #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3
4 {-| Unittests for ganeti-htools.
5
6 -}
7
8 {-
9
10 Copyright (C) 2009, 2010, 2011, 2012, 2013 Google Inc.
11 All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are
15 met:
16
17 1. Redistributions of source code must retain the above copyright notice,
18 this list of conditions and the following disclaimer.
19
20 2. Redistributions in binary form must reproduce the above copyright
21 notice, this list of conditions and the following disclaimer in the
22 documentation and/or other materials provided with the distribution.
23
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
28 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
36 -}
37
38 module Test.Ganeti.HTools.Backend.Text (testHTools_Backend_Text) where
39
40 import Test.QuickCheck
41
42 import qualified Data.Map as Map
43 import Data.List
44 import Data.Maybe
45 import System.Time (ClockTime(..))
46
47 import Test.Ganeti.TestHelper
48 import Test.Ganeti.TestCommon
49 import Test.Ganeti.TestHTools
50 import Test.Ganeti.HTools.Instance (genInstanceSmallerThanNode,
51 genInstanceOnNodeList)
52 import Test.Ganeti.HTools.Node (genNode, genOnlineNode, genEmptyOnlineNode
53 , genUniqueNodeList)
54
55 import Ganeti.BasicTypes
56 import Ganeti.Types (InstanceStatus(..))
57 import qualified Ganeti.HTools.AlgorithmParams as Alg
58 import qualified Ganeti.HTools.Backend.Text as Text
59 import qualified Ganeti.HTools.Cluster as Cluster
60 import qualified Ganeti.HTools.Container as Container
61 import qualified Ganeti.HTools.Group as Group
62 import qualified Ganeti.HTools.Instance as Instance
63 import qualified Ganeti.HTools.Loader as Loader
64 import qualified Ganeti.HTools.Node as Node
65 import qualified Ganeti.HTools.Types as Types
66 import qualified Ganeti.Utils as Utils
67
68 -- * Instance text loader tests
69
70 toYN :: Bool -> String
71 toYN True = "Y"
72 toYN False = "N"
73
74 prop_Load_Instance :: String -> Int -> Int -> Int -> Types.InstanceStatus
75 -> NonEmptyList Char -> String
76 -> NonNegative Int -> NonNegative Int -> Bool
77 -> Types.DiskTemplate -> Int -> NonNegative Int -> Bool
78 -> [String] -> Property
79 prop_Load_Instance name mem dsk vcpus status
80 (NonEmpty pnode) snode
81 (NonNegative pdx) (NonNegative sdx) autobal dt su
82 (NonNegative spindles) forth ignoredFields =
83 pnode /= snode && pdx /= sdx ==>
84 let vcpus_s = show vcpus
85 dsk_s = show dsk
86 mem_s = show mem
87 su_s = show su
88 status_s = Types.instanceStatusToRaw status
89 ndx = if null snode
90 then [(pnode, pdx)]
91 else [(pnode, pdx), (snode, sdx)]
92 nl = Map.fromList ndx
93 tags = ""
94 sbal = toYN autobal
95 sdt = Types.diskTemplateToRaw dt
96 inst = Text.loadInst nl $
97 [name, mem_s, dsk_s, vcpus_s, status_s,
98 sbal, pnode, snode, sdt, tags, su_s, show spindles, toYN forth]
99 ++ ignoredFields
100 fail1 = Text.loadInst nl
101 [name, mem_s, dsk_s, vcpus_s, status_s,
102 sbal, pnode, pnode, tags]
103 in case inst of
104 Bad msg -> failTest $ "Failed to load instance: " ++ msg
105 Ok (_, i) -> counterexample "Mismatch in some field while\
106 \ loading the instance" $
107 Instance.name i == name &&
108 Instance.vcpus i == vcpus &&
109 Instance.mem i == mem &&
110 Instance.pNode i == pdx &&
111 Instance.sNode i == (if null snode
112 then Node.noSecondary
113 else sdx) &&
114 Instance.autoBalance i == autobal &&
115 Instance.spindleUse i == su &&
116 Instance.getTotalSpindles i == Just spindles &&
117 Instance.forthcoming i == forth &&
118 isBad fail1
119
120 prop_Load_InstanceFail :: [(String, Int)] -> [String] -> Property
121 prop_Load_InstanceFail ktn fields =
122 length fields < 10 ==>
123 case Text.loadInst nl fields of
124 Ok _ -> failTest "Managed to load instance from invalid data"
125 Bad msg -> counterexample ("Unrecognised error message: " ++ msg) $
126 "Invalid/incomplete instance data: '" `isPrefixOf` msg
127 where nl = Map.fromList ktn
128
129 genInstanceNodes :: Gen (Instance.Instance, Node.List, Types.NameAssoc)
130 genInstanceNodes = do
131 (nl, na) <- genUniqueNodeList genOnlineNode
132 inst <- genInstanceOnNodeList nl
133 return (inst, nl, na)
134
135 prop_InstanceLSIdempotent :: Property
136 prop_InstanceLSIdempotent =
137 forAll genInstanceNodes $ \(inst, nl, assoc) ->
138 (Text.loadInst assoc . Utils.sepSplit '|' . Text.serializeInstance nl)
139 inst ==? Ok (Instance.name inst, inst)
140
141 prop_Load_Node :: String -> Int -> Int -> Int -> Int -> Int
142 -> Int -> Bool -> Bool
143 prop_Load_Node name tm nm fm td fd tc fo =
144 let conv v = if v < 0
145 then "?"
146 else show v
147 tm_s = conv tm
148 nm_s = conv nm
149 fm_s = conv fm
150 td_s = conv td
151 fd_s = conv fd
152 tc_s = conv tc
153 fo_s = toYN fo
154 any_broken = any (< 0) [tm, nm, fm, td, fd, tc]
155 gid = Group.uuid defGroup
156 in case Text.loadNode defGroupAssoc
157 [name, tm_s, nm_s, fm_s, td_s, fd_s, tc_s, fo_s, gid] of
158 Nothing -> False
159 Just (name', node) ->
160 if fo || any_broken
161 then Node.offline node
162 else Node.name node == name' && name' == name &&
163 Node.alias node == name &&
164 Node.tMem node == fromIntegral tm &&
165 Node.nMem node == nm &&
166 Node.fMem node == fm &&
167 Node.tDsk node == fromIntegral td &&
168 Node.fDsk node == fd &&
169 Node.tCpu node == fromIntegral tc
170
171 prop_Load_NodeFail :: [String] -> Property
172 prop_Load_NodeFail fields =
173 length fields < 8 ==> isNothing $ Text.loadNode Map.empty fields
174
175 prop_Load_NodeSuccess :: String -> NonNegative Int -> NonNegative Int
176 -> NonNegative Int -> NonNegative Int -> NonNegative Int
177 -> NonNegative Int -> Bool -> NonNegative Int
178 -> Bool -> NonNegative Int -> NonNegative Int
179 -> NonNegative Int -> [String] -> Property
180 prop_Load_NodeSuccess name (NonNegative tm) (NonNegative nm) (NonNegative fm)
181 (NonNegative td) (NonNegative fd) (NonNegative tc) fo
182 (NonNegative spindles) excl_stor
183 (NonNegative free_spindles) (NonNegative nos_cpu)
184 (NonNegative cpu_speed) ignoredFields =
185 forAll genTags $ \tags ->
186 let node' = Text.loadNode defGroupAssoc $
187 [ name, show tm, show nm, show fm
188 , show td, show fd, show tc
189 , toYN fo, Group.uuid defGroup
190 , show spindles
191 , intercalate "," tags
192 , toYN excl_stor
193 , show free_spindles
194 , show nos_cpu
195 , show cpu_speed
196 ] ++ ignoredFields
197 in case node' of
198 Bad msg -> failTest $ "Failed to load node: " ++ msg
199 Ok (_, node) -> conjoin [ Node.name node ==? name
200 , Node.tMem node ==? fromIntegral tm
201 , Node.nMem node ==? nm
202 , Node.fMem node ==? fm
203 , Node.tDsk node ==? fromIntegral td
204 , Node.fDsk node ==? fd
205 , Node.tCpu node ==? fromIntegral tc
206 , Node.nTags node ==? tags
207 , Node.fSpindles node ==? free_spindles
208 , Node.nCpu node ==? nos_cpu
209 , Node.tCpuSpeed node ==? fromIntegral cpu_speed
210 ]
211
212 prop_NodeLSIdempotent :: Property
213 prop_NodeLSIdempotent =
214 forAll (genNode (Just 1) Nothing) $ \node ->
215 -- override failN1 to what loadNode returns by default
216 -- override pMem, xMem as they are updated after loading [in updateMemStat]
217 let n = Node.setPolicy Types.defIPolicy $
218 node { Node.failN1 = True,
219 Node.offline = False,
220 Node.pMem = 0,
221 Node.xMem = 0 }
222 in
223 (Text.loadNode defGroupAssoc.
224 Utils.sepSplit '|' . Text.serializeNode defGroupList) n ==?
225 Just (Node.name n, n)
226
227 prop_ISpecIdempotent :: Types.ISpec -> Property
228 prop_ISpecIdempotent ispec =
229 case Text.loadISpec "dummy" . Utils.sepSplit ',' .
230 Text.serializeISpec $ ispec of
231 Bad msg -> failTest $ "Failed to load ispec: " ++ msg
232 Ok ispec' -> ispec' ==? ispec
233
234 prop_MultipleMinMaxISpecsIdempotent :: [Types.MinMaxISpecs] -> Property
235 prop_MultipleMinMaxISpecsIdempotent minmaxes =
236 case Text.loadMultipleMinMaxISpecs "dummy" . Utils.sepSplit ';' .
237 Text.serializeMultipleMinMaxISpecs $ minmaxes of
238 Bad msg -> failTest $ "Failed to load min/max ispecs: " ++ msg
239 Ok minmaxes' -> minmaxes' ==? minmaxes
240
241 prop_IPolicyIdempotent :: Types.IPolicy -> Property
242 prop_IPolicyIdempotent ipol =
243 case Text.loadIPolicy . Utils.sepSplit '|' $
244 Text.serializeIPolicy owner ipol of
245 Bad msg -> failTest $ "Failed to load ispec: " ++ msg
246 Ok res -> res ==? (owner, ipol)
247 where owner = "dummy"
248
249 -- | This property, while being in the text tests, does more than just
250 -- test end-to-end the serialisation and loading back workflow; it
251 -- also tests the Loader.mergeData and the actual
252 -- Cluster.iterateAlloc (for well-behaving w.r.t. instance
253 -- allocations, not for the business logic). As such, it's a quite
254 -- complex and slow test, and that's the reason we restrict it to
255 -- small cluster sizes.
256 prop_CreateSerialise :: Property
257 prop_CreateSerialise =
258 forAll genTags $ \ctags ->
259 forAll (choose (1, 20)) $ \maxiter ->
260 forAll (choose (2, 10)) $ \count ->
261 forAll genEmptyOnlineNode $ \node ->
262 forAll (genInstanceSmallerThanNode node `suchThat`
263 -- We want to test with a working node, so don't generate a
264 -- status that indicates a problem with the node.
265 (\i -> Instance.runSt i `elem` [ StatusDown
266 , StatusOffline
267 , ErrorDown
268 , ErrorUp
269 , Running
270 , UserDown
271 ])) $ \inst ->
272 let nl = makeSmallCluster node count
273 reqnodes = Instance.requiredNodes $ Instance.diskTemplate inst
274 opts = Alg.defaultOptions
275 in case Cluster.genAllocNodes opts defGroupList nl reqnodes True
276 >>= \allocn -> Cluster.iterateAlloc opts nl Container.empty
277 (Just maxiter) inst allocn [] []
278 of
279 Bad msg -> failTest $ "Failed to allocate: " ++ msg
280 Ok (_, _, _, [], _) -> counterexample
281 "Failed to allocate: no allocations" False
282 Ok (_, nl', il, _, _) ->
283 let
284 -- makeSmallCluster created an empty cluster, that had some
285 -- instances allocated, so we need to simulate that the hyperwisor
286 -- now reports less fMem, otherwise Loader.checkData will detect
287 -- missing memory after deserialization.
288 nl1 = Container.map (\n -> n
289 { Node.fMem = Node.recordedFreeMem n }) nl'
290 cdata = Loader.ClusterData defGroupList nl1 il ctags
291 Types.defIPolicy
292 saved = Text.serializeCluster cdata
293 in case Text.parseData saved >>= Loader.mergeData [] [] [] [] (TOD 0 0)
294 of
295 Bad msg -> failTest $ "Failed to load/merge: " ++ msg
296 Ok (Loader.ClusterData gl2 nl2 il2 ctags2 cpol2) ->
297 let (_, nl3) = Loader.updateMissing nl2 il2 0
298 in conjoin [ ctags2 ==? ctags
299 , cpol2 ==? Types.defIPolicy
300 , il2 ==? il
301 , gl2 ==? defGroupList
302 , nl3 ==? nl1
303 ]
304
305 testSuite "HTools/Backend/Text"
306 [ 'prop_Load_Instance
307 , 'prop_Load_InstanceFail
308 , 'prop_InstanceLSIdempotent
309 , 'prop_Load_Node
310 , 'prop_Load_NodeFail
311 , 'prop_Load_NodeSuccess
312 , 'prop_NodeLSIdempotent
313 , 'prop_ISpecIdempotent
314 , 'prop_MultipleMinMaxISpecsIdempotent
315 , 'prop_IPolicyIdempotent
316 , 'prop_CreateSerialise
317 ]