OpCodes.hs 19.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
{-# LANGUAGE TemplateHaskell #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}

{-| Unittests for ganeti-htools.

-}

{-

Copyright (C) 2009, 2010, 2011, 2012 Google Inc.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.

-}

module Test.Ganeti.OpCodes
  ( testOpCodes
  , OpCodes.OpCode(..)
  ) where

import qualified Test.HUnit as HUnit
import Test.QuickCheck

import Control.Applicative
38
39
import Control.Monad
import Data.Char
40
import Data.List
41
import qualified Data.Map as Map
42
import qualified Text.JSON as J
43
import Text.Printf (printf)
44
45
46

import Test.Ganeti.TestHelper
import Test.Ganeti.TestCommon
47
import Test.Ganeti.Types ()
48
import Test.Ganeti.Query.Language
49
50
51

import qualified Ganeti.Constants as C
import qualified Ganeti.OpCodes as OpCodes
52
53
import Ganeti.Types
import Ganeti.OpParams
54
import Ganeti.JSON
55

Iustin Pop's avatar
Iustin Pop committed
56
57
{-# ANN module "HLint: ignore Use camelCase" #-}

58
59
-- * Arbitrary instances

60
instance Arbitrary OpCodes.TagObject where
61
62
63
  arbitrary = oneof [ OpCodes.TagInstance <$> genFQDN
                    , OpCodes.TagNode     <$> genFQDN
                    , OpCodes.TagGroup    <$> genFQDN
64
65
                    , pure OpCodes.TagCluster
                    ]
66

67
$(genArbitrary ''OpCodes.ReplaceDisksMode)
68

69
70
$(genArbitrary ''DiskAccess)

71
72
73
instance Arbitrary OpCodes.DiskIndex where
  arbitrary = choose (0, C.maxDisks - 1) >>= OpCodes.mkDiskIndex

74
instance Arbitrary INicParams where
75
76
  arbitrary = INicParams <$> genMaybe genNameNE <*> genMaybe genName <*>
              genMaybe genNameNE <*> genMaybe genNameNE
77

78
79
instance Arbitrary IDiskParams where
  arbitrary = IDiskParams <$> arbitrary <*> arbitrary <*>
80
81
              genMaybe genNameNE <*> genMaybe genNameNE <*>
              genMaybe genNameNE
82

83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
instance Arbitrary RecreateDisksInfo where
  arbitrary = oneof [ pure RecreateDisksAll
                    , RecreateDisksIndices <$> arbitrary
                    , RecreateDisksParams <$> arbitrary
                    ]

instance Arbitrary DdmOldChanges where
  arbitrary = oneof [ DdmOldIndex <$> arbitrary
                    , DdmOldMod   <$> arbitrary
                    ]

instance (Arbitrary a) => Arbitrary (SetParamsMods a) where
  arbitrary = oneof [ pure SetParamsEmpty
                    , SetParamsDeprecated <$> arbitrary
                    , SetParamsNew        <$> arbitrary
                    ]

Iustin Pop's avatar
Iustin Pop committed
100
101
102
103
104
instance Arbitrary ExportTarget where
  arbitrary = oneof [ ExportTargetLocal <$> genNodeNameNE
                    , ExportTargetRemote <$> pure []
                    ]

105
106
107
108
109
instance Arbitrary OpCodes.OpCode where
  arbitrary = do
    op_id <- elements OpCodes.allOpIDs
    case op_id of
      "OP_TEST_DELAY" ->
110
111
        OpCodes.OpTestDelay <$> arbitrary <*> arbitrary <*>
          genNodeNamesNE <*> arbitrary
112
      "OP_INSTANCE_REPLACE_DISKS" ->
113
114
115
        OpCodes.OpInstanceReplaceDisks <$> genFQDN <*> arbitrary <*>
          arbitrary <*> arbitrary <*> genDiskIndices <*>
          genMaybe genNodeNameNE <*> genMaybe genNameNE
116
      "OP_INSTANCE_FAILOVER" ->
117
118
        OpCodes.OpInstanceFailover <$> genFQDN <*> arbitrary <*> arbitrary <*>
          genMaybe genNodeNameNE <*> arbitrary <*> genMaybe genNameNE
119
      "OP_INSTANCE_MIGRATE" ->
120
121
122
        OpCodes.OpInstanceMigrate <$> genFQDN <*> arbitrary <*> arbitrary <*>
          genMaybe genNodeNameNE <*> arbitrary <*>
          arbitrary <*> arbitrary <*> genMaybe genNameNE <*> arbitrary
123
124
125
126
      "OP_TAGS_GET" ->
        OpCodes.OpTagsGet <$> arbitrary <*> arbitrary
      "OP_TAGS_SEARCH" ->
        OpCodes.OpTagsSearch <$> genNameNE
127
      "OP_TAGS_SET" ->
128
        OpCodes.OpTagsSet <$> arbitrary <*> genTags
129
      "OP_TAGS_DEL" ->
130
        OpCodes.OpTagsSet <$> arbitrary <*> genTags
131
132
133
134
135
      "OP_CLUSTER_POST_INIT" -> pure OpCodes.OpClusterPostInit
      "OP_CLUSTER_DESTROY" -> pure OpCodes.OpClusterDestroy
      "OP_CLUSTER_QUERY" -> pure OpCodes.OpClusterQuery
      "OP_CLUSTER_VERIFY" ->
        OpCodes.OpClusterVerify <$> arbitrary <*> arbitrary <*>
136
          genSet Nothing <*> genSet Nothing <*> arbitrary <*>
137
          genMaybe genNameNE
138
139
140
141
      "OP_CLUSTER_VERIFY_CONFIG" ->
        OpCodes.OpClusterVerifyConfig <$> arbitrary <*> arbitrary <*>
          genSet Nothing <*> arbitrary
      "OP_CLUSTER_VERIFY_GROUP" ->
142
        OpCodes.OpClusterVerifyGroup <$> genNameNE <*> arbitrary <*>
143
144
145
          arbitrary <*> genSet Nothing <*> genSet Nothing <*> arbitrary
      "OP_CLUSTER_VERIFY_DISKS" -> pure OpCodes.OpClusterVerifyDisks
      "OP_GROUP_VERIFY_DISKS" ->
146
        OpCodes.OpGroupVerifyDisks <$> genNameNE
147
      "OP_CLUSTER_REPAIR_DISK_SIZES" ->
148
        OpCodes.OpClusterRepairDiskSizes <$> genNodeNamesNE
149
      "OP_CLUSTER_CONFIG_QUERY" ->
150
        OpCodes.OpClusterConfigQuery <$> genFieldsNE
151
      "OP_CLUSTER_RENAME" ->
152
        OpCodes.OpClusterRename <$> genNameNE
153
154
      "OP_CLUSTER_SET_PARAMS" ->
        OpCodes.OpClusterSetParams <$> emptyMUD <*> emptyMUD <*>
155
156
157
158
          arbitrary <*> genMaybe (listOf1 arbitrary >>= mkNonEmpty) <*>
          genMaybe genEmptyContainer <*> emptyMUD <*>
          genMaybe genEmptyContainer <*> genMaybe genEmptyContainer <*>
          genMaybe genEmptyContainer <*> genMaybe arbitrary <*>
159
160
161
          arbitrary <*> arbitrary <*> arbitrary <*>
          arbitrary <*> arbitrary <*> arbitrary <*>
          emptyMUD <*> emptyMUD <*> arbitrary <*>
162
          arbitrary <*> arbitrary <*> arbitrary <*> arbitrary <*>
163
164
165
166
167
168
169
170
171
172
173
174
175
          arbitrary <*> arbitrary <*> arbitrary
      "OP_CLUSTER_REDIST_CONF" -> pure OpCodes.OpClusterRedistConf
      "OP_CLUSTER_ACTIVATE_MASTER_IP" ->
        pure OpCodes.OpClusterActivateMasterIp
      "OP_CLUSTER_DEACTIVATE_MASTER_IP" ->
        pure OpCodes.OpClusterDeactivateMasterIp
      "OP_QUERY" ->
        OpCodes.OpQuery <$> arbitrary <*> arbitrary <*> arbitrary <*> genFilter
      "OP_QUERY_FIELDS" ->
        OpCodes.OpQueryFields <$> arbitrary <*> arbitrary
      "OP_OOB_COMMAND" ->
        OpCodes.OpOobCommand <$> genNodeNamesNE <*> arbitrary <*>
          arbitrary <*> arbitrary <*> (arbitrary `suchThat` (>0))
176
      "OP_NODE_REMOVE" -> OpCodes.OpNodeRemove <$> genNodeNameNE
177
      "OP_NODE_ADD" ->
178
        OpCodes.OpNodeAdd <$> genNodeNameNE <*> emptyMUD <*> emptyMUD <*>
179
180
          genMaybe genName <*> genMaybe genNameNE <*> arbitrary <*>
          genMaybe genNameNE <*> arbitrary <*> arbitrary <*> emptyMUD
181
      "OP_NODE_QUERY" ->
182
        OpCodes.OpNodeQuery <$> genFieldsNE <*> genNamesNE <*> arbitrary
183
184
185
186
      "OP_NODE_QUERYVOLS" ->
        OpCodes.OpNodeQueryvols <$> arbitrary <*> genNodeNamesNE
      "OP_NODE_QUERY_STORAGE" ->
        OpCodes.OpNodeQueryStorage <$> arbitrary <*> arbitrary <*>
187
          genNodeNamesNE <*> genNameNE
188
189
      "OP_NODE_MODIFY_STORAGE" ->
        OpCodes.OpNodeModifyStorage <$> genNodeNameNE <*> arbitrary <*>
190
          genNameNE <*> pure emptyJSObject
191
192
      "OP_REPAIR_NODE_STORAGE" ->
        OpCodes.OpRepairNodeStorage <$> genNodeNameNE <*> arbitrary <*>
193
          genNameNE <*> arbitrary
194
195
196
      "OP_NODE_SET_PARAMS" ->
        OpCodes.OpNodeSetParams <$> genNodeNameNE <*> arbitrary <*>
          emptyMUD <*> emptyMUD <*> arbitrary <*> arbitrary <*> arbitrary <*>
197
          arbitrary <*> arbitrary <*> arbitrary <*> genMaybe genNameNE <*>
198
          emptyMUD <*> arbitrary
199
200
201
202
      "OP_NODE_POWERCYCLE" ->
        OpCodes.OpNodePowercycle <$> genNodeNameNE <*> arbitrary
      "OP_NODE_MIGRATE" ->
        OpCodes.OpNodeMigrate <$> genNodeNameNE <*> arbitrary <*>
203
204
          arbitrary <*> genMaybe genNodeNameNE <*> arbitrary <*>
          arbitrary <*> genMaybe genNameNE
205
206
      "OP_NODE_EVACUATE" ->
        OpCodes.OpNodeEvacuate <$> arbitrary <*> genNodeNameNE <*>
207
          genMaybe genNodeNameNE <*> genMaybe genNameNE <*> arbitrary
208
      "OP_INSTANCE_CREATE" ->
209
        OpCodes.OpInstanceCreate <$> genFQDN <*> arbitrary <*>
210
          arbitrary <*> arbitrary <*> arbitrary <*> pure emptyJSObject <*>
211
212
          arbitrary <*> arbitrary <*> arbitrary <*> genMaybe genNameNE <*>
          pure emptyJSObject <*> arbitrary <*> genMaybe genNameNE <*>
213
214
          arbitrary <*> arbitrary <*> arbitrary <*> arbitrary <*>
          arbitrary <*> arbitrary <*> pure emptyJSObject <*>
215
216
217
218
219
          genMaybe genNameNE <*>
          genMaybe genNodeNameNE <*> genMaybe genNodeNameNE <*>
          genMaybe (pure []) <*> genMaybe genNodeNameNE <*>
          arbitrary <*> genMaybe genNodeNameNE <*>
          genMaybe genNodeNameNE <*> genMaybe genNameNE <*>
220
          arbitrary <*> (genTags >>= mapM mkNonEmpty)
221
      "OP_INSTANCE_MULTI_ALLOC" ->
222
        OpCodes.OpInstanceMultiAlloc <$> genMaybe genNameNE <*> pure []
223
      "OP_INSTANCE_REINSTALL" ->
224
225
        OpCodes.OpInstanceReinstall <$> genFQDN <*> arbitrary <*>
          genMaybe genNameNE <*> genMaybe (pure emptyJSObject)
226
      "OP_INSTANCE_REMOVE" ->
227
        OpCodes.OpInstanceRemove <$> genFQDN <*> arbitrary <*> arbitrary
228
      "OP_INSTANCE_RENAME" ->
229
        OpCodes.OpInstanceRename <$> genFQDN <*> genNodeNameNE <*>
230
231
          arbitrary <*> arbitrary
      "OP_INSTANCE_STARTUP" ->
232
        OpCodes.OpInstanceStartup <$> genFQDN <*> arbitrary <*> arbitrary <*>
233
234
235
          pure emptyJSObject <*> pure emptyJSObject <*>
          arbitrary <*> arbitrary
      "OP_INSTANCE_SHUTDOWN" ->
236
        OpCodes.OpInstanceShutdown <$> genFQDN <*> arbitrary <*>
237
238
          arbitrary <*> arbitrary
      "OP_INSTANCE_REBOOT" ->
239
        OpCodes.OpInstanceReboot <$> genFQDN <*> arbitrary <*>
240
241
          arbitrary <*> arbitrary
      "OP_INSTANCE_MOVE" ->
242
        OpCodes.OpInstanceMove <$> genFQDN <*> arbitrary <*> arbitrary <*>
243
          genNodeNameNE <*> arbitrary
244
      "OP_INSTANCE_CONSOLE" -> OpCodes.OpInstanceConsole <$> genFQDN
245
      "OP_INSTANCE_ACTIVATE_DISKS" ->
246
        OpCodes.OpInstanceActivateDisks <$> genFQDN <*>
247
248
          arbitrary <*> arbitrary
      "OP_INSTANCE_DEACTIVATE_DISKS" ->
249
        OpCodes.OpInstanceDeactivateDisks <$> genFQDN <*> arbitrary
250
      "OP_INSTANCE_RECREATE_DISKS" ->
251
252
        OpCodes.OpInstanceRecreateDisks <$> genFQDN <*> arbitrary <*>
          genNodeNamesNE <*> genMaybe genNameNE
Iustin Pop's avatar
Iustin Pop committed
253
254
      "OP_INSTANCE_QUERY" ->
        OpCodes.OpInstanceQuery <$> genFieldsNE <*> genNamesNE <*> arbitrary
255
256
257
258
      "OP_INSTANCE_QUERY_DATA" ->
        OpCodes.OpInstanceQueryData <$> arbitrary <*>
          genNodeNamesNE <*> arbitrary
      "OP_INSTANCE_SET_PARAMS" ->
259
        OpCodes.OpInstanceSetParams <$> genFQDN <*> arbitrary <*>
260
261
          arbitrary <*> arbitrary <*> arbitrary <*> arbitrary <*>
          pure emptyJSObject <*> arbitrary <*> pure emptyJSObject <*>
262
          arbitrary <*> genMaybe genNodeNameNE <*> genMaybe genNameNE <*>
263
264
          pure emptyJSObject <*> arbitrary <*> arbitrary <*> arbitrary
      "OP_INSTANCE_GROW_DISK" ->
265
        OpCodes.OpInstanceGrowDisk <$> genFQDN <*> arbitrary <*>
266
267
          arbitrary <*> arbitrary <*> arbitrary
      "OP_INSTANCE_CHANGE_GROUP" ->
268
269
        OpCodes.OpInstanceChangeGroup <$> genFQDN <*> arbitrary <*>
          genMaybe genNameNE <*> genMaybe (resize maxNodes (listOf genNameNE))
Iustin Pop's avatar
Iustin Pop committed
270
271
      "OP_GROUP_ADD" ->
        OpCodes.OpGroupAdd <$> genNameNE <*> arbitrary <*>
272
          emptyMUD <*> genMaybe genEmptyContainer <*>
Iustin Pop's avatar
Iustin Pop committed
273
274
275
276
277
278
279
280
          emptyMUD <*> emptyMUD <*> emptyMUD
      "OP_GROUP_ASSIGN_NODES" ->
        OpCodes.OpGroupAssignNodes <$> genNameNE <*> arbitrary <*>
          genNodeNamesNE
      "OP_GROUP_QUERY" ->
        OpCodes.OpGroupQuery <$> genFieldsNE <*> genNamesNE
      "OP_GROUP_SET_PARAMS" ->
        OpCodes.OpGroupSetParams <$> genNameNE <*> arbitrary <*>
281
          emptyMUD <*> genMaybe genEmptyContainer <*>
Iustin Pop's avatar
Iustin Pop committed
282
283
284
285
286
287
288
          emptyMUD <*> emptyMUD <*> emptyMUD
      "OP_GROUP_REMOVE" ->
        OpCodes.OpGroupRemove <$> genNameNE
      "OP_GROUP_RENAME" ->
        OpCodes.OpGroupRename <$> genNameNE <*> genNameNE
      "OP_GROUP_EVACUATE" ->
        OpCodes.OpGroupEvacuate <$> genNameNE <*> arbitrary <*>
289
          genMaybe genNameNE <*> genMaybe genNamesNE
Iustin Pop's avatar
Iustin Pop committed
290
291
292
293
294
      "OP_OS_DIAGNOSE" ->
        OpCodes.OpOsDiagnose <$> genFieldsNE <*> genNamesNE
      "OP_BACKUP_QUERY" ->
        OpCodes.OpBackupQuery <$> arbitrary <*> genNodeNamesNE
      "OP_BACKUP_PREPARE" ->
295
        OpCodes.OpBackupPrepare <$> genFQDN <*> arbitrary
Iustin Pop's avatar
Iustin Pop committed
296
      "OP_BACKUP_EXPORT" ->
297
        OpCodes.OpBackupExport <$> genFQDN <*> arbitrary <*>
Iustin Pop's avatar
Iustin Pop committed
298
          arbitrary <*> arbitrary <*> arbitrary <*> arbitrary <*>
299
          arbitrary <*> genMaybe (pure []) <*> genMaybe genNameNE
Iustin Pop's avatar
Iustin Pop committed
300
      "OP_BACKUP_REMOVE" ->
301
        OpCodes.OpBackupRemove <$> genFQDN
Iustin Pop's avatar
Iustin Pop committed
302
303
304
      "OP_TEST_ALLOCATOR" ->
        OpCodes.OpTestAllocator <$> arbitrary <*> arbitrary <*>
          genNameNE <*> pure [] <*> pure [] <*>
305
          arbitrary <*> genMaybe genNameNE <*>
Iustin Pop's avatar
Iustin Pop committed
306
          (genTags >>= mapM mkNonEmpty) <*>
307
308
309
          arbitrary <*> arbitrary <*> genMaybe genNameNE <*>
          arbitrary <*> genMaybe genNodeNamesNE <*> arbitrary <*>
          genMaybe genNamesNE <*> arbitrary <*> arbitrary
Iustin Pop's avatar
Iustin Pop committed
310
311
      "OP_TEST_JQUEUE" ->
        OpCodes.OpTestJqueue <$> arbitrary <*> arbitrary <*>
312
          resize 20 (listOf genFQDN) <*> arbitrary
Iustin Pop's avatar
Iustin Pop committed
313
314
315
      "OP_TEST_DUMMY" ->
        OpCodes.OpTestDummy <$> pure J.JSNull <*> pure J.JSNull <*>
          pure J.JSNull <*> pure J.JSNull
316
317
      "OP_NETWORK_ADD" ->
        OpCodes.OpNetworkAdd <$> genNameNE <*> arbitrary <*> genIp4Net <*>
318
319
          genMaybe genIp4Addr <*> pure Nothing <*> pure Nothing <*>
          genMaybe genMacPrefix <*> genMaybe (listOf genIp4Addr) <*>
Iustin Pop's avatar
Iustin Pop committed
320
          arbitrary <*> (genTags >>= mapM mkNonEmpty)
321
322
323
324
      "OP_NETWORK_REMOVE" ->
        OpCodes.OpNetworkRemove <$> genNameNE <*> arbitrary
      "OP_NETWORK_SET_PARAMS" ->
        OpCodes.OpNetworkSetParams <$> genNameNE <*> arbitrary <*>
325
326
327
          genMaybe genIp4Addr <*> pure Nothing <*> pure Nothing <*>
          genMaybe genMacPrefix <*> genMaybe (listOf genIp4Addr) <*>
          genMaybe (listOf genIp4Addr)
328
329
330
331
332
333
334
      "OP_NETWORK_CONNECT" ->
        OpCodes.OpNetworkConnect <$> genNameNE <*> genNameNE <*>
          arbitrary <*> genNameNE <*> arbitrary
      "OP_NETWORK_DISCONNECT" ->
        OpCodes.OpNetworkDisconnect <$> genNameNE <*> genNameNE <*> arbitrary
      "OP_NETWORK_QUERY" ->
        OpCodes.OpNetworkQuery <$> genFieldsNE <*> genNamesNE
Iustin Pop's avatar
Iustin Pop committed
335
336
337
      "OP_RESTRICTED_COMMAND" ->
        OpCodes.OpRestrictedCommand <$> arbitrary <*> genNodeNamesNE <*>
          genNameNE
338
      _ -> fail $ "Undefined arbitrary for opcode " ++ op_id
339

340
341
-- * Helper functions

342
343
344
345
346
347
-- | Empty JSObject.
emptyJSObject :: J.JSObject J.JSValue
emptyJSObject = J.toJSObject []

-- | Empty maybe unchecked dictionary.
emptyMUD :: Gen (Maybe (J.JSObject J.JSValue))
348
emptyMUD = genMaybe $ pure emptyJSObject
349
350
351
352
353

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

354
355
356
357
358
359
360
361
-- | Generates list of disk indices.
genDiskIndices :: Gen [DiskIndex]
genDiskIndices = do
  cnt <- choose (0, C.maxDisks)
  genUniquesList cnt

-- | Generates a list of node names.
genNodeNames :: Gen [String]
362
genNodeNames = resize maxNodes (listOf genFQDN)
363

364
365
-- | Generates a list of node names in non-empty string type.
genNodeNamesNE :: Gen [NonEmptyString]
Iustin Pop's avatar
Iustin Pop committed
366
genNodeNamesNE = genNodeNames >>= mapM mkNonEmpty
367

368
369
-- | Gets a node name in non-empty type.
genNodeNameNE :: Gen NonEmptyString
370
genNodeNameNE = genFQDN >>= mkNonEmpty
371

372
373
-- | Gets a name (non-fqdn) in non-empty type.
genNameNE :: Gen NonEmptyString
374
genNameNE = genName >>= mkNonEmpty
375

Iustin Pop's avatar
Iustin Pop committed
376
377
378
379
-- | Gets a list of names (non-fqdn) in non-empty type.
genNamesNE :: Gen [NonEmptyString]
genNamesNE = resize maxNodes (listOf genNameNE)

380
381
-- | Returns a list of non-empty fields.
genFieldsNE :: Gen [NonEmptyString]
382
genFieldsNE = genFields >>= mapM mkNonEmpty
383

384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
-- | Generate an arbitrary IPv4 address in textual form.
genIp4Addr :: Gen NonEmptyString
genIp4Addr = do
  a <- choose (1::Int, 255)
  b <- choose (0::Int, 255)
  c <- choose (0::Int, 255)
  d <- choose (0::Int, 255)
  mkNonEmpty $ intercalate "." (map show [a, b, c, d])

-- | Generate an arbitrary IPv4 network address in textual form.
genIp4Net :: Gen NonEmptyString
genIp4Net = do
  netmask <- choose (8::Int, 30)
  ip <- genIp4Addr
  mkNonEmpty $ fromNonEmpty ip ++ "/" ++ show netmask

-- | Generate a 3-byte MAC prefix.
genMacPrefix :: Gen NonEmptyString
genMacPrefix = do
  octets <- vectorOf 3 $ choose (0::Int, 255)
  mkNonEmpty . intercalate ":" $ map (printf "%02x") octets

406
407
408
-- * Test cases

-- | Check that opcode serialization is idempotent.
409
prop_serialization :: OpCodes.OpCode -> Property
410
prop_serialization = testSerialisation
411
412

-- | Check that Python and Haskell defined the same opcode list.
413
414
case_AllDefined :: HUnit.Assertion
case_AllDefined = do
415
416
417
  let py_ops = sort C.opcodesOpIds
      hs_ops = sort OpCodes.allOpIDs
      extra_py = py_ops \\ hs_ops
418
      extra_hs = hs_ops \\ py_ops
419
420
  HUnit.assertBool ("Missing OpCodes from the Haskell code:\n" ++
                    unlines extra_py) (null extra_py)
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
  HUnit.assertBool ("Extra OpCodes in the Haskell code code:\n" ++
                    unlines extra_hs) (null extra_hs)

-- | Custom HUnit test case that forks a Python process and checks
-- correspondence between Haskell-generated OpCodes and their Python
-- decoded, validated and re-encoded version.
--
-- Note that we have a strange beast here: since launching Python is
-- expensive, we don't do this via a usual QuickProperty, since that's
-- slow (I've tested it, and it's indeed quite slow). Rather, we use a
-- single HUnit assertion, and in it we manually use QuickCheck to
-- generate 500 opcodes times the number of defined opcodes, which
-- then we pass in bulk to Python. The drawbacks to this method are
-- two fold: we cannot control the number of generated opcodes, since
-- HUnit assertions don't get access to the test options, and for the
-- same reason we can't run a repeatable seed. We should probably find
-- a better way to do this, for example by having a
-- separately-launched Python process (if not running the tests would
-- be skipped).
440
441
case_py_compat :: HUnit.Assertion
case_py_compat = do
442
  let num_opcodes = length OpCodes.allOpIDs * 100
443
444
445
446
  sample_opcodes <- sample' (vectorOf num_opcodes
                             (arbitrary::Gen OpCodes.OpCode))
  let opcodes = head sample_opcodes
      serialized = J.encode opcodes
447
448
449
450
451
  -- check for non-ASCII fields, usually due to 'arbitrary :: String'
  mapM_ (\op -> when (any (not . isAscii) (J.encode op)) .
                HUnit.assertFailure $
                  "OpCode has non-ASCII fields: " ++ show op
        ) opcodes
452
453
454
455
456
457
458
459
460
461
462
  py_stdout <-
     runPython "from ganeti import opcodes\n\
               \import sys\n\
               \from ganeti import serializer\n\
               \op_data = serializer.Load(sys.stdin.read())\n\
               \decoded = [opcodes.OpCode.LoadOpCode(o) for o in op_data]\n\
               \for op in decoded:\n\
               \  op.Validate(True)\n\
               \encoded = [op.__getstate__() for op in decoded]\n\
               \print serializer.Dump(encoded)" serialized
     >>= checkPythonResult
Iustin Pop's avatar
Iustin Pop committed
463
  let deserialised = J.decode py_stdout::J.Result [OpCodes.OpCode]
464
465
466
467
468
469
470
471
472
473
474
475
476
  decoded <- case deserialised of
               J.Ok ops -> return ops
               J.Error msg ->
                 HUnit.assertFailure ("Unable to decode opcodes: " ++ msg)
                 -- this already raised an expection, but we need it
                 -- for proper types
                 >> fail "Unable to decode opcodes"
  HUnit.assertEqual "Mismatch in number of returned opcodes"
    (length opcodes) (length decoded)
  mapM_ (uncurry (HUnit.assertEqual "Different result after encoding/decoding")
        ) $ zip opcodes decoded

testSuite "OpCodes"
477
478
479
            [ 'prop_serialization
            , 'case_AllDefined
            , 'case_py_compat
480
            ]