Revision 0b288282 htest/Test/Ganeti/Network.hs

b/htest/Test/Ganeti/Network.hs
3 3

  
4 4
module Test.Ganeti.Network
5 5
  ( testNetwork
6
  , genBitStringMaxLen
7
  , genNetworkType
6 8
  ) where
7 9

  
8 10
import Test.QuickCheck
9 11

  
10
import Control.Monad
11

  
12 12
import Ganeti.Network as Network
13 13
import Ganeti.Objects as Objects
14
import Ganeti.Types
15 14

  
16
import Test.Ganeti.Query.Language (genJSValue)
15
import Test.Ganeti.Objects
16
  ( genBitStringMaxLen
17
  , genNetworkType
18
  , genValidNetwork )
17 19
import Test.Ganeti.TestHelper
18 20
import Test.Ganeti.TestCommon
19 21

  
20 22
import qualified Data.Vector.Unboxed as V
21
import qualified Data.Set as S
22 23

  
23 24
-- * Generators and arbitrary instances
24 25

  
25
-- | Generate an arbitrary string consisting of '0' and '1' of the given length.
26
genBitString :: Int -> Gen String
27
genBitString len = vectorOf len (elements "01")
28

  
29
-- | Generate an arbitrary string consisting of '0' and '1' of the maximum given
30
-- length.
31
genBitStringMaxLen :: Int -> Gen String
32
genBitStringMaxLen maxLen = choose (0, maxLen) >>= genBitString
33

  
34
-- | Generates an arbitrary bit vector of the given length.
35
genBitVector :: Int -> Gen (V.Vector Bool)
36
genBitVector len = do
37
  boolList <- vector len::Gen [Bool]
38
  return $ V.fromList boolList
39

  
40
-- | Generates a network instance with bit vectors of the given lengths for
41
-- reservations and external reservations.
42
genValidNetwork :: Int -> Gen Objects.Network
43
genValidNetwork maxLenBitStr = do
44
  lenBitStr <- choose (0, maxLenBitStr)
45
  name <- genName >>= mkNonEmpty
46
  network_type <- genMaybe genNetworkType
47
  mac_prefix <- genMaybe genName
48
  fam <- arbitrary
49
  net <- genName >>= mkNonEmpty
50
  net6 <- genMaybe genName
51
  gateway <- genMaybe genName
52
  gateway6 <- genMaybe genName
53
  size <- genMaybe genJSValue
54
  res <- liftM Just (genBitString lenBitStr)
55
  ext_res <- liftM Just (genBitString lenBitStr)
56
  let n = Network name network_type mac_prefix fam net net6 gateway
57
          gateway6 size res ext_res 0 S.empty
58
  return n
59

  
60
-- | Generates an arbitrary network type.
61
genNetworkType :: Gen NetworkType
62
genNetworkType = elements [ PrivateNetwork, PublicNetwork ]
63

  
64
-- | Network instances are generated arbitrarily only with short bit strings to
65
-- not slow down the test execution too much.
66
instance Arbitrary Objects.Network where
67
  arbitrary = genValidNetwork 256
68

  
69 26
-- | Generates address pools. The size of the network is intentionally
70 27
-- decoupled from the size of the bit vectors, to avoid slowing down
71 28
-- the tests by generating unnecessary bit strings.
72 29
genAddressPool :: Int -> Gen AddressPool
73 30
genAddressPool maxLenBitVec = do
74
  net <- arbitrary
31
  -- Generating networks with netmask of minimum /24 to avoid too long
32
  -- bit strings being generated.
33
  net <- genValidNetwork
75 34
  lenBitVec <- choose (0, maxLenBitVec)
76 35
  res <- genBitVector lenBitVec
77 36
  ext_res <- genBitVector lenBitVec
......
79 38
                     , reservations = res
80 39
                     , extReservations = ext_res }
81 40

  
41
-- | Generates an arbitrary bit vector of the given length.
42
genBitVector :: Int -> Gen (V.Vector Bool)
43
genBitVector len = do
44
  boolList <- vector len::Gen [Bool]
45
  return $ V.fromList boolList
46

  
82 47
instance Arbitrary AddressPool where
83 48
  arbitrary = genAddressPool ((2::Int)^(8::Int))
84 49

  

Also available in: Unified diff