QC.hs 48.3 KB
Newer Older
1
2
{-# LANGUAGE TemplateHaskell #-}

3
{-| Unittests for ganeti-htools.
Iustin Pop's avatar
Iustin Pop committed
4
5
6
7
8

-}

{-

Iustin Pop's avatar
Iustin Pop committed
9
Copyright (C) 2009, 2010, 2011, 2012 Google Inc.
Iustin Pop's avatar
Iustin Pop committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.

-}

28
module Ganeti.HTools.QC
Iustin Pop's avatar
Iustin Pop committed
29
30
31
32
33
34
35
36
37
38
39
40
  ( testUtils
  , testPeerMap
  , testContainer
  , testInstance
  , testNode
  , testText
  , testOpCodes
  , testJobs
  , testCluster
  , testLoader
  , testTypes
  ) where
41
42

import Test.QuickCheck
Iustin Pop's avatar
Iustin Pop committed
43
import Data.List (findIndex, intercalate, nub, isPrefixOf)
44
import Data.Maybe
Iustin Pop's avatar
Iustin Pop committed
45
46
import Control.Monad
import qualified Text.JSON as J
Iustin Pop's avatar
Iustin Pop committed
47
import qualified Data.Map
48
import qualified Data.IntMap as IntMap
Iustin Pop's avatar
Iustin Pop committed
49
import qualified Ganeti.OpCodes as OpCodes
50
import qualified Ganeti.Jobs as Jobs
Iustin Pop's avatar
Iustin Pop committed
51
import qualified Ganeti.Luxi
52
53
54
import qualified Ganeti.HTools.CLI as CLI
import qualified Ganeti.HTools.Cluster as Cluster
import qualified Ganeti.HTools.Container as Container
Iustin Pop's avatar
Iustin Pop committed
55
import qualified Ganeti.HTools.ExtLoader
56
57
import qualified Ganeti.HTools.IAlloc as IAlloc
import qualified Ganeti.HTools.Instance as Instance
58
import qualified Ganeti.HTools.JSON as JSON
59
import qualified Ganeti.HTools.Loader as Loader
Iustin Pop's avatar
Iustin Pop committed
60
import qualified Ganeti.HTools.Luxi
61
import qualified Ganeti.HTools.Node as Node
Iustin Pop's avatar
Iustin Pop committed
62
import qualified Ganeti.HTools.Group as Group
63
import qualified Ganeti.HTools.PeerMap as PeerMap
64
import qualified Ganeti.HTools.Rapi
Iustin Pop's avatar
Iustin Pop committed
65
import qualified Ganeti.HTools.Simu
66
67
68
import qualified Ganeti.HTools.Text as Text
import qualified Ganeti.HTools.Types as Types
import qualified Ganeti.HTools.Utils as Utils
Iustin Pop's avatar
Iustin Pop committed
69
import qualified Ganeti.HTools.Version
70
import qualified Ganeti.Constants as C
71

72
73
74
75
76
import qualified Ganeti.HTools.Program.Hail
import qualified Ganeti.HTools.Program.Hbal
import qualified Ganeti.HTools.Program.Hscan
import qualified Ganeti.HTools.Program.Hspace

77
import Ganeti.HTools.QCHelper (testSuite)
Iustin Pop's avatar
Iustin Pop committed
78

79
80
-- * Constants

81
-- | Maximum memory (1TiB, somewhat random value).
Iustin Pop's avatar
Iustin Pop committed
82
83
84
maxMem :: Int
maxMem = 1024 * 1024

85
-- | Maximum disk (8TiB, somewhat random value).
Iustin Pop's avatar
Iustin Pop committed
86
maxDsk :: Int
Iustin Pop's avatar
Iustin Pop committed
87
maxDsk = 1024 * 1024 * 8
Iustin Pop's avatar
Iustin Pop committed
88

89
-- | Max CPUs (1024, somewhat random value).
Iustin Pop's avatar
Iustin Pop committed
90
91
92
maxCpu :: Int
maxCpu = 1024

93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
-- | Null iPolicy, and by null we mean very liberal.
nullIPolicy = Types.IPolicy
  { Types.iPolicyMinSpec = Types.ISpec { Types.iSpecMemorySize = 0
                                       , Types.iSpecCpuCount   = 0
                                       , Types.iSpecDiskSize   = 0
                                       , Types.iSpecDiskCount  = 0
                                       , Types.iSpecNicCount   = 0
                                       }
  , Types.iPolicyMaxSpec = Types.ISpec { Types.iSpecMemorySize = maxBound
                                       , Types.iSpecCpuCount   = maxBound
                                       , Types.iSpecDiskSize   = maxBound
                                       , Types.iSpecDiskCount  = C.maxDisks
                                       , Types.iSpecNicCount   = C.maxNics
                                       }
  , Types.iPolicyStdSpec = Types.ISpec { Types.iSpecMemorySize = Types.unitMem
                                       , Types.iSpecCpuCount   = Types.unitCpu
                                       , Types.iSpecDiskSize   = Types.unitDsk
                                       , Types.iSpecDiskCount  = 1
                                       , Types.iSpecNicCount   = 1
                                       }
  , Types.iPolicyDiskTemplates = [Types.DTDrbd8, Types.DTPlain]
  }


Iustin Pop's avatar
Iustin Pop committed
117
118
defGroup :: Group.Group
defGroup = flip Group.setIdx 0 $
Iustin Pop's avatar
Iustin Pop committed
119
             Group.create "default" Types.defaultGroupID Types.AllocPreferred
120
                  nullIPolicy
Iustin Pop's avatar
Iustin Pop committed
121
122

defGroupList :: Group.List
Iustin Pop's avatar
Iustin Pop committed
123
defGroupList = Container.fromList [(Group.idx defGroup, defGroup)]
Iustin Pop's avatar
Iustin Pop committed
124
125
126
127

defGroupAssoc :: Data.Map.Map String Types.Gdx
defGroupAssoc = Data.Map.singleton (Group.uuid defGroup) (Group.idx defGroup)

128
129
-- * Helper functions

130
-- | Simple checker for whether OpResult is fail or pass.
Iustin Pop's avatar
Iustin Pop committed
131
132
133
134
isFailure :: Types.OpResult a -> Bool
isFailure (Types.OpFail _) = True
isFailure _ = False

Iustin Pop's avatar
Iustin Pop committed
135
136
137
138
139
140
141
-- | Checks for equality with proper annotation.
(==?) :: (Show a, Eq a) => a -> a -> Property
(==?) x y = printTestCase
            ("Expected equality, but '" ++
             show x ++ "' /= '" ++ show y ++ "'") (x == y)
infix 3 ==?

142
-- | Update an instance to be smaller than a node.
143
setInstanceSmallerThanNode node inst =
Iustin Pop's avatar
Iustin Pop committed
144
145
146
147
  inst { Instance.mem = Node.availMem node `div` 2
       , Instance.dsk = Node.availDisk node `div` 2
       , Instance.vcpus = Node.availCpu node `div` 2
       }
148

149
150
151
152
153
154
-- | Check if an instance is smaller than a node.
isInstanceSmallerThanNode node inst =
  Instance.mem inst   <= Node.availMem node `div` 2 &&
  Instance.dsk inst   <= Node.availDisk node `div` 2 &&
  Instance.vcpus inst <= Node.availCpu node `div` 2

155
-- | Create an instance given its spec.
156
createInstance mem dsk vcpus =
Iustin Pop's avatar
Iustin Pop committed
157
158
  Instance.create "inst-unnamed" mem dsk vcpus Types.Running [] True (-1) (-1)
    Types.DTDrbd8
159

160
-- | Create a small cluster by repeating a node spec.
161
162
makeSmallCluster :: Node.Node -> Int -> Node.List
makeSmallCluster node count =
163
164
165
166
167
168
169
  let origname = Node.name node
      origalias = Node.alias node
      nodes = map (\idx -> node { Node.name = origname ++ "-" ++ show idx
                                , Node.alias = origalias ++ "-" ++ show idx })
              [1..count]
      fn = flip Node.buildPeers Container.empty
      namelst = map (\n -> (Node.name n, fn n)) nodes
Iustin Pop's avatar
Iustin Pop committed
170
171
      (_, nlst) = Loader.assignIndices namelst
  in nlst
172

Iustin Pop's avatar
Iustin Pop committed
173
174
175
176
177
178
179
-- | Make a small cluster, both nodes and instances.
makeSmallEmptyCluster :: Node.Node -> Int -> Instance.Instance
                      -> (Node.List, Instance.List, Instance.Instance)
makeSmallEmptyCluster node count inst =
  (makeSmallCluster node count, Container.empty,
   setInstanceSmallerThanNode node inst)

180
-- | Checks if a node is "big" enough.
181
182
isNodeBig :: Int -> Node.Node -> Bool
isNodeBig size node = Node.availDisk node > size * Types.unitDsk
183
184
185
                      && Node.availMem node > size * Types.unitMem
                      && Node.availCpu node > size * Types.unitCpu

186
187
canBalance :: Cluster.Table -> Bool -> Bool -> Bool -> Bool
canBalance tbl dm im evac = isJust $ Cluster.tryBalance tbl dm im evac 0 0
188

189
-- | Assigns a new fresh instance to a cluster; this is not
190
-- allocation, so no resource checks are done.
191
192
193
194
195
196
197
assignInstance :: Node.List -> Instance.List -> Instance.Instance ->
                  Types.Idx -> Types.Idx ->
                  (Node.List, Instance.List)
assignInstance nl il inst pdx sdx =
  let pnode = Container.find pdx nl
      snode = Container.find sdx nl
      maxiidx = if Container.null il
Iustin Pop's avatar
Iustin Pop committed
198
199
                  then 0
                  else fst (Container.findMax il) + 1
200
201
202
203
204
205
206
207
      inst' = inst { Instance.idx = maxiidx,
                     Instance.pNode = pdx, Instance.sNode = sdx }
      pnode' = Node.setPri pnode inst'
      snode' = Node.setSec snode inst'
      nl' = Container.addTwo pdx pnode' sdx snode' nl
      il' = Container.add maxiidx inst' il
  in (nl', il')

208
209
-- * Arbitrary instances

210
-- | Defines a DNS name.
211
newtype DNSChar = DNSChar { dnsGetChar::Char }
212

213
instance Arbitrary DNSChar where
Iustin Pop's avatar
Iustin Pop committed
214
215
216
  arbitrary = do
    x <- elements (['a'..'z'] ++ ['0'..'9'] ++ "_-")
    return (DNSChar x)
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231

getName :: Gen String
getName = do
  n <- choose (1, 64)
  dn <- vector n::Gen [DNSChar]
  return (map dnsGetChar dn)

getFQDN :: Gen String
getFQDN = do
  felem <- getName
  ncomps <- choose (1, 4)
  frest <- vector ncomps::Gen [[DNSChar]]
  let frest' = map (map dnsGetChar) frest
  return (felem ++ "." ++ intercalate "." frest')

232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
-- | Defines a tag type.
newtype TagChar = TagChar { tagGetChar :: Char }

-- | All valid tag chars. This doesn't need to match _exactly_
-- Ganeti's own tag regex, just enough for it to be close.
tagChar :: [Char]
tagChar = ['a'..'z'] ++ ['A'..'Z'] ++ ['0'..'9'] ++ ".+*/:@-"

instance Arbitrary TagChar where
  arbitrary = do
    c <- elements tagChar
    return (TagChar c)

-- | Generates a tag
genTag :: Gen [TagChar]
genTag = do
  -- the correct value would be C.maxTagLen, but that's way too
  -- verbose in unittests, and at the moment I don't see any possible
  -- bugs with longer tags and the way we use tags in htools
  n <- choose (1, 10)
  vector n

-- | Generates a list of tags (correctly upper bounded).
genTags :: Gen [String]
genTags = do
  -- the correct value would be C.maxTagsPerObj, but per the comment
  -- in genTag, we don't use tags enough in htools to warrant testing
  -- such big values
  n <- choose (0, 10::Int)
  tags <- mapM (const genTag) [1..n]
  return $ map (map tagGetChar) tags

264
instance Arbitrary Types.InstanceStatus where
265
    arbitrary = elements [minBound..maxBound]
266

267
268
-- let's generate a random instance
instance Arbitrary Instance.Instance where
Iustin Pop's avatar
Iustin Pop committed
269
270
271
272
273
274
275
276
277
278
  arbitrary = do
    name <- getFQDN
    mem <- choose (0, maxMem)
    dsk <- choose (0, maxDsk)
    run_st <- arbitrary
    pn <- arbitrary
    sn <- arbitrary
    vcpus <- choose (0, maxCpu)
    return $ Instance.create name mem dsk vcpus run_st [] True pn sn
              Types.DTDrbd8
279

280
281
282
283
284
-- | Generas an arbitrary node based on sizing information.
genNode :: Maybe Int -- ^ Minimum node size in terms of units
        -> Maybe Int -- ^ Maximum node size (when Nothing, bounded
                     -- just by the max... constants)
        -> Gen Node.Node
285
286
genNode min_multiplier max_multiplier = do
  let (base_mem, base_dsk, base_cpu) =
Iustin Pop's avatar
Iustin Pop committed
287
288
289
290
291
        case min_multiplier of
          Just mm -> (mm * Types.unitMem,
                      mm * Types.unitDsk,
                      mm * Types.unitCpu)
          Nothing -> (0, 0, 0)
292
      (top_mem, top_dsk, top_cpu)  =
Iustin Pop's avatar
Iustin Pop committed
293
294
295
296
297
        case max_multiplier of
          Just mm -> (mm * Types.unitMem,
                      mm * Types.unitDsk,
                      mm * Types.unitCpu)
          Nothing -> (maxMem, maxDsk, maxCpu)
298
299
300
301
302
303
304
305
306
307
  name  <- getFQDN
  mem_t <- choose (base_mem, top_mem)
  mem_f <- choose (base_mem, mem_t)
  mem_n <- choose (0, mem_t - mem_f)
  dsk_t <- choose (base_dsk, top_dsk)
  dsk_f <- choose (base_dsk, dsk_t)
  cpu_t <- choose (base_cpu, top_cpu)
  offl  <- arbitrary
  let n = Node.create name (fromIntegral mem_t) mem_n mem_f
          (fromIntegral dsk_t) dsk_f (fromIntegral cpu_t) offl 0
Iustin Pop's avatar
Iustin Pop committed
308
309
      n' = Node.setPolicy nullIPolicy n
  return $ Node.buildPeers n' Container.empty
310

311
312
313
314
315
316
317
318
319
-- | Helper function to generate a sane node.
genOnlineNode :: Gen Node.Node
genOnlineNode = do
  arbitrary `suchThat` (\n -> not (Node.offline n) &&
                              not (Node.failN1 n) &&
                              Node.availDisk n > 0 &&
                              Node.availMem n > 0 &&
                              Node.availCpu n > 0)

320
321
-- and a random node
instance Arbitrary Node.Node where
Iustin Pop's avatar
Iustin Pop committed
322
  arbitrary = genNode Nothing Nothing
323

Iustin Pop's avatar
Iustin Pop committed
324
325
-- replace disks
instance Arbitrary OpCodes.ReplaceDisksMode where
326
  arbitrary = elements [minBound..maxBound]
Iustin Pop's avatar
Iustin Pop committed
327
328
329
330
331
332
333
334

instance Arbitrary OpCodes.OpCode where
  arbitrary = do
    op_id <- elements [ "OP_TEST_DELAY"
                      , "OP_INSTANCE_REPLACE_DISKS"
                      , "OP_INSTANCE_FAILOVER"
                      , "OP_INSTANCE_MIGRATE"
                      ]
Iustin Pop's avatar
Iustin Pop committed
335
336
337
338
339
340
341
342
343
344
345
346
347
    case op_id of
      "OP_TEST_DELAY" ->
        liftM3 OpCodes.OpTestDelay arbitrary arbitrary arbitrary
      "OP_INSTANCE_REPLACE_DISKS" ->
        liftM5 OpCodes.OpInstanceReplaceDisks arbitrary arbitrary
          arbitrary arbitrary arbitrary
      "OP_INSTANCE_FAILOVER" ->
        liftM3 OpCodes.OpInstanceFailover arbitrary arbitrary
          arbitrary
      "OP_INSTANCE_MIGRATE" ->
        liftM5 OpCodes.OpInstanceMigrate arbitrary arbitrary
          arbitrary arbitrary arbitrary
      _ -> fail "Wrong opcode"
Iustin Pop's avatar
Iustin Pop committed
348

349
350
351
352
353
354
instance Arbitrary Jobs.OpStatus where
  arbitrary = elements [minBound..maxBound]

instance Arbitrary Jobs.JobStatus where
  arbitrary = elements [minBound..maxBound]

355
356
newtype SmallRatio = SmallRatio Double deriving Show
instance Arbitrary SmallRatio where
Iustin Pop's avatar
Iustin Pop committed
357
358
359
  arbitrary = do
    v <- choose (0, 1)
    return $ SmallRatio v
360

361
362
363
364
365
366
instance Arbitrary Types.AllocPolicy where
  arbitrary = elements [minBound..maxBound]

instance Arbitrary Types.DiskTemplate where
  arbitrary = elements [minBound..maxBound]

367
instance Arbitrary Types.FailMode where
Iustin Pop's avatar
Iustin Pop committed
368
  arbitrary = elements [minBound..maxBound]
369

370
371
372
instance Arbitrary Types.EvacMode where
  arbitrary = elements [minBound..maxBound]

373
instance Arbitrary a => Arbitrary (Types.OpResult a) where
Iustin Pop's avatar
Iustin Pop committed
374
  arbitrary = arbitrary >>= \c ->
Iustin Pop's avatar
Iustin Pop committed
375
376
377
              if c
                then liftM Types.OpGood arbitrary
                else liftM Types.OpFail arbitrary
378

379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
instance Arbitrary Types.ISpec where
  arbitrary = do
    mem <- arbitrary::Gen (NonNegative Int)
    dsk_c <- arbitrary::Gen (NonNegative Int)
    dsk_s <- arbitrary::Gen (NonNegative Int)
    cpu <- arbitrary::Gen (NonNegative Int)
    nic <- arbitrary::Gen (NonNegative Int)
    return Types.ISpec { Types.iSpecMemorySize = fromIntegral mem
                       , Types.iSpecCpuCount   = fromIntegral cpu
                       , Types.iSpecDiskSize   = fromIntegral dsk_s
                       , Types.iSpecDiskCount  = fromIntegral dsk_c
                       , Types.iSpecNicCount   = fromIntegral nic
                       }

-- | Helper function to check whether a spec is LTE than another
iSpecSmaller :: Types.ISpec -> Types.ISpec -> Bool
iSpecSmaller imin imax =
  Types.iSpecMemorySize imin <= Types.iSpecMemorySize imax &&
  Types.iSpecCpuCount imin   <= Types.iSpecCpuCount imax &&
  Types.iSpecDiskSize imin   <= Types.iSpecDiskSize imax &&
  Types.iSpecDiskCount imin  <= Types.iSpecDiskCount imax &&
  Types.iSpecNicCount imin   <= Types.iSpecNicCount imax

instance Arbitrary Types.IPolicy where
  arbitrary = do
    imin <- arbitrary
    istd <- arbitrary `suchThat` (iSpecSmaller imin)
    imax <- arbitrary `suchThat` (iSpecSmaller istd)
    dts  <- arbitrary
    return Types.IPolicy { Types.iPolicyMinSpec = imin
                         , Types.iPolicyStdSpec = istd
                         , Types.iPolicyMaxSpec = imax
                         , Types.iPolicyDiskTemplates = dts
                         }

414
-- * Actual tests
Iustin Pop's avatar
Iustin Pop committed
415

416
417
418
419
-- ** Utils tests

-- | If the list is not just an empty element, and if the elements do
-- not contain commas, then join+split should be idempotent.
Iustin Pop's avatar
Iustin Pop committed
420
prop_Utils_commaJoinSplit =
Iustin Pop's avatar
Iustin Pop committed
421
  forAll (arbitrary `suchThat`
Iustin Pop's avatar
Iustin Pop committed
422
          (\l -> l /= [""] && all (notElem ',') l )) $ \lst ->
Iustin Pop's avatar
Iustin Pop committed
423
  Utils.sepSplit ',' (Utils.commaJoin lst) ==? lst
Iustin Pop's avatar
Iustin Pop committed
424

425
-- | Split and join should always be idempotent.
Iustin Pop's avatar
Iustin Pop committed
426
prop_Utils_commaSplitJoin s =
Iustin Pop's avatar
Iustin Pop committed
427
  Utils.commaJoin (Utils.sepSplit ',' s) ==? s
428

429
-- | fromObjWithDefault, we test using the Maybe monad and an integer
430
-- value.
431
prop_Utils_fromObjWithDefault def_value random_key =
Iustin Pop's avatar
Iustin Pop committed
432
  -- a missing key will be returned with the default
433
  JSON.fromObjWithDefault [] random_key def_value == Just def_value &&
Iustin Pop's avatar
Iustin Pop committed
434
  -- a found key will be returned as is, not with default
435
  JSON.fromObjWithDefault [(random_key, J.showJSON def_value)]
Iustin Pop's avatar
Iustin Pop committed
436
437
       random_key (def_value+1) == Just def_value
    where _types = def_value :: Integer
438

439
-- | Test that functional if' behaves like the syntactic sugar if.
Iustin Pop's avatar
Iustin Pop committed
440
441
prop_Utils_if'if :: Bool -> Int -> Int -> Gen Prop
prop_Utils_if'if cnd a b =
Iustin Pop's avatar
Iustin Pop committed
442
  Utils.if' cnd a b ==? if cnd then a else b
443

Guido Trotter's avatar
Guido Trotter committed
444
-- | Test basic select functionality
Iustin Pop's avatar
Iustin Pop committed
445
446
447
448
prop_Utils_select :: Int      -- ^ Default result
                  -> [Int]    -- ^ List of False values
                  -> [Int]    -- ^ List of True values
                  -> Gen Prop -- ^ Test result
Guido Trotter's avatar
Guido Trotter committed
449
prop_Utils_select def lst1 lst2 =
Iustin Pop's avatar
Iustin Pop committed
450
  Utils.select def (flist ++ tlist) ==? expectedresult
Iustin Pop's avatar
Iustin Pop committed
451
452
453
    where expectedresult = Utils.if' (null lst2) def (head lst2)
          flist = zip (repeat False) lst1
          tlist = zip (repeat True)  lst2
Guido Trotter's avatar
Guido Trotter committed
454
455

-- | Test basic select functionality with undefined default
Iustin Pop's avatar
Iustin Pop committed
456
prop_Utils_select_undefd :: [Int]            -- ^ List of False values
Guido Trotter's avatar
Guido Trotter committed
457
                         -> NonEmptyList Int -- ^ List of True values
Iustin Pop's avatar
Iustin Pop committed
458
                         -> Gen Prop         -- ^ Test result
Guido Trotter's avatar
Guido Trotter committed
459
prop_Utils_select_undefd lst1 (NonEmpty lst2) =
Iustin Pop's avatar
Iustin Pop committed
460
  Utils.select undefined (flist ++ tlist) ==? head lst2
Iustin Pop's avatar
Iustin Pop committed
461
462
    where flist = zip (repeat False) lst1
          tlist = zip (repeat True)  lst2
Guido Trotter's avatar
Guido Trotter committed
463
464

-- | Test basic select functionality with undefined list values
Iustin Pop's avatar
Iustin Pop committed
465
prop_Utils_select_undefv :: [Int]            -- ^ List of False values
Guido Trotter's avatar
Guido Trotter committed
466
                         -> NonEmptyList Int -- ^ List of True values
Iustin Pop's avatar
Iustin Pop committed
467
                         -> Gen Prop         -- ^ Test result
Guido Trotter's avatar
Guido Trotter committed
468
prop_Utils_select_undefv lst1 (NonEmpty lst2) =
Iustin Pop's avatar
Iustin Pop committed
469
  Utils.select undefined cndlist ==? head lst2
Iustin Pop's avatar
Iustin Pop committed
470
471
472
    where flist = zip (repeat False) lst1
          tlist = zip (repeat True)  lst2
          cndlist = flist ++ tlist ++ [undefined]
473

Iustin Pop's avatar
Iustin Pop committed
474
prop_Utils_parseUnit (NonNegative n) =
Iustin Pop's avatar
Iustin Pop committed
475
476
477
478
479
480
481
482
483
484
  Utils.parseUnit (show n) == Types.Ok n &&
  Utils.parseUnit (show n ++ "m") == Types.Ok n &&
  (case Utils.parseUnit (show n ++ "M") of
     Types.Ok m -> if n > 0
                     then m < n  -- for positive values, X MB is < than X MiB
                     else m == 0 -- but for 0, 0 MB == 0 MiB
     Types.Bad _ -> False) &&
  Utils.parseUnit (show n ++ "g") == Types.Ok (n*1024) &&
  Utils.parseUnit (show n ++ "t") == Types.Ok (n*1048576) &&
  Types.isBad (Utils.parseUnit (show n ++ "x")::Types.Result Int)
Iustin Pop's avatar
Iustin Pop committed
485
    where _types = n::Int
Iustin Pop's avatar
Iustin Pop committed
486

487
-- | Test list for the Utils module.
488
testSuite "Utils"
Iustin Pop's avatar
Iustin Pop committed
489
490
491
492
493
494
495
496
497
            [ 'prop_Utils_commaJoinSplit
            , 'prop_Utils_commaSplitJoin
            , 'prop_Utils_fromObjWithDefault
            , 'prop_Utils_if'if
            , 'prop_Utils_select
            , 'prop_Utils_select_undefd
            , 'prop_Utils_select_undefv
            , 'prop_Utils_parseUnit
            ]
498

499
500
501
-- ** PeerMap tests

-- | Make sure add is idempotent.
Iustin Pop's avatar
Iustin Pop committed
502
prop_PeerMap_addIdempotent pmap key em =
Iustin Pop's avatar
Iustin Pop committed
503
  fn puniq ==? fn (fn puniq)
504
    where _types = (pmap::PeerMap.PeerMap,
Iustin Pop's avatar
Iustin Pop committed
505
506
                    key::PeerMap.Key, em::PeerMap.Elem)
          fn = PeerMap.add key em
507
          puniq = PeerMap.accumArray const pmap
508

509
-- | Make sure remove is idempotent.
510
prop_PeerMap_removeIdempotent pmap key =
Iustin Pop's avatar
Iustin Pop committed
511
  fn puniq ==? fn (fn puniq)
512
513
    where _types = (pmap::PeerMap.PeerMap, key::PeerMap.Key)
          fn = PeerMap.remove key
514
515
          puniq = PeerMap.accumArray const pmap

516
-- | Make sure a missing item returns 0.
517
prop_PeerMap_findMissing pmap key =
Iustin Pop's avatar
Iustin Pop committed
518
  PeerMap.find key (PeerMap.remove key puniq) ==? 0
519
    where _types = (pmap::PeerMap.PeerMap, key::PeerMap.Key)
520
521
          puniq = PeerMap.accumArray const pmap

522
-- | Make sure an added item is found.
Iustin Pop's avatar
Iustin Pop committed
523
prop_PeerMap_addFind pmap key em =
Iustin Pop's avatar
Iustin Pop committed
524
  PeerMap.find key (PeerMap.add key em puniq) ==? em
525
    where _types = (pmap::PeerMap.PeerMap,
Iustin Pop's avatar
Iustin Pop committed
526
                    key::PeerMap.Key, em::PeerMap.Elem)
527
          puniq = PeerMap.accumArray const pmap
528

529
-- | Manual check that maxElem returns the maximum indeed, or 0 for null.
530
prop_PeerMap_maxElem pmap =
Iustin Pop's avatar
Iustin Pop committed
531
  PeerMap.maxElem puniq ==? if null puniq then 0
Iustin Pop's avatar
Iustin Pop committed
532
                              else (maximum . snd . unzip) puniq
533
    where _types = pmap::PeerMap.PeerMap
534
535
          puniq = PeerMap.accumArray const pmap

536
-- | List of tests for the PeerMap module.
537
testSuite "PeerMap"
Iustin Pop's avatar
Iustin Pop committed
538
539
540
541
542
543
            [ 'prop_PeerMap_addIdempotent
            , 'prop_PeerMap_removeIdempotent
            , 'prop_PeerMap_maxElem
            , 'prop_PeerMap_addFind
            , 'prop_PeerMap_findMissing
            ]
544

545
-- ** Container tests
546

Iustin Pop's avatar
Iustin Pop committed
547
548
-- we silence the following due to hlint bug fixed in later versions
{-# ANN prop_Container_addTwo "HLint: ignore Avoid lambda" #-}
549
prop_Container_addTwo cdata i1 i2 =
Iustin Pop's avatar
Iustin Pop committed
550
551
  fn i1 i2 cont == fn i2 i1 cont &&
  fn i1 i2 cont == fn i1 i2 (fn i1 i2 cont)
552
553
554
555
556
    where _types = (cdata::[Int],
                    i1::Int, i2::Int)
          cont = foldl (\c x -> Container.add x x c) Container.empty cdata
          fn x1 x2 = Container.addTwo x1 x1 x2 x2

557
558
559
prop_Container_nameOf node =
  let nl = makeSmallCluster node 1
      fnode = head (Container.elems nl)
Iustin Pop's avatar
Iustin Pop committed
560
  in Container.nameOf nl (Node.idx fnode) ==? Node.name fnode
561

562
-- | We test that in a cluster, given a random node, we can find it by
563
-- its name and alias, as long as all names and aliases are unique,
564
-- and that we fail to find a non-existing name.
565
566
567
568
569
570
prop_Container_findByName node othername =
  forAll (choose (1, 20)) $ \ cnt ->
  forAll (choose (0, cnt - 1)) $ \ fidx ->
  forAll (vector cnt) $ \ names ->
  (length . nub) (map fst names ++ map snd names) ==
  length names * 2 &&
Iustin Pop's avatar
Iustin Pop committed
571
  othername `notElem` (map fst names ++ map snd names) ==>
572
573
574
575
576
577
  let nl = makeSmallCluster node cnt
      nodes = Container.elems nl
      nodes' = map (\((name, alias), nn) -> (Node.idx nn,
                                             nn { Node.name = name,
                                                  Node.alias = alias }))
               $ zip names nodes
Iustin Pop's avatar
Iustin Pop committed
578
      nl' = Container.fromList nodes'
579
580
581
      target = snd (nodes' !! fidx)
  in Container.findByName nl' (Node.name target) == Just target &&
     Container.findByName nl' (Node.alias target) == Just target &&
Iustin Pop's avatar
Iustin Pop committed
582
     isNothing (Container.findByName nl' othername)
583

584
testSuite "Container"
Iustin Pop's avatar
Iustin Pop committed
585
586
587
588
            [ 'prop_Container_addTwo
            , 'prop_Container_nameOf
            , 'prop_Container_findByName
            ]
589

590
591
-- ** Instance tests

592
593
-- Simple instance tests, we only have setter/getters

Iustin Pop's avatar
Iustin Pop committed
594
prop_Instance_creat inst =
Iustin Pop's avatar
Iustin Pop committed
595
  Instance.name inst ==? Instance.alias inst
Iustin Pop's avatar
Iustin Pop committed
596

597
prop_Instance_setIdx inst idx =
Iustin Pop's avatar
Iustin Pop committed
598
  Instance.idx (Instance.setIdx inst idx) ==? idx
599
600
601
    where _types = (inst::Instance.Instance, idx::Types.Idx)

prop_Instance_setName inst name =
Iustin Pop's avatar
Iustin Pop committed
602
603
  Instance.name newinst == name &&
  Instance.alias newinst == name
Iustin Pop's avatar
Iustin Pop committed
604
605
606
607
    where _types = (inst::Instance.Instance, name::String)
          newinst = Instance.setName inst name

prop_Instance_setAlias inst name =
Iustin Pop's avatar
Iustin Pop committed
608
609
  Instance.name newinst == Instance.name inst &&
  Instance.alias newinst == name
610
    where _types = (inst::Instance.Instance, name::String)
Iustin Pop's avatar
Iustin Pop committed
611
          newinst = Instance.setAlias inst name
612
613

prop_Instance_setPri inst pdx =
Iustin Pop's avatar
Iustin Pop committed
614
  Instance.pNode (Instance.setPri inst pdx) ==? pdx
615
616
617
    where _types = (inst::Instance.Instance, pdx::Types.Ndx)

prop_Instance_setSec inst sdx =
Iustin Pop's avatar
Iustin Pop committed
618
  Instance.sNode (Instance.setSec inst sdx) ==? sdx
619
620
621
    where _types = (inst::Instance.Instance, sdx::Types.Ndx)

prop_Instance_setBoth inst pdx sdx =
Iustin Pop's avatar
Iustin Pop committed
622
  Instance.pNode si == pdx && Instance.sNode si == sdx
623
624
625
    where _types = (inst::Instance.Instance, pdx::Types.Ndx, sdx::Types.Ndx)
          si = Instance.setBoth inst pdx sdx

Iustin Pop's avatar
Iustin Pop committed
626
prop_Instance_shrinkMG inst =
Iustin Pop's avatar
Iustin Pop committed
627
628
629
630
  Instance.mem inst >= 2 * Types.unitMem ==>
    case Instance.shrinkByType inst Types.FailMem of
      Types.Ok inst' -> Instance.mem inst' == Instance.mem inst - Types.unitMem
      _ -> False
Iustin Pop's avatar
Iustin Pop committed
631
632

prop_Instance_shrinkMF inst =
Iustin Pop's avatar
Iustin Pop committed
633
  forAll (choose (0, 2 * Types.unitMem - 1)) $ \mem ->
Iustin Pop's avatar
Iustin Pop committed
634
635
    let inst' = inst { Instance.mem = mem}
    in Types.isBad $ Instance.shrinkByType inst' Types.FailMem
Iustin Pop's avatar
Iustin Pop committed
636
637

prop_Instance_shrinkCG inst =
Iustin Pop's avatar
Iustin Pop committed
638
639
640
641
642
  Instance.vcpus inst >= 2 * Types.unitCpu ==>
    case Instance.shrinkByType inst Types.FailCPU of
      Types.Ok inst' ->
        Instance.vcpus inst' == Instance.vcpus inst - Types.unitCpu
      _ -> False
Iustin Pop's avatar
Iustin Pop committed
643
644

prop_Instance_shrinkCF inst =
Iustin Pop's avatar
Iustin Pop committed
645
  forAll (choose (0, 2 * Types.unitCpu - 1)) $ \vcpus ->
Iustin Pop's avatar
Iustin Pop committed
646
647
    let inst' = inst { Instance.vcpus = vcpus }
    in Types.isBad $ Instance.shrinkByType inst' Types.FailCPU
Iustin Pop's avatar
Iustin Pop committed
648
649

prop_Instance_shrinkDG inst =
Iustin Pop's avatar
Iustin Pop committed
650
651
652
653
654
  Instance.dsk inst >= 2 * Types.unitDsk ==>
    case Instance.shrinkByType inst Types.FailDisk of
      Types.Ok inst' ->
        Instance.dsk inst' == Instance.dsk inst - Types.unitDsk
      _ -> False
Iustin Pop's avatar
Iustin Pop committed
655
656

prop_Instance_shrinkDF inst =
Iustin Pop's avatar
Iustin Pop committed
657
  forAll (choose (0, 2 * Types.unitDsk - 1)) $ \dsk ->
Iustin Pop's avatar
Iustin Pop committed
658
659
    let inst' = inst { Instance.dsk = dsk }
    in Types.isBad $ Instance.shrinkByType inst' Types.FailDisk
Iustin Pop's avatar
Iustin Pop committed
660
661

prop_Instance_setMovable inst m =
Iustin Pop's avatar
Iustin Pop committed
662
  Instance.movable inst' ==? m
663
    where inst' = Instance.setMovable inst m
Iustin Pop's avatar
Iustin Pop committed
664

665
testSuite "Instance"
Iustin Pop's avatar
Iustin Pop committed
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
            [ 'prop_Instance_creat
            , 'prop_Instance_setIdx
            , 'prop_Instance_setName
            , 'prop_Instance_setAlias
            , 'prop_Instance_setPri
            , 'prop_Instance_setSec
            , 'prop_Instance_setBoth
            , 'prop_Instance_shrinkMG
            , 'prop_Instance_shrinkMF
            , 'prop_Instance_shrinkCG
            , 'prop_Instance_shrinkCF
            , 'prop_Instance_shrinkDG
            , 'prop_Instance_shrinkDF
            , 'prop_Instance_setMovable
            ]
Iustin Pop's avatar
Iustin Pop committed
681

682
683
-- ** Text backend tests

Iustin Pop's avatar
Iustin Pop committed
684
685
-- Instance text loader tests

Iustin Pop's avatar
Iustin Pop committed
686
687
prop_Text_Load_Instance name mem dsk vcpus status
                        (NonEmpty pnode) snode
688
                        (NonNegative pdx) (NonNegative sdx) autobal dt =
Iustin Pop's avatar
Iustin Pop committed
689
690
691
692
693
694
  pnode /= snode && pdx /= sdx ==>
  let vcpus_s = show vcpus
      dsk_s = show dsk
      mem_s = show mem
      status_s = Types.instanceStatusToRaw status
      ndx = if null snode
Iustin Pop's avatar
Iustin Pop committed
695
              then [(pnode, pdx)]
696
              else [(pnode, pdx), (snode, sdx)]
Iustin Pop's avatar
Iustin Pop committed
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
      nl = Data.Map.fromList ndx
      tags = ""
      sbal = if autobal then "Y" else "N"
      sdt = Types.diskTemplateToRaw dt
      inst = Text.loadInst nl
             [name, mem_s, dsk_s, vcpus_s, status_s,
              sbal, pnode, snode, sdt, tags]
      fail1 = Text.loadInst nl
              [name, mem_s, dsk_s, vcpus_s, status_s,
               sbal, pnode, pnode, tags]
      _types = ( name::String, mem::Int, dsk::Int
               , vcpus::Int, status::Types.InstanceStatus
               , snode::String
               , autobal::Bool)
  in case inst of
       Types.Bad msg -> printTestCase ("Failed to load instance: " ++ msg)
                        False
       Types.Ok (_, i) -> printTestCase "Mismatch in some field while\
                                        \ loading the instance" $
               Instance.name i == name &&
               Instance.vcpus i == vcpus &&
               Instance.mem i == mem &&
               Instance.pNode i == pdx &&
               Instance.sNode i == (if null snode
                                      then Node.noSecondary
                                      else sdx) &&
               Instance.autoBalance i == autobal &&
               Types.isBad fail1
Iustin Pop's avatar
Iustin Pop committed
725
726

prop_Text_Load_InstanceFail ktn fields =
Iustin Pop's avatar
Iustin Pop committed
727
  length fields /= 10 ==>
Iustin Pop's avatar
Iustin Pop committed
728
    case Text.loadInst nl fields of
729
730
731
732
      Types.Ok _ -> printTestCase "Managed to load instance from invalid\
                                  \ data" False
      Types.Bad msg -> printTestCase ("Unrecognised error message: " ++ msg) $
                       "Invalid/incomplete instance data: '" `isPrefixOf` msg
733
    where nl = Data.Map.fromList ktn
Iustin Pop's avatar
Iustin Pop committed
734
735

prop_Text_Load_Node name tm nm fm td fd tc fo =
Iustin Pop's avatar
Iustin Pop committed
736
737
738
739
740
741
742
743
744
745
  let conv v = if v < 0
                 then "?"
                 else show v
      tm_s = conv tm
      nm_s = conv nm
      fm_s = conv fm
      td_s = conv td
      fd_s = conv fd
      tc_s = conv tc
      fo_s = if fo
Iustin Pop's avatar
Iustin Pop committed
746
747
               then "Y"
               else "N"
Iustin Pop's avatar
Iustin Pop committed
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
      any_broken = any (< 0) [tm, nm, fm, td, fd, tc]
      gid = Group.uuid defGroup
  in case Text.loadNode defGroupAssoc
       [name, tm_s, nm_s, fm_s, td_s, fd_s, tc_s, fo_s, gid] of
       Nothing -> False
       Just (name', node) ->
         if fo || any_broken
           then Node.offline node
           else Node.name node == name' && name' == name &&
                Node.alias node == name &&
                Node.tMem node == fromIntegral tm &&
                Node.nMem node == nm &&
                Node.fMem node == fm &&
                Node.tDsk node == fromIntegral td &&
                Node.fDsk node == fd &&
                Node.tCpu node == fromIntegral tc
Iustin Pop's avatar
Iustin Pop committed
764
765

prop_Text_Load_NodeFail fields =
Iustin Pop's avatar
Iustin Pop committed
766
  length fields /= 8 ==> isNothing $ Text.loadNode Data.Map.empty fields
Iustin Pop's avatar
Iustin Pop committed
767

768
prop_Text_NodeLSIdempotent node =
Iustin Pop's avatar
Iustin Pop committed
769
770
771
  (Text.loadNode defGroupAssoc.
       Utils.sepSplit '|' . Text.serializeNode defGroupList) n ==
  Just (Node.name n, n)
772
    -- override failN1 to what loadNode returns by default
Iustin Pop's avatar
Iustin Pop committed
773
774
    where n = node { Node.failN1 = True, Node.offline = False
                   , Node.iPolicy = Types.defIPolicy }
775

776
777
778
779
780
781
782
783
784
785
786
787
788
prop_Text_ISpecIdempotent ispec =
  case Text.loadISpec "dummy" . Utils.sepSplit ',' .
       Text.serializeISpec $ ispec of
    Types.Bad msg -> printTestCase ("Failed to load ispec: " ++ msg) False
    Types.Ok ispec' -> ispec ==? ispec'

prop_Text_IPolicyIdempotent ipol =
  case Text.loadIPolicy . Utils.sepSplit '|' $
       Text.serializeIPolicy owner ipol of
    Types.Bad msg -> printTestCase ("Failed to load ispec: " ++ msg) False
    Types.Ok res -> (owner, ipol) ==? res
  where owner = "dummy"

789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
-- | This property, while being in the text tests, does more than just
-- test end-to-end the serialisation and loading back workflow; it
-- also tests the Loader.mergeData and the actuall
-- Cluster.iterateAlloc (for well-behaving w.r.t. instance
-- allocations, not for the business logic). As such, it's a quite
-- complex and slow test, and that's the reason we restrict it to
-- small cluster sizes.
prop_Text_CreateSerialise =
  forAll genTags $ \ctags ->
  forAll (choose (1, 2)) $ \reqnodes ->
  forAll (choose (1, 20)) $ \maxiter ->
  forAll (choose (2, 10)) $ \count ->
  forAll genOnlineNode $ \node ->
  forAll (arbitrary `suchThat` isInstanceSmallerThanNode node) $ \inst ->
  let inst' = Instance.setMovable inst $ Utils.if' (reqnodes == 2) True False
      nl = makeSmallCluster node count
  in case Cluster.genAllocNodes defGroupList nl reqnodes True >>= \allocn ->
     Cluster.iterateAlloc nl Container.empty (Just maxiter) inst' allocn [] []
     of
       Types.Bad msg -> printTestCase ("Failed to allocate: " ++ msg) False
       Types.Ok (_, _, _, [], _) -> printTestCase
                                    "Failed to allocate: no allocations" False
       Types.Ok (_, nl', il', _, _) ->
         let cdata = Loader.ClusterData defGroupList nl' il' ctags
                     Types.defIPolicy
             saved = Text.serializeCluster cdata
         in case Text.parseData saved >>= Loader.mergeData [] [] [] [] of
              Types.Bad msg -> printTestCase ("Failed to load/merge: " ++
                                              msg) False
              Types.Ok (Loader.ClusterData gl2 nl2 il2 ctags2 cpol2) ->
                ctags ==? ctags2 .&&.
                Types.defIPolicy ==? cpol2 .&&.
                il' ==? il2 .&&.
822
823
                defGroupList ==? gl2 .&&.
                nl' ==? nl2
824

825
testSuite "Text"
Iustin Pop's avatar
Iustin Pop committed
826
827
828
829
830
            [ 'prop_Text_Load_Instance
            , 'prop_Text_Load_InstanceFail
            , 'prop_Text_Load_Node
            , 'prop_Text_Load_NodeFail
            , 'prop_Text_NodeLSIdempotent
831
832
            , 'prop_Text_ISpecIdempotent
            , 'prop_Text_IPolicyIdempotent
833
            , 'prop_Text_CreateSerialise
Iustin Pop's avatar
Iustin Pop committed
834
            ]
835

836
-- ** Node tests
837

Iustin Pop's avatar
Iustin Pop committed
838
prop_Node_setAlias node name =
Iustin Pop's avatar
Iustin Pop committed
839
840
  Node.name newnode == Node.name node &&
  Node.alias newnode == name
Iustin Pop's avatar
Iustin Pop committed
841
842
843
844
    where _types = (node::Node.Node, name::String)
          newnode = Node.setAlias node name

prop_Node_setOffline node status =
Iustin Pop's avatar
Iustin Pop committed
845
  Node.offline newnode ==? status
Iustin Pop's avatar
Iustin Pop committed
846
847
848
    where newnode = Node.setOffline node status

prop_Node_setXmem node xm =
Iustin Pop's avatar
Iustin Pop committed
849
  Node.xMem newnode ==? xm
Iustin Pop's avatar
Iustin Pop committed
850
851
852
    where newnode = Node.setXmem node xm

prop_Node_setMcpu node mc =
Iustin Pop's avatar
Iustin Pop committed
853
  Node.mCpu newnode ==? mc
Iustin Pop's avatar
Iustin Pop committed
854
855
    where newnode = Node.setMcpu node mc

856
857
-- | Check that an instance add with too high memory or disk will be
-- rejected.
Iustin Pop's avatar
Iustin Pop committed
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
prop_Node_addPriFM node inst =
  Instance.mem inst >= Node.fMem node && not (Node.failN1 node) &&
  not (Instance.instanceOffline inst) ==>
  case Node.addPri node inst'' of
    Types.OpFail Types.FailMem -> True
    _ -> False
  where _types = (node::Node.Node, inst::Instance.Instance)
        inst' = setInstanceSmallerThanNode node inst
        inst'' = inst' { Instance.mem = Instance.mem inst }

prop_Node_addPriFD node inst =
  Instance.dsk inst >= Node.fDsk node && not (Node.failN1 node) ==>
    case Node.addPri node inst'' of
      Types.OpFail Types.FailDisk -> True
      _ -> False
Iustin Pop's avatar
Iustin Pop committed
873
874
875
876
    where _types = (node::Node.Node, inst::Instance.Instance)
          inst' = setInstanceSmallerThanNode node inst
          inst'' = inst' { Instance.dsk = Instance.dsk inst }

Iustin Pop's avatar
Iustin Pop committed
877
prop_Node_addPriFC node inst (Positive extra) =
Iustin Pop's avatar
Iustin Pop committed
878
879
880
881
  not (Node.failN1 node) && not (Instance.instanceOffline inst) ==>
      case Node.addPri node inst'' of
        Types.OpFail Types.FailCPU -> True
        _ -> False
Iustin Pop's avatar
Iustin Pop committed
882
883
    where _types = (node::Node.Node, inst::Instance.Instance)
          inst' = setInstanceSmallerThanNode node inst
Iustin Pop's avatar
Iustin Pop committed
884
          inst'' = inst' { Instance.vcpus = Node.availCpu node + extra }
885

886
887
-- | Check that an instance add with too high memory or disk will be
-- rejected.
888
prop_Node_addSec node inst pdx =
Iustin Pop's avatar
Iustin Pop committed
889
890
891
892
893
  ((Instance.mem inst >= (Node.fMem node - Node.rMem node) &&
    not (Instance.instanceOffline inst)) ||
   Instance.dsk inst >= Node.fDsk node) &&
  not (Node.failN1 node) ==>
      isFailure (Node.addSec node inst pdx)
894
        where _types = (node::Node.Node, inst::Instance.Instance, pdx::Int)
895

896
897
-- | Check that an offline instance with reasonable disk size can always
-- be added.
898
899
prop_Node_addPriOffline =
  forAll (arbitrary `suchThat` ((> 0) . Node.fMem)) $ \node ->
Iustin Pop's avatar
Iustin Pop committed
900
901
902
903
904
905
  forAll (arbitrary `suchThat`
          (\ x ->  (Instance.dsk x  < Node.fDsk node) &&
                   Instance.instanceOffline x)) $ \inst ->
  case Node.addPri node inst of
    Types.OpGood _ -> True
    _ -> False
906

907
908
prop_Node_addSecOffline pdx =
  forAll (arbitrary `suchThat` ((> 0) . Node.fMem)) $ \node ->
Iustin Pop's avatar
Iustin Pop committed
909
910
911
912
913
914
  forAll (arbitrary `suchThat`
          (\ x ->  (Instance.dsk x  < Node.fDsk node) &&
                   Instance.instanceOffline x)) $ \inst ->
  case Node.addSec node inst pdx of
    Types.OpGood _ -> True
    _ -> False
915

916
-- | Checks for memory reservation changes.
917
prop_Node_rMem inst =
Iustin Pop's avatar
Iustin Pop committed
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
  not (Instance.instanceOffline inst) ==>
  forAll (arbitrary `suchThat` ((> Types.unitMem) . Node.fMem)) $ \node ->
  -- ab = auto_balance, nb = non-auto_balance
  -- we use -1 as the primary node of the instance
  let inst' = inst { Instance.pNode = -1, Instance.autoBalance = True }
      inst_ab = setInstanceSmallerThanNode node inst'
      inst_nb = inst_ab { Instance.autoBalance = False }
      -- now we have the two instances, identical except the
      -- autoBalance attribute
      orig_rmem = Node.rMem node
      inst_idx = Instance.idx inst_ab
      node_add_ab = Node.addSec node inst_ab (-1)
      node_add_nb = Node.addSec node inst_nb (-1)
      node_del_ab = liftM (`Node.removeSec` inst_ab) node_add_ab
      node_del_nb = liftM (`Node.removeSec` inst_nb) node_add_nb
  in case (node_add_ab, node_add_nb, node_del_ab, node_del_nb) of
       (Types.OpGood a_ab, Types.OpGood a_nb,
        Types.OpGood d_ab, Types.OpGood d_nb) ->
         printTestCase "Consistency checks failed" $
           Node.rMem a_ab >  orig_rmem &&
           Node.rMem a_ab - orig_rmem == Instance.mem inst_ab &&
           Node.rMem a_nb == orig_rmem &&
           Node.rMem d_ab == orig_rmem &&
           Node.rMem d_nb == orig_rmem &&
           -- this is not related to rMem, but as good a place to
           -- test as any
           inst_idx `elem` Node.sList a_ab &&
Iustin Pop's avatar
Iustin Pop committed
945
           inst_idx `notElem` Node.sList d_ab
Iustin Pop's avatar
Iustin Pop committed
946
       x -> printTestCase ("Failed to add/remove instances: " ++ show x) False
947

948
-- | Check mdsk setting.
Iustin Pop's avatar
Iustin Pop committed
949
prop_Node_setMdsk node mx =
Iustin Pop's avatar
Iustin Pop committed
950
951
952
953
954
955
  Node.loDsk node' >= 0 &&
  fromIntegral (Node.loDsk node') <= Node.tDsk node &&
  Node.availDisk node' >= 0 &&
  Node.availDisk node' <= Node.fDsk node' &&
  fromIntegral (Node.availDisk node') <= Node.tDsk node' &&
  Node.mDsk node' == mx'
Iustin Pop's avatar
Iustin Pop committed
956
957
958
959
960
961
    where _types = (node::Node.Node, mx::SmallRatio)
          node' = Node.setMdsk node mx'
          SmallRatio mx' = mx

-- Check tag maps
prop_Node_tagMaps_idempotent tags =
Iustin Pop's avatar
Iustin Pop committed
962
  Node.delTags (Node.addTags m tags) tags ==? m
963
    where m = Data.Map.empty
Iustin Pop's avatar
Iustin Pop committed
964
965

prop_Node_tagMaps_reject tags =
Iustin Pop's avatar
Iustin Pop committed
966
967
  not (null tags) ==>
  all (\t -> Node.rejectAddTags m [t]) tags
968
    where m = Node.addTags Data.Map.empty tags
Iustin Pop's avatar
Iustin Pop committed
969

Iustin Pop's avatar
Iustin Pop committed
970
971
972
973
974
prop_Node_showField node =
  forAll (elements Node.defaultFields) $ \ field ->
  fst (Node.showHeader field) /= Types.unknownField &&
  Node.showField node field /= Types.unknownField

975
976
977
978
979
980
prop_Node_computeGroups nodes =
  let ng = Node.computeGroups nodes
      onlyuuid = map fst ng
  in length nodes == sum (map (length . snd) ng) &&
     all (\(guuid, ns) -> all ((== guuid) . Node.group) ns) ng &&
     length (nub onlyuuid) == length onlyuuid &&
Iustin Pop's avatar
Iustin Pop committed
981
     (null nodes || not (null ng))
982

983
testSuite "Node"
Iustin Pop's avatar
Iustin Pop committed
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
            [ 'prop_Node_setAlias
            , 'prop_Node_setOffline
            , 'prop_Node_setMcpu
            , 'prop_Node_setXmem
            , 'prop_Node_addPriFM
            , 'prop_Node_addPriFD
            , 'prop_Node_addPriFC
            , 'prop_Node_addSec
            , 'prop_Node_addPriOffline
            , 'prop_Node_addSecOffline
            , 'prop_Node_rMem
            , 'prop_Node_setMdsk
            , 'prop_Node_tagMaps_idempotent
            , 'prop_Node_tagMaps_reject
            , 'prop_Node_showField
            , 'prop_Node_computeGroups
            ]
1001

1002
-- ** Cluster tests
1003

1004
1005
-- | Check that the cluster score is close to zero for a homogeneous
-- cluster.
Iustin Pop's avatar
Iustin Pop committed
1006
prop_Score_Zero node =
Iustin Pop's avatar
Iustin Pop committed
1007
  forAll (choose (1, 1024)) $ \count ->
Iustin Pop's avatar
Iustin Pop committed
1008
    (not (Node.offline node) && not (Node.failN1 node) && (count > 0) &&
1009
     (Node.tDsk node > 0) && (Node.tMem node > 0)) ==>
Iustin Pop's avatar
Iustin Pop committed
1010
1011
1012
1013
1014
1015
  let fn = Node.buildPeers node Container.empty
      nlst = replicate count fn
      score = Cluster.compCVNodes nlst
  -- we can't say == 0 here as the floating point errors accumulate;
  -- this should be much lower than the default score in CLI.hs
  in score <= 1e-12
1016

1017
-- | Check that cluster stats are sane.
1018
prop_CStats_sane =
Iustin Pop's avatar
Iustin Pop committed
1019
  forAll (choose (1, 1024)) $ \count ->
1020
  forAll genOnlineNode $ \node ->
Iustin Pop's avatar
Iustin Pop committed
1021
1022
1023
1024
1025
1026
  let fn = Node.buildPeers node Container.empty
      nlst = zip [1..] $ replicate count fn::[(Types.Ndx, Node.Node)]
      nl = Container.fromList nlst
      cstats = Cluster.totalResources nl
  in Cluster.csAdsk cstats >= 0 &&
     Cluster.csAdsk cstats <= Cluster.csFdsk cstats
Iustin Pop's avatar
Iustin Pop committed
1027

1028
-- | Check that one instance is allocated correctly, without
1029
-- rebalances needed.
1030
prop_ClusterAlloc_sane inst =
Iustin Pop's avatar
Iustin Pop committed
1031
  forAll (choose (5, 20)) $ \count ->
1032
  forAll genOnlineNode $ \node ->
Iustin Pop's avatar
Iustin Pop committed
1033
  let (nl, il, inst') = makeSmallEmptyCluster node count inst
Iustin Pop's avatar
Iustin Pop committed
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
  in case Cluster.genAllocNodes defGroupList nl 2 True >>=
     Cluster.tryAlloc nl il inst' of
       Types.Bad _ -> False
       Types.Ok as ->
         case Cluster.asSolution as of
           Nothing -> False
           Just (xnl, xi, _, cv) ->
             let il' = Container.add (Instance.idx xi) xi il
                 tbl = Cluster.Table xnl il' cv []
             in not (canBalance tbl True True False)
1044
1045
1046

-- | Checks that on a 2-5 node cluster, we can allocate a random
-- instance spec via tiered allocation (whatever the original instance
1047
-- spec), on either one or two nodes.
1048
prop_ClusterCanTieredAlloc inst =
Iustin Pop's avatar
Iustin Pop committed
1049
1050
  forAll (choose (2, 5)) $ \count ->
  forAll (choose (1, 2)) $ \rqnodes ->
1051
  forAll (genOnlineNode `suchThat` (isNodeBig 4)) $ \node ->
Iustin Pop's avatar
Iustin Pop committed
1052
1053
1054
1055
1056
1057
1058
1059
1060
  let nl = makeSmallCluster node count
      il = Container.empty
      allocnodes = Cluster.genAllocNodes defGroupList nl rqnodes True
  in case allocnodes >>= \allocnodes' ->
    Cluster.tieredAlloc nl il (Just 1)