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

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

-}

{-

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

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

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

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

-}

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

import Test.QuickCheck
46
import Text.Printf (printf)
Iustin Pop's avatar
Iustin Pop committed
47
import Data.List (findIndex, intercalate, nub, isPrefixOf)
48
import qualified Data.Set as Set
49
import Data.Maybe
Iustin Pop's avatar
Iustin Pop committed
50
import Control.Monad
Iustin Pop's avatar
Iustin Pop committed
51
import Control.Applicative
Iustin Pop's avatar
Iustin Pop committed
52
import qualified System.Console.GetOpt as GetOpt
Iustin Pop's avatar
Iustin Pop committed
53
import qualified Text.JSON as J
Iustin Pop's avatar
Iustin Pop committed
54
import qualified Data.Map
55
import qualified Data.IntMap as IntMap
Iustin Pop's avatar
Iustin Pop committed
56

Iustin Pop's avatar
Iustin Pop committed
57
import qualified Ganeti.OpCodes as OpCodes
58
import qualified Ganeti.Jobs as Jobs
Iustin Pop's avatar
Iustin Pop committed
59
import qualified Ganeti.Luxi
60
61
62
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
63
import qualified Ganeti.HTools.ExtLoader
64
65
import qualified Ganeti.HTools.IAlloc as IAlloc
import qualified Ganeti.HTools.Instance as Instance
66
import qualified Ganeti.HTools.JSON as JSON
67
import qualified Ganeti.HTools.Loader as Loader
Iustin Pop's avatar
Iustin Pop committed
68
import qualified Ganeti.HTools.Luxi
69
import qualified Ganeti.HTools.Node as Node
Iustin Pop's avatar
Iustin Pop committed
70
import qualified Ganeti.HTools.Group as Group
71
import qualified Ganeti.HTools.PeerMap as PeerMap
72
import qualified Ganeti.HTools.Rapi
73
import qualified Ganeti.HTools.Simu as Simu
74
75
76
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
77
import qualified Ganeti.HTools.Version
78
import qualified Ganeti.Constants as C
79

80
import qualified Ganeti.HTools.Program as Program
81
82
83
84
85
import qualified Ganeti.HTools.Program.Hail
import qualified Ganeti.HTools.Program.Hbal
import qualified Ganeti.HTools.Program.Hscan
import qualified Ganeti.HTools.Program.Hspace

86
import Ganeti.HTools.QCHelper (testSuite)
Iustin Pop's avatar
Iustin Pop committed
87

88
89
-- * Constants

90
-- | Maximum memory (1TiB, somewhat random value).
Iustin Pop's avatar
Iustin Pop committed
91
92
93
maxMem :: Int
maxMem = 1024 * 1024

94
-- | Maximum disk (8TiB, somewhat random value).
Iustin Pop's avatar
Iustin Pop committed
95
maxDsk :: Int
Iustin Pop's avatar
Iustin Pop committed
96
maxDsk = 1024 * 1024 * 8
Iustin Pop's avatar
Iustin Pop committed
97

98
-- | Max CPUs (1024, somewhat random value).
Iustin Pop's avatar
Iustin Pop committed
99
100
101
maxCpu :: Int
maxCpu = 1024

102
103
104
105
106
107
108
109
-- | Max vcpu ratio (random value).
maxVcpuRatio :: Double
maxVcpuRatio = 1024.0

-- | Max spindle ratio (random value).
maxSpindleRatio :: Double
maxSpindleRatio = 1024.0

Iustin Pop's avatar
Iustin Pop committed
110
111
112
113
114
115
116
117
118
-- | Max nodes, used just to limit arbitrary instances for smaller
-- opcode definitions (e.g. list of nodes in OpTestDelay).
maxNodes :: Int
maxNodes = 32

-- | Max opcodes or jobs in a submit job and submit many jobs.
maxOpCodes :: Int
maxOpCodes = 16

119
120
121
122
-- | All disk templates (used later)
allDiskTemplates :: [Types.DiskTemplate]
allDiskTemplates = [minBound..maxBound]

123
124
125
126
127
128
129
-- | Null iPolicy, and by null we mean very liberal.
nullIPolicy = Types.IPolicy
  { Types.iPolicyMinSpec = Types.ISpec { Types.iSpecMemorySize = 0
                                       , Types.iSpecCpuCount   = 0
                                       , Types.iSpecDiskSize   = 0
                                       , Types.iSpecDiskCount  = 0
                                       , Types.iSpecNicCount   = 0
130
                                       , Types.iSpecSpindleUse = 0
131
132
133
134
135
136
                                       }
  , Types.iPolicyMaxSpec = Types.ISpec { Types.iSpecMemorySize = maxBound
                                       , Types.iSpecCpuCount   = maxBound
                                       , Types.iSpecDiskSize   = maxBound
                                       , Types.iSpecDiskCount  = C.maxDisks
                                       , Types.iSpecNicCount   = C.maxNics
137
                                       , Types.iSpecSpindleUse = maxBound
138
139
140
141
142
143
                                       }
  , Types.iPolicyStdSpec = Types.ISpec { Types.iSpecMemorySize = Types.unitMem
                                       , Types.iSpecCpuCount   = Types.unitCpu
                                       , Types.iSpecDiskSize   = Types.unitDsk
                                       , Types.iSpecDiskCount  = 1
                                       , Types.iSpecNicCount   = 1
144
                                       , Types.iSpecSpindleUse = 1
145
                                       }
146
  , Types.iPolicyDiskTemplates = [minBound..maxBound]
147
148
149
  , Types.iPolicyVcpuRatio = maxVcpuRatio -- somewhat random value, high
                                          -- enough to not impact us
  , Types.iPolicySpindleRatio = maxSpindleRatio
150
151
152
  }


Iustin Pop's avatar
Iustin Pop committed
153
154
defGroup :: Group.Group
defGroup = flip Group.setIdx 0 $
Iustin Pop's avatar
Iustin Pop committed
155
             Group.create "default" Types.defaultGroupID Types.AllocPreferred
156
                  nullIPolicy
Iustin Pop's avatar
Iustin Pop committed
157
158

defGroupList :: Group.List
Iustin Pop's avatar
Iustin Pop committed
159
defGroupList = Container.fromList [(Group.idx defGroup, defGroup)]
Iustin Pop's avatar
Iustin Pop committed
160
161
162
163

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

164
165
-- * Helper functions

166
-- | Simple checker for whether OpResult is fail or pass.
Iustin Pop's avatar
Iustin Pop committed
167
168
169
170
isFailure :: Types.OpResult a -> Bool
isFailure (Types.OpFail _) = True
isFailure _ = False

Iustin Pop's avatar
Iustin Pop committed
171
172
173
174
175
176
177
-- | 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 ==?

Iustin Pop's avatar
Iustin Pop committed
178
179
180
181
-- | Show a message and fail the test.
failTest :: String -> Property
failTest msg = printTestCase msg False

182
-- | Update an instance to be smaller than a node.
183
setInstanceSmallerThanNode node inst =
Iustin Pop's avatar
Iustin Pop committed
184
185
186
187
  inst { Instance.mem = Node.availMem node `div` 2
       , Instance.dsk = Node.availDisk node `div` 2
       , Instance.vcpus = Node.availCpu node `div` 2
       }
188

189
-- | Create an instance given its spec.
190
createInstance mem dsk vcpus =
Iustin Pop's avatar
Iustin Pop committed
191
  Instance.create "inst-unnamed" mem dsk vcpus Types.Running [] True (-1) (-1)
192
    Types.DTDrbd8 1
193

194
-- | Create a small cluster by repeating a node spec.
195
196
makeSmallCluster :: Node.Node -> Int -> Node.List
makeSmallCluster node count =
197
198
199
200
201
202
203
  let origname = Node.name node
      origalias = Node.alias node
      nodes = map (\idx -> node { Node.name = origname ++ "-" ++ show idx
                                , Node.alias = origalias ++ "-" ++ show idx })
              [1..count]
      fn = flip Node.buildPeers Container.empty
      namelst = map (\n -> (Node.name n, fn n)) nodes
Iustin Pop's avatar
Iustin Pop committed
204
205
      (_, nlst) = Loader.assignIndices namelst
  in nlst
206

Iustin Pop's avatar
Iustin Pop committed
207
208
209
210
211
212
213
-- | Make a small cluster, both nodes and instances.
makeSmallEmptyCluster :: Node.Node -> Int -> Instance.Instance
                      -> (Node.List, Instance.List, Instance.Instance)
makeSmallEmptyCluster node count inst =
  (makeSmallCluster node count, Container.empty,
   setInstanceSmallerThanNode node inst)

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

220
221
canBalance :: Cluster.Table -> Bool -> Bool -> Bool -> Bool
canBalance tbl dm im evac = isJust $ Cluster.tryBalance tbl dm im evac 0 0
222

223
-- | Assigns a new fresh instance to a cluster; this is not
224
-- allocation, so no resource checks are done.
225
226
227
228
229
230
231
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
232
233
                  then 0
                  else fst (Container.findMax il) + 1
234
235
236
237
238
239
240
241
      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')

242
243
244
245
246
247
248
-- | Generates a list of a given size with non-duplicate elements.
genUniquesList :: (Eq a, Arbitrary a) => Int -> Gen [a]
genUniquesList cnt =
  foldM (\lst _ -> do
           newelem <- arbitrary `suchThat` (`notElem` lst)
           return (newelem:lst)) [] [1..cnt]

249
250
-- | Checks if an instance is mirrored.
isMirrored :: Instance.Instance -> Bool
251
isMirrored = (/= Types.MirrorNone) . Instance.mirrorType
252
253
254
255
256
257
258

-- | Returns the possible change node types for a disk template.
evacModeOptions :: Types.MirrorType -> [Types.EvacMode]
evacModeOptions Types.MirrorNone     = []
evacModeOptions Types.MirrorInternal = [minBound..maxBound] -- DRBD can do all
evacModeOptions Types.MirrorExternal = [Types.ChangePrimary, Types.ChangeAll]

259
260
-- * Arbitrary instances

261
-- | Defines a DNS name.
262
newtype DNSChar = DNSChar { dnsGetChar::Char }
263

264
instance Arbitrary DNSChar where
Iustin Pop's avatar
Iustin Pop committed
265
266
267
  arbitrary = do
    x <- elements (['a'..'z'] ++ ['0'..'9'] ++ "_-")
    return (DNSChar x)
268

269
-- | Generates a single name component.
270
271
272
getName :: Gen String
getName = do
  n <- choose (1, 64)
Iustin Pop's avatar
Iustin Pop committed
273
  dn <- vector n
274
275
  return (map dnsGetChar dn)

276
-- | Generates an entire FQDN.
277
278
279
getFQDN :: Gen String
getFQDN = do
  ncomps <- choose (1, 4)
Iustin Pop's avatar
Iustin Pop committed
280
  names <- vectorOf ncomps getName
281
  return $ intercalate "." names
282

Iustin Pop's avatar
Iustin Pop committed
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
-- | Combinator that generates a 'Maybe' using a sub-combinator.
getMaybe :: Gen a -> Gen (Maybe a)
getMaybe subgen = do
  bool <- arbitrary
  if bool
    then Just <$> subgen
    else return Nothing

-- | Generates a fields list. This uses the same character set as a
-- DNS name (just for simplicity).
getFields :: Gen [String]
getFields = do
  n <- choose (1, 32)
  vectorOf n getName

298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
-- | Defines a tag type.
newtype TagChar = TagChar { tagGetChar :: Char }

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

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

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

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

330
instance Arbitrary Types.InstanceStatus where
331
    arbitrary = elements [minBound..maxBound]
332

333
334
335
336
337
338
339
340
341
342
-- | Generates a random instance with maximum disk/mem/cpu values.
genInstanceSmallerThan :: Int -> Int -> Int -> Gen Instance.Instance
genInstanceSmallerThan lim_mem lim_dsk lim_cpu = do
  name <- getFQDN
  mem <- choose (0, lim_mem)
  dsk <- choose (0, lim_dsk)
  run_st <- arbitrary
  pn <- arbitrary
  sn <- arbitrary
  vcpus <- choose (0, lim_cpu)
343
344
  dt <- arbitrary
  return $ Instance.create name mem dsk vcpus run_st [] True pn sn dt 1
345
346
347
348
349
350
351
352

-- | Generates an instance smaller than a node.
genInstanceSmallerThanNode :: Node.Node -> Gen Instance.Instance
genInstanceSmallerThanNode node =
  genInstanceSmallerThan (Node.availMem node `div` 2)
                         (Node.availDisk node `div` 2)
                         (Node.availCpu node `div` 2)

353
354
-- let's generate a random instance
instance Arbitrary Instance.Instance where
355
  arbitrary = genInstanceSmallerThan maxMem maxDsk maxCpu
356

357
358
359
360
361
-- | 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
362
363
genNode min_multiplier max_multiplier = do
  let (base_mem, base_dsk, base_cpu) =
Iustin Pop's avatar
Iustin Pop committed
364
365
366
367
368
        case min_multiplier of
          Just mm -> (mm * Types.unitMem,
                      mm * Types.unitDsk,
                      mm * Types.unitCpu)
          Nothing -> (0, 0, 0)
369
      (top_mem, top_dsk, top_cpu)  =
Iustin Pop's avatar
Iustin Pop committed
370
371
372
373
374
        case max_multiplier of
          Just mm -> (mm * Types.unitMem,
                      mm * Types.unitDsk,
                      mm * Types.unitCpu)
          Nothing -> (maxMem, maxDsk, maxCpu)
375
376
377
378
379
380
381
382
383
  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
384
          (fromIntegral dsk_t) dsk_f (fromIntegral cpu_t) offl 1 0
Iustin Pop's avatar
Iustin Pop committed
385
386
      n' = Node.setPolicy nullIPolicy n
  return $ Node.buildPeers n' Container.empty
387

388
389
390
391
392
393
394
395
396
-- | Helper function to generate a sane node.
genOnlineNode :: Gen Node.Node
genOnlineNode = do
  arbitrary `suchThat` (\n -> not (Node.offline n) &&
                              not (Node.failN1 n) &&
                              Node.availDisk n > 0 &&
                              Node.availMem n > 0 &&
                              Node.availCpu n > 0)

397
398
-- and a random node
instance Arbitrary Node.Node where
Iustin Pop's avatar
Iustin Pop committed
399
  arbitrary = genNode Nothing Nothing
400

Iustin Pop's avatar
Iustin Pop committed
401
402
-- replace disks
instance Arbitrary OpCodes.ReplaceDisksMode where
403
  arbitrary = elements [minBound..maxBound]
Iustin Pop's avatar
Iustin Pop committed
404
405
406
407
408
409
410
411

instance Arbitrary OpCodes.OpCode where
  arbitrary = do
    op_id <- elements [ "OP_TEST_DELAY"
                      , "OP_INSTANCE_REPLACE_DISKS"
                      , "OP_INSTANCE_FAILOVER"
                      , "OP_INSTANCE_MIGRATE"
                      ]
Iustin Pop's avatar
Iustin Pop committed
412
413
    case op_id of
      "OP_TEST_DELAY" ->
Iustin Pop's avatar
Iustin Pop committed
414
415
        OpCodes.OpTestDelay <$> arbitrary <*> arbitrary
                 <*> resize maxNodes (listOf getFQDN)
Iustin Pop's avatar
Iustin Pop committed
416
      "OP_INSTANCE_REPLACE_DISKS" ->
Iustin Pop's avatar
Iustin Pop committed
417
418
        OpCodes.OpInstanceReplaceDisks <$> getFQDN <*> getMaybe getFQDN <*>
          arbitrary <*> resize C.maxDisks arbitrary <*> getMaybe getName
Iustin Pop's avatar
Iustin Pop committed
419
      "OP_INSTANCE_FAILOVER" ->
Iustin Pop's avatar
Iustin Pop committed
420
421
        OpCodes.OpInstanceFailover <$> getFQDN <*> arbitrary <*>
          getMaybe getFQDN
Iustin Pop's avatar
Iustin Pop committed
422
      "OP_INSTANCE_MIGRATE" ->
Iustin Pop's avatar
Iustin Pop committed
423
424
        OpCodes.OpInstanceMigrate <$> getFQDN <*> arbitrary <*>
          arbitrary <*> arbitrary <*> getMaybe getFQDN
Iustin Pop's avatar
Iustin Pop committed
425
      _ -> fail "Wrong opcode"
Iustin Pop's avatar
Iustin Pop committed
426

427
428
429
430
431
432
instance Arbitrary Jobs.OpStatus where
  arbitrary = elements [minBound..maxBound]

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

433
434
newtype SmallRatio = SmallRatio Double deriving Show
instance Arbitrary SmallRatio where
Iustin Pop's avatar
Iustin Pop committed
435
436
437
  arbitrary = do
    v <- choose (0, 1)
    return $ SmallRatio v
438

439
440
441
442
443
444
instance Arbitrary Types.AllocPolicy where
  arbitrary = elements [minBound..maxBound]

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

445
instance Arbitrary Types.FailMode where
Iustin Pop's avatar
Iustin Pop committed
446
  arbitrary = elements [minBound..maxBound]
447

448
449
450
instance Arbitrary Types.EvacMode where
  arbitrary = elements [minBound..maxBound]

451
instance Arbitrary a => Arbitrary (Types.OpResult a) where
Iustin Pop's avatar
Iustin Pop committed
452
  arbitrary = arbitrary >>= \c ->
Iustin Pop's avatar
Iustin Pop committed
453
              if c
Iustin Pop's avatar
Iustin Pop committed
454
455
                then Types.OpGood <$> arbitrary
                else Types.OpFail <$> arbitrary
456

457
458
instance Arbitrary Types.ISpec where
  arbitrary = do
459
    mem_s <- arbitrary::Gen (NonNegative Int)
460
461
    dsk_c <- arbitrary::Gen (NonNegative Int)
    dsk_s <- arbitrary::Gen (NonNegative Int)
462
463
    cpu_c <- arbitrary::Gen (NonNegative Int)
    nic_c <- arbitrary::Gen (NonNegative Int)
464
    su    <- arbitrary::Gen (NonNegative Int)
465
466
    return Types.ISpec { Types.iSpecMemorySize = fromIntegral mem_s
                       , Types.iSpecCpuCount   = fromIntegral cpu_c
467
468
                       , Types.iSpecDiskSize   = fromIntegral dsk_s
                       , Types.iSpecDiskCount  = fromIntegral dsk_c
469
                       , Types.iSpecNicCount   = fromIntegral nic_c
470
                       , Types.iSpecSpindleUse = fromIntegral su
471
472
                       }

473
474
475
476
477
478
479
480
-- | Generates an ispec bigger than the given one.
genBiggerISpec :: Types.ISpec -> Gen Types.ISpec
genBiggerISpec imin = do
  mem_s <- choose (Types.iSpecMemorySize imin, maxBound)
  dsk_c <- choose (Types.iSpecDiskCount imin, maxBound)
  dsk_s <- choose (Types.iSpecDiskSize imin, maxBound)
  cpu_c <- choose (Types.iSpecCpuCount imin, maxBound)
  nic_c <- choose (Types.iSpecNicCount imin, maxBound)
481
  su    <- choose (Types.iSpecSpindleUse imin, maxBound)
482
483
484
485
486
  return Types.ISpec { Types.iSpecMemorySize = fromIntegral mem_s
                     , Types.iSpecCpuCount   = fromIntegral cpu_c
                     , Types.iSpecDiskSize   = fromIntegral dsk_s
                     , Types.iSpecDiskCount  = fromIntegral dsk_c
                     , Types.iSpecNicCount   = fromIntegral nic_c
487
                     , Types.iSpecSpindleUse = fromIntegral su
488
                     }
489
490
491
492

instance Arbitrary Types.IPolicy where
  arbitrary = do
    imin <- arbitrary
493
494
495
496
    istd <- genBiggerISpec imin
    imax <- genBiggerISpec istd
    num_tmpl <- choose (0, length allDiskTemplates)
    dts  <- genUniquesList num_tmpl
497
498
    vcpu_ratio <- choose (1.0, maxVcpuRatio)
    spindle_ratio <- choose (1.0, maxSpindleRatio)
499
500
501
502
    return Types.IPolicy { Types.iPolicyMinSpec = imin
                         , Types.iPolicyStdSpec = istd
                         , Types.iPolicyMaxSpec = imax
                         , Types.iPolicyDiskTemplates = dts
503
                         , Types.iPolicyVcpuRatio = vcpu_ratio
504
                         , Types.iPolicySpindleRatio = spindle_ratio
505
506
                         }

507
-- * Actual tests
Iustin Pop's avatar
Iustin Pop committed
508

509
510
-- ** Utils tests

511
512
513
514
515
-- | Helper to generate a small string that doesn't contain commas.
genNonCommaString = do
  size <- choose (0, 20) -- arbitrary max size
  vectorOf size (arbitrary `suchThat` ((/=) ','))

516
517
-- | 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
518
prop_Utils_commaJoinSplit =
519
520
  forAll (choose (0, 20)) $ \llen ->
  forAll (vectorOf llen genNonCommaString `suchThat` ((/=) [""])) $ \lst ->
Iustin Pop's avatar
Iustin Pop committed
521
  Utils.sepSplit ',' (Utils.commaJoin lst) ==? lst
Iustin Pop's avatar
Iustin Pop committed
522

523
-- | Split and join should always be idempotent.
Iustin Pop's avatar
Iustin Pop committed
524
prop_Utils_commaSplitJoin s =
Iustin Pop's avatar
Iustin Pop committed
525
  Utils.commaJoin (Utils.sepSplit ',' s) ==? s
526

527
-- | fromObjWithDefault, we test using the Maybe monad and an integer
528
-- value.
529
prop_Utils_fromObjWithDefault def_value random_key =
Iustin Pop's avatar
Iustin Pop committed
530
  -- a missing key will be returned with the default
531
  JSON.fromObjWithDefault [] random_key def_value == Just def_value &&
Iustin Pop's avatar
Iustin Pop committed
532
  -- a found key will be returned as is, not with default
533
  JSON.fromObjWithDefault [(random_key, J.showJSON def_value)]
Iustin Pop's avatar
Iustin Pop committed
534
535
       random_key (def_value+1) == Just def_value
    where _types = def_value :: Integer
536

537
-- | Test that functional if' behaves like the syntactic sugar if.
Iustin Pop's avatar
Iustin Pop committed
538
539
prop_Utils_if'if :: Bool -> Int -> Int -> Gen Prop
prop_Utils_if'if cnd a b =
Iustin Pop's avatar
Iustin Pop committed
540
  Utils.if' cnd a b ==? if cnd then a else b
541

Guido Trotter's avatar
Guido Trotter committed
542
-- | Test basic select functionality
Iustin Pop's avatar
Iustin Pop committed
543
544
545
546
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
547
prop_Utils_select def lst1 lst2 =
Iustin Pop's avatar
Iustin Pop committed
548
  Utils.select def (flist ++ tlist) ==? expectedresult
Iustin Pop's avatar
Iustin Pop committed
549
550
551
    where expectedresult = Utils.if' (null lst2) def (head lst2)
          flist = zip (repeat False) lst1
          tlist = zip (repeat True)  lst2
Guido Trotter's avatar
Guido Trotter committed
552
553

-- | Test basic select functionality with undefined default
Iustin Pop's avatar
Iustin Pop committed
554
prop_Utils_select_undefd :: [Int]            -- ^ List of False values
Guido Trotter's avatar
Guido Trotter committed
555
                         -> NonEmptyList Int -- ^ List of True values
Iustin Pop's avatar
Iustin Pop committed
556
                         -> Gen Prop         -- ^ Test result
Guido Trotter's avatar
Guido Trotter committed
557
prop_Utils_select_undefd lst1 (NonEmpty lst2) =
Iustin Pop's avatar
Iustin Pop committed
558
  Utils.select undefined (flist ++ tlist) ==? head lst2
Iustin Pop's avatar
Iustin Pop committed
559
560
    where flist = zip (repeat False) lst1
          tlist = zip (repeat True)  lst2
Guido Trotter's avatar
Guido Trotter committed
561
562

-- | Test basic select functionality with undefined list values
Iustin Pop's avatar
Iustin Pop committed
563
prop_Utils_select_undefv :: [Int]            -- ^ List of False values
Guido Trotter's avatar
Guido Trotter committed
564
                         -> NonEmptyList Int -- ^ List of True values
Iustin Pop's avatar
Iustin Pop committed
565
                         -> Gen Prop         -- ^ Test result
Guido Trotter's avatar
Guido Trotter committed
566
prop_Utils_select_undefv lst1 (NonEmpty lst2) =
Iustin Pop's avatar
Iustin Pop committed
567
  Utils.select undefined cndlist ==? head lst2
Iustin Pop's avatar
Iustin Pop committed
568
569
570
    where flist = zip (repeat False) lst1
          tlist = zip (repeat True)  lst2
          cndlist = flist ++ tlist ++ [undefined]
571

Iustin Pop's avatar
Iustin Pop committed
572
prop_Utils_parseUnit (NonNegative n) =
Iustin Pop's avatar
Iustin Pop committed
573
574
575
576
577
578
579
580
581
582
583
584
585
586
  Utils.parseUnit (show n) ==? Types.Ok n .&&.
  Utils.parseUnit (show n ++ "m") ==? Types.Ok n .&&.
  Utils.parseUnit (show n ++ "M") ==? Types.Ok (truncate n_mb::Int) .&&.
  Utils.parseUnit (show n ++ "g") ==? Types.Ok (n*1024) .&&.
  Utils.parseUnit (show n ++ "G") ==? Types.Ok (truncate n_gb::Int) .&&.
  Utils.parseUnit (show n ++ "t") ==? Types.Ok (n*1048576) .&&.
  Utils.parseUnit (show n ++ "T") ==? Types.Ok (truncate n_tb::Int) .&&.
  printTestCase "Internal error/overflow?"
    (n_mb >=0 && n_gb >= 0 && n_tb >= 0) .&&.
  property (Types.isBad (Utils.parseUnit (show n ++ "x")::Types.Result Int))
  where _types = (n::Int)
        n_mb = (fromIntegral n::Rational) * 1000 * 1000 / 1024 / 1024
        n_gb = n_mb * 1000
        n_tb = n_gb * 1000
Iustin Pop's avatar
Iustin Pop committed
587

588
-- | Test list for the Utils module.
589
testSuite "Utils"
Iustin Pop's avatar
Iustin Pop committed
590
591
592
593
594
595
596
597
598
            [ '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
            ]
599

600
601
602
-- ** PeerMap tests

-- | Make sure add is idempotent.
Iustin Pop's avatar
Iustin Pop committed
603
prop_PeerMap_addIdempotent pmap key em =
Iustin Pop's avatar
Iustin Pop committed
604
  fn puniq ==? fn (fn puniq)
605
    where _types = (pmap::PeerMap.PeerMap,
Iustin Pop's avatar
Iustin Pop committed
606
607
                    key::PeerMap.Key, em::PeerMap.Elem)
          fn = PeerMap.add key em
608
          puniq = PeerMap.accumArray const pmap
609

610
-- | Make sure remove is idempotent.
611
prop_PeerMap_removeIdempotent pmap key =
Iustin Pop's avatar
Iustin Pop committed
612
  fn puniq ==? fn (fn puniq)
613
614
    where _types = (pmap::PeerMap.PeerMap, key::PeerMap.Key)
          fn = PeerMap.remove key
615
616
          puniq = PeerMap.accumArray const pmap

617
-- | Make sure a missing item returns 0.
618
prop_PeerMap_findMissing pmap key =
Iustin Pop's avatar
Iustin Pop committed
619
  PeerMap.find key (PeerMap.remove key puniq) ==? 0
620
    where _types = (pmap::PeerMap.PeerMap, key::PeerMap.Key)
621
622
          puniq = PeerMap.accumArray const pmap

623
-- | Make sure an added item is found.
Iustin Pop's avatar
Iustin Pop committed
624
prop_PeerMap_addFind pmap key em =
Iustin Pop's avatar
Iustin Pop committed
625
  PeerMap.find key (PeerMap.add key em puniq) ==? em
626
    where _types = (pmap::PeerMap.PeerMap,
Iustin Pop's avatar
Iustin Pop committed
627
                    key::PeerMap.Key, em::PeerMap.Elem)
628
          puniq = PeerMap.accumArray const pmap
629

630
-- | Manual check that maxElem returns the maximum indeed, or 0 for null.
631
prop_PeerMap_maxElem pmap =
Iustin Pop's avatar
Iustin Pop committed
632
  PeerMap.maxElem puniq ==? if null puniq then 0
Iustin Pop's avatar
Iustin Pop committed
633
                              else (maximum . snd . unzip) puniq
634
    where _types = pmap::PeerMap.PeerMap
635
636
          puniq = PeerMap.accumArray const pmap

637
-- | List of tests for the PeerMap module.
638
testSuite "PeerMap"
Iustin Pop's avatar
Iustin Pop committed
639
640
641
642
643
644
            [ 'prop_PeerMap_addIdempotent
            , 'prop_PeerMap_removeIdempotent
            , 'prop_PeerMap_maxElem
            , 'prop_PeerMap_addFind
            , 'prop_PeerMap_findMissing
            ]
645

646
-- ** Container tests
647

Iustin Pop's avatar
Iustin Pop committed
648
649
-- we silence the following due to hlint bug fixed in later versions
{-# ANN prop_Container_addTwo "HLint: ignore Avoid lambda" #-}
650
prop_Container_addTwo cdata i1 i2 =
Iustin Pop's avatar
Iustin Pop committed
651
652
  fn i1 i2 cont == fn i2 i1 cont &&
  fn i1 i2 cont == fn i1 i2 (fn i1 i2 cont)
653
654
655
656
657
    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

658
659
660
prop_Container_nameOf node =
  let nl = makeSmallCluster node 1
      fnode = head (Container.elems nl)
Iustin Pop's avatar
Iustin Pop committed
661
  in Container.nameOf nl (Node.idx fnode) ==? Node.name fnode
662

663
-- | We test that in a cluster, given a random node, we can find it by
664
-- its name and alias, as long as all names and aliases are unique,
665
-- and that we fail to find a non-existing name.
666
prop_Container_findByName node =
667
668
  forAll (choose (1, 20)) $ \ cnt ->
  forAll (choose (0, cnt - 1)) $ \ fidx ->
669
670
671
672
  forAll (genUniquesList (cnt * 2)) $ \ allnames ->
  forAll (arbitrary `suchThat` (`notElem` allnames)) $ \ othername ->
  let names = zip (take cnt allnames) (drop cnt allnames)
      nl = makeSmallCluster node cnt
673
674
675
676
677
      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
678
      nl' = Container.fromList nodes'
679
680
681
      target = snd (nodes' !! fidx)
  in Container.findByName nl' (Node.name target) == Just target &&
     Container.findByName nl' (Node.alias target) == Just target &&
Iustin Pop's avatar
Iustin Pop committed
682
     isNothing (Container.findByName nl' othername)
683

684
testSuite "Container"
Iustin Pop's avatar
Iustin Pop committed
685
686
687
688
            [ 'prop_Container_addTwo
            , 'prop_Container_nameOf
            , 'prop_Container_findByName
            ]
689

690
691
-- ** Instance tests

692
693
-- Simple instance tests, we only have setter/getters

Iustin Pop's avatar
Iustin Pop committed
694
prop_Instance_creat inst =
Iustin Pop's avatar
Iustin Pop committed
695
  Instance.name inst ==? Instance.alias inst
Iustin Pop's avatar
Iustin Pop committed
696

697
prop_Instance_setIdx inst idx =
Iustin Pop's avatar
Iustin Pop committed
698
  Instance.idx (Instance.setIdx inst idx) ==? idx
699
700
701
    where _types = (inst::Instance.Instance, idx::Types.Idx)

prop_Instance_setName inst name =
Iustin Pop's avatar
Iustin Pop committed
702
703
  Instance.name newinst == name &&
  Instance.alias newinst == name
Iustin Pop's avatar
Iustin Pop committed
704
705
706
707
    where _types = (inst::Instance.Instance, name::String)
          newinst = Instance.setName inst name

prop_Instance_setAlias inst name =
Iustin Pop's avatar
Iustin Pop committed
708
709
  Instance.name newinst == Instance.name inst &&
  Instance.alias newinst == name
710
    where _types = (inst::Instance.Instance, name::String)
Iustin Pop's avatar
Iustin Pop committed
711
          newinst = Instance.setAlias inst name
712
713

prop_Instance_setPri inst pdx =
Iustin Pop's avatar
Iustin Pop committed
714
  Instance.pNode (Instance.setPri inst pdx) ==? pdx
715
716
717
    where _types = (inst::Instance.Instance, pdx::Types.Ndx)

prop_Instance_setSec inst sdx =
Iustin Pop's avatar
Iustin Pop committed
718
  Instance.sNode (Instance.setSec inst sdx) ==? sdx
719
720
721
    where _types = (inst::Instance.Instance, sdx::Types.Ndx)

prop_Instance_setBoth inst pdx sdx =
Iustin Pop's avatar
Iustin Pop committed
722
  Instance.pNode si == pdx && Instance.sNode si == sdx
723
724
725
    where _types = (inst::Instance.Instance, pdx::Types.Ndx, sdx::Types.Ndx)
          si = Instance.setBoth inst pdx sdx

Iustin Pop's avatar
Iustin Pop committed
726
prop_Instance_shrinkMG inst =
Iustin Pop's avatar
Iustin Pop committed
727
728
729
730
  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
731
732

prop_Instance_shrinkMF inst =
Iustin Pop's avatar
Iustin Pop committed
733
  forAll (choose (0, 2 * Types.unitMem - 1)) $ \mem ->
Iustin Pop's avatar
Iustin Pop committed
734
735
    let inst' = inst { Instance.mem = mem}
    in Types.isBad $ Instance.shrinkByType inst' Types.FailMem
Iustin Pop's avatar
Iustin Pop committed
736
737

prop_Instance_shrinkCG inst =
Iustin Pop's avatar
Iustin Pop committed
738
739
740
741
742
  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
743
744

prop_Instance_shrinkCF inst =
Iustin Pop's avatar
Iustin Pop committed
745
  forAll (choose (0, 2 * Types.unitCpu - 1)) $ \vcpus ->
Iustin Pop's avatar
Iustin Pop committed
746
747
    let inst' = inst { Instance.vcpus = vcpus }
    in Types.isBad $ Instance.shrinkByType inst' Types.FailCPU
Iustin Pop's avatar
Iustin Pop committed
748
749

prop_Instance_shrinkDG inst =
Iustin Pop's avatar
Iustin Pop committed
750
751
752
753
754
  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
755
756

prop_Instance_shrinkDF inst =
Iustin Pop's avatar
Iustin Pop committed
757
  forAll (choose (0, 2 * Types.unitDsk - 1)) $ \dsk ->
Iustin Pop's avatar
Iustin Pop committed
758
759
    let inst' = inst { Instance.dsk = dsk }
    in Types.isBad $ Instance.shrinkByType inst' Types.FailDisk
Iustin Pop's avatar
Iustin Pop committed
760
761

prop_Instance_setMovable inst m =
Iustin Pop's avatar
Iustin Pop committed
762
  Instance.movable inst' ==? m
763
    where inst' = Instance.setMovable inst m
Iustin Pop's avatar
Iustin Pop committed
764

765
testSuite "Instance"
Iustin Pop's avatar
Iustin Pop committed
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
            [ '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
781

782
783
784
-- ** Backends

-- *** Text backend tests
785

Iustin Pop's avatar
Iustin Pop committed
786
787
-- Instance text loader tests

Iustin Pop's avatar
Iustin Pop committed
788
789
prop_Text_Load_Instance name mem dsk vcpus status
                        (NonEmpty pnode) snode
790
                        (NonNegative pdx) (NonNegative sdx) autobal dt su =
Iustin Pop's avatar
Iustin Pop committed
791
792
793
794
  pnode /= snode && pdx /= sdx ==>
  let vcpus_s = show vcpus
      dsk_s = show dsk
      mem_s = show mem
795
      su_s = show su
Iustin Pop's avatar
Iustin Pop committed
796
797
      status_s = Types.instanceStatusToRaw status
      ndx = if null snode
Iustin Pop's avatar
Iustin Pop committed
798
              then [(pnode, pdx)]
799
              else [(pnode, pdx), (snode, sdx)]
Iustin Pop's avatar
Iustin Pop committed
800
801
802
803
804
805
      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,
806
              sbal, pnode, snode, sdt, tags, su_s]
Iustin Pop's avatar
Iustin Pop committed
807
808
809
810
811
812
813
814
      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
Iustin Pop's avatar
Iustin Pop committed
815
       Types.Bad msg -> failTest $ "Failed to load instance: " ++ msg
Iustin Pop's avatar
Iustin Pop committed
816
817
818
819
820
821
822
823
824
825
       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 &&
826
               Instance.spindleUse i == su &&
Iustin Pop's avatar
Iustin Pop committed
827
               Types.isBad fail1
Iustin Pop's avatar
Iustin Pop committed
828
829

prop_Text_Load_InstanceFail ktn fields =
830
  length fields /= 10 && length fields /= 11 ==>
Iustin Pop's avatar
Iustin Pop committed
831
    case Text.loadInst nl fields of
Iustin Pop's avatar
Iustin Pop committed
832
      Types.Ok _ -> failTest "Managed to load instance from invalid data"
833
834
      Types.Bad msg -> printTestCase ("Unrecognised error message: " ++ msg) $
                       "Invalid/incomplete instance data: '" `isPrefixOf` msg
835
    where nl = Data.Map.fromList ktn
Iustin Pop's avatar
Iustin Pop committed
836
837

prop_Text_Load_Node name tm nm fm td fd tc fo =
Iustin Pop's avatar
Iustin Pop committed
838
839
840
841
842
843
844
845
846
847
  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
848
849
               then "Y"
               else "N"
Iustin Pop's avatar
Iustin Pop committed
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
      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
866
867

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

870
prop_Text_NodeLSIdempotent node =
Iustin Pop's avatar
Iustin Pop committed
871
  (Text.loadNode defGroupAssoc.
872
       Utils.sepSplit '|' . Text.serializeNode defGroupList) n ==?
Iustin Pop's avatar
Iustin Pop committed
873
  Just (Node.name n, n)
874
    -- override failN1 to what loadNode returns by default
875
876
    where n = Node.setPolicy Types.defIPolicy $
              node { Node.failN1 = True, Node.offline = False }
877

878
879
880
prop_Text_ISpecIdempotent ispec =
  case Text.loadISpec "dummy" . Utils.sepSplit ',' .
       Text.serializeISpec $ ispec of
Iustin Pop's avatar
Iustin Pop committed
881
    Types.Bad msg -> failTest $ "Failed to load ispec: " ++ msg
882
883
884
885
886
    Types.Ok ispec' -> ispec ==? ispec'

prop_Text_IPolicyIdempotent ipol =
  case Text.loadIPolicy . Utils.sepSplit '|' $
       Text.serializeIPolicy owner ipol of
Iustin Pop's avatar
Iustin Pop committed
887
    Types.Bad msg -> failTest $ "Failed to load ispec: " ++ msg
888
889
890
    Types.Ok res -> (owner, ipol) ==? res
  where owner = "dummy"

891
892
893
894
895
896
897
898
899
900
901
902
-- | This property, while being in the text tests, does more than just
-- test end-to-end the serialisation and loading back workflow; it
-- also tests the Loader.mergeData and the actuall
-- Cluster.iterateAlloc (for well-behaving w.r.t. instance
-- allocations, not for the business logic). As such, it's a quite
-- complex and slow test, and that's the reason we restrict it to
-- small cluster sizes.
prop_Text_CreateSerialise =
  forAll genTags $ \ctags ->
  forAll (choose (1, 20)) $ \maxiter ->
  forAll (choose (2, 10)) $ \count ->
  forAll genOnlineNode $ \node ->
903
  forAll (genInstanceSmallerThanNode node) $ \inst ->
904
  let nl = makeSmallCluster node count
905
      reqnodes = Instance.requiredNodes $ Instance.diskTemplate inst
906
  in case Cluster.genAllocNodes defGroupList nl reqnodes True >>= \allocn ->
907
     Cluster.iterateAlloc nl Container.empty (Just maxiter) inst allocn [] []
908
     of
Iustin Pop's avatar
Iustin Pop committed
909
       Types.Bad msg -> failTest $ "Failed to allocate: " ++ msg
910
911
912
913
914
915
916
       Types.Ok (_, _, _, [], _) -> printTestCase
                                    "Failed to allocate: no allocations" False
       Types.Ok (_, nl', il', _, _) ->
         let cdata = Loader.ClusterData defGroupList nl' il' ctags
                     Types.defIPolicy
             saved = Text.serializeCluster cdata
         in case Text.parseData saved >>= Loader.mergeData [] [] [] [] of
Iustin Pop's avatar
Iustin Pop committed
917
              Types.Bad msg -> failTest $ "Failed to load/merge: " ++ msg
918
919
920
921
              Types.Ok (Loader.ClusterData gl2 nl2 il2 ctags2 cpol2) ->
                ctags ==? ctags2 .&&.
                Types.defIPolicy ==? cpol2 .&&.
                il' ==? il2 .&&.
922
923
                defGroupList ==? gl2 .&&.
                nl' ==? nl2
924

925
testSuite "Text"
Iustin Pop's avatar
Iustin Pop committed
926
927
928
929
930
            [ 'prop_Text_Load_Instance
            , 'prop_Text_Load_InstanceFail
            , 'prop_Text_Load_Node
            , 'prop_Text_Load_NodeFail
            , 'prop_Text_NodeLSIdempotent
931
932
            , 'prop_Text_ISpecIdempotent
            , 'prop_Text_IPolicyIdempotent
933
            , 'prop_Text_CreateSerialise
Iustin Pop's avatar
Iustin Pop committed
934
            ]
935

936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
-- *** Simu backend

-- | Generates a tuple of specs for simulation.
genSimuSpec :: Gen (String, Int, Int, Int, Int)
genSimuSpec = do
  pol <- elements [C.allocPolicyPreferred,
                   C.allocPolicyLastResort, C.allocPolicyUnallocable,
                  "p", "a", "u"]
 -- should be reasonable (nodes/group), bigger values only complicate
 -- the display of failed tests, and we don't care (in this particular
 -- test) about big node groups
  nodes <- choose (0, 20)
  dsk <- choose (0, maxDsk)
  mem <- choose (0, maxMem)
  cpu <- choose (0, maxCpu)
  return (pol, nodes, dsk, mem, cpu)

-- | Checks that given a set of corrects specs, we can load them
-- successfully, and that at high-level the values look right.
prop_SimuLoad =
  forAll (choose (0, 10)) $ \ngroups ->
  forAll (replicateM ngroups genSimuSpec) $ \specs ->
  let strspecs = map (\(p, n, d, m, c) -> printf "%s,%d,%d,%d,%d"
                                          p n d m c::String) specs
      totnodes = sum $ map (\(_, n, _, _, _) -> n) specs
      mdc_in = concatMap (\(_, n, d, m, c) ->
                            replicate n (fromIntegral m, fromIntegral d,
                                         fromIntegral c,
                                         fromIntegral m, fromIntegral d)) specs
  in case Simu.parseData strspecs of
       Types.Bad msg -> failTest $ "Failed to load specs: " ++ msg
       Types.Ok (Loader.ClusterData gl nl il tags ipol) ->
         let nodes = map snd $ IntMap.toAscList nl
             nidx = map Node.idx nodes
             mdc_out = map (\n -> (Node.tMem n, Node.tDsk n, Node.tCpu n,
                                   Node.fMem n, Node.fDsk n)) nodes
         in
         Container.size gl ==? ngroups .&&.
         Container.size nl ==? totnodes .&&.
         Container.size il ==? 0 .&&.
         length tags ==? 0 .&&.
         ipol ==? Types.defIPolicy .&&.
         nidx ==? [1..totnodes] .&&.
         mdc_in ==? mdc_out .&&.
         map Group.iPolicy (Container.elems gl) ==?
             replicate ngroups Types.defIPolicy

testSuite "Simu"
            [ 'prop_SimuLoad
            ]

987
-- ** Node tests