Revision 3a991f2d test/hs/Test/Ganeti/Objects.hs

b/test/hs/Test/Ganeti/Objects.hs
1
{-# LANGUAGE TemplateHaskell, TypeSynonymInstances, FlexibleInstances #-}
1
{-# LANGUAGE TemplateHaskell, TypeSynonymInstances, FlexibleInstances,
2
  OverloadedStrings #-}
2 3
{-# OPTIONS_GHC -fno-warn-orphans #-}
3 4

  
4 5
{-| Unittests for ganeti-htools.
......
44 45
import Data.Char
45 46
import qualified Data.List as List
46 47
import qualified Data.Map as Map
48
import Data.Maybe (fromMaybe)
47 49
import qualified Data.Set as Set
50
import GHC.Exts (IsString(..))
48 51
import qualified Text.JSON as J
49 52

  
50 53
import Test.Ganeti.TestHelper
......
182 185
genValidNetwork :: Gen Objects.Network
183 186
genValidNetwork = do
184 187
  -- generate netmask for the IPv4 network
185
  netmask <- choose (24::Int, 30)
188
  netmask <- fromIntegral <$> choose (24::Int, 30)
186 189
  name <- genName >>= mkNonEmpty
187 190
  mac_prefix <- genMaybe genName
188
  net <- genIp4NetWithNetmask netmask
191
  net <- arbitrary
189 192
  net6 <- genMaybe genIp6Net
190
  gateway <- genMaybe genIp4AddrStr
193
  gateway <- genMaybe arbitrary
191 194
  gateway6 <- genMaybe genIp6Addr
192 195
  res <- liftM Just (genBitString $ netmask2NumHosts netmask)
193 196
  ext_res <- liftM Just (genBitString $ netmask2NumHosts netmask)
194 197
  uuid <- arbitrary
195
  let n = Network name mac_prefix net net6 gateway
198
  let n = Network name mac_prefix (Ip4Network net netmask) net6 gateway
196 199
          gateway6 res ext_res uuid 0 Set.empty
197 200
  return n
198 201

  
......
408 411
instance Arbitrary NodeGroup where
409 412
  arbitrary = genNodeGroup
410 413

  
414
$(genArbitrary ''Ip4Address)
415

  
416
$(genArbitrary ''Ip4Network)
417

  
418
-- | Helper to compute absolute value of an IPv4 address.
419
ip4AddrValue :: Ip4Address -> Integer
420
ip4AddrValue (Ip4Address a b c d) =
421
  fromIntegral a * (2^(24::Integer)) +
422
  fromIntegral b * (2^(16::Integer)) +
423
  fromIntegral c * (2^(8::Integer)) + fromIntegral d
424

  
425
-- | Tests that any difference between IPv4 consecutive addresses is 1.
426
prop_nextIp4Address :: Ip4Address -> Property
427
prop_nextIp4Address ip4 =
428
  ip4AddrValue (nextIp4Address ip4) ==? ip4AddrValue ip4 + 1
429

  
430
-- | IsString instance for 'Ip4Address', to help write the tests.
431
instance IsString Ip4Address where
432
  fromString s =
433
    fromMaybe (error $ "Failed to parse address from " ++ s) (readIp4Address s)
434

  
435
-- | Tests a few simple cases of IPv4 next address.
436
caseNextIp4Address :: HUnit.Assertion
437
caseNextIp4Address = do
438
  HUnit.assertEqual "" "0.0.0.1" $ nextIp4Address "0.0.0.0"
439
  HUnit.assertEqual "" "0.0.0.0" $ nextIp4Address "255.255.255.255"
440
  HUnit.assertEqual "" "1.2.3.5" $ nextIp4Address "1.2.3.4"
441
  HUnit.assertEqual "" "1.3.0.0" $ nextIp4Address "1.2.255.255"
442
  HUnit.assertEqual "" "1.2.255.63" $ nextIp4Address "1.2.255.62"
443

  
411 444
testSuite "Objects"
412 445
  [ 'prop_fillDict
413 446
  , 'prop_Disk_serialisation
......
417 450
  , 'prop_Config_serialisation
418 451
  , 'casePyCompatNetworks
419 452
  , 'casePyCompatNodegroups
453
  , 'prop_nextIp4Address
454
  , 'caseNextIp4Address
420 455
  ]

Also available in: Unified diff