Statistics
| Branch: | Tag: | Revision:

root / Ganeti / HTools / QC.hs @ 7dd5ee6c

History | View | Annotate | Download (5.4 kB)

1
module Ganeti.HTools.QC
2
    ( test_PeerMap
3
    , test_Container
4
    , test_Instance
5
    , test_Node
6
    ) where
7

    
8
import Test.QuickCheck
9
import Test.QuickCheck.Batch
10
import Data.Maybe
11
import qualified Ganeti.HTools.CLI as CLI
12
import qualified Ganeti.HTools.Cluster as Cluster
13
import qualified Ganeti.HTools.Container as Container
14
import qualified Ganeti.HTools.IAlloc as IAlloc
15
import qualified Ganeti.HTools.Instance as Instance
16
import qualified Ganeti.HTools.Loader as Loader
17
import qualified Ganeti.HTools.Node as Node
18
import qualified Ganeti.HTools.PeerMap as PeerMap
19
import qualified Ganeti.HTools.Rapi as Rapi
20
import qualified Ganeti.HTools.Text as Text
21
import qualified Ganeti.HTools.Types as Types
22
import qualified Ganeti.HTools.Utils as Utils
23

    
24
-- copied from the introduction to quickcheck
25
instance Arbitrary Char where
26
    arbitrary = choose ('\32', '\128')
27

    
28
-- let's generate a random instance
29
instance Arbitrary Instance.Instance where
30
    arbitrary = do
31
      name <- arbitrary
32
      mem <- choose(0, 100)
33
      dsk <- choose(0, 100)
34
      run_st <- arbitrary
35
      pn <- arbitrary
36
      sn <- arbitrary
37
      return $ Instance.create name mem dsk run_st pn sn
38

    
39
-- and a random node
40
instance Arbitrary Node.Node where
41
    arbitrary = do
42
      name <- arbitrary
43
      mem_t <- arbitrary
44
      mem_f <- choose (0, mem_t)
45
      mem_n <- choose (0, mem_t - mem_f)
46
      dsk_t <- arbitrary
47
      dsk_f <- choose (0, dsk_t)
48
      offl <- arbitrary
49
      let n = Node.create name (fromIntegral mem_t) mem_n mem_f
50
              (fromIntegral dsk_t) dsk_f offl
51
          n' = Node.buildPeers n Container.empty
52
      return n'
53

    
54
-- | Make sure add is idempotent
55
prop_PeerMap_addIdempotent pmap key elem =
56
    fn puniq == fn (fn puniq)
57
    where _types = (pmap::PeerMap.PeerMap,
58
                    key::PeerMap.Key, elem::PeerMap.Elem)
59
          fn = PeerMap.add key elem
60
          puniq = PeerMap.accumArray const pmap
61

    
62
-- | Make sure remove is idempotent
63
prop_PeerMap_removeIdempotent pmap key =
64
    fn puniq == fn (fn puniq)
65
    where _types = (pmap::PeerMap.PeerMap, key::PeerMap.Key)
66
          fn = PeerMap.remove key
67
          puniq = PeerMap.accumArray const pmap
68

    
69
-- | Make sure a missing item returns 0
70
prop_PeerMap_findMissing pmap key =
71
    PeerMap.find key (PeerMap.remove key puniq) == 0
72
    where _types = (pmap::PeerMap.PeerMap, key::PeerMap.Key)
73
          puniq = PeerMap.accumArray const pmap
74

    
75
-- | Make sure an added item is found
76
prop_PeerMap_addFind pmap key elem =
77
    PeerMap.find key (PeerMap.add key elem puniq) == elem
78
    where _types = (pmap::PeerMap.PeerMap,
79
                    key::PeerMap.Key, elem::PeerMap.Elem)
80
          puniq = PeerMap.accumArray const pmap
81

    
82
-- | Manual check that maxElem returns the maximum indeed, or 0 for null
83
prop_PeerMap_maxElem pmap =
84
    PeerMap.maxElem puniq == if null puniq then 0
85
                             else (maximum . snd . unzip) puniq
86
    where _types = pmap::PeerMap.PeerMap
87
          puniq = PeerMap.accumArray const pmap
88

    
89
test_PeerMap =
90
    [ run prop_PeerMap_addIdempotent
91
    , run prop_PeerMap_removeIdempotent
92
    , run prop_PeerMap_maxElem
93
    , run prop_PeerMap_addFind
94
    , run prop_PeerMap_findMissing
95
    ]
96

    
97
-- Container tests
98

    
99
prop_Container_addTwo cdata i1 i2 =
100
    fn i1 i2 cont == fn i2 i1 cont &&
101
       fn i1 i2 cont == fn i1 i2 (fn i1 i2 cont)
102
    where _types = (cdata::[Int],
103
                    i1::Int, i2::Int)
104
          cont = foldl (\c x -> Container.add x x c) Container.empty cdata
105
          fn x1 x2 = Container.addTwo x1 x1 x2 x2
106

    
107
test_Container =
108
    [ run prop_Container_addTwo ]
109

    
110
-- Simple instance tests, we only have setter/getters
111

    
112
prop_Instance_setIdx inst idx =
113
    Instance.idx (Instance.setIdx inst idx) == idx
114
    where _types = (inst::Instance.Instance, idx::Types.Idx)
115

    
116
prop_Instance_setName inst name =
117
    Instance.name (Instance.setName inst name) == name
118
    where _types = (inst::Instance.Instance, name::String)
119

    
120
prop_Instance_setPri inst pdx =
121
    Instance.pnode (Instance.setPri inst pdx) == pdx
122
    where _types = (inst::Instance.Instance, pdx::Types.Ndx)
123

    
124
prop_Instance_setSec inst sdx =
125
    Instance.snode (Instance.setSec inst sdx) == sdx
126
    where _types = (inst::Instance.Instance, sdx::Types.Ndx)
127

    
128
prop_Instance_setBoth inst pdx sdx =
129
    Instance.pnode si == pdx && Instance.snode si == sdx
130
    where _types = (inst::Instance.Instance, pdx::Types.Ndx, sdx::Types.Ndx)
131
          si = Instance.setBoth inst pdx sdx
132

    
133
test_Instance =
134
    [ run prop_Instance_setIdx
135
    , run prop_Instance_setName
136
    , run prop_Instance_setPri
137
    , run prop_Instance_setSec
138
    , run prop_Instance_setBoth
139
    ]
140

    
141
-- Node tests
142

    
143
-- | Check that an instance add with too high memory or disk will be rejected
144
prop_Node_addPri node inst = (Instance.mem inst >= Node.f_mem node ||
145
                              Instance.dsk inst >= Node.f_dsk node) &&
146
                             (not $ Node.failN1 node)
147
                             ==>
148
                             isNothing(Node.addPri node inst)
149
    where _types = (node::Node.Node, inst::Instance.Instance)
150

    
151

    
152
-- | Check that an instance add with too high memory or disk will be rejected
153
prop_Node_addSec node inst pdx =
154
    (Instance.mem inst >= (Node.f_mem node - Node.r_mem node) ||
155
     Instance.dsk inst >= Node.f_dsk node) &&
156
    (not $ Node.failN1 node)
157
    ==> isNothing(Node.addSec node inst pdx)
158
        where _types = (node::Node.Node, inst::Instance.Instance, pdx::Int)
159

    
160
test_Node =
161
    [ run prop_Node_addPri
162
    , run prop_Node_addSec
163
    ]