| 1 |
{-# LANGUAGE TemplateHaskell #-} |
| 2 |
{-# OPTIONS_GHC -fno-warn-orphans #-} |
| 3 |
|
| 4 |
{-| Tests for lock allocation. |
| 5 |
|
| 6 |
-} |
| 7 |
|
| 8 |
{- |
| 9 |
|
| 10 |
Copyright (C) 2014 Google Inc. |
| 11 |
All rights reserved. |
| 12 |
|
| 13 |
Redistribution and use in source and binary forms, with or without |
| 14 |
modification, are permitted provided that the following conditions are |
| 15 |
met: |
| 16 |
|
| 17 |
1. Redistributions of source code must retain the above copyright notice, |
| 18 |
this list of conditions and the following disclaimer. |
| 19 |
|
| 20 |
2. Redistributions in binary form must reproduce the above copyright |
| 21 |
notice, this list of conditions and the following disclaimer in the |
| 22 |
documentation and/or other materials provided with the distribution. |
| 23 |
|
| 24 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 25 |
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 26 |
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 27 |
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 28 |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 29 |
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 30 |
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 31 |
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 32 |
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 33 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 34 |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 35 |
|
| 36 |
-} |
| 37 |
|
| 38 |
module Test.Ganeti.Locking.Allocation |
| 39 |
( testLocking_Allocation |
| 40 |
, TestLock |
| 41 |
, TestOwner |
| 42 |
, requestSucceeded |
| 43 |
) where |
| 44 |
|
| 45 |
import qualified Data.Foldable as F |
| 46 |
import qualified Data.Map as M |
| 47 |
import Data.Maybe (fromMaybe) |
| 48 |
import qualified Data.Set as S |
| 49 |
import qualified Text.JSON as J |
| 50 |
|
| 51 |
import Test.QuickCheck |
| 52 |
|
| 53 |
import Test.Ganeti.TestCommon |
| 54 |
import Test.Ganeti.TestHelper |
| 55 |
|
| 56 |
import Ganeti.BasicTypes |
| 57 |
import Ganeti.Locking.Allocation |
| 58 |
import Ganeti.Locking.Types |
| 59 |
|
| 60 |
{- |
| 61 |
|
| 62 |
Ganeti.Locking.Allocation is polymorphic in the types of locks |
| 63 |
and lock owners. So we can use much simpler types here than Ganeti's |
| 64 |
real locks and lock owners, knowning that polymorphic functions cannot |
| 65 |
exploit the simplicity of the types they're deling with. |
| 66 |
|
| 67 |
-} |
| 68 |
|
| 69 |
data TestOwner = TestOwner Int deriving (Ord, Eq, Show) |
| 70 |
|
| 71 |
instance Arbitrary TestOwner where |
| 72 |
arbitrary = TestOwner <$> choose (0, 2) |
| 73 |
|
| 74 |
data TestLock = TestBigLock |
| 75 |
| TestCollectionLockA |
| 76 |
| TestLockA Int |
| 77 |
| TestCollectionLockB |
| 78 |
| TestLockB Int |
| 79 |
deriving (Ord, Eq, Show, Read) |
| 80 |
|
| 81 |
instance Arbitrary TestLock where |
| 82 |
arbitrary = frequency [ (1, elements [ TestBigLock |
| 83 |
, TestCollectionLockA |
| 84 |
, TestCollectionLockB |
| 85 |
]) |
| 86 |
, (2, TestLockA <$> choose (0, 2)) |
| 87 |
, (2, TestLockB <$> choose (0, 2)) |
| 88 |
] |
| 89 |
|
| 90 |
instance Lock TestLock where |
| 91 |
lockImplications (TestLockA _) = [TestCollectionLockA, TestBigLock] |
| 92 |
lockImplications (TestLockB _) = [TestCollectionLockB, TestBigLock] |
| 93 |
lockImplications TestBigLock = [] |
| 94 |
lockImplications _ = [TestBigLock] |
| 95 |
|
| 96 |
{- |
| 97 |
|
| 98 |
All states of a LockAllocation ever available outside the |
| 99 |
Ganeti.Locking.Allocation module must be constructed by starting |
| 100 |
with emptyAllocation and applying the exported functions. |
| 101 |
|
| 102 |
-} |
| 103 |
|
| 104 |
instance Arbitrary OwnerState where |
| 105 |
arbitrary = elements [OwnShared, OwnExclusive] |
| 106 |
|
| 107 |
instance Arbitrary a => Arbitrary (LockRequest a) where |
| 108 |
arbitrary = LockRequest <$> arbitrary <*> genMaybe arbitrary |
| 109 |
|
| 110 |
data UpdateRequest b a = UpdateRequest b [LockRequest a] |
| 111 |
| FreeLockRequest b |
| 112 |
deriving Show |
| 113 |
|
| 114 |
instance (Arbitrary a, Arbitrary b) => Arbitrary (UpdateRequest a b) where |
| 115 |
arbitrary = |
| 116 |
frequency [ (4, UpdateRequest <$> arbitrary <*> (choose (1, 4) >>= vector)) |
| 117 |
, (1, FreeLockRequest <$> arbitrary) |
| 118 |
] |
| 119 |
|
| 120 |
-- | Transform an UpdateRequest into the corresponding state transformer. |
| 121 |
asAllocTrans :: (Lock a, Ord b, Show b) |
| 122 |
=> LockAllocation a b -> UpdateRequest b a -> LockAllocation a b |
| 123 |
asAllocTrans state (UpdateRequest owner updates) = |
| 124 |
fst $ updateLocks owner updates state |
| 125 |
asAllocTrans state (FreeLockRequest owner) = freeLocks state owner |
| 126 |
|
| 127 |
-- | Fold a sequence of requests to transform a lock allocation onto the empty |
| 128 |
-- allocation. As we consider all exported LockAllocation transformers, any |
| 129 |
-- LockAllocation definable is obtained in this way. |
| 130 |
foldUpdates :: (Lock a, Ord b, Show b) |
| 131 |
=> [UpdateRequest b a] -> LockAllocation a b |
| 132 |
foldUpdates = foldl asAllocTrans emptyAllocation |
| 133 |
|
| 134 |
instance (Arbitrary a, Lock a, Arbitrary b, Ord b, Show b) |
| 135 |
=> Arbitrary (LockAllocation a b) where |
| 136 |
arbitrary = foldUpdates <$> (choose (0, 8) >>= vector) |
| 137 |
|
| 138 |
-- | Basic property of locking: the exclusive locks of one user |
| 139 |
-- are disjoint from any locks of any other user. |
| 140 |
prop_LocksDisjoint :: Property |
| 141 |
prop_LocksDisjoint = |
| 142 |
forAll (arbitrary :: Gen (LockAllocation TestLock TestOwner)) $ \state -> |
| 143 |
forAll (arbitrary :: Gen TestOwner) $ \a -> |
| 144 |
forAll (arbitrary `suchThat` (/= a)) $ \b -> |
| 145 |
let aExclusive = M.keysSet . M.filter (== OwnExclusive) $ listLocks a state |
| 146 |
bAll = M.keysSet $ listLocks b state |
| 147 |
in counterexample |
| 148 |
(show a ++ "'s exclusive lock" ++ " is not respected by " ++ show b) |
| 149 |
(S.null $ S.intersection aExclusive bAll) |
| 150 |
|
| 151 |
-- | Verify that the list of active locks indeed contains all locks that |
| 152 |
-- are owned by someone. |
| 153 |
prop_LockslistComplete :: Property |
| 154 |
prop_LockslistComplete = |
| 155 |
forAll (arbitrary :: Gen TestOwner) $ \a -> |
| 156 |
forAll ((arbitrary :: Gen (LockAllocation TestLock TestOwner)) |
| 157 |
`suchThat` (not . M.null . listLocks a)) $ \state -> |
| 158 |
counterexample "All owned locks must be mentioned in the all-locks list" $ |
| 159 |
let allLocks = listAllLocks state in |
| 160 |
all (`elem` allLocks) (M.keys $ listLocks a state) |
| 161 |
|
| 162 |
-- | Verify that the list of all locks with states is contained in the list |
| 163 |
-- of all locks. |
| 164 |
prop_LocksAllOwnersSubsetLockslist :: Property |
| 165 |
prop_LocksAllOwnersSubsetLockslist = |
| 166 |
forAll (arbitrary :: Gen (LockAllocation TestLock TestOwner)) $ \state -> |
| 167 |
counterexample "The list of all active locks must contain all locks mentioned\ |
| 168 |
\ in the locks state" $ |
| 169 |
S.isSubsetOf (S.fromList . map fst $ listAllLocksOwners state) |
| 170 |
(S.fromList $ listAllLocks state) |
| 171 |
|
| 172 |
-- | Verify that all locks of all owners are mentioned in the list of all locks' |
| 173 |
-- owner's state. |
| 174 |
prop_LocksAllOwnersComplete :: Property |
| 175 |
prop_LocksAllOwnersComplete = |
| 176 |
forAll (arbitrary :: Gen TestOwner) $ \a -> |
| 177 |
forAll ((arbitrary :: Gen (LockAllocation TestLock TestOwner)) |
| 178 |
`suchThat` (not . M.null . listLocks a)) $ \state -> |
| 179 |
counterexample "Owned locks must be mentioned in list of all locks' state" $ |
| 180 |
let allLocksState = listAllLocksOwners state |
| 181 |
in flip all (M.toList $ listLocks a state) $ \(lock, ownership) -> |
| 182 |
elem (a, ownership) . fromMaybe [] $ lookup lock allLocksState |
| 183 |
|
| 184 |
-- | Verify that all lock owners mentioned in the list of all locks' owner's |
| 185 |
-- state actually own their lock. |
| 186 |
prop_LocksAllOwnersSound :: Property |
| 187 |
prop_LocksAllOwnersSound = |
| 188 |
forAll ((arbitrary :: Gen (LockAllocation TestLock TestOwner)) |
| 189 |
`suchThat` (not . null . listAllLocksOwners)) $ \state -> |
| 190 |
counterexample "All locks mentioned in listAllLocksOwners must be owned by\ |
| 191 |
\ the mentioned owner" . |
| 192 |
flip all (listAllLocksOwners state) $ \(lock, owners) -> |
| 193 |
flip all owners $ \(owner, ownership) -> holdsLock owner lock ownership state |
| 194 |
|
| 195 |
-- | Verify that exclusive group locks are honored, i.e., verify that if someone |
| 196 |
-- holds a lock, then no one else can hold a lock on an exclusive lock on an |
| 197 |
-- implied lock. |
| 198 |
prop_LockImplicationX :: Property |
| 199 |
prop_LockImplicationX = |
| 200 |
forAll (arbitrary :: Gen (LockAllocation TestLock TestOwner)) $ \state -> |
| 201 |
forAll (arbitrary :: Gen TestOwner) $ \a -> |
| 202 |
forAll (arbitrary `suchThat` (/= a)) $ \b -> |
| 203 |
let bExclusive = M.keysSet . M.filter (== OwnExclusive) $ listLocks b state |
| 204 |
in counterexample "Others cannot have an exclusive lock on an implied lock" . |
| 205 |
flip all (M.keys $ listLocks a state) $ \lock -> |
| 206 |
flip all (lockImplications lock) $ \impliedlock -> |
| 207 |
not $ S.member impliedlock bExclusive |
| 208 |
|
| 209 |
-- | Verify that shared group locks are honored, i.e., verify that if someone |
| 210 |
-- holds an exclusive lock, then no one else can hold any form on lock on an |
| 211 |
-- implied lock. |
| 212 |
prop_LockImplicationS :: Property |
| 213 |
prop_LockImplicationS = |
| 214 |
forAll (arbitrary :: Gen (LockAllocation TestLock TestOwner)) $ \state -> |
| 215 |
forAll (arbitrary :: Gen TestOwner) $ \a -> |
| 216 |
forAll (arbitrary `suchThat` (/= a)) $ \b -> |
| 217 |
let aExclusive = M.keys . M.filter (== OwnExclusive) $ listLocks a state |
| 218 |
bAll = M.keysSet $ listLocks b state |
| 219 |
in counterexample "Others cannot hold locks implied by an exclusive lock" . |
| 220 |
flip all aExclusive $ \lock -> |
| 221 |
flip all (lockImplications lock) $ \impliedlock -> |
| 222 |
not $ S.member impliedlock bAll |
| 223 |
|
| 224 |
-- | Verify that locks can only be modified by updates of the owner. |
| 225 |
prop_LocksStable :: Property |
| 226 |
prop_LocksStable = |
| 227 |
forAll (arbitrary :: Gen (LockAllocation TestLock TestOwner)) $ \state -> |
| 228 |
forAll (arbitrary :: Gen TestOwner) $ \a -> |
| 229 |
forAll (arbitrary `suchThat` (/= a)) $ \b -> |
| 230 |
forAll (arbitrary :: Gen [LockRequest TestLock]) $ \request -> |
| 231 |
let (state', _) = updateLocks b request state |
| 232 |
in (listLocks a state ==? listLocks a state') |
| 233 |
|
| 234 |
-- | Verify that a given request is statisfied in list of owned locks |
| 235 |
requestSucceeded :: Ord a => M.Map a OwnerState -> LockRequest a -> Bool |
| 236 |
requestSucceeded owned (LockRequest lock status) = M.lookup lock owned == status |
| 237 |
|
| 238 |
-- | Verify that lock updates are atomic, i.e., either we get all the required |
| 239 |
-- locks, or the state is completely unchanged. |
| 240 |
prop_LockupdateAtomic :: Property |
| 241 |
prop_LockupdateAtomic = |
| 242 |
forAll (arbitrary :: Gen (LockAllocation TestLock TestOwner)) $ \state -> |
| 243 |
forAll (arbitrary :: Gen TestOwner) $ \a -> |
| 244 |
forAll (arbitrary :: Gen [LockRequest TestLock]) $ \request -> |
| 245 |
let (state', result) = updateLocks a request state |
| 246 |
in if result == Ok S.empty |
| 247 |
then counterexample |
| 248 |
("Update succeeded, but in final state " ++ show state' |
| 249 |
++ "not all locks are as requested") |
| 250 |
$ let owned = listLocks a state' |
| 251 |
in all (requestSucceeded owned) request |
| 252 |
else counterexample |
| 253 |
("Update failed, but state changed to " ++ show state') |
| 254 |
(state == state') |
| 255 |
|
| 256 |
-- | Verify that releasing a lock always succeeds. |
| 257 |
prop_LockReleaseSucceeds :: Property |
| 258 |
prop_LockReleaseSucceeds = |
| 259 |
forAll (arbitrary :: Gen (LockAllocation TestLock TestOwner)) $ \state -> |
| 260 |
forAll (arbitrary :: Gen TestOwner) $ \a -> |
| 261 |
forAll (arbitrary :: Gen TestLock) $ \lock -> |
| 262 |
let (_, result) = updateLocks a [requestRelease lock] state |
| 263 |
in counterexample |
| 264 |
("Releasing a lock has to suceed uncondiationally, but got " |
| 265 |
++ show result) |
| 266 |
(isOk result) |
| 267 |
|
| 268 |
-- | Verify the property that only the blocking owners prevent |
| 269 |
-- lock allocation. We deliberatly go for the expensive variant |
| 270 |
-- restraining by suchThat, as otherwise the number of cases actually |
| 271 |
-- covered is too small. |
| 272 |
prop_BlockSufficient :: Property |
| 273 |
prop_BlockSufficient = |
| 274 |
forAll (arbitrary :: Gen TestOwner) $ \a -> |
| 275 |
forAll (arbitrary :: Gen TestLock) $ \lock -> |
| 276 |
forAll (elements [ [requestShared lock] |
| 277 |
, [requestExclusive lock]]) $ \request -> |
| 278 |
forAll ((arbitrary :: Gen (LockAllocation TestLock TestOwner)) |
| 279 |
`suchThat` (genericResult (const False) (not . S.null) |
| 280 |
. snd . updateLocks a request)) $ \state -> |
| 281 |
let (_, result) = updateLocks a request state |
| 282 |
blockedOn = genericResult (const S.empty) id result |
| 283 |
in counterexample "After all blockers release, a request must succeed" |
| 284 |
. isOk . snd . updateLocks a request $ F.foldl freeLocks state blockedOn |
| 285 |
|
| 286 |
-- | Verify the property that every blocking owner is necessary, i.e., even |
| 287 |
-- if we only keep the locks of one of the blocking owners, the request still |
| 288 |
-- will be blocked. We deliberatly use the expensive variant of restraining |
| 289 |
-- to ensure good coverage. To make sure the request can always be blocked |
| 290 |
-- by two owners, for a shared request we request two different locks. |
| 291 |
prop_BlockNecessary :: Property |
| 292 |
prop_BlockNecessary = |
| 293 |
forAll (arbitrary :: Gen TestOwner) $ \a -> |
| 294 |
forAll (arbitrary :: Gen TestLock) $ \lock -> |
| 295 |
forAll (arbitrary `suchThat` (/= lock)) $ \lock' -> |
| 296 |
forAll (elements [ [requestShared lock, requestShared lock'] |
| 297 |
, [requestExclusive lock]]) $ \request -> |
| 298 |
forAll ((arbitrary :: Gen (LockAllocation TestLock TestOwner)) |
| 299 |
`suchThat` (genericResult (const False) ((>= 2) . S.size) |
| 300 |
. snd . updateLocks a request)) $ \state -> |
| 301 |
let (_, result) = updateLocks a request state |
| 302 |
blockers = genericResult (const S.empty) id result |
| 303 |
in counterexample "Each blocker alone must block the request" |
| 304 |
. flip all (S.elems blockers) $ \blocker -> |
| 305 |
(==) (Ok $ S.singleton blocker) . snd . updateLocks a request |
| 306 |
. F.foldl freeLocks state |
| 307 |
$ S.filter (/= blocker) blockers |
| 308 |
|
| 309 |
instance J.JSON TestOwner where |
| 310 |
showJSON (TestOwner x) = J.showJSON x |
| 311 |
readJSON = (>>= return . TestOwner) . J.readJSON |
| 312 |
|
| 313 |
instance J.JSON TestLock where |
| 314 |
showJSON = J.showJSON . show |
| 315 |
readJSON = (>>= return . read) . J.readJSON |
| 316 |
|
| 317 |
-- | Verify that for LockAllocation we have readJSON . showJSON = Ok. |
| 318 |
prop_ReadShow :: Property |
| 319 |
prop_ReadShow = |
| 320 |
forAll (arbitrary :: Gen (LockAllocation TestLock TestOwner)) $ \state -> |
| 321 |
J.readJSON (J.showJSON state) ==? J.Ok state |
| 322 |
|
| 323 |
-- | Verify that the list of lock owners is complete. |
| 324 |
prop_OwnerComplete :: Property |
| 325 |
prop_OwnerComplete = |
| 326 |
forAll (arbitrary :: Gen (LockAllocation TestLock TestOwner)) $ \state -> |
| 327 |
foldl freeLocks state (lockOwners state) ==? emptyAllocation |
| 328 |
|
| 329 |
-- | Verify that each owner actually owns a lock. |
| 330 |
prop_OwnerSound :: Property |
| 331 |
prop_OwnerSound = |
| 332 |
forAll ((arbitrary :: Gen (LockAllocation TestLock TestOwner)) |
| 333 |
`suchThat` (not . null . lockOwners)) $ \state -> |
| 334 |
counterexample "All subjects listed as owners must own at least one lock" |
| 335 |
. flip all (lockOwners state) $ \owner -> |
| 336 |
not . M.null $ listLocks owner state |
| 337 |
|
| 338 |
-- | Verify that for LockRequest we have readJSON . showJSON = Ok. |
| 339 |
prop_ReadShowRequest :: Property |
| 340 |
prop_ReadShowRequest = |
| 341 |
forAll (arbitrary :: Gen (LockRequest TestLock)) $ \state -> |
| 342 |
J.readJSON (J.showJSON state) ==? J.Ok state |
| 343 |
|
| 344 |
|
| 345 |
testSuite "Locking/Allocation" |
| 346 |
[ 'prop_LocksDisjoint |
| 347 |
, 'prop_LockslistComplete |
| 348 |
, 'prop_LocksAllOwnersSubsetLockslist |
| 349 |
, 'prop_LocksAllOwnersComplete |
| 350 |
, 'prop_LocksAllOwnersSound |
| 351 |
, 'prop_LockImplicationX |
| 352 |
, 'prop_LockImplicationS |
| 353 |
, 'prop_LocksStable |
| 354 |
, 'prop_LockupdateAtomic |
| 355 |
, 'prop_LockReleaseSucceeds |
| 356 |
, 'prop_BlockSufficient |
| 357 |
, 'prop_BlockNecessary |
| 358 |
, 'prop_ReadShow |
| 359 |
, 'prop_OwnerComplete |
| 360 |
, 'prop_OwnerSound |
| 361 |
, 'prop_ReadShowRequest |
| 362 |
] |