QC.hs 30 KB
Newer Older
1
{-| Unittests for ganeti-htools
Iustin Pop's avatar
Iustin Pop committed
2
3
4
5
6

-}

{-

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

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.

-}

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

import Test.QuickCheck
40
import Test.QuickCheck.Batch
41
import Data.List (findIndex, intercalate, nub)
42
import Data.Maybe
Iustin Pop's avatar
Iustin Pop committed
43
44
import Control.Monad
import qualified Text.JSON as J
Iustin Pop's avatar
Iustin Pop committed
45
import qualified Data.Map
46
import qualified Data.IntMap as IntMap
Iustin Pop's avatar
Iustin Pop committed
47
import qualified Ganeti.OpCodes as OpCodes
48
import qualified Ganeti.Jobs as Jobs
Iustin Pop's avatar
Iustin Pop committed
49
import qualified Ganeti.Luxi
50
51
52
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
53
import qualified Ganeti.HTools.ExtLoader
54
55
56
import qualified Ganeti.HTools.IAlloc as IAlloc
import qualified Ganeti.HTools.Instance as Instance
import qualified Ganeti.HTools.Loader as Loader
Iustin Pop's avatar
Iustin Pop committed
57
import qualified Ganeti.HTools.Luxi
58
import qualified Ganeti.HTools.Node as Node
Iustin Pop's avatar
Iustin Pop committed
59
import qualified Ganeti.HTools.Group as Group
60
import qualified Ganeti.HTools.PeerMap as PeerMap
Iustin Pop's avatar
Iustin Pop committed
61
import qualified Ganeti.HTools.Simu
62
63
64
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
65
import qualified Ganeti.HTools.Version
66

67
68
-- * Constants

Iustin Pop's avatar
Iustin Pop committed
69
70
71
72
-- | Maximum memory (1TiB, somewhat random value)
maxMem :: Int
maxMem = 1024 * 1024

Iustin Pop's avatar
Iustin Pop committed
73
-- | Maximum disk (8TiB, somewhat random value)
Iustin Pop's avatar
Iustin Pop committed
74
maxDsk :: Int
Iustin Pop's avatar
Iustin Pop committed
75
maxDsk = 1024 * 1024 * 8
Iustin Pop's avatar
Iustin Pop committed
76
77
78
79
80

-- | Max CPUs (1024, somewhat random value)
maxCpu :: Int
maxCpu = 1024

Iustin Pop's avatar
Iustin Pop committed
81
82
83
84
85
86
defGroup :: Group.Group
defGroup = flip Group.setIdx 0 $
               Group.create "default" Utils.defaultGroupID
                    Types.AllocPreferred

defGroupList :: Group.List
Iustin Pop's avatar
Iustin Pop committed
87
defGroupList = Container.fromList [(Group.idx defGroup, defGroup)]
Iustin Pop's avatar
Iustin Pop committed
88
89
90
91

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

92
93
-- * Helper functions

Iustin Pop's avatar
Iustin Pop committed
94
95
96
97
98
-- | Simple checker for whether OpResult is fail or pass
isFailure :: Types.OpResult a -> Bool
isFailure (Types.OpFail _) = True
isFailure _ = False

99
100
-- | Update an instance to be smaller than a node
setInstanceSmallerThanNode node inst =
101
102
103
    inst { Instance.mem = Node.availMem node `div` 2
         , Instance.dsk = Node.availDisk node `div` 2
         , Instance.vcpus = Node.availCpu node `div` 2
104
105
106
107
108
109
110
111
112
113
114
115
         }

-- | Create an instance given its spec
createInstance mem dsk vcpus =
    Instance.create "inst-unnamed" mem dsk vcpus "running" [] (-1) (-1)

-- | Create a small cluster by repeating a node spec
makeSmallCluster :: Node.Node -> Int -> Node.List
makeSmallCluster node count =
    let fn = Node.buildPeers node Container.empty
        namelst = map (\n -> (Node.name n, n)) (replicate count fn)
        (_, nlst) = Loader.assignIndices namelst
116
    in nlst
117
118
119
120
121
122
123
124

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

canBalance :: Cluster.Table -> Bool -> Bool -> Bool
125
canBalance tbl dm evac = isJust $ Cluster.tryBalance tbl dm evac 0 0
126

127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
-- | Assigns a new fresh instance to a cluster; this is not
-- allocation, so no resource checks are done
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
                then 0
                else fst (Container.findMax il) + 1
      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')

146
147
-- * Arbitrary instances

148
149
-- copied from the introduction to quickcheck
instance Arbitrary Char where
150
    arbitrary = choose ('\32', '\128')
151

152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
newtype DNSChar = DNSChar { dnsGetChar::Char }
instance Arbitrary DNSChar where
    arbitrary = do
      x <- elements (['a'..'z'] ++ ['0'..'9'] ++ "_-")
      return (DNSChar x)

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')

173
174
175
-- let's generate a random instance
instance Arbitrary Instance.Instance where
    arbitrary = do
176
      name <- getFQDN
Iustin Pop's avatar
Iustin Pop committed
177
178
      mem <- choose (0, maxMem)
      dsk <- choose (0, maxDsk)
Iustin Pop's avatar
Iustin Pop committed
179
180
181
182
      run_st <- elements ["ERROR_up", "ERROR_down", "ADMIN_down"
                         , "ERROR_nodedown", "ERROR_nodeoffline"
                         , "running"
                         , "no_such_status1", "no_such_status2"]
183
184
      pn <- arbitrary
      sn <- arbitrary
Iustin Pop's avatar
Iustin Pop committed
185
      vcpus <- choose (0, maxCpu)
186
      return $ Instance.create name mem dsk vcpus run_st [] pn sn
187
188
189
190

-- and a random node
instance Arbitrary Node.Node where
    arbitrary = do
191
      name <- getFQDN
Iustin Pop's avatar
Iustin Pop committed
192
      mem_t <- choose (0, maxMem)
193
194
      mem_f <- choose (0, mem_t)
      mem_n <- choose (0, mem_t - mem_f)
Iustin Pop's avatar
Iustin Pop committed
195
      dsk_t <- choose (0, maxDsk)
196
      dsk_f <- choose (0, dsk_t)
Iustin Pop's avatar
Iustin Pop committed
197
      cpu_t <- choose (0, maxCpu)
198
199
      offl <- arbitrary
      let n = Node.create name (fromIntegral mem_t) mem_n mem_f
Iustin Pop's avatar
Iustin Pop committed
200
              (fromIntegral dsk_t) dsk_f (fromIntegral cpu_t) offl
Iustin Pop's avatar
Iustin Pop committed
201
              0
202
          n' = Node.buildPeers n Container.empty
203
204
      return n'

Iustin Pop's avatar
Iustin Pop committed
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
-- replace disks
instance Arbitrary OpCodes.ReplaceDisksMode where
  arbitrary = elements [ OpCodes.ReplaceOnPrimary
                       , OpCodes.ReplaceOnSecondary
                       , OpCodes.ReplaceNewSecondary
                       , OpCodes.ReplaceAuto
                       ]

instance Arbitrary OpCodes.OpCode where
  arbitrary = do
    op_id <- elements [ "OP_TEST_DELAY"
                      , "OP_INSTANCE_REPLACE_DISKS"
                      , "OP_INSTANCE_FAILOVER"
                      , "OP_INSTANCE_MIGRATE"
                      ]
    (case op_id of
        "OP_TEST_DELAY" ->
          liftM3 OpCodes.OpTestDelay arbitrary arbitrary arbitrary
        "OP_INSTANCE_REPLACE_DISKS" ->
          liftM5 OpCodes.OpReplaceDisks arbitrary arbitrary
          arbitrary arbitrary arbitrary
        "OP_INSTANCE_FAILOVER" ->
          liftM2 OpCodes.OpFailoverInstance arbitrary arbitrary
        "OP_INSTANCE_MIGRATE" ->
          liftM3 OpCodes.OpMigrateInstance arbitrary arbitrary arbitrary
        _ -> fail "Wrong opcode")

232
233
234
235
236
237
instance Arbitrary Jobs.OpStatus where
  arbitrary = elements [minBound..maxBound]

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

238
-- * Actual tests
Iustin Pop's avatar
Iustin Pop committed
239

240
241
242
243
244
245
246
247
248
249
250
251
252
-- If the list is not just an empty element, and if the elements do
-- not contain commas, then join+split should be idepotent
prop_Utils_commaJoinSplit lst = lst /= [""] &&
                                all (not . elem ',') lst ==>
                                Utils.sepSplit ',' (Utils.commaJoin lst) == lst
-- Split and join should always be idempotent
prop_Utils_commaSplitJoin s = Utils.commaJoin (Utils.sepSplit ',' s) == s

testUtils =
  [ run prop_Utils_commaJoinSplit
  , run prop_Utils_commaSplitJoin
  ]

253
-- | Make sure add is idempotent
Iustin Pop's avatar
Iustin Pop committed
254
prop_PeerMap_addIdempotent pmap key em =
255
    fn puniq == fn (fn puniq)
256
    where _types = (pmap::PeerMap.PeerMap,
Iustin Pop's avatar
Iustin Pop committed
257
258
                    key::PeerMap.Key, em::PeerMap.Elem)
          fn = PeerMap.add key em
259
          puniq = PeerMap.accumArray const pmap
260
261
262
263

-- | Make sure remove is idempotent
prop_PeerMap_removeIdempotent pmap key =
    fn puniq == fn (fn puniq)
264
265
    where _types = (pmap::PeerMap.PeerMap, key::PeerMap.Key)
          fn = PeerMap.remove key
266
267
268
269
270
          puniq = PeerMap.accumArray const pmap

-- | Make sure a missing item returns 0
prop_PeerMap_findMissing pmap key =
    PeerMap.find key (PeerMap.remove key puniq) == 0
271
    where _types = (pmap::PeerMap.PeerMap, key::PeerMap.Key)
272
273
274
          puniq = PeerMap.accumArray const pmap

-- | Make sure an added item is found
Iustin Pop's avatar
Iustin Pop committed
275
276
prop_PeerMap_addFind pmap key em =
    PeerMap.find key (PeerMap.add key em puniq) == em
277
    where _types = (pmap::PeerMap.PeerMap,
Iustin Pop's avatar
Iustin Pop committed
278
                    key::PeerMap.Key, em::PeerMap.Elem)
279
          puniq = PeerMap.accumArray const pmap
280
281
282
283
284

-- | Manual check that maxElem returns the maximum indeed, or 0 for null
prop_PeerMap_maxElem pmap =
    PeerMap.maxElem puniq == if null puniq then 0
                             else (maximum . snd . unzip) puniq
285
    where _types = pmap::PeerMap.PeerMap
286
287
          puniq = PeerMap.accumArray const pmap

288
testPeerMap =
289
290
291
292
293
294
295
    [ run prop_PeerMap_addIdempotent
    , run prop_PeerMap_removeIdempotent
    , run prop_PeerMap_maxElem
    , run prop_PeerMap_addFind
    , run prop_PeerMap_findMissing
    ]

296
297
298
299
300
301
302
303
304
305
-- Container tests

prop_Container_addTwo cdata i1 i2 =
    fn i1 i2 cont == fn i2 i1 cont &&
       fn i1 i2 cont == fn i1 i2 (fn i1 i2 cont)
    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

306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
prop_Container_nameOf node =
  let nl = makeSmallCluster node 1
      fnode = head (Container.elems nl)
  in Container.nameOf nl (Node.idx fnode) == Node.name fnode

-- We test that in a cluster, given a random node, we can find it by
-- its name and alias, as long as all names and aliases are unique,
-- and that we fail to find a non-existing name
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 &&
  not (othername `elem` (map fst names ++ map snd names)) ==>
  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
327
      nl' = Container.fromList nodes'
328
329
330
331
332
      target = snd (nodes' !! fidx)
  in Container.findByName nl' (Node.name target) == Just target &&
     Container.findByName nl' (Node.alias target) == Just target &&
     Container.findByName nl' othername == Nothing

333
testContainer =
334
335
336
337
    [ run prop_Container_addTwo
    , run prop_Container_nameOf
    , run prop_Container_findByName
    ]
338

339
340
-- Simple instance tests, we only have setter/getters

Iustin Pop's avatar
Iustin Pop committed
341
342
343
prop_Instance_creat inst =
    Instance.name inst == Instance.alias inst

344
345
346
347
348
prop_Instance_setIdx inst idx =
    Instance.idx (Instance.setIdx inst idx) == idx
    where _types = (inst::Instance.Instance, idx::Types.Idx)

prop_Instance_setName inst name =
Iustin Pop's avatar
Iustin Pop committed
349
350
351
352
353
354
355
356
    Instance.name newinst == name &&
    Instance.alias newinst == name
    where _types = (inst::Instance.Instance, name::String)
          newinst = Instance.setName inst name

prop_Instance_setAlias inst name =
    Instance.name newinst == Instance.name inst &&
    Instance.alias newinst == name
357
    where _types = (inst::Instance.Instance, name::String)
Iustin Pop's avatar
Iustin Pop committed
358
          newinst = Instance.setAlias inst name
359
360

prop_Instance_setPri inst pdx =
361
    Instance.pNode (Instance.setPri inst pdx) == pdx
362
363
364
    where _types = (inst::Instance.Instance, pdx::Types.Ndx)

prop_Instance_setSec inst sdx =
365
    Instance.sNode (Instance.setSec inst sdx) == sdx
366
367
368
    where _types = (inst::Instance.Instance, sdx::Types.Ndx)

prop_Instance_setBoth inst pdx sdx =
369
    Instance.pNode si == pdx && Instance.sNode si == sdx
370
371
372
    where _types = (inst::Instance.Instance, pdx::Types.Ndx, sdx::Types.Ndx)
          si = Instance.setBoth inst pdx sdx

Iustin Pop's avatar
Iustin Pop committed
373
374
prop_Instance_runStatus_True inst =
    let run_st = Instance.running inst
375
        run_tx = Instance.runSt inst
Iustin Pop's avatar
Iustin Pop committed
376
    in
377
      run_tx `elem` Instance.runningStates ==> run_st
Iustin Pop's avatar
Iustin Pop committed
378
379
380

prop_Instance_runStatus_False inst =
    let run_st = Instance.running inst
381
        run_tx = Instance.runSt inst
Iustin Pop's avatar
Iustin Pop committed
382
    in
383
      run_tx `notElem` Instance.runningStates ==> not run_st
Iustin Pop's avatar
Iustin Pop committed
384

Iustin Pop's avatar
Iustin Pop committed
385
386
387
388
389
390
391
392
393
prop_Instance_shrinkMG inst =
    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

prop_Instance_shrinkMF inst =
    Instance.mem inst < 2 * Types.unitMem ==>
394
        Types.isBad $ Instance.shrinkByType inst Types.FailMem
Iustin Pop's avatar
Iustin Pop committed
395
396
397
398
399
400
401
402
403
404

prop_Instance_shrinkCG inst =
    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

prop_Instance_shrinkCF inst =
    Instance.vcpus inst < 2 * Types.unitCpu ==>
405
        Types.isBad $ Instance.shrinkByType inst Types.FailCPU
Iustin Pop's avatar
Iustin Pop committed
406
407
408
409
410
411
412
413
414
415

prop_Instance_shrinkDG inst =
    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

prop_Instance_shrinkDF inst =
    Instance.dsk inst < 2 * Types.unitDsk ==>
416
        Types.isBad $ Instance.shrinkByType inst Types.FailDisk
Iustin Pop's avatar
Iustin Pop committed
417
418
419

prop_Instance_setMovable inst m =
    Instance.movable inst' == m
420
    where inst' = Instance.setMovable inst m
Iustin Pop's avatar
Iustin Pop committed
421

422
testInstance =
Iustin Pop's avatar
Iustin Pop committed
423
424
    [ run prop_Instance_creat
    , run prop_Instance_setIdx
425
    , run prop_Instance_setName
Iustin Pop's avatar
Iustin Pop committed
426
    , run prop_Instance_setAlias
427
428
429
    , run prop_Instance_setPri
    , run prop_Instance_setSec
    , run prop_Instance_setBoth
Iustin Pop's avatar
Iustin Pop committed
430
431
    , run prop_Instance_runStatus_True
    , run prop_Instance_runStatus_False
Iustin Pop's avatar
Iustin Pop committed
432
433
434
435
436
437
438
    , run prop_Instance_shrinkMG
    , run prop_Instance_shrinkMF
    , run prop_Instance_shrinkCG
    , run prop_Instance_shrinkCF
    , run prop_Instance_shrinkDG
    , run prop_Instance_shrinkDF
    , run prop_Instance_setMovable
Iustin Pop's avatar
Iustin Pop committed
439
440
441
442
443
    ]

-- Instance text loader tests

prop_Text_Load_Instance name mem dsk vcpus status pnode snode pdx sdx =
Iustin Pop's avatar
Iustin Pop committed
444
    not (null pnode) && pdx >= 0 && sdx >= 0 ==>
Iustin Pop's avatar
Iustin Pop committed
445
446
447
448
449
450
    let vcpus_s = show vcpus
        dsk_s = show dsk
        mem_s = show mem
        rsdx = if pdx == sdx
               then sdx + 1
               else sdx
Iustin Pop's avatar
Iustin Pop committed
451
452
453
        ndx = if null snode
              then [(pnode, pdx)]
              else [(pnode, pdx), (snode, rsdx)]
454
        nl = Data.Map.fromList ndx
455
        tags = ""
456
        inst = Text.loadInst nl
Iustin Pop's avatar
Iustin Pop committed
457
458
               [name, mem_s, dsk_s, vcpus_s, status, pnode, snode, tags]::
               Maybe (String, Instance.Instance)
459
        fail1 = Text.loadInst nl
Iustin Pop's avatar
Iustin Pop committed
460
               [name, mem_s, dsk_s, vcpus_s, status, pnode, pnode, tags]::
Iustin Pop's avatar
Iustin Pop committed
461
462
463
464
465
466
467
468
469
470
471
472
               Maybe (String, Instance.Instance)
        _types = ( name::String, mem::Int, dsk::Int
                 , vcpus::Int, status::String
                 , pnode::String, snode::String
                 , pdx::Types.Ndx, sdx::Types.Ndx)
    in
      case inst of
        Nothing -> False
        Just (_, i) ->
            (Instance.name i == name &&
             Instance.vcpus i == vcpus &&
             Instance.mem i == mem &&
473
             Instance.pNode i == pdx &&
Iustin Pop's avatar
Iustin Pop committed
474
475
476
477
478
479
             Instance.sNode i == (if null snode
                                  then Node.noSecondary
                                  else rsdx) &&
             isNothing fail1)

prop_Text_Load_InstanceFail ktn fields =
480
481
    length fields /= 8 ==> isNothing $ Text.loadInst nl fields
    where nl = Data.Map.fromList ktn
Iustin Pop's avatar
Iustin Pop committed
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496

prop_Text_Load_Node name tm nm fm td fd tc fo =
    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
               then "Y"
               else "N"
        any_broken = any (< 0) [tm, nm, fm, td, fd, tc]
Iustin Pop's avatar
Iustin Pop committed
497
498
499
        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
Iustin Pop's avatar
Iustin Pop committed
500
501
502
503
         Nothing -> False
         Just (name', node) ->
             if fo || any_broken
             then Node.offline node
504
505
506
507
508
509
510
511
             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
512
513

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

516
prop_Text_NodeLSIdempotent node =
Iustin Pop's avatar
Iustin Pop committed
517
518
    (Text.loadNode defGroupAssoc.
         Utils.sepSplit '|' . Text.serializeNode defGroupList) n ==
519
520
521
522
    Just (Node.name n, n)
    -- override failN1 to what loadNode returns by default
    where n = node { Node.failN1 = True, Node.offline = False }

523
testText =
Iustin Pop's avatar
Iustin Pop committed
524
    [ run prop_Text_Load_Instance
Iustin Pop's avatar
Iustin Pop committed
525
526
527
    , run prop_Text_Load_InstanceFail
    , run prop_Text_Load_Node
    , run prop_Text_Load_NodeFail
528
    , run prop_Text_NodeLSIdempotent
529
530
531
532
    ]

-- Node tests

Iustin Pop's avatar
Iustin Pop committed
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
prop_Node_setAlias node name =
    Node.name newnode == Node.name node &&
    Node.alias newnode == name
    where _types = (node::Node.Node, name::String)
          newnode = Node.setAlias node name

prop_Node_setOffline node status =
    Node.offline newnode == status
    where newnode = Node.setOffline node status

prop_Node_setXmem node xm =
    Node.xMem newnode == xm
    where newnode = Node.setXmem node xm

prop_Node_setMcpu node mc =
    Node.mCpu newnode == mc
    where newnode = Node.setMcpu node mc

551
-- | Check that an instance add with too high memory or disk will be rejected
Iustin Pop's avatar
Iustin Pop committed
552
553
554
555
556
557
prop_Node_addPriFM node inst = Instance.mem inst >= Node.fMem node &&
                               not (Node.failN1 node)
                               ==>
                               case Node.addPri node inst'' of
                                 Types.OpFail Types.FailMem -> True
                                 _ -> False
558
    where _types = (node::Node.Node, inst::Instance.Instance)
Iustin Pop's avatar
Iustin Pop committed
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
          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
    where _types = (node::Node.Node, inst::Instance.Instance)
          inst' = setInstanceSmallerThanNode node inst
          inst'' = inst' { Instance.dsk = Instance.dsk inst }

prop_Node_addPriFC node inst = Instance.vcpus inst > Node.availCpu node &&
                               not (Node.failN1 node)
                               ==>
                               case Node.addPri node inst'' of
                                 Types.OpFail Types.FailCPU -> True
                                 _ -> False
    where _types = (node::Node.Node, inst::Instance.Instance)
          inst' = setInstanceSmallerThanNode node inst
          inst'' = inst' { Instance.vcpus = Instance.vcpus inst }
581
582

-- | Check that an instance add with too high memory or disk will be rejected
583
prop_Node_addSec node inst pdx =
584
585
    (Instance.mem inst >= (Node.fMem node - Node.rMem node) ||
     Instance.dsk inst >= Node.fDsk node) &&
Iustin Pop's avatar
Iustin Pop committed
586
    not (Node.failN1 node)
Iustin Pop's avatar
Iustin Pop committed
587
    ==> isFailure (Node.addSec node inst pdx)
588
        where _types = (node::Node.Node, inst::Instance.Instance, pdx::Int)
589

Iustin Pop's avatar
Iustin Pop committed
590
591
592
593
594
595
596
597
598
599
600
601
newtype SmallRatio = SmallRatio Double deriving Show
instance Arbitrary SmallRatio where
    arbitrary = do
      v <- choose (0, 1)
      return $ SmallRatio v

-- | Check mdsk setting
prop_Node_setMdsk node mx =
    Node.loDsk node' >= 0 &&
    fromIntegral (Node.loDsk node') <= Node.tDsk node &&
    Node.availDisk node' >= 0 &&
    Node.availDisk node' <= Node.fDsk node' &&
Iustin Pop's avatar
Iustin Pop committed
602
603
    fromIntegral (Node.availDisk node') <= Node.tDsk node' &&
    Node.mDsk node' == mx'
Iustin Pop's avatar
Iustin Pop committed
604
605
606
607
608
609
610
    where _types = (node::Node.Node, mx::SmallRatio)
          node' = Node.setMdsk node mx'
          SmallRatio mx' = mx

-- Check tag maps
prop_Node_tagMaps_idempotent tags =
    Node.delTags (Node.addTags m tags) tags == m
611
    where m = Data.Map.empty
Iustin Pop's avatar
Iustin Pop committed
612
613
614
615

prop_Node_tagMaps_reject tags =
    not (null tags) ==>
    any (\t -> Node.rejectAddTags m [t]) tags
616
    where m = Node.addTags Data.Map.empty tags
Iustin Pop's avatar
Iustin Pop committed
617

Iustin Pop's avatar
Iustin Pop committed
618
619
620
621
622
prop_Node_showField node =
  forAll (elements Node.defaultFields) $ \ field ->
  fst (Node.showHeader field) /= Types.unknownField &&
  Node.showField node field /= Types.unknownField

623
624
625
626
627
628
629
630
631

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 &&
     if null nodes then True else not (null ng)

632
testNode =
Iustin Pop's avatar
Iustin Pop committed
633
634
635
636
637
    [ run prop_Node_setAlias
    , run prop_Node_setOffline
    , run prop_Node_setMcpu
    , run prop_Node_setXmem
    , run prop_Node_addPriFM
Iustin Pop's avatar
Iustin Pop committed
638
639
    , run prop_Node_addPriFD
    , run prop_Node_addPriFC
640
    , run prop_Node_addSec
Iustin Pop's avatar
Iustin Pop committed
641
642
643
    , run prop_Node_setMdsk
    , run prop_Node_tagMaps_idempotent
    , run prop_Node_tagMaps_reject
Iustin Pop's avatar
Iustin Pop committed
644
    , run prop_Node_showField
645
    , run prop_Node_computeGroups
646
    ]
647
648
649
650
651
652


-- Cluster tests

-- | Check that the cluster score is close to zero for a homogeneous cluster
prop_Score_Zero node count =
Iustin Pop's avatar
Iustin Pop committed
653
    (not (Node.offline node) && not (Node.failN1 node) && (count > 0) &&
654
     (Node.tDsk node > 0) && (Node.tMem node > 0)) ==>
655
    let fn = Node.buildPeers node Container.empty
Iustin Pop's avatar
Iustin Pop committed
656
        nlst = zip [1..] $ replicate count fn::[(Types.Ndx, Node.Node)]
Iustin Pop's avatar
Iustin Pop committed
657
        nl = Container.fromList nlst
658
659
660
        score = Cluster.compCV nl
    -- we can't say == 0 here as the floating point errors accumulate;
    -- this should be much lower than the default score in CLI.hs
Iustin Pop's avatar
Iustin Pop committed
661
    in score <= 1e-15
662

Iustin Pop's avatar
Iustin Pop committed
663
664
665
-- | Check that cluster stats are sane
prop_CStats_sane node count =
    (not (Node.offline node) && not (Node.failN1 node) && (count > 0) &&
666
     (Node.availDisk node > 0) && (Node.availMem node > 0)) ==>
Iustin Pop's avatar
Iustin Pop committed
667
668
    let fn = Node.buildPeers node Container.empty
        nlst = zip [1..] $ replicate count fn::[(Types.Ndx, Node.Node)]
Iustin Pop's avatar
Iustin Pop committed
669
        nl = Container.fromList nlst
Iustin Pop's avatar
Iustin Pop committed
670
671
672
673
        cstats = Cluster.totalResources nl
    in Cluster.csAdsk cstats >= 0 &&
       Cluster.csAdsk cstats <= Cluster.csFdsk cstats

674
675
676
677
678
679
680
681
682
683
684
685
-- | Check that one instance is allocated correctly, without
-- rebalances needed
prop_ClusterAlloc_sane node inst =
    forAll (choose (5, 20)) $ \count ->
    not (Node.offline node)
            && not (Node.failN1 node)
            && Node.availDisk node > 0
            && Node.availMem node > 0
            ==>
    let nl = makeSmallCluster node count
        il = Container.empty
        inst' = setInstanceSmallerThanNode node inst
686
687
    in case Cluster.genAllocNodes defGroupList nl 2 True >>=
       Cluster.tryAlloc nl il inst' of
688
         Types.Bad _ -> False
689
690
         Types.Ok as ->
             case Cluster.asSolutions as of
691
               [] -> False
692
               (xnl, xi, _, cv):[] ->
693
                   let il' = Container.add (Instance.idx xi) xi il
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
                       tbl = Cluster.Table xnl il' cv []
                   in not (canBalance tbl True False)
               _ -> False

-- | Checks that on a 2-5 node cluster, we can allocate a random
-- instance spec via tiered allocation (whatever the original instance
-- spec), on either one or two nodes
prop_ClusterCanTieredAlloc node inst =
    forAll (choose (2, 5)) $ \count ->
    forAll (choose (1, 2)) $ \rqnodes ->
    not (Node.offline node)
            && not (Node.failN1 node)
            && isNodeBig node 4
            ==>
    let nl = makeSmallCluster node count
        il = Container.empty
710
        allocnodes = Cluster.genAllocNodes defGroupList nl rqnodes True
Iustin Pop's avatar
Iustin Pop committed
711
712
    in case allocnodes >>= \allocnodes' ->
        Cluster.tieredAlloc nl il inst allocnodes' [] [] of
713
         Types.Bad _ -> False
714
715
716
         Types.Ok (_, _, il', ixes, cstats) -> not (null ixes) &&
                                      IntMap.size il' == length ixes &&
                                      length ixes == length cstats
717
718
719
720
721
722
723
724
725
726
727
728

-- | Checks that on a 4-8 node cluster, once we allocate an instance,
-- we can also evacuate it
prop_ClusterAllocEvac node inst =
    forAll (choose (4, 8)) $ \count ->
    not (Node.offline node)
            && not (Node.failN1 node)
            && isNodeBig node 4
            ==>
    let nl = makeSmallCluster node count
        il = Container.empty
        inst' = setInstanceSmallerThanNode node inst
729
730
    in case Cluster.genAllocNodes defGroupList nl 2 True >>=
       Cluster.tryAlloc nl il inst' of
731
         Types.Bad _ -> False
732
733
         Types.Ok as ->
             case Cluster.asSolutions as of
734
               [] -> False
735
               (xnl, xi, _, _):[] ->
736
737
                   let sdx = Instance.sNode xi
                       il' = Container.add (Instance.idx xi) xi il
738
                   in case Cluster.tryEvac xnl il' [Instance.idx xi] [sdx] of
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
                        Just _ -> True
                        _ -> False
               _ -> False

-- | Check that allocating multiple instances on a cluster, then
-- adding an empty node, results in a valid rebalance
prop_ClusterAllocBalance node =
    forAll (choose (3, 5)) $ \count ->
    not (Node.offline node)
            && not (Node.failN1 node)
            && isNodeBig node 4
            && not (isNodeBig node 8)
            ==>
    let nl = makeSmallCluster node count
        (hnode, nl') = IntMap.deleteFindMax nl
        il = Container.empty
755
        allocnodes = Cluster.genAllocNodes defGroupList nl' 2 True
756
        i_templ = createInstance Types.unitMem Types.unitDsk Types.unitCpu
Iustin Pop's avatar
Iustin Pop committed
757
758
    in case allocnodes >>= \allocnodes' ->
        Cluster.iterateAlloc nl' il i_templ allocnodes' [] [] of
759
         Types.Bad _ -> False
760
         Types.Ok (_, xnl, il', _, _) ->
761
762
763
764
765
                   let ynl = Container.add (Node.idx hnode) hnode xnl
                       cv = Cluster.compCV ynl
                       tbl = Cluster.Table ynl il' cv []
                   in canBalance tbl True False

766
767
768
769
-- | Checks consistency
prop_ClusterCheckConsistency node inst =
  let nl = makeSmallCluster node 3
      [node1, node2, node3] = Container.elems nl
Iustin Pop's avatar
Iustin Pop committed
770
      node3' = node3 { Node.group = 1 }
771
772
773
774
      nl' = Container.add (Node.idx node3') node3' nl
      inst1 = Instance.setBoth inst (Node.idx node1) (Node.idx node2)
      inst2 = Instance.setBoth inst (Node.idx node1) Node.noSecondary
      inst3 = Instance.setBoth inst (Node.idx node1) (Node.idx node3)
Iustin Pop's avatar
Iustin Pop committed
775
      ccheck = Cluster.findSplitInstances nl' . Container.fromList
776
777
778
779
  in null (ccheck [(0, inst1)]) &&
     null (ccheck [(0, inst2)]) &&
     (not . null $ ccheck [(0, inst3)])

780
781
782
783
784
785
786
787
788
789
-- For now, we only test that we don't lose instances during the split
prop_ClusterSplitCluster node inst =
  forAll (choose (0, 100)) $ \icnt ->
  let nl = makeSmallCluster node 2
      (nl', il') = foldl (\(ns, is) _ -> assignInstance ns is inst 0 1)
                   (nl, Container.empty) [1..icnt]
      gni = Cluster.splitCluster nl' il'
  in sum (map (Container.size . snd . snd) gni) == icnt &&
     all (\(guuid, (nl'', _)) -> all ((== guuid) . Node.group)
                                 (Container.elems nl'')) gni
790

791
testCluster =
792
    [ run prop_Score_Zero
Iustin Pop's avatar
Iustin Pop committed
793
    , run prop_CStats_sane
794
795
796
797
    , run prop_ClusterAlloc_sane
    , run prop_ClusterCanTieredAlloc
    , run prop_ClusterAllocEvac
    , run prop_ClusterAllocBalance
798
    , run prop_ClusterCheckConsistency
799
    , run prop_ClusterSplitCluster
800
    ]
Iustin Pop's avatar
Iustin Pop committed
801
802
803
804
805
806
807

-- | Check that opcode serialization is idempotent

prop_OpCodes_serialization op =
  case J.readJSON (J.showJSON op) of
    J.Error _ -> False
    J.Ok op' -> op == op'
808
  where _types = op::OpCodes.OpCode
Iustin Pop's avatar
Iustin Pop committed
809
810
811
812

testOpCodes =
  [ run prop_OpCodes_serialization
  ]
Iustin Pop's avatar
Iustin Pop committed
813

814
-- | Check that (queued) job\/opcode status serialization is idempotent
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
prop_OpStatus_serialization os =
  case J.readJSON (J.showJSON os) of
    J.Error _ -> False
    J.Ok os' -> os == os'
  where _types = os::Jobs.OpStatus

prop_JobStatus_serialization js =
  case J.readJSON (J.showJSON js) of
    J.Error _ -> False
    J.Ok js' -> js == js'
  where _types = js::Jobs.JobStatus

testJobs =
  [ run prop_OpStatus_serialization
  , run prop_JobStatus_serialization
  ]

Iustin Pop's avatar
Iustin Pop committed
832
833
834
-- | Loader tests

prop_Loader_lookupNode ktn inst node =
835
836
  Loader.lookupNode nl inst node == Data.Map.lookup node nl
  where nl = Data.Map.fromList ktn
Iustin Pop's avatar
Iustin Pop committed
837
838

prop_Loader_lookupInstance kti inst =
839
840
841
842
843
844
845
846
  Loader.lookupInstance il inst == Data.Map.lookup inst il
  where il = Data.Map.fromList kti

prop_Loader_assignIndices nodes =
  Data.Map.size nassoc == length nodes &&
  Container.size kt == length nodes &&
  (if not (null nodes)
   then maximum (IntMap.keys kt) == length nodes - 1
Iustin Pop's avatar
Iustin Pop committed
847
   else True)
848
  where (nassoc, kt) = Loader.assignIndices (map (\n -> (Node.name n, n)) nodes)
Iustin Pop's avatar
Iustin Pop committed
849
850
851
852
853


-- | Checks that the number of primary instances recorded on the nodes
-- is zero
prop_Loader_mergeData ns =
Iustin Pop's avatar
Iustin Pop committed
854
  let na = Container.fromList $ map (\n -> (Node.idx n, n)) ns
855
  in case Loader.mergeData [] [] []
856
         (Loader.emptyCluster {Loader.cdNodes = na}) of
Iustin Pop's avatar
Iustin Pop committed
857
    Types.Bad _ -> False
858
    Types.Ok (Loader.ClusterData _ nl il _) ->
Iustin Pop's avatar
Iustin Pop committed
859
860
861
      let nodes = Container.elems nl
          instances = Container.elems il
      in (sum . map (length . Node.pList)) nodes == 0 &&
862
         null instances
Iustin Pop's avatar
Iustin Pop committed
863
864
865
866
867
868
869

testLoader =
  [ run prop_Loader_lookupNode
  , run prop_Loader_lookupInstance
  , run prop_Loader_assignIndices
  , run prop_Loader_mergeData
  ]