Debian Codegraph @

test/hs/Test/Ganeti/HTools/Node.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.Node
39 ( testHTools_Node
40 , Node.Node(..)
41 , setInstanceSmallerThanNode
42 , genNode
43 , genOnlineNode
44 , genEmptyOnlineNode
45 , genNodeList
46 , genUniqueNodeList
47 ) where
48
49 import Test.QuickCheck
50 import Test.HUnit
51
52 import Control.Monad
53 import qualified Data.Map as Map
54 import qualified Data.Graph as Graph
55 import Data.List
56
57 import Test.Ganeti.TestHelper
58 import Test.Ganeti.TestCommon
59 import Test.Ganeti.TestHTools
60 import Test.Ganeti.HTools.Instance ( genInstanceSmallerThanNode
61 , genInstanceList
62 , genInstanceOnNodeList)
63
64 import Ganeti.BasicTypes
65 import qualified Ganeti.HTools.Loader as Loader
66 import qualified Ganeti.HTools.Container as Container
67 import qualified Ganeti.HTools.Instance as Instance
68 import qualified Ganeti.HTools.Node as Node
69 import qualified Ganeti.HTools.Types as Types
70 import qualified Ganeti.HTools.Graph as HGraph
71
72 {-# ANN module "HLint: ignore Use camelCase" #-}
73
74 -- * Arbitrary instances
75
76 -- | Generates an arbitrary node based on sizing information.
77 genNode :: Maybe Int -- ^ Minimum node size in terms of units
78 -> Maybe Int -- ^ Maximum node size (when Nothing, bounded
79 -- just by the max... constants)
80 -> Gen Node.Node
81 genNode min_multiplier max_multiplier = do
82 let (base_mem, base_dsk, base_cpu, base_spindles) =
83 case min_multiplier of
84 Just mm -> (mm * Types.unitMem,
85 mm * Types.unitDsk,
86 mm * Types.unitCpu,
87 mm)
88 Nothing -> (0, 0, 0, 0)
89 (top_mem, top_dsk, top_cpu, top_spindles) =
90 case max_multiplier of
91 Just mm -> (mm * Types.unitMem,
92 mm * Types.unitDsk,
93 mm * Types.unitCpu,
94 mm)
95 Nothing -> (maxMem, maxDsk, maxCpu, maxSpindles)
96 name <- genFQDN
97 mem_t <- choose (base_mem, top_mem)
98 mem_f <- choose (base_mem, mem_t)
99 mem_n <- choose (0, mem_t - mem_f)
100 dsk_t <- choose (base_dsk, top_dsk)
101 dsk_f <- choose (base_dsk, dsk_t)
102 cpu_t <- choose (base_cpu, top_cpu)
103 cpu_n <- choose (base_cpu, cpu_t)
104 offl <- arbitrary
105 spindles <- choose (base_spindles, top_spindles)
106 let n = Node.create name (fromIntegral mem_t) mem_n mem_f
107 (fromIntegral dsk_t) dsk_f (fromIntegral cpu_t) cpu_n offl spindles
108 0 0 False
109 n1 = Node.setPolicy nullIPolicy n
110 n2 = Loader.updateMemStat n1 Container.empty
111 return $ Node.buildPeers n2 Container.empty
112
113 -- | Helper function to generate a sane node.
114 genOnlineNode :: Gen Node.Node
115 genOnlineNode =
116 arbitrary `suchThat` (\n -> not (Node.offline n) &&
117 not (Node.failN1 n) &&
118 Node.availDisk n > 2 * Types.unitDsk &&
119 Node.availMem n > 2 * Types.unitMem &&
120 Node.availCpu n > 2 &&
121 Node.tSpindles n > 2)
122
123 -- | Helper function to generate a sane empty node with consistent
124 -- internal data.
125 genEmptyOnlineNode :: Gen Node.Node
126 genEmptyOnlineNode =
127 (do node <- arbitrary
128 let fmem = truncate (Node.tMem node) - Node.nMem node
129 let node' = node { Node.offline = False
130 , Node.fMem = fmem
131 , Node.fMemForth = fmem
132 , Node.pMem = fromIntegral fmem / Node.tMem node
133 , Node.pMemForth = fromIntegral fmem / Node.tMem node
134 , Node.rMem = 0
135 , Node.rMemForth = 0
136 , Node.pRem = 0
137 , Node.pRemForth = 0
138 , Node.xMem = 0
139 }
140 return node') `suchThat` (\ n -> not (Node.failN1 n) &&
141 Node.availDisk n > 0 &&
142 Node.availMem n > 0 &&
143 Node.availCpu n > 0 &&
144 Node.tSpindles n > 0)
145
146 -- | Generate a node with exclusive storage enabled.
147 genExclStorNode :: Gen Node.Node
148 genExclStorNode = do
149 n <- genOnlineNode
150 fs <- choose (Types.unitSpindle, Node.tSpindles n)
151 fsForth <- choose (Types.unitSpindle, fs)
152 let pd = fromIntegral fs / fromIntegral (Node.tSpindles n)::Double
153 let pdForth = fromIntegral fsForth / fromIntegral (Node.tSpindles n)::Double
154 return n { Node.exclStorage = True
155 , Node.fSpindles = fs
156 , Node.fSpindlesForth = fsForth
157 , Node.pDsk = pd
158 , Node.pDskForth = pdForth
159 }
160
161 -- | Generate a node with exclusive storage possibly enabled.
162 genMaybeExclStorNode :: Gen Node.Node
163 genMaybeExclStorNode = oneof [genOnlineNode, genExclStorNode]
164
165 -- and a random node
166 instance Arbitrary Node.Node where
167 arbitrary = genNode Nothing Nothing
168
169 -- | Node list generator.
170 -- Given a node generator, create a random length node list. Note that "real"
171 -- clusters always have at least one node, so we don't generate empty node
172 -- lists here.
173 genNodeList :: Gen Node.Node -> Gen Node.List
174 genNodeList ngen = fmap (snd . Loader.assignIndices) names_nodes
175 where names_nodes = (fmap . map) (\n -> (Node.name n, n)) nodes
176 nodes = listOf1 ngen `suchThat`
177 ((\ns -> ns == nub ns) . map Node.name)
178
179 -- | Node list generator where node names are unique
180 genUniqueNodeList :: Gen Node.Node -> Gen (Node.List, Types.NameAssoc)
181 genUniqueNodeList ngen = (do
182 nl <- genNodeList ngen
183 let na = (fst . Loader.assignIndices) $
184 map (\n -> (Node.name n, n)) (Container.elems nl)
185 return (nl, na)) `suchThat`
186 (\(nl, na) -> Container.size nl == Map.size na)
187
188 -- | Generate a node list, an instance list, and a node graph.
189 -- We choose instances with nodes contained in the node list.
190 genNodeGraph :: Gen (Maybe Graph.Graph, Node.List, Instance.List)
191 genNodeGraph = do
192 nl <- genNodeList genOnlineNode `suchThat` ((2<=).Container.size)
193 il <- genInstanceList (genInstanceOnNodeList nl)
194 return (Node.mkNodeGraph nl il, nl, il)
195
196 -- * Test cases
197
198 prop_setAlias :: Node.Node -> String -> Bool
199 prop_setAlias node name =
200 Node.name newnode == Node.name node &&
201 Node.alias newnode == name
202 where newnode = Node.setAlias node name
203
204 prop_setOffline :: Node.Node -> Bool -> Property
205 prop_setOffline node status =
206 Node.offline newnode ==? status
207 where newnode = Node.setOffline node status
208
209 prop_setMcpu :: Node.Node -> Double -> Property
210 prop_setMcpu node mc =
211 Types.iPolicyVcpuRatio (Node.iPolicy newnode) ==? mc
212 where newnode = Node.setMcpu node mc
213
214 -- Check if adding an instance that consumes exactly all reserved
215 -- memory does not raise an N+1 error
216 prop_addPri_NoN1Fail :: Property
217 prop_addPri_NoN1Fail =
218 forAll genMaybeExclStorNode $ \node ->
219 forAll (genInstanceSmallerThanNode node) $ \inst ->
220 let inst' = inst { Instance.mem = Node.fMem node - Node.rMem node }
221 in (Node.addPri node inst' /=? Bad Types.FailN1)
222
223 -- | Check that an instance add with too high memory or disk will be
224 -- rejected.
225 prop_addPriFM :: Node.Node -> Instance.Instance -> Property
226 prop_addPriFM node inst =
227 Instance.mem inst >= Node.fMem node && not (Node.failN1 node) &&
228 Instance.usesMemory inst ==>
229 (Node.addPri node inst'' ==? Bad Types.FailMem)
230 where inst' = setInstanceSmallerThanNode node inst
231 inst'' = inst' { Instance.mem = Instance.mem inst }
232
233 -- | Check that adding a primary instance with too much disk fails
234 -- with type FailDisk.
235 prop_addPriFD :: Instance.Instance -> Property
236 prop_addPriFD inst =
237 forAll (genNode (Just 1) Nothing) $ \node ->
238 forAll (elements Instance.localStorageTemplates) $ \dt ->
239 Instance.dsk inst >= Node.fDsk node && not (Node.failN1 node) ==>
240 let inst' = setInstanceSmallerThanNode node inst
241 inst'' = inst' { Instance.dsk = Instance.dsk inst
242 , Instance.diskTemplate = dt }
243 in (Node.addPri node inst'' ==? Bad Types.FailDisk)
244
245 -- | Check if an instance exceeds a spindles limit or has no spindles set.
246 hasInstTooManySpindles :: Instance.Instance -> Int -> Bool
247 hasInstTooManySpindles inst sp_lim =
248 case Instance.getTotalSpindles inst of
249 Just s -> s > sp_lim
250 Nothing -> True
251
252 -- | Check that adding a primary instance with too many spindles fails
253 -- with type FailSpindles (when exclusive storage is enabled).
254 prop_addPriFS :: Instance.Instance -> Property
255 prop_addPriFS inst =
256 forAll genExclStorNode $ \node ->
257 forAll (elements Instance.localStorageTemplates) $ \dt ->
258 hasInstTooManySpindles inst (Node.fSpindles node) &&
259 not (Node.failN1 node) ==>
260 let inst' = setInstanceSmallerThanNode node inst
261 inst'' = inst' { Instance.disks = Instance.disks inst
262 , Instance.diskTemplate = dt }
263 in (Node.addPri node inst'' ==? Bad Types.FailSpindles)
264
265 -- | Check that adding a primary instance with too many VCPUs fails
266 -- with type FailCPU.
267 prop_addPriFC :: Property
268 prop_addPriFC =
269 forAll (choose (1, maxCpu)) $ \extra ->
270 forAll genMaybeExclStorNode $ \node ->
271 forAll (arbitrary `suchThat` Instance.notOffline
272 `suchThat` (not . Instance.forthcoming)) $ \inst ->
273 let inst' = setInstanceSmallerThanNode node inst
274 inst'' = inst' { Instance.vcpus = Node.availCpu node + extra }
275 in case Node.addPri node inst'' of
276 Bad Types.FailCPU -> passTest
277 v -> failTest $ "Expected OpFail FailCPU, but got " ++ show v
278
279 -- | Check that an instance add with too high memory or disk will be
280 -- rejected.
281 prop_addSec :: Node.Node -> Instance.Instance -> Int -> Property
282 prop_addSec node inst pdx =
283 ((Instance.mem inst >= (Node.fMem node - Node.rMem node) &&
284 not (Instance.isOffline inst)) ||
285 Instance.dsk inst >= Node.fDsk node ||
286 (Node.exclStorage node &&
287 hasInstTooManySpindles inst (Node.fSpindles node))) &&
288 not (Node.failN1 node) ==>
289 isBad (Node.addSec node inst pdx)
290
291 -- | Check that an offline instance with reasonable disk size but
292 -- extra mem/cpu can always be added.
293 prop_addOfflinePri :: NonNegative Int -> NonNegative Int -> Property
294 prop_addOfflinePri (NonNegative extra_mem) (NonNegative extra_cpu) =
295 forAll genMaybeExclStorNode $ \node ->
296 forAll (genInstanceSmallerThanNode node) $ \inst ->
297 let inst' = inst { Instance.runSt = Types.StatusOffline
298 , Instance.mem = Node.availMem node + extra_mem
299 , Instance.vcpus = Node.availCpu node + extra_cpu }
300 in case Node.addPriEx True node inst' of
301 Ok _ -> passTest
302 v -> failTest $ "Expected OpGood, but got: " ++ show v
303
304 -- | Check that an offline instance with reasonable disk size but
305 -- extra mem/cpu can always be added.
306 prop_addOfflineSec :: NonNegative Int -> NonNegative Int
307 -> Types.Ndx -> Property
308 prop_addOfflineSec (NonNegative extra_mem) (NonNegative extra_cpu) pdx =
309 forAll genMaybeExclStorNode $ \node ->
310 forAll (genInstanceSmallerThanNode node) $ \inst ->
311 let inst' = inst { Instance.runSt = Types.StatusOffline
312 , Instance.mem = Node.availMem node + extra_mem
313 , Instance.vcpus = Node.availCpu node + extra_cpu
314 , Instance.diskTemplate = Types.DTDrbd8 }
315 in case Node.addSec node inst' pdx of
316 Ok _ -> passTest
317 v -> failTest $ "Expected OpGood/OpGood, but got: " ++ show v
318
319 -- | Checks for memory reservation changes.
320 prop_rMem :: Instance.Instance -> Property
321 prop_rMem inst =
322 not (Instance.isOffline inst) && not (Instance.forthcoming inst) ==>
323 -- TODO Should we also require ((> Types.unitMem) . Node.fMemForth) ?
324 forAll (genMaybeExclStorNode `suchThat` ((> Types.unitMem) . Node.fMem)) $
325 \node ->
326 -- ab = auto_balance, nb = non-auto_balance
327 -- we use -1 as the primary node of the instance
328 let inst' = inst { Instance.pNode = -1, Instance.autoBalance = True
329 , Instance.diskTemplate = Types.DTDrbd8 }
330 inst_ab = setInstanceSmallerThanNode node inst'
331 inst_nb = inst_ab { Instance.autoBalance = False }
332 -- now we have the two instances, identical except the
333 -- autoBalance attribute
334 orig_rmem = Node.rMem node
335 inst_idx = Instance.idx inst_ab
336 node_add_ab = Node.addSec node inst_ab (-1)
337 node_add_nb = Node.addSec node inst_nb (-1)
338 node_del_ab = liftM (`Node.removeSec` inst_ab) node_add_ab
339 node_del_nb = liftM (`Node.removeSec` inst_nb) node_add_nb
340 in case (node_add_ab, node_add_nb, node_del_ab, node_del_nb) of
341 (Ok a_ab, Ok a_nb,
342 Ok d_ab, Ok d_nb) ->
343 counterexample "Consistency checks failed" $
344 Node.rMem a_ab > orig_rmem &&
345 Node.rMem a_ab - orig_rmem == Instance.mem inst_ab &&
346 Node.rMem a_nb == orig_rmem &&
347 Node.rMem d_ab == orig_rmem &&
348 Node.rMem d_nb == orig_rmem &&
349 -- this is not related to rMem, but as good a place to
350 -- test as any
351 inst_idx `elem` Node.sList a_ab &&
352 inst_idx `notElem` Node.sList d_ab
353 x -> failTest $ "Failed to add/remove instances: " ++ show x
354
355 -- | Check mdsk setting.
356 prop_setMdsk :: Node.Node -> SmallRatio -> Bool
357 prop_setMdsk node mx =
358 Node.loDsk node' >= 0 &&
359 fromIntegral (Node.loDsk node') <= Node.tDsk node &&
360 Node.availDisk node' >= 0 &&
361 Node.availDisk node' <= Node.fDsk node' &&
362 fromIntegral (Node.availDisk node') <= Node.tDsk node' &&
363 Node.mDsk node' == mx'
364 where node' = Node.setMdsk node mx'
365 SmallRatio mx' = mx
366
367 -- Check tag maps
368 prop_tagMaps_idempotent :: Property
369 prop_tagMaps_idempotent =
370 forAll genTags $ \tags ->
371 Node.delTags (Node.addTags m tags) tags ==? m
372 where m = Map.empty
373
374 prop_tagMaps_reject :: Property
375 prop_tagMaps_reject =
376 forAll (genTags `suchThat` (not . null)) $ \tags ->
377 let m = Node.addTags Map.empty tags
378 in all (\t -> Node.rejectAddTags m [t]) tags
379
380 prop_showField :: Node.Node -> Property
381 prop_showField node =
382 forAll (elements Node.defaultFields) $ \ field ->
383 fst (Node.showHeader field) /= Types.unknownField &&
384 Node.showField node field /= Types.unknownField
385
386 prop_computeGroups :: [Node.Node] -> Bool
387 prop_computeGroups nodes =
388 let ng = Node.computeGroups nodes
389 onlyuuid = map fst ng
390 in length nodes == sum (map (length . snd) ng) &&
391 all (\(guuid, ns) -> all ((== guuid) . Node.group) ns) ng &&
392 length (nub onlyuuid) == length onlyuuid &&
393 (null nodes || not (null ng))
394
395 -- Check idempotence of add/remove operations
396 prop_addPri_idempotent :: Property
397 prop_addPri_idempotent =
398 forAll genMaybeExclStorNode $ \node ->
399 forAll (genInstanceSmallerThanNode node) $ \inst ->
400 case Node.addPri node inst of
401 Ok node' -> Node.removePri node' inst ==? node
402 _ -> failTest "Can't add instance"
403
404 prop_addSec_idempotent :: Property
405 prop_addSec_idempotent =
406 forAll genMaybeExclStorNode $ \node ->
407 forAll (genInstanceSmallerThanNode node) $ \inst ->
408 let pdx = Node.idx node + 1
409 inst' = Instance.setPri inst pdx
410 inst'' = inst' { Instance.diskTemplate = Types.DTDrbd8 }
411 in case Node.addSec node inst'' pdx of
412 Ok node' -> Node.removeSec node' inst'' ==? node
413 _ -> failTest "Can't add instance"
414
415 -- | Check that no graph is created on an empty node list.
416 case_emptyNodeList :: Assertion
417 case_emptyNodeList =
418 assertEqual "" Nothing $ Node.mkNodeGraph emptynodes emptyinstances
419 where emptynodes = Container.empty :: Node.List
420 emptyinstances = Container.empty :: Instance.List
421
422 -- | Check that the number of vertices of a nodegraph is equal to the number of
423 -- nodes in the original node list.
424 prop_numVertices :: Property
425 prop_numVertices =
426 forAll genNodeGraph $ \(graph, nl, _) ->
427 (fmap numvertices graph ==? Just (Container.size nl))
428 where numvertices = length . Graph.vertices
429
430 -- | Check that the number of edges of a nodegraph is equal to twice the number
431 -- of instances with secondary nodes in the original instance list.
432 prop_numEdges :: Property
433 prop_numEdges =
434 forAll genNodeGraph $ \(graph, _, il) ->
435 (fmap numedges graph ==? Just (numwithsec il * 2))
436 where numedges = length . Graph.edges
437 numwithsec = length . filter Instance.hasSecondary . Container.elems
438
439 -- | Check that a node graph is colorable.
440 prop_nodeGraphIsColorable :: Property
441 prop_nodeGraphIsColorable =
442 forAll genNodeGraph $ \(graph, _, _) ->
443 fmap HGraph.isColorable graph ==? Just True
444
445 -- | Check that each edge in a nodegraph is an instance.
446 prop_instanceIsEdge :: Property
447 prop_instanceIsEdge =
448 forAll genNodeGraph $ \(graph, _, il) ->
449 fmap (\g -> all (`isEdgeOn` g) (iwithsec il)) graph ==? Just True
450 where i `isEdgeOn` g = iEdges i `intersect` Graph.edges g == iEdges i
451 iEdges i = [ (Instance.pNode i, Instance.sNode i)
452 , (Instance.sNode i, Instance.pNode i)]
453 iwithsec = filter Instance.hasSecondary . Container.elems
454
455 -- | Check that each instance in an edge in the resulting nodegraph.
456 prop_edgeIsInstance :: Property
457 prop_edgeIsInstance =
458 forAll genNodeGraph $ \(graph, _, il) ->
459 fmap (all (`isInstanceIn` il).Graph.edges) graph ==? Just True
460 where e `isInstanceIn` il = any (`hasNodes` e) (Container.elems il)
461 i `hasNodes` (v1,v2) =
462 Instance.allNodes i `elem` permutations [v1,v2]
463
464 -- | List of tests for the Node module.
465 testSuite "HTools/Node"
466 [ 'prop_setAlias
467 , 'prop_setOffline
468 , 'prop_setMcpu
469 , 'prop_addPriFM
470 , 'prop_addPriFD
471 , 'prop_addPriFS
472 , 'prop_addPriFC
473 , 'prop_addPri_NoN1Fail
474 , 'prop_addSec
475 , 'prop_addOfflinePri
476 , 'prop_addOfflineSec
477 , 'prop_rMem
478 , 'prop_setMdsk
479 , 'prop_tagMaps_idempotent
480 , 'prop_tagMaps_reject
481 , 'prop_showField
482 , 'prop_computeGroups
483 , 'prop_addPri_idempotent
484 , 'prop_addSec_idempotent
485 , 'case_emptyNodeList
486 , 'prop_numVertices
487 , 'prop_numEdges
488 , 'prop_nodeGraphIsColorable
489 , 'prop_edgeIsInstance
490 , 'prop_instanceIsEdge
491 ]