root / test / hs / Test / Ganeti / Rpc.hs @ 212b66c3
History | View | Annotate | Download (3.8 kB)
1 |
{-# LANGUAGE TemplateHaskell #-} |
---|---|
2 |
{-# OPTIONS_GHC -fno-warn-orphans #-} |
3 |
|
4 |
{-| Unittests for ganeti-htools. |
5 |
|
6 |
-} |
7 |
|
8 |
{- |
9 |
|
10 |
Copyright (C) 2009, 2010, 2011, 2012, 2013 Google Inc. |
11 |
|
12 |
This program is free software; you can redistribute it and/or modify |
13 |
it under the terms of the GNU General Public License as published by |
14 |
the Free Software Foundation; either version 2 of the License, or |
15 |
(at your option) any later version. |
16 |
|
17 |
This program is distributed in the hope that it will be useful, but |
18 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
19 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
20 |
General Public License for more details. |
21 |
|
22 |
You should have received a copy of the GNU General Public License |
23 |
along with this program; if not, write to the Free Software |
24 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
25 |
02110-1301, USA. |
26 |
|
27 |
-} |
28 |
|
29 |
module Test.Ganeti.Rpc (testRpc) where |
30 |
|
31 |
import Test.QuickCheck |
32 |
import Test.QuickCheck.Monadic (monadicIO, run, stop) |
33 |
|
34 |
import Control.Applicative |
35 |
import qualified Data.Map as Map |
36 |
|
37 |
import Test.Ganeti.TestHelper |
38 |
import Test.Ganeti.TestCommon |
39 |
import Test.Ganeti.Objects () |
40 |
|
41 |
import qualified Ganeti.Rpc as Rpc |
42 |
import qualified Ganeti.Objects as Objects |
43 |
import qualified Ganeti.Types as Types |
44 |
import qualified Ganeti.JSON as JSON |
45 |
import Ganeti.Types |
46 |
|
47 |
instance Arbitrary Rpc.RpcCallAllInstancesInfo where |
48 |
arbitrary = Rpc.RpcCallAllInstancesInfo <$> arbitrary |
49 |
|
50 |
instance Arbitrary Rpc.RpcCallInstanceList where |
51 |
arbitrary = Rpc.RpcCallInstanceList <$> arbitrary |
52 |
|
53 |
instance Arbitrary Rpc.RpcCallNodeInfo where |
54 |
arbitrary = Rpc.RpcCallNodeInfo <$> genStorageUnitMap <*> genHvSpecs |
55 |
|
56 |
genStorageUnit :: Gen StorageUnit |
57 |
genStorageUnit = do |
58 |
storage_type <- arbitrary |
59 |
storage_key <- genName |
60 |
storage_es <- arbitrary |
61 |
return $ addParamsToStorageUnit storage_es (SURaw storage_type storage_key) |
62 |
|
63 |
genStorageUnits :: Gen [StorageUnit] |
64 |
genStorageUnits = do |
65 |
num_storage_units <- choose (0, 5) |
66 |
vectorOf num_storage_units genStorageUnit |
67 |
|
68 |
genStorageUnitMap :: Gen (Map.Map String [StorageUnit]) |
69 |
genStorageUnitMap = do |
70 |
num_nodes <- choose (0,5) |
71 |
node_uuids <- vectorOf num_nodes genName |
72 |
storage_units_list <- vectorOf num_nodes genStorageUnits |
73 |
return $ Map.fromList (zip node_uuids storage_units_list) |
74 |
|
75 |
-- | Generate hypervisor specifications to be used for the NodeInfo call |
76 |
genHvSpecs :: Gen [ (Types.Hypervisor, Objects.HvParams) ] |
77 |
genHvSpecs = do |
78 |
numhv <- choose (0, 5) |
79 |
hvs <- vectorOf numhv arbitrary |
80 |
hvparams <- vectorOf numhv genHvParams |
81 |
let specs = zip hvs hvparams |
82 |
return specs |
83 |
|
84 |
-- FIXME: Generate more interesting hvparams |
85 |
-- | Generate Hvparams |
86 |
genHvParams :: Gen Objects.HvParams |
87 |
genHvParams = return $ JSON.GenericContainer Map.empty |
88 |
|
89 |
-- | Monadic check that, for an offline node and a call that does not |
90 |
-- offline nodes, we get a OfflineNodeError response. |
91 |
-- FIXME: We need a way of generalizing this, running it for |
92 |
-- every call manually will soon get problematic |
93 |
prop_noffl_request_allinstinfo :: Rpc.RpcCallAllInstancesInfo -> Property |
94 |
prop_noffl_request_allinstinfo call = |
95 |
forAll (arbitrary `suchThat` Objects.nodeOffline) $ \node -> monadicIO $ do |
96 |
res <- run $ Rpc.executeRpcCall [node] call |
97 |
stop $ res ==? [(node, Left Rpc.OfflineNodeError)] |
98 |
|
99 |
prop_noffl_request_instlist :: Rpc.RpcCallInstanceList -> Property |
100 |
prop_noffl_request_instlist call = |
101 |
forAll (arbitrary `suchThat` Objects.nodeOffline) $ \node -> monadicIO $ do |
102 |
res <- run $ Rpc.executeRpcCall [node] call |
103 |
stop $ res ==? [(node, Left Rpc.OfflineNodeError)] |
104 |
|
105 |
prop_noffl_request_nodeinfo :: Rpc.RpcCallNodeInfo -> Property |
106 |
prop_noffl_request_nodeinfo call = |
107 |
forAll (arbitrary `suchThat` Objects.nodeOffline) $ \node -> monadicIO $ do |
108 |
res <- run $ Rpc.executeRpcCall [node] call |
109 |
stop $ res ==? [(node, Left Rpc.OfflineNodeError)] |
110 |
|
111 |
testSuite "Rpc" |
112 |
[ 'prop_noffl_request_allinstinfo |
113 |
, 'prop_noffl_request_instlist |
114 |
, 'prop_noffl_request_nodeinfo |
115 |
] |