Debian Codegraph @

test/hs/Test/Ganeti/Rpc.hs

raw
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 All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are
15 met:
16
17 1. Redistributions of source code must retain the above copyright notice,
18 this list of conditions and the following disclaimer.
19
20 2. Redistributions in binary form must reproduce the above copyright
21 notice, this list of conditions and the following disclaimer in the
22 documentation and/or other materials provided with the distribution.
23
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
28 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
36 -}
37
38 module Test.Ganeti.Rpc (testRpc) where
39
40 import Test.QuickCheck
41 import Test.QuickCheck.Monadic (monadicIO, run, stop)
42
43 import qualified Data.Map as Map
44
45 import Test.Ganeti.TestHelper
46 import Test.Ganeti.TestCommon
47 import Test.Ganeti.Objects (genInst)
48
49 import qualified Ganeti.Rpc as Rpc
50 import qualified Ganeti.Objects as Objects
51 import qualified Ganeti.Types as Types
52 import qualified Ganeti.JSON as JSON
53 import Ganeti.Types
54
55 instance Arbitrary Rpc.RpcCallInstanceConsoleInfo where
56 arbitrary = Rpc.RpcCallInstanceConsoleInfo <$> genConsoleInfoCallParams
57
58 instance Arbitrary Rpc.Compressed where
59 arbitrary = Rpc.toCompressed <$> arbitrary
60
61 genStorageUnit :: Gen StorageUnit
62 genStorageUnit = do
63 storage_type <- arbitrary
64 storage_key <- genName
65 storage_es <- arbitrary
66 return $ addParamsToStorageUnit storage_es (SURaw storage_type storage_key)
67
68 genStorageUnits :: Gen [StorageUnit]
69 genStorageUnits = do
70 num_storage_units <- choose (0, 5)
71 vectorOf num_storage_units genStorageUnit
72
73 -- FIXME: Generate more interesting hvparams
74 -- | Generate Hvparams
75 genHvParams :: Gen Objects.HvParams
76 genHvParams = return $ JSON.GenericContainer Map.empty
77
78 -- | Generate hypervisor specifications to be used for the NodeInfo call
79 genHvSpecs :: Gen [(Types.Hypervisor, Objects.HvParams)]
80 genHvSpecs = do
81 numhv <- choose (0, 5)
82 hvs <- vectorOf numhv arbitrary
83 hvparams <- vectorOf numhv genHvParams
84 let specs = zip hvs hvparams
85 return specs
86
87 instance Arbitrary Rpc.RpcCallAllInstancesInfo where
88 arbitrary = Rpc.RpcCallAllInstancesInfo <$> genHvSpecs
89
90 instance Arbitrary Rpc.RpcCallInstanceList where
91 arbitrary = Rpc.RpcCallInstanceList <$> arbitrary
92
93 instance Arbitrary Rpc.RpcCallNodeInfo where
94 arbitrary = Rpc.RpcCallNodeInfo <$> genStorageUnits <*> genHvSpecs
95
96 -- | Generates per-instance console info params for the 'InstanceConsoleInfo'
97 -- call.
98 genConsoleInfoCallParams :: Gen [(String, Rpc.InstanceConsoleInfoParams)]
99 genConsoleInfoCallParams = do
100 numInstances <- choose (0, 3)
101 names <- vectorOf numInstances arbitrary
102 params <- vectorOf numInstances genInstanceConsoleInfoParams
103 return $ zip names params
104
105 -- | Generates parameters for the console info call, consisting of an instance
106 -- object, node object, 'HvParams', and 'FilledBeParams'.
107 genInstanceConsoleInfoParams :: Gen Rpc.InstanceConsoleInfoParams
108 genInstanceConsoleInfoParams = Rpc.InstanceConsoleInfoParams <$>
109 genInst <*> arbitrary <*> arbitrary <*> genHvParams <*> arbitrary
110
111 -- | Monadic check that, for an offline node and a call that does not support
112 -- offline nodes, we get a OfflineNodeError response.
113 runOfflineTest :: (Rpc.Rpc a b, Eq b, Show b) => a -> Property
114 runOfflineTest call =
115 forAll (arbitrary `suchThat` Objects.nodeOffline) $ \node -> monadicIO $ do
116 res <- run $ Rpc.executeRpcCall [node] call
117 _ <- stop $ res ==? [(node, Left Rpc.OfflineNodeError)]
118 return ()
119
120 prop_noffl_request_allinstinfo :: Rpc.RpcCallAllInstancesInfo -> Property
121 prop_noffl_request_allinstinfo = runOfflineTest
122
123 prop_noffl_request_instconsinfo :: Rpc.RpcCallInstanceConsoleInfo -> Property
124 prop_noffl_request_instconsinfo = runOfflineTest
125
126 prop_noffl_request_instlist :: Rpc.RpcCallInstanceList -> Property
127 prop_noffl_request_instlist = runOfflineTest
128
129 prop_noffl_request_nodeinfo :: Rpc.RpcCallNodeInfo -> Property
130 prop_noffl_request_nodeinfo = runOfflineTest
131
132 -- | Test that the serialisation of 'Compressed' is idempotent.
133 prop_Compressed_serialisation :: Rpc.Compressed -> Property
134 prop_Compressed_serialisation = testSerialisation
135
136 testSuite "Rpc"
137 [ 'prop_noffl_request_allinstinfo
138 , 'prop_noffl_request_instconsinfo
139 , 'prop_noffl_request_instlist
140 , 'prop_noffl_request_nodeinfo
141 , 'prop_Compressed_serialisation
142 ]