Revision 8d239fa4 htest/Test/Ganeti/OpCodes.hs

b/htest/Test/Ganeti/OpCodes.hs
38 38
import Data.List
39 39
import qualified Data.Map as Map
40 40
import qualified Text.JSON as J
41
import Text.Printf (printf)
41 42

  
42 43
import Test.Ganeti.TestHelper
43 44
import Test.Ganeti.TestCommon
......
308 309
      "OP_TEST_DUMMY" ->
309 310
        OpCodes.OpTestDummy <$> pure J.JSNull <*> pure J.JSNull <*>
310 311
          pure J.JSNull <*> pure J.JSNull
312
      "OP_NETWORK_ADD" ->
313
        OpCodes.OpNetworkAdd <$> genNameNE <*> arbitrary <*> genIp4Net <*>
314
          getMaybe genIp4Addr <*> pure Nothing <*> pure Nothing <*>
315
          getMaybe genMacPrefix <*> getMaybe (listOf genIp4Addr) <*>
316
          (genTags >>= mapM mkNonEmpty)
317
      "OP_NETWORK_REMOVE" ->
318
        OpCodes.OpNetworkRemove <$> genNameNE <*> arbitrary
319
      "OP_NETWORK_SET_PARAMS" ->
320
        OpCodes.OpNetworkSetParams <$> genNameNE <*> arbitrary <*>
321
          getMaybe genIp4Addr <*> pure Nothing <*> pure Nothing <*>
322
          getMaybe genMacPrefix <*> getMaybe (listOf genIp4Addr) <*>
323
          getMaybe (listOf genIp4Addr)
324
      "OP_NETWORK_CONNECT" ->
325
        OpCodes.OpNetworkConnect <$> genNameNE <*> genNameNE <*>
326
          arbitrary <*> genNameNE <*> arbitrary
327
      "OP_NETWORK_DISCONNECT" ->
328
        OpCodes.OpNetworkDisconnect <$> genNameNE <*> genNameNE <*> arbitrary
329
      "OP_NETWORK_QUERY" ->
330
        OpCodes.OpNetworkQuery <$> genFieldsNE <*> genNamesNE
311 331
      _ -> fail $ "Undefined arbitrary for opcode " ++ op_id
312 332

  
313 333
-- * Helper functions
......
354 374
genFieldsNE :: Gen [NonEmptyString]
355 375
genFieldsNE = getFields >>= mapM mkNonEmpty
356 376

  
377
-- | Generate an arbitrary IPv4 address in textual form.
378
genIp4Addr :: Gen NonEmptyString
379
genIp4Addr = do
380
  a <- choose (1::Int, 255)
381
  b <- choose (0::Int, 255)
382
  c <- choose (0::Int, 255)
383
  d <- choose (0::Int, 255)
384
  mkNonEmpty $ intercalate "." (map show [a, b, c, d])
385

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

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

  
357 399
-- * Test cases
358 400

  
359 401
-- | Check that opcode serialization is idempotent.

Also available in: Unified diff