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

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

-}

{-

9
Copyright (C) 2009, 2010, 2011 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
58
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
59
import qualified Ganeti.HTools.Luxi
60
import qualified Ganeti.HTools.Node as Node
Iustin Pop's avatar
Iustin Pop committed
61
import qualified Ganeti.HTools.Group as Group
62
import qualified Ganeti.HTools.PeerMap as PeerMap
63
import qualified Ganeti.HTools.Rapi
Iustin Pop's avatar
Iustin Pop committed
64
import qualified Ganeti.HTools.Simu
65
66
67
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
68
import qualified Ganeti.HTools.Version
69
import qualified Ganeti.Constants as C
70

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

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

78
79
-- * Constants

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

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

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

Iustin Pop's avatar
Iustin Pop committed
92
93
defGroup :: Group.Group
defGroup = flip Group.setIdx 0 $
Iustin Pop's avatar
Iustin Pop committed
94
             Group.create "default" Utils.defaultGroupID Types.AllocPreferred
Iustin Pop's avatar
Iustin Pop committed
95
96

defGroupList :: Group.List
Iustin Pop's avatar
Iustin Pop committed
97
defGroupList = Container.fromList [(Group.idx defGroup, defGroup)]
Iustin Pop's avatar
Iustin Pop committed
98
99
100
101

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

102
103
-- * Helper functions

104
-- | Simple checker for whether OpResult is fail or pass.
Iustin Pop's avatar
Iustin Pop committed
105
106
107
108
isFailure :: Types.OpResult a -> Bool
isFailure (Types.OpFail _) = True
isFailure _ = False

Iustin Pop's avatar
Iustin Pop committed
109
110
111
112
113
114
115
-- | 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 ==?

116
-- | Update an instance to be smaller than a node.
117
setInstanceSmallerThanNode node inst =
Iustin Pop's avatar
Iustin Pop committed
118
119
120
121
  inst { Instance.mem = Node.availMem node `div` 2
       , Instance.dsk = Node.availDisk node `div` 2
       , Instance.vcpus = Node.availCpu node `div` 2
       }
122

123
-- | Create an instance given its spec.
124
createInstance mem dsk vcpus =
Iustin Pop's avatar
Iustin Pop committed
125
126
  Instance.create "inst-unnamed" mem dsk vcpus Types.Running [] True (-1) (-1)
    Types.DTDrbd8
127

128
-- | Create a small cluster by repeating a node spec.
129
130
makeSmallCluster :: Node.Node -> Int -> Node.List
makeSmallCluster node count =
Iustin Pop's avatar
Iustin Pop committed
131
132
133
134
  let fn = Node.buildPeers node Container.empty
      namelst = map (\n -> (Node.name n, n)) (replicate count fn)
      (_, nlst) = Loader.assignIndices namelst
  in nlst
135

136
-- | Checks if a node is "big" enough.
137
138
139
140
141
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

142
143
canBalance :: Cluster.Table -> Bool -> Bool -> Bool -> Bool
canBalance tbl dm im evac = isJust $ Cluster.tryBalance tbl dm im evac 0 0
144

145
-- | Assigns a new fresh instance to a cluster; this is not
146
-- allocation, so no resource checks are done.
147
148
149
150
151
152
153
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
154
155
                  then 0
                  else fst (Container.findMax il) + 1
156
157
158
159
160
161
162
163
      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')

164
165
-- * Arbitrary instances

166
-- | Defines a DNS name.
167
newtype DNSChar = DNSChar { dnsGetChar::Char }
168

169
instance Arbitrary DNSChar where
Iustin Pop's avatar
Iustin Pop committed
170
171
172
  arbitrary = do
    x <- elements (['a'..'z'] ++ ['0'..'9'] ++ "_-")
    return (DNSChar x)
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187

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

188
instance Arbitrary Types.InstanceStatus where
189
    arbitrary = elements [minBound..maxBound]
190

191
192
-- let's generate a random instance
instance Arbitrary Instance.Instance where
Iustin Pop's avatar
Iustin Pop committed
193
194
195
196
197
198
199
200
201
202
  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
203

204
205
206
207
208
-- | 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
209
210
genNode min_multiplier max_multiplier = do
  let (base_mem, base_dsk, base_cpu) =
Iustin Pop's avatar
Iustin Pop committed
211
212
213
214
215
        case min_multiplier of
          Just mm -> (mm * Types.unitMem,
                      mm * Types.unitDsk,
                      mm * Types.unitCpu)
          Nothing -> (0, 0, 0)
216
      (top_mem, top_dsk, top_cpu)  =
Iustin Pop's avatar
Iustin Pop committed
217
218
219
220
221
        case max_multiplier of
          Just mm -> (mm * Types.unitMem,
                      mm * Types.unitDsk,
                      mm * Types.unitCpu)
          Nothing -> (maxMem, maxDsk, maxCpu)
222
223
224
225
226
227
228
229
230
231
232
233
  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

234
235
-- and a random node
instance Arbitrary Node.Node where
Iustin Pop's avatar
Iustin Pop committed
236
  arbitrary = genNode Nothing Nothing
237

Iustin Pop's avatar
Iustin Pop committed
238
239
-- replace disks
instance Arbitrary OpCodes.ReplaceDisksMode where
240
  arbitrary = elements [minBound..maxBound]
Iustin Pop's avatar
Iustin Pop committed
241
242
243
244
245
246
247
248
249

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
Iustin Pop's avatar
Iustin Pop committed
250
251
252
253
254
255
256
257
258
259
260
261
       "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
262

263
264
265
266
267
268
instance Arbitrary Jobs.OpStatus where
  arbitrary = elements [minBound..maxBound]

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

269
270
newtype SmallRatio = SmallRatio Double deriving Show
instance Arbitrary SmallRatio where
Iustin Pop's avatar
Iustin Pop committed
271
272
273
  arbitrary = do
    v <- choose (0, 1)
    return $ SmallRatio v
274

275
276
277
278
279
280
instance Arbitrary Types.AllocPolicy where
  arbitrary = elements [minBound..maxBound]

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

281
instance Arbitrary Types.FailMode where
Iustin Pop's avatar
Iustin Pop committed
282
  arbitrary = elements [minBound..maxBound]
283
284

instance Arbitrary a => Arbitrary (Types.OpResult a) where
Iustin Pop's avatar
Iustin Pop committed
285
286
287
288
  arbitrary = arbitrary >>= \c ->
              case c of
                False -> liftM Types.OpFail arbitrary
                True -> liftM Types.OpGood arbitrary
289

290
-- * Actual tests
Iustin Pop's avatar
Iustin Pop committed
291

292
293
294
295
-- ** 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
296
prop_Utils_commaJoinSplit =
Iustin Pop's avatar
Iustin Pop committed
297
298
299
  forAll (arbitrary `suchThat`
          (\l -> l /= [""] && all (not . elem ',') l )) $ \lst ->
  Utils.sepSplit ',' (Utils.commaJoin lst) ==? lst
Iustin Pop's avatar
Iustin Pop committed
300

301
-- | Split and join should always be idempotent.
Iustin Pop's avatar
Iustin Pop committed
302
prop_Utils_commaSplitJoin s =
Iustin Pop's avatar
Iustin Pop committed
303
  Utils.commaJoin (Utils.sepSplit ',' s) ==? s
304

305
-- | fromObjWithDefault, we test using the Maybe monad and an integer
306
-- value.
307
prop_Utils_fromObjWithDefault def_value random_key =
Iustin Pop's avatar
Iustin Pop committed
308
309
310
311
312
313
  -- 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
    where _types = def_value :: Integer
314

315
-- | Test that functional if' behaves like the syntactic sugar if.
Iustin Pop's avatar
Iustin Pop committed
316
317
prop_Utils_if'if :: Bool -> Int -> Int -> Gen Prop
prop_Utils_if'if cnd a b =
Iustin Pop's avatar
Iustin Pop committed
318
  Utils.if' cnd a b ==? if cnd then a else b
319

Guido Trotter's avatar
Guido Trotter committed
320
-- | Test basic select functionality
Iustin Pop's avatar
Iustin Pop committed
321
322
323
324
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
325
prop_Utils_select def lst1 lst2 =
Iustin Pop's avatar
Iustin Pop committed
326
  Utils.select def cndlist ==? expectedresult
Guido Trotter's avatar
Guido Trotter committed
327
  where expectedresult = Utils.if' (null lst2) def (head lst2)
328
329
        flist = map (\e -> (False, e)) lst1
        tlist = map (\e -> (True, e)) lst2
Guido Trotter's avatar
Guido Trotter committed
330
331
332
        cndlist = flist ++ tlist

-- | Test basic select functionality with undefined default
Iustin Pop's avatar
Iustin Pop committed
333
prop_Utils_select_undefd :: [Int]            -- ^ List of False values
Guido Trotter's avatar
Guido Trotter committed
334
                         -> NonEmptyList Int -- ^ List of True values
Iustin Pop's avatar
Iustin Pop committed
335
                         -> Gen Prop         -- ^ Test result
Guido Trotter's avatar
Guido Trotter committed
336
prop_Utils_select_undefd lst1 (NonEmpty lst2) =
Iustin Pop's avatar
Iustin Pop committed
337
  Utils.select undefined cndlist ==? head lst2
Guido Trotter's avatar
Guido Trotter committed
338
339
340
341
342
  where flist = map (\e -> (False, e)) lst1
        tlist = map (\e -> (True, e)) lst2
        cndlist = flist ++ tlist

-- | Test basic select functionality with undefined list values
Iustin Pop's avatar
Iustin Pop committed
343
prop_Utils_select_undefv :: [Int]            -- ^ List of False values
Guido Trotter's avatar
Guido Trotter committed
344
                         -> NonEmptyList Int -- ^ List of True values
Iustin Pop's avatar
Iustin Pop committed
345
                         -> Gen Prop         -- ^ Test result
Guido Trotter's avatar
Guido Trotter committed
346
prop_Utils_select_undefv lst1 (NonEmpty lst2) =
Iustin Pop's avatar
Iustin Pop committed
347
  Utils.select undefined cndlist ==? head lst2
Guido Trotter's avatar
Guido Trotter committed
348
349
350
  where flist = map (\e -> (False, e)) lst1
        tlist = map (\e -> (True, e)) lst2
        cndlist = flist ++ tlist ++ [undefined]
351

Iustin Pop's avatar
Iustin Pop committed
352
prop_Utils_parseUnit (NonNegative n) =
Iustin Pop's avatar
Iustin Pop committed
353
354
355
356
357
358
359
360
361
362
  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
363
    where _types = n::Int
Iustin Pop's avatar
Iustin Pop committed
364

365
-- | Test list for the Utils module.
366
testSuite "Utils"
Iustin Pop's avatar
Iustin Pop committed
367
368
369
370
371
372
373
374
375
            [ '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
            ]
376

377
378
379
-- ** PeerMap tests

-- | Make sure add is idempotent.
Iustin Pop's avatar
Iustin Pop committed
380
prop_PeerMap_addIdempotent pmap key em =
Iustin Pop's avatar
Iustin Pop committed
381
  fn puniq ==? fn (fn puniq)
382
    where _types = (pmap::PeerMap.PeerMap,
Iustin Pop's avatar
Iustin Pop committed
383
384
                    key::PeerMap.Key, em::PeerMap.Elem)
          fn = PeerMap.add key em
385
          puniq = PeerMap.accumArray const pmap
386

387
-- | Make sure remove is idempotent.
388
prop_PeerMap_removeIdempotent pmap key =
Iustin Pop's avatar
Iustin Pop committed
389
  fn puniq ==? fn (fn puniq)
390
391
    where _types = (pmap::PeerMap.PeerMap, key::PeerMap.Key)
          fn = PeerMap.remove key
392
393
          puniq = PeerMap.accumArray const pmap

394
-- | Make sure a missing item returns 0.
395
prop_PeerMap_findMissing pmap key =
Iustin Pop's avatar
Iustin Pop committed
396
  PeerMap.find key (PeerMap.remove key puniq) ==? 0
397
    where _types = (pmap::PeerMap.PeerMap, key::PeerMap.Key)
398
399
          puniq = PeerMap.accumArray const pmap

400
-- | Make sure an added item is found.
Iustin Pop's avatar
Iustin Pop committed
401
prop_PeerMap_addFind pmap key em =
Iustin Pop's avatar
Iustin Pop committed
402
  PeerMap.find key (PeerMap.add key em puniq) ==? em
403
    where _types = (pmap::PeerMap.PeerMap,
Iustin Pop's avatar
Iustin Pop committed
404
                    key::PeerMap.Key, em::PeerMap.Elem)
405
          puniq = PeerMap.accumArray const pmap
406

407
-- | Manual check that maxElem returns the maximum indeed, or 0 for null.
408
prop_PeerMap_maxElem pmap =
Iustin Pop's avatar
Iustin Pop committed
409
  PeerMap.maxElem puniq ==? if null puniq then 0
Iustin Pop's avatar
Iustin Pop committed
410
                              else (maximum . snd . unzip) puniq
411
    where _types = pmap::PeerMap.PeerMap
412
413
          puniq = PeerMap.accumArray const pmap

414
-- | List of tests for the PeerMap module.
415
testSuite "PeerMap"
Iustin Pop's avatar
Iustin Pop committed
416
417
418
419
420
421
            [ 'prop_PeerMap_addIdempotent
            , 'prop_PeerMap_removeIdempotent
            , 'prop_PeerMap_maxElem
            , 'prop_PeerMap_addFind
            , 'prop_PeerMap_findMissing
            ]
422

423
-- ** Container tests
424
425

prop_Container_addTwo cdata i1 i2 =
Iustin Pop's avatar
Iustin Pop committed
426
427
  fn i1 i2 cont == fn i2 i1 cont &&
  fn i1 i2 cont == fn i1 i2 (fn i1 i2 cont)
428
429
430
431
432
    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

433
434
435
prop_Container_nameOf node =
  let nl = makeSmallCluster node 1
      fnode = head (Container.elems nl)
Iustin Pop's avatar
Iustin Pop committed
436
  in Container.nameOf nl (Node.idx fnode) ==? Node.name fnode
437

438
-- | We test that in a cluster, given a random node, we can find it by
439
-- its name and alias, as long as all names and aliases are unique,
440
-- and that we fail to find a non-existing name.
441
442
443
444
445
446
447
448
449
450
451
452
453
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
454
      nl' = Container.fromList nodes'
455
456
457
458
459
      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

460
testSuite "Container"
Iustin Pop's avatar
Iustin Pop committed
461
462
463
464
            [ 'prop_Container_addTwo
            , 'prop_Container_nameOf
            , 'prop_Container_findByName
            ]
465

466
467
-- ** Instance tests

468
469
-- Simple instance tests, we only have setter/getters

Iustin Pop's avatar
Iustin Pop committed
470
prop_Instance_creat inst =
Iustin Pop's avatar
Iustin Pop committed
471
  Instance.name inst ==? Instance.alias inst
Iustin Pop's avatar
Iustin Pop committed
472

473
prop_Instance_setIdx inst idx =
Iustin Pop's avatar
Iustin Pop committed
474
  Instance.idx (Instance.setIdx inst idx) ==? idx
475
476
477
    where _types = (inst::Instance.Instance, idx::Types.Idx)

prop_Instance_setName inst name =
Iustin Pop's avatar
Iustin Pop committed
478
479
  Instance.name newinst == name &&
  Instance.alias newinst == name
Iustin Pop's avatar
Iustin Pop committed
480
481
482
483
    where _types = (inst::Instance.Instance, name::String)
          newinst = Instance.setName inst name

prop_Instance_setAlias inst name =
Iustin Pop's avatar
Iustin Pop committed
484
485
  Instance.name newinst == Instance.name inst &&
  Instance.alias newinst == name
486
    where _types = (inst::Instance.Instance, name::String)
Iustin Pop's avatar
Iustin Pop committed
487
          newinst = Instance.setAlias inst name
488
489

prop_Instance_setPri inst pdx =
Iustin Pop's avatar
Iustin Pop committed
490
  Instance.pNode (Instance.setPri inst pdx) ==? pdx
491
492
493
    where _types = (inst::Instance.Instance, pdx::Types.Ndx)

prop_Instance_setSec inst sdx =
Iustin Pop's avatar
Iustin Pop committed
494
  Instance.sNode (Instance.setSec inst sdx) ==? sdx
495
496
497
    where _types = (inst::Instance.Instance, sdx::Types.Ndx)

prop_Instance_setBoth inst pdx sdx =
Iustin Pop's avatar
Iustin Pop committed
498
  Instance.pNode si == pdx && Instance.sNode si == sdx
499
500
501
    where _types = (inst::Instance.Instance, pdx::Types.Ndx, sdx::Types.Ndx)
          si = Instance.setBoth inst pdx sdx

Iustin Pop's avatar
Iustin Pop committed
502
prop_Instance_shrinkMG inst =
Iustin Pop's avatar
Iustin Pop committed
503
504
505
506
  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
507
508

prop_Instance_shrinkMF inst =
Iustin Pop's avatar
Iustin Pop committed
509
  forAll (choose (0, 2 * Types.unitMem - 1)) $ \mem ->
Iustin Pop's avatar
Iustin Pop committed
510
511
    let inst' = inst { Instance.mem = mem}
    in Types.isBad $ Instance.shrinkByType inst' Types.FailMem
Iustin Pop's avatar
Iustin Pop committed
512
513

prop_Instance_shrinkCG inst =
Iustin Pop's avatar
Iustin Pop committed
514
515
516
517
518
  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
519
520

prop_Instance_shrinkCF inst =
Iustin Pop's avatar
Iustin Pop committed
521
  forAll (choose (0, 2 * Types.unitCpu - 1)) $ \vcpus ->
Iustin Pop's avatar
Iustin Pop committed
522
523
    let inst' = inst { Instance.vcpus = vcpus }
    in Types.isBad $ Instance.shrinkByType inst' Types.FailCPU
Iustin Pop's avatar
Iustin Pop committed
524
525

prop_Instance_shrinkDG inst =
Iustin Pop's avatar
Iustin Pop committed
526
527
528
529
530
  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
531
532

prop_Instance_shrinkDF inst =
Iustin Pop's avatar
Iustin Pop committed
533
  forAll (choose (0, 2 * Types.unitDsk - 1)) $ \dsk ->
Iustin Pop's avatar
Iustin Pop committed
534
535
    let inst' = inst { Instance.dsk = dsk }
    in Types.isBad $ Instance.shrinkByType inst' Types.FailDisk
Iustin Pop's avatar
Iustin Pop committed
536
537

prop_Instance_setMovable inst m =
Iustin Pop's avatar
Iustin Pop committed
538
  Instance.movable inst' ==? m
539
    where inst' = Instance.setMovable inst m
Iustin Pop's avatar
Iustin Pop committed
540

541
testSuite "Instance"
Iustin Pop's avatar
Iustin Pop committed
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
            [ '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
557

558
559
-- ** Text backend tests

Iustin Pop's avatar
Iustin Pop committed
560
561
-- Instance text loader tests

Iustin Pop's avatar
Iustin Pop committed
562
563
prop_Text_Load_Instance name mem dsk vcpus status
                        (NonEmpty pnode) snode
564
                        (NonNegative pdx) (NonNegative sdx) autobal dt =
Iustin Pop's avatar
Iustin Pop committed
565
566
567
568
569
570
  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
571
              then [(pnode, pdx)]
572
              else [(pnode, pdx), (snode, sdx)]
Iustin Pop's avatar
Iustin Pop committed
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
      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
601
602

prop_Text_Load_InstanceFail ktn fields =
Iustin Pop's avatar
Iustin Pop committed
603
  length fields /= 10 ==>
Iustin Pop's avatar
Iustin Pop committed
604
    case Text.loadInst nl fields of
605
606
607
608
      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
609
    where nl = Data.Map.fromList ktn
Iustin Pop's avatar
Iustin Pop committed
610
611

prop_Text_Load_Node name tm nm fm td fd tc fo =
Iustin Pop's avatar
Iustin Pop committed
612
613
614
615
616
617
618
619
620
621
  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
622
623
               then "Y"
               else "N"
Iustin Pop's avatar
Iustin Pop committed
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
      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
640
641

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

644
prop_Text_NodeLSIdempotent node =
Iustin Pop's avatar
Iustin Pop committed
645
646
647
  (Text.loadNode defGroupAssoc.
       Utils.sepSplit '|' . Text.serializeNode defGroupList) n ==
  Just (Node.name n, n)
648
649
650
    -- override failN1 to what loadNode returns by default
    where n = node { Node.failN1 = True, Node.offline = False }

651
testSuite "Text"
Iustin Pop's avatar
Iustin Pop committed
652
653
654
655
656
657
            [ 'prop_Text_Load_Instance
            , 'prop_Text_Load_InstanceFail
            , 'prop_Text_Load_Node
            , 'prop_Text_Load_NodeFail
            , 'prop_Text_NodeLSIdempotent
            ]
658

659
-- ** Node tests
660

Iustin Pop's avatar
Iustin Pop committed
661
prop_Node_setAlias node name =
Iustin Pop's avatar
Iustin Pop committed
662
663
  Node.name newnode == Node.name node &&
  Node.alias newnode == name
Iustin Pop's avatar
Iustin Pop committed
664
665
666
667
    where _types = (node::Node.Node, name::String)
          newnode = Node.setAlias node name

prop_Node_setOffline node status =
Iustin Pop's avatar
Iustin Pop committed
668
  Node.offline newnode ==? status
Iustin Pop's avatar
Iustin Pop committed
669
670
671
    where newnode = Node.setOffline node status

prop_Node_setXmem node xm =
Iustin Pop's avatar
Iustin Pop committed
672
  Node.xMem newnode ==? xm
Iustin Pop's avatar
Iustin Pop committed
673
674
675
    where newnode = Node.setXmem node xm

prop_Node_setMcpu node mc =
Iustin Pop's avatar
Iustin Pop committed
676
  Node.mCpu newnode ==? mc
Iustin Pop's avatar
Iustin Pop committed
677
678
    where newnode = Node.setMcpu node mc

679
680
-- | Check that an instance add with too high memory or disk will be
-- rejected.
Iustin Pop's avatar
Iustin Pop committed
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
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
696
697
698
699
    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
700
prop_Node_addPriFC node inst (Positive extra) =
Iustin Pop's avatar
Iustin Pop committed
701
702
703
704
  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
705
706
    where _types = (node::Node.Node, inst::Instance.Instance)
          inst' = setInstanceSmallerThanNode node inst
Iustin Pop's avatar
Iustin Pop committed
707
          inst'' = inst' { Instance.vcpus = Node.availCpu node + extra }
708

709
710
-- | Check that an instance add with too high memory or disk will be
-- rejected.
711
prop_Node_addSec node inst pdx =
Iustin Pop's avatar
Iustin Pop committed
712
713
714
715
716
  ((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)
717
        where _types = (node::Node.Node, inst::Instance.Instance, pdx::Int)
718

719
720
-- | Check that an offline instance with reasonable disk size can always
-- be added.
721
722
prop_Node_addPriOffline =
  forAll (arbitrary `suchThat` ((> 0) . Node.fMem)) $ \node ->
Iustin Pop's avatar
Iustin Pop committed
723
724
725
726
727
728
  forAll (arbitrary `suchThat`
          (\ x ->  (Instance.dsk x  < Node.fDsk node) &&
                   Instance.instanceOffline x)) $ \inst ->
  case Node.addPri node inst of
    Types.OpGood _ -> True
    _ -> False
729

730
731
prop_Node_addSecOffline pdx =
  forAll (arbitrary `suchThat` ((> 0) . Node.fMem)) $ \node ->
Iustin Pop's avatar
Iustin Pop committed
732
733
734
735
736
737
  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
738

739
-- | Checks for memory reservation changes.
740
prop_Node_rMem inst =
Iustin Pop's avatar
Iustin Pop committed
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
  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 &&
           not (inst_idx `elem` Node.sList d_ab)
       x -> printTestCase ("Failed to add/remove instances: " ++ show x) False
770

771
-- | Check mdsk setting.
Iustin Pop's avatar
Iustin Pop committed
772
prop_Node_setMdsk node mx =
Iustin Pop's avatar
Iustin Pop committed
773
774
775
776
777
778
  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
779
780
781
782
783
784
    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
785
  Node.delTags (Node.addTags m tags) tags ==? m
786
    where m = Data.Map.empty
Iustin Pop's avatar
Iustin Pop committed
787
788

prop_Node_tagMaps_reject tags =
Iustin Pop's avatar
Iustin Pop committed
789
790
  not (null tags) ==>
  all (\t -> Node.rejectAddTags m [t]) tags
791
    where m = Node.addTags Data.Map.empty tags
Iustin Pop's avatar
Iustin Pop committed
792

Iustin Pop's avatar
Iustin Pop committed
793
794
795
796
797
prop_Node_showField node =
  forAll (elements Node.defaultFields) $ \ field ->
  fst (Node.showHeader field) /= Types.unknownField &&
  Node.showField node field /= Types.unknownField

798
799
800
801
802
803
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
804
     (null nodes || not (null ng))
805

806
testSuite "Node"
Iustin Pop's avatar
Iustin Pop committed
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
            [ '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
            ]
824

825
-- ** Cluster tests
826

827
828
-- | Check that the cluster score is close to zero for a homogeneous
-- cluster.
Iustin Pop's avatar
Iustin Pop committed
829
prop_Score_Zero node =
Iustin Pop's avatar
Iustin Pop committed
830
  forAll (choose (1, 1024)) $ \count ->
Iustin Pop's avatar
Iustin Pop committed
831
    (not (Node.offline node) && not (Node.failN1 node) && (count > 0) &&
832
     (Node.tDsk node > 0) && (Node.tMem node > 0)) ==>
Iustin Pop's avatar
Iustin Pop committed
833
834
835
836
837
838
  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
839

840
-- | Check that cluster stats are sane.
Iustin Pop's avatar
Iustin Pop committed
841
prop_CStats_sane node =
Iustin Pop's avatar
Iustin Pop committed
842
  forAll (choose (1, 1024)) $ \count ->
Iustin Pop's avatar
Iustin Pop committed
843
    (not (Node.offline node) && not (Node.failN1 node) &&
844
     (Node.availDisk node > 0) && (Node.availMem node > 0)) ==>
Iustin Pop's avatar
Iustin Pop committed
845
846
847
848
849
850
  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
851

852
-- | Check that one instance is allocated correctly, without
853
-- rebalances needed.
854
prop_ClusterAlloc_sane node inst =
Iustin Pop's avatar
Iustin Pop committed
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
  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
  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)
874
875
876

-- | Checks that on a 2-5 node cluster, we can allocate a random
-- instance spec via tiered allocation (whatever the original instance
877
-- spec), on either one or two nodes.
878
prop_ClusterCanTieredAlloc node inst =
Iustin Pop's avatar
Iustin Pop committed
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
  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
      allocnodes = Cluster.genAllocNodes defGroupList nl rqnodes True
  in case allocnodes >>= \allocnodes' ->
    Cluster.tieredAlloc nl il (Just 1) inst allocnodes' [] [] of
       Types.Bad _ -> False
       Types.Ok (_, _, il', ixes, cstats) -> not (null ixes) &&
                                             IntMap.size il' == length ixes &&
                                             length ixes == length cstats
894
895

-- | Checks that on a 4-8 node cluster, once we allocate an instance,
896
-- we can also evacuate it.
897
prop_ClusterAllocEvac node inst =
Iustin Pop's avatar
Iustin Pop committed
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
  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
  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, _, _) ->
             let sdx = Instance.sNode xi
                 il' = Container.add (Instance.idx xi) xi il
             in case IAlloc.processRelocate defGroupList xnl il'
                  (Instance.idx xi) 1 [sdx] of
                  Types.Ok _ -> True
                  _ -> False
919
920

-- | Check that allocating multiple instances on a cluster, then
921
-- adding an empty node, results in a valid rebalance.
922
prop_ClusterAllocBalance =
Iustin Pop's avatar
Iustin Pop committed
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
  forAll (genNode (Just 5) (Just 128)) $ \node ->
  forAll (choose (3, 5)) $ \count ->
  not (Node.offline node) && not (Node.failN1 node) ==>
  let nl = makeSmallCluster node count
      (hnode, nl') = IntMap.deleteFindMax nl
      il = Container.empty
      allocnodes = Cluster.genAllocNodes defGroupList nl' 2 True
      i_templ = createInstance Types.unitMem Types.unitDsk Types.unitCpu
  in case allocnodes >>= \allocnodes' ->
    Cluster.iterateAlloc nl' il (Just 5) i_templ allocnodes' [] [] of
       Types.Bad _ -> False
       Types.Ok (_, xnl, il', _, _) ->
         let ynl = Container.add (Node.idx hnode) hnode xnl
             cv = Cluster.compCV ynl
             tbl = Cluster.Table ynl il' cv []
         in canBalance tbl True True False
939

940
-- | Checks consistency.
941
942
943
prop_ClusterCheckConsistency node inst =
  let nl = makeSmallCluster node 3
      [node1, node2, node3] = Container.elems nl
Iustin Pop's avatar
Iustin Pop committed
944
      node3' = node3 { Node.group = 1 }
945
946
947
948
      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
949
      ccheck = Cluster.findSplitInstances nl' . Container.fromList
950
951
952
953
  in null (ccheck [(0, inst1)]) &&
     null (ccheck [(0, inst2)]) &&
     (not . null $ ccheck [(0, inst3)])

954
-- | For now, we only test that we don't lose instances during the split.
955
956
957
958
959
960
961
962
963
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
964

965
testSuite "Cluster"
Iustin Pop's avatar
Iustin Pop committed
966
967
968
969
970
971
972
973
974
            [ 'prop_Score_Zero
            , 'prop_CStats_sane
            , 'prop_ClusterAlloc_sane
            , 'prop_ClusterCanTieredAlloc
            , 'prop_ClusterAllocEvac
            , 'prop_ClusterAllocBalance
            , 'prop_ClusterCheckConsistency
            , 'prop_ClusterSplitCluster
            ]
Iustin Pop's avatar
Iustin Pop committed
975

976
-- ** OpCodes tests
Iustin Pop's avatar
Iustin Pop committed
977

978
-- | Check that opcode serialization is idempotent.
Iustin Pop's avatar
Iustin Pop committed
979
980
prop_OpCodes_serialization op =
  case J.readJSON (J.showJSON op) of
Iustin Pop's avatar
Iustin Pop committed
981
982
    J.Error e -> printTestCase ("Cannot deserialise: " ++ e) False
    J.Ok op' -> op ==? op'
983
  where _types = op::OpCodes.OpCode
Iustin Pop's avatar
Iustin Pop committed
984

985
testSuite "OpCodes"
Iustin Pop's avatar
Iustin Pop committed
986
            [ 'prop_OpCodes_serialization ]
Iustin Pop's avatar
Iustin Pop committed
987

988
989
990
-- ** Jobs tests

-- | Check that (queued) job\/opcode status serialization is idempotent.
991
992
prop_OpStatus_serialization os =
  case J.readJSON (J.showJSON os) of
Iustin Pop's avatar
Iustin Pop committed
993
994
    J.Error e -> printTestCase ("Cannot deserialise: " ++ e) False
    J.Ok os' -> os ==? os'
995
996
997
998
  where _types = os::Jobs.OpStatus

prop_JobStatus_serialization js =
  case J.readJSON (J.showJSON js) of
Iustin Pop's avatar
Iustin Pop committed
999
1000
    J.Error e -> printTestCase ("Cannot deserialise: " ++ e) False
    J.Ok js' -> js ==? js'
1001
1002
  where _types = js::Jobs.JobStatus

1003
testSuite "Jobs"
Iustin Pop's avatar
Iustin Pop committed
1004
1005
1006
            [ 'prop_OpStatus_serialization
            , 'prop_JobStatus_serialization
            ]
1007

1008
-- ** Loader tests
Iustin Pop's avatar
Iustin Pop committed
1009
1010

prop_Loader_lookupNode ktn inst node =
Iustin Pop's avatar
Iustin Pop committed
1011
  Loader.lookupNode nl inst node ==? Data.Map.lookup node nl
Iustin Pop's avatar
Iustin Pop committed
1012
    where nl = Data.Map.fromList ktn
Iustin Pop's avatar
Iustin Pop committed
1013
1014

prop_Loader_lookupInstance kti inst =
Iustin Pop's avatar
Iustin Pop committed
1015
  Loader.lookupInstance il inst ==? Data.Map.lookup inst il
Iustin Pop's avatar
Iustin Pop committed
1016
    where il = Data.Map.fromList kti
1017
1018
1019
1020
1021
1022

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
1023
   else True)
Iustin Pop's avatar
Iustin Pop committed
1024
1025
    where (nassoc, kt) =
            Loader.assignIndices (map (\n -> (Node.name n, n)) nodes)