root / Ganeti / HTools / QC.hs @ 7bc82927
History | View | Annotate | Download (4.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 _types = (pmap::PeerMap.PeerMap, |
53 |
key::PeerMap.Key, elem::PeerMap.Elem) |
54 |
fn = PeerMap.add key elem |
55 |
puniq = PeerMap.accumArray const pmap |
56 |
|
57 |
-- | Make sure remove is idempotent |
58 |
prop_PeerMap_removeIdempotent pmap key = |
59 |
fn puniq == fn (fn puniq) |
60 |
where _types = (pmap::PeerMap.PeerMap, key::PeerMap.Key) |
61 |
fn = PeerMap.remove key |
62 |
puniq = PeerMap.accumArray const pmap |
63 |
|
64 |
-- | Make sure a missing item returns 0 |
65 |
prop_PeerMap_findMissing pmap key = |
66 |
PeerMap.find key (PeerMap.remove key puniq) == 0 |
67 |
where _types = (pmap::PeerMap.PeerMap, key::PeerMap.Key) |
68 |
puniq = PeerMap.accumArray const pmap |
69 |
|
70 |
-- | Make sure an added item is found |
71 |
prop_PeerMap_addFind pmap key elem = |
72 |
PeerMap.find key (PeerMap.add key elem puniq) == elem |
73 |
where _types = (pmap::PeerMap.PeerMap, |
74 |
key::PeerMap.Key, elem::PeerMap.Elem) |
75 |
puniq = PeerMap.accumArray const pmap |
76 |
|
77 |
-- | Manual check that maxElem returns the maximum indeed, or 0 for null |
78 |
prop_PeerMap_maxElem pmap = |
79 |
PeerMap.maxElem puniq == if null puniq then 0 |
80 |
else (maximum . snd . unzip) puniq |
81 |
where _types = pmap::PeerMap.PeerMap |
82 |
puniq = PeerMap.accumArray const pmap |
83 |
|
84 |
-- Simple instance tests, we only have setter/getters |
85 |
|
86 |
prop_Instance_setIdx inst idx = |
87 |
Instance.idx (Instance.setIdx inst idx) == idx |
88 |
where _types = (inst::Instance.Instance, idx::Types.Idx) |
89 |
|
90 |
prop_Instance_setName inst name = |
91 |
Instance.name (Instance.setName inst name) == name |
92 |
where _types = (inst::Instance.Instance, name::String) |
93 |
|
94 |
prop_Instance_setPri inst pdx = |
95 |
Instance.pnode (Instance.setPri inst pdx) == pdx |
96 |
where _types = (inst::Instance.Instance, pdx::Types.Ndx) |
97 |
|
98 |
prop_Instance_setSec inst sdx = |
99 |
Instance.snode (Instance.setSec inst sdx) == sdx |
100 |
where _types = (inst::Instance.Instance, sdx::Types.Ndx) |
101 |
|
102 |
prop_Instance_setBoth inst pdx sdx = |
103 |
Instance.pnode si == pdx && Instance.snode si == sdx |
104 |
where _types = (inst::Instance.Instance, pdx::Types.Ndx, sdx::Types.Ndx) |
105 |
si = Instance.setBoth inst pdx sdx |
106 |
|
107 |
-- | Check that an instance add with too high memory or disk will be rejected |
108 |
prop_Node_addPri node inst = (Instance.mem inst >= Node.f_mem node || |
109 |
Instance.dsk inst >= Node.f_dsk node) && |
110 |
(not $ Node.failN1 node) |
111 |
==> |
112 |
isNothing(Node.addPri node inst) |
113 |
where _types = (node::Node.Node, inst::Instance.Instance) |
114 |
|
115 |
|
116 |
-- | Check that an instance add with too high memory or disk will be rejected |
117 |
prop_Node_addSec node inst pdx = |
118 |
(Instance.mem inst >= (Node.f_mem node - Node.r_mem node) || |
119 |
Instance.dsk inst >= Node.f_dsk node) && |
120 |
(not $ Node.failN1 node) |
121 |
==> isNothing(Node.addSec node inst pdx) |
122 |
where _types = (node::Node.Node, inst::Instance.Instance, pdx::Int) |