QC.hs 32.9 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
Iustin Pop's avatar
Iustin Pop committed
40
import Data.List (findIndex, intercalate, nub, isPrefixOf)
41
import Data.Maybe
Iustin Pop's avatar
Iustin Pop committed
42
43
import Control.Monad
import qualified Text.JSON as J
Iustin Pop's avatar
Iustin Pop committed
44
import qualified Data.Map
45
import qualified Data.IntMap as IntMap
Iustin Pop's avatar
Iustin Pop committed
46
import qualified Ganeti.OpCodes as OpCodes
47
import qualified Ganeti.Jobs as Jobs
Iustin Pop's avatar
Iustin Pop committed
48
import qualified Ganeti.Luxi
49
50
51
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
52
import qualified Ganeti.HTools.ExtLoader
53
54
55
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
56
import qualified Ganeti.HTools.Luxi
57
import qualified Ganeti.HTools.Node as Node
Iustin Pop's avatar
Iustin Pop committed
58
import qualified Ganeti.HTools.Group as Group
59
import qualified Ganeti.HTools.PeerMap as PeerMap
60
import qualified Ganeti.HTools.Rapi
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
import qualified Ganeti.Constants as C
67

Iustin Pop's avatar
Iustin Pop committed
68
69
70
run :: Testable prop => prop -> Args -> IO Result
run = flip quickCheckWithResult

71
72
-- * Constants

Iustin Pop's avatar
Iustin Pop committed
73
74
75
76
-- | Maximum memory (1TiB, somewhat random value)
maxMem :: Int
maxMem = 1024 * 1024

Iustin Pop's avatar
Iustin Pop committed
77
-- | Maximum disk (8TiB, somewhat random value)
Iustin Pop's avatar
Iustin Pop committed
78
maxDsk :: Int
Iustin Pop's avatar
Iustin Pop committed
79
maxDsk = 1024 * 1024 * 8
Iustin Pop's avatar
Iustin Pop committed
80
81
82
83
84

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

Iustin Pop's avatar
Iustin Pop committed
85
86
87
88
89
90
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
91
defGroupList = Container.fromList [(Group.idx defGroup, defGroup)]
Iustin Pop's avatar
Iustin Pop committed
92
93
94
95

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

96
97
-- * Helper functions

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

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

-- | Create an instance given its spec
createInstance mem dsk vcpus =
112
    Instance.create "inst-unnamed" mem dsk vcpus "running" [] True (-1) (-1)
113
114
115
116
117
118
119

-- | 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
120
    in nlst
121
122
123
124
125
126
127

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

128
129
canBalance :: Cluster.Table -> Bool -> Bool -> Bool -> Bool
canBalance tbl dm im evac = isJust $ Cluster.tryBalance tbl dm im evac 0 0
130

131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
-- | 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')

150
151
-- * Arbitrary instances

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)
179
180
181
182
183
184
185
186
      run_st <- elements [ C.inststErrorup
                         , C.inststErrordown
                         , C.inststAdmindown
                         , C.inststNodedown
                         , C.inststNodeoffline
                         , C.inststRunning
                         , "no_such_status1"
                         , "no_such_status2"]
187
188
      pn <- arbitrary
      sn <- arbitrary
Iustin Pop's avatar
Iustin Pop committed
189
      vcpus <- choose (0, maxCpu)
190
      return $ Instance.create name mem dsk vcpus run_st [] True pn sn
191

192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
genNode :: Maybe Int -> Maybe Int -> Gen Node.Node
genNode min_multiplier max_multiplier = do
  let (base_mem, base_dsk, base_cpu) =
          case min_multiplier of
            Just mm -> (mm * Types.unitMem,
                        mm * Types.unitDsk,
                        mm * Types.unitCpu)
            Nothing -> (0, 0, 0)
      (top_mem, top_dsk, top_cpu)  =
          case max_multiplier of
            Just mm -> (mm * Types.unitMem,
                        mm * Types.unitDsk,
                        mm * Types.unitCpu)
            Nothing -> (maxMem, maxDsk, maxCpu)
  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
  return $ Node.buildPeers n Container.empty

218
219
-- and a random node
instance Arbitrary Node.Node where
220
    arbitrary = genNode Nothing Nothing
221

Iustin Pop's avatar
Iustin Pop committed
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
-- 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" ->
241
          liftM5 OpCodes.OpInstanceReplaceDisks arbitrary arbitrary
Iustin Pop's avatar
Iustin Pop committed
242
243
          arbitrary arbitrary arbitrary
        "OP_INSTANCE_FAILOVER" ->
244
          liftM2 OpCodes.OpInstanceFailover arbitrary arbitrary
Iustin Pop's avatar
Iustin Pop committed
245
        "OP_INSTANCE_MIGRATE" ->
246
          liftM4 OpCodes.OpInstanceMigrate arbitrary arbitrary arbitrary
247
          arbitrary
Iustin Pop's avatar
Iustin Pop committed
248
249
        _ -> fail "Wrong opcode")

250
251
252
253
254
255
instance Arbitrary Jobs.OpStatus where
  arbitrary = elements [minBound..maxBound]

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

256
-- * Actual tests
Iustin Pop's avatar
Iustin Pop committed
257

258
259
260
261
262
263
264
265
-- 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

266
267
268
269
270
271
272
273
-- | fromObjWithDefault, we test using the Maybe monad and an integer
-- value
prop_Utils_fromObjWithDefault def_value random_key =
    -- a missing key will be returned with the default
    Utils.fromObjWithDefault [] random_key def_value == Just def_value &&
    -- a found key will be returned as is, not with default
    Utils.fromObjWithDefault [(random_key, J.showJSON def_value)]
         random_key (def_value+1) == Just def_value
Iustin Pop's avatar
Iustin Pop committed
274
        where _types = def_value :: Integer
275

276
277
278
testUtils =
  [ run prop_Utils_commaJoinSplit
  , run prop_Utils_commaSplitJoin
279
  , run prop_Utils_fromObjWithDefault
280
281
  ]

282
-- | Make sure add is idempotent
Iustin Pop's avatar
Iustin Pop committed
283
prop_PeerMap_addIdempotent pmap key em =
284
    fn puniq == fn (fn puniq)
285
    where _types = (pmap::PeerMap.PeerMap,
Iustin Pop's avatar
Iustin Pop committed
286
287
                    key::PeerMap.Key, em::PeerMap.Elem)
          fn = PeerMap.add key em
288
          puniq = PeerMap.accumArray const pmap
289
290
291
292

-- | Make sure remove is idempotent
prop_PeerMap_removeIdempotent pmap key =
    fn puniq == fn (fn puniq)
293
294
    where _types = (pmap::PeerMap.PeerMap, key::PeerMap.Key)
          fn = PeerMap.remove key
295
296
297
298
299
          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
300
    where _types = (pmap::PeerMap.PeerMap, key::PeerMap.Key)
301
302
303
          puniq = PeerMap.accumArray const pmap

-- | Make sure an added item is found
Iustin Pop's avatar
Iustin Pop committed
304
305
prop_PeerMap_addFind pmap key em =
    PeerMap.find key (PeerMap.add key em puniq) == em
306
    where _types = (pmap::PeerMap.PeerMap,
Iustin Pop's avatar
Iustin Pop committed
307
                    key::PeerMap.Key, em::PeerMap.Elem)
308
          puniq = PeerMap.accumArray const pmap
309
310
311
312
313

-- | 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
314
    where _types = pmap::PeerMap.PeerMap
315
316
          puniq = PeerMap.accumArray const pmap

317
testPeerMap =
318
319
320
321
322
323
324
    [ run prop_PeerMap_addIdempotent
    , run prop_PeerMap_removeIdempotent
    , run prop_PeerMap_maxElem
    , run prop_PeerMap_addFind
    , run prop_PeerMap_findMissing
    ]

325
326
327
328
329
330
331
332
333
334
-- 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

335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
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
356
      nl' = Container.fromList nodes'
357
358
359
360
361
      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

362
testContainer =
363
364
365
366
    [ run prop_Container_addTwo
    , run prop_Container_nameOf
    , run prop_Container_findByName
    ]
367

368
369
-- Simple instance tests, we only have setter/getters

Iustin Pop's avatar
Iustin Pop committed
370
371
372
prop_Instance_creat inst =
    Instance.name inst == Instance.alias inst

373
374
375
376
377
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
378
379
380
381
382
383
384
385
    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
386
    where _types = (inst::Instance.Instance, name::String)
Iustin Pop's avatar
Iustin Pop committed
387
          newinst = Instance.setAlias inst name
388
389

prop_Instance_setPri inst pdx =
390
    Instance.pNode (Instance.setPri inst pdx) == pdx
391
392
393
    where _types = (inst::Instance.Instance, pdx::Types.Ndx)

prop_Instance_setSec inst sdx =
394
    Instance.sNode (Instance.setSec inst sdx) == sdx
395
396
397
    where _types = (inst::Instance.Instance, sdx::Types.Ndx)

prop_Instance_setBoth inst pdx sdx =
398
    Instance.pNode si == pdx && Instance.sNode si == sdx
399
400
401
    where _types = (inst::Instance.Instance, pdx::Types.Ndx, sdx::Types.Ndx)
          si = Instance.setBoth inst pdx sdx

Iustin Pop's avatar
Iustin Pop committed
402
403
prop_Instance_runStatus_True inst =
    let run_st = Instance.running inst
404
        run_tx = Instance.runSt inst
Iustin Pop's avatar
Iustin Pop committed
405
    in
406
      run_tx `elem` Instance.runningStates ==> run_st
Iustin Pop's avatar
Iustin Pop committed
407
408
409

prop_Instance_runStatus_False inst =
    let run_st = Instance.running inst
410
        run_tx = Instance.runSt inst
Iustin Pop's avatar
Iustin Pop committed
411
    in
412
      run_tx `notElem` Instance.runningStates ==> not run_st
Iustin Pop's avatar
Iustin Pop committed
413

Iustin Pop's avatar
Iustin Pop committed
414
415
416
417
418
419
420
421
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 =
Iustin Pop's avatar
Iustin Pop committed
422
423
424
    forAll (choose (0, 2 * Types.unitMem - 1)) $ \mem ->
    let inst' = inst { Instance.mem = mem}
    in Types.isBad $ Instance.shrinkByType inst' Types.FailMem
Iustin Pop's avatar
Iustin Pop committed
425
426
427
428
429
430
431
432
433

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 =
Iustin Pop's avatar
Iustin Pop committed
434
435
436
    forAll (choose (0, 2 * Types.unitCpu - 1)) $ \vcpus ->
    let inst' = inst { Instance.vcpus = vcpus }
    in Types.isBad $ Instance.shrinkByType inst' Types.FailCPU
Iustin Pop's avatar
Iustin Pop committed
437
438
439
440
441
442
443
444
445

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 =
Iustin Pop's avatar
Iustin Pop committed
446
447
448
    forAll (choose (0, 2 * Types.unitDsk - 1)) $ \dsk ->
    let inst' = inst { Instance.dsk = dsk }
    in Types.isBad $ Instance.shrinkByType inst' Types.FailDisk
Iustin Pop's avatar
Iustin Pop committed
449
450
451

prop_Instance_setMovable inst m =
    Instance.movable inst' == m
452
    where inst' = Instance.setMovable inst m
Iustin Pop's avatar
Iustin Pop committed
453

454
testInstance =
Iustin Pop's avatar
Iustin Pop committed
455
456
    [ run prop_Instance_creat
    , run prop_Instance_setIdx
457
    , run prop_Instance_setName
Iustin Pop's avatar
Iustin Pop committed
458
    , run prop_Instance_setAlias
459
460
461
    , run prop_Instance_setPri
    , run prop_Instance_setSec
    , run prop_Instance_setBoth
Iustin Pop's avatar
Iustin Pop committed
462
463
    , run prop_Instance_runStatus_True
    , run prop_Instance_runStatus_False
Iustin Pop's avatar
Iustin Pop committed
464
465
466
467
468
469
470
    , 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
471
472
473
474
    ]

-- Instance text loader tests

Iustin Pop's avatar
Iustin Pop committed
475
prop_Text_Load_Instance name mem dsk vcpus status pnode snode pdx sdx autobal =
Iustin Pop's avatar
Iustin Pop committed
476
    not (null pnode) && pdx >= 0 && sdx >= 0 ==>
Iustin Pop's avatar
Iustin Pop committed
477
478
479
480
481
482
    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
483
484
485
        ndx = if null snode
              then [(pnode, pdx)]
              else [(pnode, pdx), (snode, rsdx)]
486
        nl = Data.Map.fromList ndx
487
        tags = ""
Iustin Pop's avatar
Iustin Pop committed
488
        sbal = if autobal then "Y" else "N"
489
        inst = Text.loadInst nl
Iustin Pop's avatar
Iustin Pop committed
490
491
               [name, mem_s, dsk_s, vcpus_s, status,
                sbal, pnode, snode, tags]:: Maybe (String, Instance.Instance)
492
        fail1 = Text.loadInst nl
Iustin Pop's avatar
Iustin Pop committed
493
494
               [name, mem_s, dsk_s, vcpus_s, status,
                sbal, pnode, pnode, tags]:: Maybe (String, Instance.Instance)
Iustin Pop's avatar
Iustin Pop committed
495
496
497
        _types = ( name::String, mem::Int, dsk::Int
                 , vcpus::Int, status::String
                 , pnode::String, snode::String
Iustin Pop's avatar
Iustin Pop committed
498
499
                 , pdx::Types.Ndx, sdx::Types.Ndx
                 , autobal::Bool)
Iustin Pop's avatar
Iustin Pop committed
500
501
502
503
    in
      case inst of
        Nothing -> False
        Just (_, i) ->
Iustin Pop's avatar
Iustin Pop committed
504
505
506
507
508
509
510
511
512
            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 rsdx) &&
            Instance.auto_balance i == autobal &&
            isNothing fail1
Iustin Pop's avatar
Iustin Pop committed
513
514

prop_Text_Load_InstanceFail ktn fields =
Iustin Pop's avatar
Iustin Pop committed
515
516
    length fields /= 9 ==>
    case Text.loadInst nl fields of
Iustin Pop's avatar
Iustin Pop committed
517
518
      Types.Ok _ -> False
      Types.Bad msg -> "Invalid/incomplete instance data: '" `isPrefixOf` msg
519
    where nl = Data.Map.fromList ktn
Iustin Pop's avatar
Iustin Pop committed
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534

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
535
536
537
        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
538
539
540
541
         Nothing -> False
         Just (name', node) ->
             if fo || any_broken
             then Node.offline node
542
543
544
545
546
547
548
549
             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
550
551

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

554
prop_Text_NodeLSIdempotent node =
Iustin Pop's avatar
Iustin Pop committed
555
556
    (Text.loadNode defGroupAssoc.
         Utils.sepSplit '|' . Text.serializeNode defGroupList) n ==
557
558
559
560
    Just (Node.name n, n)
    -- override failN1 to what loadNode returns by default
    where n = node { Node.failN1 = True, Node.offline = False }

561
testText =
Iustin Pop's avatar
Iustin Pop committed
562
    [ run prop_Text_Load_Instance
Iustin Pop's avatar
Iustin Pop committed
563
564
565
    , run prop_Text_Load_InstanceFail
    , run prop_Text_Load_Node
    , run prop_Text_Load_NodeFail
566
    , run prop_Text_NodeLSIdempotent
567
568
569
570
    ]

-- Node tests

Iustin Pop's avatar
Iustin Pop committed
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
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

589
-- | Check that an instance add with too high memory or disk will be rejected
Iustin Pop's avatar
Iustin Pop committed
590
591
592
593
594
595
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
596
    where _types = (node::Node.Node, inst::Instance.Instance)
Iustin Pop's avatar
Iustin Pop committed
597
598
599
600
601
602
603
604
605
606
607
608
609
          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 }

Iustin Pop's avatar
Iustin Pop committed
610
611
612
613
614
prop_Node_addPriFC node inst (Positive extra) =
    not (Node.failN1 node) ==>
        case Node.addPri node inst'' of
          Types.OpFail Types.FailCPU -> True
          _ -> False
Iustin Pop's avatar
Iustin Pop committed
615
616
    where _types = (node::Node.Node, inst::Instance.Instance)
          inst' = setInstanceSmallerThanNode node inst
Iustin Pop's avatar
Iustin Pop committed
617
          inst'' = inst' { Instance.vcpus = Node.availCpu node + extra }
618
619

-- | Check that an instance add with too high memory or disk will be rejected
620
prop_Node_addSec node inst pdx =
621
622
    (Instance.mem inst >= (Node.fMem node - Node.rMem node) ||
     Instance.dsk inst >= Node.fDsk node) &&
Iustin Pop's avatar
Iustin Pop committed
623
    not (Node.failN1 node)
Iustin Pop's avatar
Iustin Pop committed
624
    ==> isFailure (Node.addSec node inst pdx)
625
        where _types = (node::Node.Node, inst::Instance.Instance, pdx::Int)
626

627
628
629
630
-- | Checks for memory reservation changes
prop_Node_rMem node inst =
    -- ab = auto_balance, nb = non-auto_balance
    -- we use -1 as the primary node of the instance
Iustin Pop's avatar
Iustin Pop committed
631
    let inst' = inst { Instance.pNode = -1, Instance.auto_balance = True }
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
        inst_ab = setInstanceSmallerThanNode node inst'
        inst_nb = inst_ab { Instance.auto_balance = False }
        -- now we have the two instances, identical except the
        -- auto_balance 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 (flip Node.removeSec inst_ab) node_add_ab
        node_del_nb = liftM (flip 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) ->
             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 &&
             not (inst_idx `elem` Node.sList d_ab)
         _ -> False

Iustin Pop's avatar
Iustin Pop committed
656
657
658
659
660
661
662
663
664
665
666
667
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
668
669
    fromIntegral (Node.availDisk node') <= Node.tDsk node' &&
    Node.mDsk node' == mx'
Iustin Pop's avatar
Iustin Pop committed
670
671
672
673
674
675
676
    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
677
    where m = Data.Map.empty
Iustin Pop's avatar
Iustin Pop committed
678
679
680
681

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

Iustin Pop's avatar
Iustin Pop committed
684
685
686
687
688
prop_Node_showField node =
  forAll (elements Node.defaultFields) $ \ field ->
  fst (Node.showHeader field) /= Types.unknownField &&
  Node.showField node field /= Types.unknownField

689
690
691
692
693
694
695

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
696
     (null nodes || not (null ng))
697

698
testNode =
Iustin Pop's avatar
Iustin Pop committed
699
700
701
702
703
    [ 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
704
705
    , run prop_Node_addPriFD
    , run prop_Node_addPriFC
706
    , run prop_Node_addSec
707
    , run prop_Node_rMem
Iustin Pop's avatar
Iustin Pop committed
708
709
710
    , run prop_Node_setMdsk
    , run prop_Node_tagMaps_idempotent
    , run prop_Node_tagMaps_reject
Iustin Pop's avatar
Iustin Pop committed
711
    , run prop_Node_showField
712
    , run prop_Node_computeGroups
713
    ]
714
715
716
717
718


-- Cluster tests

-- | Check that the cluster score is close to zero for a homogeneous cluster
Iustin Pop's avatar
Iustin Pop committed
719
720
prop_Score_Zero node =
    forAll (choose (1, 1024)) $ \count ->
Iustin Pop's avatar
Iustin Pop committed
721
    (not (Node.offline node) && not (Node.failN1 node) && (count > 0) &&
722
     (Node.tDsk node > 0) && (Node.tMem node > 0)) ==>
723
    let fn = Node.buildPeers node Container.empty
Iustin Pop's avatar
Iustin Pop committed
724
        nlst = zip [1..] $ replicate count fn::[(Types.Ndx, Node.Node)]
Iustin Pop's avatar
Iustin Pop committed
725
        nl = Container.fromList nlst
726
727
728
        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
729
    in score <= 1e-12
730

Iustin Pop's avatar
Iustin Pop committed
731
-- | Check that cluster stats are sane
Iustin Pop's avatar
Iustin Pop committed
732
733
734
prop_CStats_sane node =
    forAll (choose (1, 1024)) $ \count ->
    (not (Node.offline node) && not (Node.failN1 node) &&
735
     (Node.availDisk node > 0) && (Node.availMem node > 0)) ==>
Iustin Pop's avatar
Iustin Pop committed
736
737
    let fn = Node.buildPeers node Container.empty
        nlst = zip [1..] $ replicate count fn::[(Types.Ndx, Node.Node)]
Iustin Pop's avatar
Iustin Pop committed
738
        nl = Container.fromList nlst
Iustin Pop's avatar
Iustin Pop committed
739
740
741
742
        cstats = Cluster.totalResources nl
    in Cluster.csAdsk cstats >= 0 &&
       Cluster.csAdsk cstats <= Cluster.csFdsk cstats

743
744
745
746
747
748
749
750
751
752
753
754
-- | 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
755
756
    in case Cluster.genAllocNodes defGroupList nl 2 True >>=
       Cluster.tryAlloc nl il inst' of
757
         Types.Bad _ -> False
758
759
         Types.Ok as ->
             case Cluster.asSolutions as of
760
               [] -> False
761
               (xnl, xi, _, cv):[] ->
762
                   let il' = Container.add (Instance.idx xi) xi il
763
                       tbl = Cluster.Table xnl il' cv []
764
                   in not (canBalance tbl True True False)
765
766
767
768
769
770
771
772
773
774
775
776
777
778
               _ -> 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
779
        allocnodes = Cluster.genAllocNodes defGroupList nl rqnodes True
Iustin Pop's avatar
Iustin Pop committed
780
781
    in case allocnodes >>= \allocnodes' ->
        Cluster.tieredAlloc nl il inst allocnodes' [] [] of
782
         Types.Bad _ -> False
783
784
785
         Types.Ok (_, _, il', ixes, cstats) -> not (null ixes) &&
                                      IntMap.size il' == length ixes &&
                                      length ixes == length cstats
786
787
788
789
790
791
792
793
794
795
796
797

-- | 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
798
799
    in case Cluster.genAllocNodes defGroupList nl 2 True >>=
       Cluster.tryAlloc nl il inst' of
800
         Types.Bad _ -> False
801
802
         Types.Ok as ->
             case Cluster.asSolutions as of
803
               [] -> False
804
               (xnl, xi, _, _):[] ->
805
806
                   let sdx = Instance.sNode xi
                       il' = Container.add (Instance.idx xi) xi il
807
                   in case Cluster.tryEvac xnl il' [Instance.idx xi] [sdx] of
808
809
810
811
812
813
                        Just _ -> True
                        _ -> False
               _ -> False

-- | Check that allocating multiple instances on a cluster, then
-- adding an empty node, results in a valid rebalance
814
815
prop_ClusterAllocBalance =
    forAll (genNode (Just 5) (Just 128)) $ \node ->
816
    forAll (choose (3, 5)) $ \count ->
817
    not (Node.offline node) && not (Node.failN1 node) ==>
818
819
820
    let nl = makeSmallCluster node count
        (hnode, nl') = IntMap.deleteFindMax nl
        il = Container.empty
821
        allocnodes = Cluster.genAllocNodes defGroupList nl' 2 True
822
        i_templ = createInstance Types.unitMem Types.unitDsk Types.unitCpu
Iustin Pop's avatar
Iustin Pop committed
823
824
    in case allocnodes >>= \allocnodes' ->
        Cluster.iterateAlloc nl' il i_templ allocnodes' [] [] of
825
         Types.Bad _ -> False
826
         Types.Ok (_, xnl, il', _, _) ->
827
828
829
                   let ynl = Container.add (Node.idx hnode) hnode xnl
                       cv = Cluster.compCV ynl
                       tbl = Cluster.Table ynl il' cv []
830
                   in canBalance tbl True True False
831

832
833
834
835
-- | Checks consistency
prop_ClusterCheckConsistency node inst =
  let nl = makeSmallCluster node 3
      [node1, node2, node3] = Container.elems nl
Iustin Pop's avatar
Iustin Pop committed
836
      node3' = node3 { Node.group = 1 }
837
838
839
840
      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
841
      ccheck = Cluster.findSplitInstances nl' . Container.fromList
842
843
844
845
  in null (ccheck [(0, inst1)]) &&
     null (ccheck [(0, inst2)]) &&
     (not . null $ ccheck [(0, inst3)])

846
847
848
849
850
851
852
853
854
855
-- 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
856

857
testCluster =
858
    [ run prop_Score_Zero
Iustin Pop's avatar
Iustin Pop committed
859
    , run prop_CStats_sane
860
861
862
863
    , run prop_ClusterAlloc_sane
    , run prop_ClusterCanTieredAlloc
    , run prop_ClusterAllocEvac
    , run prop_ClusterAllocBalance
864
    , run prop_ClusterCheckConsistency
865
    , run prop_ClusterSplitCluster
866
    ]
Iustin Pop's avatar
Iustin Pop committed
867
868
869
870
871
872
873

-- | 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'
874
  where _types = op::OpCodes.OpCode
Iustin Pop's avatar
Iustin Pop committed
875
876
877
878

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

880
-- | Check that (queued) job\/opcode status serialization is idempotent
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
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
898
899
900
-- | Loader tests

prop_Loader_lookupNode ktn inst node =
901
902
  Loader.lookupNode nl inst node == Data.Map.lookup node nl
  where nl = Data.Map.fromList ktn
Iustin Pop's avatar
Iustin Pop committed
903
904

prop_Loader_lookupInstance kti inst =
905
906
907
908
909
910
911
912
  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
913
   else True)
914
  where (nassoc, kt) = Loader.assignIndices (map (\n -> (Node.name n, n)) nodes)
Iustin Pop's avatar
Iustin Pop committed
915
916
917
918
919


-- | Checks that the number of primary instances recorded on the nodes
-- is zero
prop_Loader_mergeData ns =
Iustin Pop's avatar
Iustin Pop committed
920
  let na = Container.fromList $ map (\n -> (Node.idx n, n)) ns
921
  in case Loader.mergeData [] [] [] []
922
         (Loader.emptyCluster {Loader.cdNodes = na}) of
Iustin Pop's avatar
Iustin Pop committed
923
    Types.Bad _ -> False
924
    Types.Ok (Loader.ClusterData _ nl il _) ->
Iustin Pop's avatar
Iustin Pop committed
925
926
927
      let nodes = Container.elems nl
          instances = Container.elems il
      in (sum . map (length . Node.pList)) nodes == 0 &&
928
         null instances
Iustin Pop's avatar
Iustin Pop committed
929
930
931
932
933
934
935

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