| 1 |
{-# LANGUAGE TemplateHaskell, ScopedTypeVariables #-} |
| 2 |
{-# OPTIONS_GHC -fno-warn-orphans #-} |
| 3 |
|
| 4 |
{-| Unittests for the SlotMap. |
| 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.SlotMap |
| 39 |
( testSlotMap |
| 40 |
, genSlotLimit |
| 41 |
, genTestKey |
| 42 |
, overfullKeys |
| 43 |
) where |
| 44 |
|
| 45 |
import Prelude hiding (all) |
| 46 |
|
| 47 |
import Control.Monad |
| 48 |
import Data.Foldable (all) |
| 49 |
import qualified Data.Map as Map |
| 50 |
import Data.Map (Map, member, keys, keysSet) |
| 51 |
import Data.Set (Set, size, union) |
| 52 |
import qualified Data.Set as Set |
| 53 |
import Test.HUnit |
| 54 |
import Test.QuickCheck |
| 55 |
|
| 56 |
import Test.Ganeti.TestCommon |
| 57 |
import Test.Ganeti.TestHelper |
| 58 |
import Test.Ganeti.Types () |
| 59 |
|
| 60 |
import Ganeti.SlotMap |
| 61 |
|
| 62 |
{-# ANN module "HLint: ignore Use camelCase" #-} |
| 63 |
|
| 64 |
|
| 65 |
-- | Generates a number typical for the limit of a `Slot`. |
| 66 |
-- Useful for constructing resource bounds when not directly constructing |
| 67 |
-- the relevant `Slot`s. |
| 68 |
genSlotLimit :: Gen Int |
| 69 |
genSlotLimit = frequency [ (9, choose (1, 5)) |
| 70 |
, (1, choose (1, 100)) |
| 71 |
] -- Don't create huge slot limits. |
| 72 |
|
| 73 |
|
| 74 |
instance Arbitrary Slot where |
| 75 |
arbitrary = do |
| 76 |
limit <- genSlotLimit |
| 77 |
occ <- choose (0, limit * 2) |
| 78 |
return $ Slot occ limit |
| 79 |
|
| 80 |
|
| 81 |
-- | Generates a number typical for the occupied count of a `Slot`. |
| 82 |
-- Useful for constructing `CountMap`s. |
| 83 |
genSlotCount :: Gen Int |
| 84 |
genSlotCount = slotOccupied <$> arbitrary |
| 85 |
|
| 86 |
|
| 87 |
-- | Takes a slot and resamples its `slotOccupied` count to fit the limit. |
| 88 |
resampleFittingSlot :: Slot -> Gen Slot |
| 89 |
resampleFittingSlot (Slot _ limit) = do |
| 90 |
occ <- choose (0, limit) |
| 91 |
return $ Slot occ limit |
| 92 |
|
| 93 |
|
| 94 |
-- | What we use as key for testing `SlotMap`s. |
| 95 |
type TestKey = String |
| 96 |
|
| 97 |
|
| 98 |
-- | Generates short strings used as `SlotMap` keys. |
| 99 |
-- |
| 100 |
-- We limit ourselves to a small set of key strings with high probability to |
| 101 |
-- increase the chance that `SlotMap`s actually have more than one slot taken. |
| 102 |
genTestKey :: Gen TestKey |
| 103 |
genTestKey = frequency [ (9, elements ["a", "b", "c", "d", "e"]) |
| 104 |
, (1, genPrintableAsciiString) |
| 105 |
] |
| 106 |
|
| 107 |
|
| 108 |
-- | Generates small lists. |
| 109 |
listSizeGen :: Gen Int |
| 110 |
listSizeGen = frequency [ (9, choose (1, 5)) |
| 111 |
, (1, choose (1, 100)) |
| 112 |
] |
| 113 |
|
| 114 |
|
| 115 |
-- | Generates a `SlotMap` given a generator for the keys (see `genTestKey`). |
| 116 |
genSlotMap :: (Ord a) => Gen a -> Gen (SlotMap a) |
| 117 |
genSlotMap keyGen = do |
| 118 |
n <- listSizeGen -- don't create huge `SlotMap`s |
| 119 |
Map.fromList <$> vectorOf n ((,) <$> keyGen <*> arbitrary) |
| 120 |
|
| 121 |
|
| 122 |
-- | Generates a `CountMap` given a generator for the keys (see `genTestKey`). |
| 123 |
genCountMap :: (Ord a) => Gen a -> Gen (CountMap a) |
| 124 |
genCountMap keyGen = do |
| 125 |
n <- listSizeGen -- don't create huge `CountMap`s |
| 126 |
Map.fromList <$> vectorOf n ((,) <$> keyGen <*> genSlotCount) |
| 127 |
|
| 128 |
|
| 129 |
-- | Tells which keys of a `SlotMap` are overfull. |
| 130 |
overfullKeys :: (Ord a) => SlotMap a -> Set a |
| 131 |
overfullKeys sm = |
| 132 |
Set.fromList [ a | (a, Slot occ limit) <- Map.toList sm, occ > limit ] |
| 133 |
|
| 134 |
|
| 135 |
-- | Generates a `SlotMap` for which all slots are within their limits. |
| 136 |
genFittingSlotMap :: (Ord a) => Gen a -> Gen (SlotMap a) |
| 137 |
genFittingSlotMap keyGen = do |
| 138 |
-- Generate a SlotMap, then resample all slots to be fitting. |
| 139 |
slotMap <- traverse resampleFittingSlot =<< genSlotMap keyGen |
| 140 |
when (isOverfull slotMap) $ error "BUG: FittingSlotMap Gen is wrong" |
| 141 |
return slotMap |
| 142 |
|
| 143 |
|
| 144 |
-- * Test cases |
| 145 |
|
| 146 |
case_isOverfull :: Assertion |
| 147 |
case_isOverfull = do |
| 148 |
|
| 149 |
assertBool "overfull" |
| 150 |
. isOverfull $ Map.fromList [("buck", Slot 3 2)] |
| 151 |
|
| 152 |
assertBool "not overfull" |
| 153 |
. not . isOverfull $ Map.fromList [("buck", Slot 2 2)] |
| 154 |
|
| 155 |
assertBool "empty" |
| 156 |
. not . isOverfull $ (Map.fromList [] :: SlotMap TestKey) |
| 157 |
|
| 158 |
|
| 159 |
case_occupySlots_examples :: Assertion |
| 160 |
case_occupySlots_examples = do |
| 161 |
let a n = ("a", Slot n 2) |
| 162 |
let b n = ("b", Slot n 4) |
| 163 |
|
| 164 |
let sm = Map.fromList [a 1, b 2] |
| 165 |
cm = Map.fromList [("a", 1), ("b", 1), ("c", 5)] |
| 166 |
|
| 167 |
assertEqual "fitting occupySlots" |
| 168 |
(sm `occupySlots` cm) |
| 169 |
(Map.fromList [a 2, b 3, ("c", Slot 5 0)]) |
| 170 |
|
| 171 |
|
| 172 |
-- | Union of the keys of two maps. |
| 173 |
keyUnion :: (Ord a) => Map a b -> Map a c -> Set a |
| 174 |
keyUnion a b = keysSet a `union` keysSet b |
| 175 |
|
| 176 |
|
| 177 |
-- | Tests properties of `SlotMap`s being filled up. |
| 178 |
prop_occupySlots :: Property |
| 179 |
prop_occupySlots = |
| 180 |
forAll arbitrary $ \(sm :: SlotMap Int, cm :: CountMap Int) -> |
| 181 |
let smOcc = sm `occupySlots` cm |
| 182 |
in conjoin |
| 183 |
[ counterexample "input keys are preserved" $ |
| 184 |
all (`member` smOcc) (keyUnion sm cm) |
| 185 |
, counterexample "all keys must come from the input keys" $ |
| 186 |
all (`Set.member` keyUnion sm cm) (keys smOcc) |
| 187 |
] |
| 188 |
|
| 189 |
|
| 190 |
-- | Tests for whether there's still space for a job given its rate |
| 191 |
-- limits. |
| 192 |
case_hasSlotsFor_examples :: Assertion |
| 193 |
case_hasSlotsFor_examples = do |
| 194 |
let a n = ("a", Slot n 2) |
| 195 |
let b n = ("b", Slot n 4) |
| 196 |
let c n = ("c", Slot n 8) |
| 197 |
|
| 198 |
let sm = Map.fromList [a 1, b 2] |
| 199 |
|
| 200 |
assertBool "fits" $ |
| 201 |
sm `hasSlotsFor` Map.fromList [("a", 1), ("b", 1)] |
| 202 |
|
| 203 |
assertBool "doesn't fit" |
| 204 |
. not $ sm `hasSlotsFor` Map.fromList [("a", 1), ("b", 3)] |
| 205 |
|
| 206 |
let smOverfull = Map.fromList [a 1, b 2, c 10] |
| 207 |
|
| 208 |
assertBool "fits (untouched keys overfull)" $ |
| 209 |
isOverfull smOverfull |
| 210 |
&& smOverfull `hasSlotsFor` Map.fromList [("a", 1), ("b", 1)] |
| 211 |
|
| 212 |
assertBool "empty fitting" $ |
| 213 |
Map.empty `hasSlotsFor` (Map.empty :: CountMap TestKey) |
| 214 |
|
| 215 |
assertBool "empty not fitting" |
| 216 |
. not $ Map.empty `hasSlotsFor` Map.fromList [("a", 1), ("b", 100)] |
| 217 |
|
| 218 |
assertBool "empty not fitting" |
| 219 |
. not $ Map.empty `hasSlotsFor` Map.fromList [("a", 1)] |
| 220 |
|
| 221 |
|
| 222 |
-- | Tests properties of `hasSlotsFor` on `SlotMap`s that are known to |
| 223 |
-- respect their limits. |
| 224 |
prop_hasSlotsFor_fitting :: Property |
| 225 |
prop_hasSlotsFor_fitting = |
| 226 |
forAll (genFittingSlotMap genTestKey) $ \sm -> |
| 227 |
forAll (genCountMap genTestKey) $ \cm -> |
| 228 |
sm `hasSlotsFor` cm ==? not (isOverfull $ sm `occupySlots` cm) |
| 229 |
|
| 230 |
|
| 231 |
-- | Tests properties of `hasSlotsFor`, irrespective of whether the |
| 232 |
-- input `SlotMap`s respect their limits or not. |
| 233 |
prop_hasSlotsFor :: Property |
| 234 |
prop_hasSlotsFor = |
| 235 |
let -- Generates `SlotMap`s for combining. |
| 236 |
genMaps = resize 10 $ do -- We don't need very large SlotMaps. |
| 237 |
sm1 <- genSlotMap genTestKey |
| 238 |
-- We need to make sm2 smaller to make `hasSlots` below more |
| 239 |
-- likely (otherwise the LHS of ==> is always false). |
| 240 |
sm2 <- sized $ \n -> resize (n `div` 3) (genSlotMap genTestKey) |
| 241 |
-- We also want to test (sm1, sm1); we have to make it more |
| 242 |
-- likely for it to ever happen. |
| 243 |
frequency [ (1, return (sm1, sm1)) |
| 244 |
, (9, return (sm1, sm2)) ] |
| 245 |
|
| 246 |
in forAll genMaps $ \(sm1, sm2) -> |
| 247 |
let fits = sm1 `hasSlotsFor` toCountMap sm2 |
| 248 |
smOcc = sm1 `occupySlots` toCountMap sm2 |
| 249 |
oldOverfullBucks = overfullKeys sm1 |
| 250 |
newOverfullBucks = overfullKeys smOcc |
| 251 |
in conjoin |
| 252 |
[ counterexample "if there's enough extra space, then the new\ |
| 253 |
\ overfull keys must be as before" $ |
| 254 |
fits ==> (newOverfullBucks ==? oldOverfullBucks) |
| 255 |
-- Note that the other way around does not hold: |
| 256 |
-- (newOverfullBucks == oldOverfullBucks) ==> fits |
| 257 |
, counterexample "joining SlotMaps must not change the number of\ |
| 258 |
\ overfull keys (but may change their slot\ |
| 259 |
\ counts" |
| 260 |
. property $ size newOverfullBucks >= size oldOverfullBucks |
| 261 |
] |
| 262 |
|
| 263 |
|
| 264 |
testSuite "SlotMap" |
| 265 |
[ 'case_isOverfull |
| 266 |
, 'case_occupySlots_examples |
| 267 |
, 'prop_occupySlots |
| 268 |
, 'case_hasSlotsFor_examples |
| 269 |
, 'prop_hasSlotsFor_fitting |
| 270 |
, 'prop_hasSlotsFor |
| 271 |
] |