Revision 7806125e

b/htools/Ganeti/HTools/QC.hs
90 90
maxCpu :: Int
91 91
maxCpu = 1024
92 92

  
93
-- | All disk templates (used later)
94
allDiskTemplates :: [Types.DiskTemplate]
95
allDiskTemplates = [minBound..maxBound]
96

  
93 97
-- | Null iPolicy, and by null we mean very liberal.
94 98
nullIPolicy = Types.IPolicy
95 99
  { Types.iPolicyMinSpec = Types.ISpec { Types.iSpecMemorySize = 0
......
389 393

  
390 394
instance Arbitrary Types.ISpec where
391 395
  arbitrary = do
392
    mem <- arbitrary::Gen (NonNegative Int)
396
    mem_s <- arbitrary::Gen (NonNegative Int)
393 397
    dsk_c <- arbitrary::Gen (NonNegative Int)
394 398
    dsk_s <- arbitrary::Gen (NonNegative Int)
395
    cpu <- arbitrary::Gen (NonNegative Int)
396
    nic <- arbitrary::Gen (NonNegative Int)
397
    return Types.ISpec { Types.iSpecMemorySize = fromIntegral mem
398
                       , Types.iSpecCpuCount   = fromIntegral cpu
399
    cpu_c <- arbitrary::Gen (NonNegative Int)
400
    nic_c <- arbitrary::Gen (NonNegative Int)
401
    return Types.ISpec { Types.iSpecMemorySize = fromIntegral mem_s
402
                       , Types.iSpecCpuCount   = fromIntegral cpu_c
399 403
                       , Types.iSpecDiskSize   = fromIntegral dsk_s
400 404
                       , Types.iSpecDiskCount  = fromIntegral dsk_c
401
                       , Types.iSpecNicCount   = fromIntegral nic
405
                       , Types.iSpecNicCount   = fromIntegral nic_c
402 406
                       }
403 407

  
404
-- | Helper function to check whether a spec is LTE than another
405
iSpecSmaller :: Types.ISpec -> Types.ISpec -> Bool
406
iSpecSmaller imin imax =
407
  Types.iSpecMemorySize imin <= Types.iSpecMemorySize imax &&
408
  Types.iSpecCpuCount imin   <= Types.iSpecCpuCount imax &&
409
  Types.iSpecDiskSize imin   <= Types.iSpecDiskSize imax &&
410
  Types.iSpecDiskCount imin  <= Types.iSpecDiskCount imax &&
411
  Types.iSpecNicCount imin   <= Types.iSpecNicCount imax
408
-- | Generates an ispec bigger than the given one.
409
genBiggerISpec :: Types.ISpec -> Gen Types.ISpec
410
genBiggerISpec imin = do
411
  mem_s <- choose (Types.iSpecMemorySize imin, maxBound)
412
  dsk_c <- choose (Types.iSpecDiskCount imin, maxBound)
413
  dsk_s <- choose (Types.iSpecDiskSize imin, maxBound)
414
  cpu_c <- choose (Types.iSpecCpuCount imin, maxBound)
415
  nic_c <- choose (Types.iSpecNicCount imin, maxBound)
416
  return Types.ISpec { Types.iSpecMemorySize = fromIntegral mem_s
417
                     , Types.iSpecCpuCount   = fromIntegral cpu_c
418
                     , Types.iSpecDiskSize   = fromIntegral dsk_s
419
                     , Types.iSpecDiskCount  = fromIntegral dsk_c
420
                     , Types.iSpecNicCount   = fromIntegral nic_c
421
                     }
412 422

  
413 423
instance Arbitrary Types.IPolicy where
414 424
  arbitrary = do
415 425
    imin <- arbitrary
416
    istd <- arbitrary `suchThat` (iSpecSmaller imin)
417
    imax <- arbitrary `suchThat` (iSpecSmaller istd)
418
    dts  <- arbitrary
426
    istd <- genBiggerISpec imin
427
    imax <- genBiggerISpec istd
428
    num_tmpl <- choose (0, length allDiskTemplates)
429
    dts  <- genUniquesList num_tmpl
419 430
    return Types.IPolicy { Types.iPolicyMinSpec = imin
420 431
                         , Types.iPolicyStdSpec = istd
421 432
                         , Types.iPolicyMaxSpec = imax

Also available in: Unified diff