OpCodes.hs 21.9 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
342
343
instance Arbitrary OpCodes.CommonOpParams where
  arbitrary = OpCodes.CommonOpParams <$> arbitrary <*> arbitrary <*>
                arbitrary <*> resize 5 arbitrary <*> genMaybe genName

344
345
-- * Helper functions

346
347
348
349
350
351
-- | Empty JSObject.
emptyJSObject :: J.JSObject J.JSValue
emptyJSObject = J.toJSObject []

-- | Empty maybe unchecked dictionary.
emptyMUD :: Gen (Maybe (J.JSObject J.JSValue))
352
emptyMUD = genMaybe $ pure emptyJSObject
353
354
355
356
357

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

358
359
360
361
362
363
364
365
-- | 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]
366
genNodeNames = resize maxNodes (listOf genFQDN)
367

368
369
-- | Generates a list of node names in non-empty string type.
genNodeNamesNE :: Gen [NonEmptyString]
Iustin Pop's avatar
Iustin Pop committed
370
genNodeNamesNE = genNodeNames >>= mapM mkNonEmpty
371

372
373
-- | Gets a node name in non-empty type.
genNodeNameNE :: Gen NonEmptyString
374
genNodeNameNE = genFQDN >>= mkNonEmpty
375

376
377
-- | Gets a name (non-fqdn) in non-empty type.
genNameNE :: Gen NonEmptyString
378
genNameNE = genName >>= mkNonEmpty
379

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

384
385
-- | Returns a list of non-empty fields.
genFieldsNE :: Gen [NonEmptyString]
386
genFieldsNE = genFields >>= mapM mkNonEmpty
387

388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
-- | 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

410
411
412
-- | Arbitrary instance for MetaOpCode, defined here due to TH ordering.
$(genArbitrary ''OpCodes.MetaOpCode)

413
414
415
-- * Test cases

-- | Check that opcode serialization is idempotent.
416
prop_serialization :: OpCodes.OpCode -> Property
417
prop_serialization = testSerialisation
418
419

-- | Check that Python and Haskell defined the same opcode list.
420
421
case_AllDefined :: HUnit.Assertion
case_AllDefined = do
422
423
424
  let py_ops = sort C.opcodesOpIds
      hs_ops = sort OpCodes.allOpIDs
      extra_py = py_ops \\ hs_ops
425
      extra_hs = hs_ops \\ py_ops
426
427
  HUnit.assertBool ("Missing OpCodes from the Haskell code:\n" ++
                    unlines extra_py) (null extra_py)
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
  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).
447
448
case_py_compat_types :: HUnit.Assertion
case_py_compat_types = do
449
  let num_opcodes = length OpCodes.allOpIDs * 100
450
  sample_opcodes <- sample' (vectorOf num_opcodes
451
                             (arbitrary::Gen OpCodes.MetaOpCode))
452
453
  let opcodes = head sample_opcodes
      serialized = J.encode opcodes
454
455
456
457
458
  -- 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
459
460
461
462
463
464
465
466
467
468
469
  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
470
  let deserialised = J.decode py_stdout::J.Result [OpCodes.MetaOpCode]
471
472
473
474
475
476
477
478
479
480
481
482
  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

483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
-- | Custom HUnit test case that forks a Python process and checks
-- correspondence between Haskell OpCodes fields and their Python
-- equivalent.
case_py_compat_fields :: HUnit.Assertion
case_py_compat_fields = do
  let hs_fields = sort $ map (\op_id -> (op_id, OpCodes.allOpFields op_id))
                         OpCodes.allOpIDs
  py_stdout <-
     runPython "from ganeti import opcodes\n\
               \import sys\n\
               \from ganeti import serializer\n\
               \fields = [(k, sorted([p[0] for p in v.OP_PARAMS]))\n\
               \           for k, v in opcodes.OP_MAPPING.items()]\n\
               \print serializer.Dump(fields)" ""
     >>= checkPythonResult
  let deserialised = J.decode py_stdout::J.Result [(String, [String])]
  py_fields <- case deserialised of
                 J.Ok v -> return $ sort v
                 J.Error msg ->
                   HUnit.assertFailure ("Unable to decode op fields: " ++ msg)
                   -- this already raised an expection, but we need it
                   -- for proper types
                   >> fail "Unable to decode op fields"
  HUnit.assertEqual "Mismatch in number of returned opcodes"
    (length hs_fields) (length py_fields)
  HUnit.assertEqual "Mismatch in defined OP_IDs"
    (map fst hs_fields) (map fst py_fields)
  mapM_ (\((py_id, py_flds), (hs_id, hs_flds)) -> do
           HUnit.assertEqual "Mismatch in OP_ID" py_id hs_id
           HUnit.assertEqual ("Mismatch in fields for " ++ hs_id)
             py_flds hs_flds
        ) $ zip py_fields hs_fields

516
517
518
519
520
521
-- | Checks that setOpComment works correctly.
prop_setOpComment :: OpCodes.MetaOpCode -> String -> Property
prop_setOpComment op comment =
  let (OpCodes.MetaOpCode common _) = OpCodes.setOpComment comment op
  in OpCodes.opComment common ==? Just comment

522
testSuite "OpCodes"
523
524
            [ 'prop_serialization
            , 'case_AllDefined
525
526
            , 'case_py_compat_types
            , 'case_py_compat_fields
527
            , 'prop_setOpComment
528
            ]