module Test.Ganeti.Locking.Locks (testLocking_Locks) where
import Prelude ()
import Ganeti.Prelude
import Control.Applicative (liftA2)
import Control.Monad (liftM)
import System.Posix.Types (CPid)
import Test.QuickCheck
import Text.JSON
import Test.Ganeti.TestHelper
import Test.Ganeti.TestCommon
import Test.Ganeti.Types ()
import Ganeti.Locking.Locks
import Ganeti.Locking.Types
instance Arbitrary GanetiLocks where
arbitrary = oneof [ return BGL
, return ClusterLockSet
, return InstanceLockSet
, Instance <$> genFQDN
, return NodeGroupLockSet
, NodeGroup <$> genUUID
, return NodeResLockSet
, NodeRes <$> genUUID
, return NodeLockSet
, Node <$> genUUID
, return NetworkLockSet
, Network <$> genUUID
]
prop_ReadShow :: Property
prop_ReadShow = forAll (arbitrary :: Gen GanetiLocks) $ \a ->
readJSON (showJSON a) ==? Ok a
prop_ImpliedOrder :: Property
prop_ImpliedOrder =
forAll ((arbitrary :: Gen GanetiLocks)
`suchThat` (not . null . lockImplications)) $ \b ->
counterexample "Implied locks must be earlier in the lock order"
. flip all (lockImplications b) $ \a ->
a < b
prop_ImpliedIntervall :: Property
prop_ImpliedIntervall =
forAll ((arbitrary :: Gen GanetiLocks)
`suchThat` (not . null . lockImplications)) $ \b ->
forAll (elements $ lockImplications b) $ \a ->
forAll (arbitrary `suchThat` liftA2 (&&) (a <) (<= b)) $ \x ->
counterexample ("Locks between a group and a member of the group"
++ " must also belong to the group")
$ a `elem` lockImplications x
instance Arbitrary LockLevel where
arbitrary = elements [LevelCluster ..]
prop_ReadShowLevel :: Property
prop_ReadShowLevel = forAll (arbitrary :: Gen LockLevel) $ \a ->
readJSON (showJSON a) ==? Ok a
instance Arbitrary ClientType where
arbitrary = oneof [ ClientOther <$> arbitrary
, ClientJob <$> arbitrary
]
prop_ReadShow_ClientType :: Property
prop_ReadShow_ClientType = forAll (arbitrary :: Gen ClientType) $ \a ->
readJSON (showJSON a) ==? Ok a
instance Arbitrary CPid where
arbitrary = liftM fromIntegral (arbitrary :: Gen Integer)
instance Arbitrary ClientId where
arbitrary = ClientId <$> arbitrary <*> arbitrary <*> arbitrary
prop_ReadShow_ClientId :: Property
prop_ReadShow_ClientId = forAll (arbitrary :: Gen ClientId) $ \a ->
readJSON (showJSON a) ==? Ok a
testSuite "Locking/Locks"
[ 'prop_ReadShow
, 'prop_ImpliedOrder
, 'prop_ImpliedIntervall
, 'prop_ReadShowLevel
, 'prop_ReadShow_ClientType
, 'prop_ReadShow_ClientId
]