root / Ganeti / HTools / QC.hs @ 9cf4267a
History | View | Annotate | Download (3.5 kB)
1 |
module Ganeti.HTools.QC |
---|---|
2 |
where |
3 |
|
4 |
import Test.QuickCheck |
5 |
import Data.Maybe |
6 |
import qualified Ganeti.HTools.CLI as CLI |
7 |
import qualified Ganeti.HTools.Cluster as Cluster |
8 |
import qualified Ganeti.HTools.Container as Container |
9 |
import qualified Ganeti.HTools.IAlloc as IAlloc |
10 |
import qualified Ganeti.HTools.Instance as Instance |
11 |
import qualified Ganeti.HTools.Loader as Loader |
12 |
import qualified Ganeti.HTools.Node as Node |
13 |
import qualified Ganeti.HTools.PeerMap as PeerMap |
14 |
import qualified Ganeti.HTools.Rapi as Rapi |
15 |
import qualified Ganeti.HTools.Text as Text |
16 |
import qualified Ganeti.HTools.Types as Types |
17 |
import qualified Ganeti.HTools.Utils as Utils |
18 |
|
19 |
-- copied from the introduction to quickcheck |
20 |
instance Arbitrary Char where |
21 |
arbitrary = choose ('\32', '\128') |
22 |
|
23 |
-- let's generate a random instance |
24 |
instance Arbitrary Instance.Instance where |
25 |
arbitrary = do |
26 |
name <- arbitrary |
27 |
mem <- choose(0, 100) |
28 |
dsk <- choose(0, 100) |
29 |
run_st <- arbitrary |
30 |
pn <- arbitrary |
31 |
sn <- arbitrary |
32 |
return $ Instance.create name mem dsk run_st pn sn |
33 |
|
34 |
-- and a random node |
35 |
instance Arbitrary Node.Node where |
36 |
arbitrary = do |
37 |
name <- arbitrary |
38 |
mem_t <- arbitrary |
39 |
mem_f <- choose (0, mem_t) |
40 |
mem_n <- choose (0, mem_t - mem_f) |
41 |
dsk_t <- arbitrary |
42 |
dsk_f <- choose (0, dsk_t) |
43 |
offl <- arbitrary |
44 |
let n = Node.create name (fromIntegral mem_t) mem_n mem_f |
45 |
(fromIntegral dsk_t) dsk_f offl |
46 |
n' = Node.buildPeers n Container.empty |
47 |
return n' |
48 |
|
49 |
-- | Make sure add is idempotent |
50 |
prop_PeerMap_addIdempotent pmap key elem = |
51 |
fn puniq == fn (fn puniq) |
52 |
where fn = PeerMap.add key elem |
53 |
puniq = PeerMap.accumArray const pmap |
54 |
_types = (pmap::PeerMap.PeerMap, |
55 |
key::PeerMap.Key, elem::PeerMap.Elem) |
56 |
|
57 |
-- | Make sure remove is idempotent |
58 |
prop_PeerMap_removeIdempotent pmap key = |
59 |
fn puniq == fn (fn puniq) |
60 |
where fn = PeerMap.remove key |
61 |
puniq = PeerMap.accumArray const pmap |
62 |
_types = (pmap::PeerMap.PeerMap, |
63 |
key::PeerMap.Key) |
64 |
|
65 |
-- | Make sure a missing item returns 0 |
66 |
prop_PeerMap_findMissing pmap key = |
67 |
PeerMap.find key (PeerMap.remove key puniq) == 0 |
68 |
where fn = PeerMap.remove key |
69 |
puniq = PeerMap.accumArray const pmap |
70 |
_types = (pmap::PeerMap.PeerMap, |
71 |
key::PeerMap.Key) |
72 |
|
73 |
-- | Make sure an added item is found |
74 |
prop_PeerMap_addFind pmap key elem = |
75 |
PeerMap.find key (PeerMap.add key elem puniq) == elem |
76 |
where puniq = PeerMap.accumArray const pmap |
77 |
_types = (pmap::PeerMap.PeerMap, |
78 |
key::PeerMap.Key, elem::PeerMap.Elem) |
79 |
|
80 |
-- | Manual check that maxElem returns the maximum indeed, or 0 for null |
81 |
prop_PeerMap_maxElem pmap = |
82 |
PeerMap.maxElem puniq == if null puniq then 0 |
83 |
else (maximum . snd . unzip) puniq |
84 |
where |
85 |
puniq = PeerMap.accumArray const pmap |
86 |
_types = pmap::PeerMap.PeerMap |
87 |
|
88 |
prop_Node_addPri node inst = (Instance.mem inst >= Node.f_mem node || |
89 |
Instance.dsk inst >= Node.f_dsk node) && |
90 |
(not $ Node.failN1 node) |
91 |
==> |
92 |
isNothing(Node.addPri node inst) |
93 |
where _types = (node::Node.Node, inst::Instance.Instance) |
94 |
|
95 |
prop_Node_addSec node inst pdx = |
96 |
(Instance.mem inst >= (Node.f_mem node - Node.r_mem node) || |
97 |
Instance.dsk inst >= Node.f_dsk node) && |
98 |
(not $ Node.failN1 node) |
99 |
==> isNothing(Node.addSec node inst pdx) |
100 |
where _types = (node::Node.Node, inst::Instance.Instance, pdx::Int) |