Statistics
| Branch: | Tag: | Revision:

root / htest / Test / Ganeti / OpCodes.hs @ 6d558717

History | View | Annotate | Download (12.2 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 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.OpCodes
30
  ( testOpCodes
31
  , OpCodes.OpCode(..)
32
  ) where
33

    
34
import qualified Test.HUnit as HUnit
35
import Test.QuickCheck
36

    
37
import Control.Applicative
38
import Data.List
39
import qualified Data.Map as Map
40
import qualified Text.JSON as J
41

    
42
import Test.Ganeti.TestHelper
43
import Test.Ganeti.TestCommon
44
import Test.Ganeti.Types ()
45
import Test.Ganeti.Query.Language
46

    
47
import qualified Ganeti.Constants as C
48
import qualified Ganeti.OpCodes as OpCodes
49
import Ganeti.Types
50
import Ganeti.OpParams
51
import Ganeti.JSON
52

    
53
{-# ANN module "HLint: ignore Use camelCase" #-}
54

    
55
-- * Arbitrary instances
56

    
57
instance Arbitrary OpCodes.TagObject where
58
  arbitrary = oneof [ OpCodes.TagInstance <$> getFQDN
59
                    , OpCodes.TagNode     <$> getFQDN
60
                    , OpCodes.TagGroup    <$> getFQDN
61
                    , pure OpCodes.TagCluster
62
                    ]
63

    
64
$(genArbitrary ''OpCodes.ReplaceDisksMode)
65

    
66
$(genArbitrary ''DiskAccess)
67

    
68
instance Arbitrary OpCodes.DiskIndex where
69
  arbitrary = choose (0, C.maxDisks - 1) >>= OpCodes.mkDiskIndex
70

    
71
instance Arbitrary INicParams where
72
  arbitrary = INicParams <$> getMaybe genNameNE <*> getMaybe getName <*>
73
              getMaybe genNameNE <*> getMaybe genNameNE
74

    
75
instance Arbitrary IDiskParams where
76
  arbitrary = IDiskParams <$> arbitrary <*> arbitrary <*>
77
              getMaybe genNameNE <*> getMaybe genNameNE <*>
78
              getMaybe genNameNE
79

    
80
instance Arbitrary OpCodes.OpCode where
81
  arbitrary = do
82
    op_id <- elements OpCodes.allOpIDs
83
    case op_id of
84
      "OP_TEST_DELAY" ->
85
        OpCodes.OpTestDelay <$> arbitrary <*> arbitrary
86
                 <*> genNodeNames
87
      "OP_INSTANCE_REPLACE_DISKS" ->
88
        OpCodes.OpInstanceReplaceDisks <$> getFQDN <*>
89
          getMaybe genNodeNameNE <*> arbitrary <*> genDiskIndices <*>
90
          getMaybe genNameNE
91
      "OP_INSTANCE_FAILOVER" ->
92
        OpCodes.OpInstanceFailover <$> getFQDN <*> arbitrary <*>
93
          getMaybe genNodeNameNE
94
      "OP_INSTANCE_MIGRATE" ->
95
        OpCodes.OpInstanceMigrate <$> getFQDN <*> arbitrary <*>
96
          arbitrary <*> arbitrary <*> getMaybe genNodeNameNE
97
      "OP_TAGS_SET" ->
98
        OpCodes.OpTagsSet <$> arbitrary <*> genTags
99
      "OP_TAGS_DEL" ->
100
        OpCodes.OpTagsSet <$> arbitrary <*> genTags
101
      "OP_CLUSTER_POST_INIT" -> pure OpCodes.OpClusterPostInit
102
      "OP_CLUSTER_DESTROY" -> pure OpCodes.OpClusterDestroy
103
      "OP_CLUSTER_QUERY" -> pure OpCodes.OpClusterQuery
104
      "OP_CLUSTER_VERIFY" ->
105
        OpCodes.OpClusterVerify <$> arbitrary <*> arbitrary <*>
106
          genSet Nothing <*> genSet Nothing <*> arbitrary <*>
107
          getMaybe genNameNE
108
      "OP_CLUSTER_VERIFY_CONFIG" ->
109
        OpCodes.OpClusterVerifyConfig <$> arbitrary <*> arbitrary <*>
110
          genSet Nothing <*> arbitrary
111
      "OP_CLUSTER_VERIFY_GROUP" ->
112
        OpCodes.OpClusterVerifyGroup <$> genNameNE <*> arbitrary <*>
113
          arbitrary <*> genSet Nothing <*> genSet Nothing <*> arbitrary
114
      "OP_CLUSTER_VERIFY_DISKS" -> pure OpCodes.OpClusterVerifyDisks
115
      "OP_GROUP_VERIFY_DISKS" ->
116
        OpCodes.OpGroupVerifyDisks <$> genNameNE
117
      "OP_CLUSTER_REPAIR_DISK_SIZES" ->
118
        OpCodes.OpClusterRepairDiskSizes <$>
119
          resize maxNodes (listOf (getFQDN >>= mkNonEmpty))
120
      "OP_CLUSTER_CONFIG_QUERY" ->
121
        OpCodes.OpClusterConfigQuery <$> genFieldsNE
122
      "OP_CLUSTER_RENAME" ->
123
        OpCodes.OpClusterRename <$> (getName >>= mkNonEmpty)
124
      "OP_CLUSTER_SET_PARAMS" ->
125
        OpCodes.OpClusterSetParams <$> emptyMUD <*> emptyMUD <*>
126
          arbitrary <*> getMaybe (listOf1 arbitrary >>= mkNonEmpty) <*>
127
          getMaybe genEmptyContainer <*> emptyMUD <*>
128
          getMaybe genEmptyContainer <*> getMaybe genEmptyContainer <*>
129
          getMaybe genEmptyContainer <*> getMaybe arbitrary <*>
130
          arbitrary <*> arbitrary <*> arbitrary <*>
131
          arbitrary <*> arbitrary <*> arbitrary <*>
132
          emptyMUD <*> emptyMUD <*> arbitrary <*>
133
          arbitrary <*> arbitrary <*> arbitrary <*>
134
          arbitrary <*> arbitrary <*> arbitrary
135
      "OP_CLUSTER_REDIST_CONF" -> pure OpCodes.OpClusterRedistConf
136
      "OP_CLUSTER_ACTIVATE_MASTER_IP" ->
137
        pure OpCodes.OpClusterActivateMasterIp
138
      "OP_CLUSTER_DEACTIVATE_MASTER_IP" ->
139
        pure OpCodes.OpClusterDeactivateMasterIp
140
      "OP_QUERY" ->
141
        OpCodes.OpQuery <$> arbitrary <*> arbitrary <*> arbitrary <*> genFilter
142
      "OP_QUERY_FIELDS" ->
143
        OpCodes.OpQueryFields <$> arbitrary <*> arbitrary
144
      "OP_OOB_COMMAND" ->
145
        OpCodes.OpOobCommand <$> genNodeNamesNE <*> arbitrary <*>
146
          arbitrary <*> arbitrary <*> (arbitrary `suchThat` (>0))
147
      "OP_NODE_REMOVE" -> OpCodes.OpNodeRemove <$> (getFQDN >>= mkNonEmpty)
148
      "OP_NODE_ADD" ->
149
        OpCodes.OpNodeAdd <$> genNodeNameNE <*> emptyMUD <*> emptyMUD <*>
150
          getMaybe getName <*> getMaybe genNameNE <*> arbitrary <*>
151
          getMaybe genNameNE <*> arbitrary <*> arbitrary <*> emptyMUD
152
      "OP_NODE_QUERY" ->
153
        OpCodes.OpNodeQuery <$> arbitrary <*> arbitrary <*> arbitrary
154
      "OP_NODE_QUERYVOLS" ->
155
        OpCodes.OpNodeQueryvols <$> arbitrary <*> genNodeNamesNE
156
      "OP_NODE_QUERY_STORAGE" ->
157
        OpCodes.OpNodeQueryStorage <$> arbitrary <*> arbitrary <*>
158
          genNodeNamesNE <*> genNameNE
159
      "OP_NODE_MODIFY_STORAGE" ->
160
        OpCodes.OpNodeModifyStorage <$> genNodeNameNE <*> arbitrary <*>
161
          genNameNE <*> pure emptyJSObject
162
      "OP_REPAIR_NODE_STORAGE" ->
163
        OpCodes.OpRepairNodeStorage <$> genNodeNameNE <*> arbitrary <*>
164
          genNameNE <*> arbitrary
165
      "OP_NODE_SET_PARAMS" ->
166
        OpCodes.OpNodeSetParams <$> genNodeNameNE <*> arbitrary <*>
167
          emptyMUD <*> emptyMUD <*> arbitrary <*> arbitrary <*> arbitrary <*>
168
          arbitrary <*> arbitrary <*> arbitrary <*> getMaybe genNameNE <*>
169
          emptyMUD
170
      "OP_NODE_POWERCYCLE" ->
171
        OpCodes.OpNodePowercycle <$> genNodeNameNE <*> arbitrary
172
      "OP_NODE_MIGRATE" ->
173
        OpCodes.OpNodeMigrate <$> genNodeNameNE <*> arbitrary <*>
174
          arbitrary <*> getMaybe genNodeNameNE <*> arbitrary <*>
175
          arbitrary <*> getMaybe genNameNE
176
      "OP_NODE_EVACUATE" ->
177
        OpCodes.OpNodeEvacuate <$> arbitrary <*> genNodeNameNE <*>
178
          getMaybe genNodeNameNE <*> getMaybe genNameNE <*> arbitrary
179
      "OP_INSTANCE_CREATE" ->
180
        OpCodes.OpInstanceCreate <$> getFQDN <*> arbitrary <*>
181
          arbitrary <*> arbitrary <*> arbitrary <*> pure emptyJSObject <*>
182
          arbitrary <*> arbitrary <*> arbitrary <*> getMaybe genNameNE <*>
183
          pure emptyJSObject <*> arbitrary <*> getMaybe genNameNE <*>
184
          arbitrary <*> arbitrary <*> arbitrary <*> arbitrary <*>
185
          arbitrary <*> arbitrary <*> pure emptyJSObject <*>
186
          getMaybe genNameNE <*>
187
          getMaybe genNodeNameNE <*> getMaybe genNodeNameNE <*>
188
          getMaybe (pure []) <*> getMaybe genNodeNameNE <*>
189
          arbitrary <*> getMaybe genNodeNameNE <*>
190
          getMaybe genNodeNameNE <*> getMaybe genNameNE <*>
191
          arbitrary <*> (genTags >>= mapM mkNonEmpty)
192
      _ -> fail $ "Undefined arbitrary for opcode " ++ op_id
193

    
194
-- * Helper functions
195

    
196
-- | Empty JSObject.
197
emptyJSObject :: J.JSObject J.JSValue
198
emptyJSObject = J.toJSObject []
199

    
200
-- | Empty maybe unchecked dictionary.
201
emptyMUD :: Gen (Maybe (J.JSObject J.JSValue))
202
emptyMUD = getMaybe $ pure emptyJSObject
203

    
204
-- | Generates an empty container.
205
genEmptyContainer :: (Ord a) => Gen (GenericContainer a b)
206
genEmptyContainer = pure . GenericContainer $ Map.fromList []
207

    
208
-- | Generates list of disk indices.
209
genDiskIndices :: Gen [DiskIndex]
210
genDiskIndices = do
211
  cnt <- choose (0, C.maxDisks)
212
  genUniquesList cnt
213

    
214
-- | Generates a list of node names.
215
genNodeNames :: Gen [String]
216
genNodeNames = resize maxNodes (listOf getFQDN)
217

    
218
-- | Generates a list of node names in non-empty string type.
219
genNodeNamesNE :: Gen [NonEmptyString]
220
genNodeNamesNE = genNodeNames >>= mapM mkNonEmpty
221

    
222
-- | Gets a node name in non-empty type.
223
genNodeNameNE :: Gen NonEmptyString
224
genNodeNameNE = getFQDN >>= mkNonEmpty
225

    
226
-- | Gets a name (non-fqdn) in non-empty type.
227
genNameNE :: Gen NonEmptyString
228
genNameNE = getName >>= mkNonEmpty
229

    
230
-- | Returns a list of non-empty fields.
231
genFieldsNE :: Gen [NonEmptyString]
232
genFieldsNE = getFields >>= mapM mkNonEmpty
233

    
234
-- * Test cases
235

    
236
-- | Check that opcode serialization is idempotent.
237
prop_serialization :: OpCodes.OpCode -> Property
238
prop_serialization = testSerialisation
239

    
240
-- | Check that Python and Haskell defined the same opcode list.
241
case_AllDefined :: HUnit.Assertion
242
case_AllDefined = do
243
  py_stdout <- runPython "from ganeti import opcodes\n\
244
                         \print '\\n'.join(opcodes.OP_MAPPING.keys())" "" >>=
245
               checkPythonResult
246
  let py_ops = sort $ lines py_stdout
247
      hs_ops = OpCodes.allOpIDs
248
      -- extra_py = py_ops \\ hs_ops
249
      extra_hs = hs_ops \\ py_ops
250
  -- FIXME: uncomment when we have parity
251
  -- HUnit.assertBool ("OpCodes missing from Haskell code:\n" ++
252
  --                  unlines extra_py) (null extra_py)
253
  HUnit.assertBool ("Extra OpCodes in the Haskell code code:\n" ++
254
                    unlines extra_hs) (null extra_hs)
255

    
256
-- | Custom HUnit test case that forks a Python process and checks
257
-- correspondence between Haskell-generated OpCodes and their Python
258
-- decoded, validated and re-encoded version.
259
--
260
-- Note that we have a strange beast here: since launching Python is
261
-- expensive, we don't do this via a usual QuickProperty, since that's
262
-- slow (I've tested it, and it's indeed quite slow). Rather, we use a
263
-- single HUnit assertion, and in it we manually use QuickCheck to
264
-- generate 500 opcodes times the number of defined opcodes, which
265
-- then we pass in bulk to Python. The drawbacks to this method are
266
-- two fold: we cannot control the number of generated opcodes, since
267
-- HUnit assertions don't get access to the test options, and for the
268
-- same reason we can't run a repeatable seed. We should probably find
269
-- a better way to do this, for example by having a
270
-- separately-launched Python process (if not running the tests would
271
-- be skipped).
272
case_py_compat :: HUnit.Assertion
273
case_py_compat = do
274
  let num_opcodes = length OpCodes.allOpIDs * 500
275
  sample_opcodes <- sample' (vectorOf num_opcodes
276
                             (arbitrary::Gen OpCodes.OpCode))
277
  let opcodes = head sample_opcodes
278
      serialized = J.encode opcodes
279
  py_stdout <-
280
     runPython "from ganeti import opcodes\n\
281
               \import sys\n\
282
               \from ganeti import serializer\n\
283
               \op_data = serializer.Load(sys.stdin.read())\n\
284
               \decoded = [opcodes.OpCode.LoadOpCode(o) for o in op_data]\n\
285
               \for op in decoded:\n\
286
               \  op.Validate(True)\n\
287
               \encoded = [op.__getstate__() for op in decoded]\n\
288
               \print serializer.Dump(encoded)" serialized
289
     >>= checkPythonResult
290
  let deserialised = J.decode py_stdout::J.Result [OpCodes.OpCode]
291
  decoded <- case deserialised of
292
               J.Ok ops -> return ops
293
               J.Error msg ->
294
                 HUnit.assertFailure ("Unable to decode opcodes: " ++ msg)
295
                 -- this already raised an expection, but we need it
296
                 -- for proper types
297
                 >> fail "Unable to decode opcodes"
298
  HUnit.assertEqual "Mismatch in number of returned opcodes"
299
    (length opcodes) (length decoded)
300
  mapM_ (uncurry (HUnit.assertEqual "Different result after encoding/decoding")
301
        ) $ zip opcodes decoded
302

    
303
testSuite "OpCodes"
304
            [ 'prop_serialization
305
            , 'case_AllDefined
306
            , 'case_py_compat
307
            ]